cache.dart 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'package:flutter/material.dart';
  4. import 'package:path_provider/path_provider.dart';
  5. import 'package:provider/provider.dart';
  6. import 'package:shared_preferences/shared_preferences.dart';
  7. import 'package:twong/providers/user.dart';
  8. import 'package:twong/utils/index.dart';
  9. import 'package:twong/models/index.dart';
  10. class Account {
  11. String account;
  12. String password;
  13. Token token;
  14. String toString() {
  15. return '{"account": "$account", "password": "$password", "token": ${json.encode(token.toJson())}}';
  16. }
  17. static Account fromString(String str) {
  18. Account account = Account();
  19. try {
  20. var map = json.decode(str);
  21. account.account = map["account"];
  22. account.password = map["password"];
  23. account.token = Token.fromJson(map["token"]);
  24. } catch (err) {
  25. Log.e(err);
  26. }
  27. return account;
  28. }
  29. }
  30. class SaveKey {
  31. static const String first = 'first';
  32. static const String account = 'account';
  33. static const String home = 'home';
  34. static const String balance = 'balance';
  35. static const String areas = 'areas';
  36. }
  37. class Cache {
  38. static UserInfo _user;
  39. static Account _account;
  40. static bool checked = false;
  41. static SharedPreferences _pre;
  42. static Map<String, dynamic> _cacheData;
  43. static GlobalKey<NavigatorState> _navigatorKey;
  44. static GlobalKey<NavigatorState> get navigator => _navigatorKey;
  45. static BuildContext get context => _navigatorKey?.currentState?.overlay?.context;
  46. static void setKey(GlobalKey<NavigatorState> key) {
  47. _navigatorKey = key;
  48. }
  49. static Future initial () async {
  50. _cacheData = new Map<String, dynamic>();
  51. _pre = await SharedPreferences.getInstance();
  52. var str = loadString(SaveKey.account);
  53. if (str != null) {
  54. _account = Account.fromString(str);
  55. } else {
  56. _account = Account();
  57. }
  58. print('cache init over');
  59. }
  60. static bool get isVip => user != null && user.vip_level > 1;
  61. static saveString(String key, String value) {
  62. _pre.setString(key, value);
  63. }
  64. static saveBool(String key, bool value) {
  65. _pre.setBool(key, value);
  66. }
  67. static saveInt(String key, int value) {
  68. _pre.setInt(key, value);
  69. }
  70. static saveStringList(String key, List<String> value) {
  71. _pre.setStringList(key, value);
  72. }
  73. static delKey(String key) {
  74. _pre.remove(key);
  75. }
  76. static loadInt(String key) => _pre.getInt(key);
  77. static loadBool(String key) => _pre.getBool(key);
  78. static loadString(String key) => _pre.getString(key);
  79. static loadStringList(String key) => _pre.getStringList(key);
  80. static updateAccount(String account, String password) {
  81. _account.account = account;
  82. _account.password = password;
  83. if(_account.account == null || _account.password == null) {
  84. delKey(SaveKey.account);
  85. } else {
  86. saveString(SaveKey.account, _account.toString());
  87. }
  88. }
  89. static Token get token {
  90. if(_account.token?.token == null) return null;
  91. var token = _account.token;
  92. var time = DateTime.parse(token.expires_time);
  93. if(time.isAfter(DateTime.now())) return token;
  94. else return null;
  95. }
  96. static Account get account => _account;
  97. static void updateToken(Token token) {
  98. _account.token = token == null ? Token() : token;
  99. saveString(SaveKey.account, _account.toString());
  100. Request.updateToken();
  101. }
  102. static UserInfo get user {
  103. return _user;
  104. }
  105. static void updateUserInfo(UserInfo user) {
  106. _user = user;
  107. Provider.of<UserModel>(context, listen: false).update(user);
  108. }
  109. static void updateUser(User user) {
  110. _user.update(user);
  111. }
  112. static void set(String key, dynamic value) {
  113. _cacheData[key] = value;
  114. }
  115. static T get<T>(String key) {
  116. if(!_cacheData.containsKey(key)) return null;
  117. return _cacheData[key] as T;
  118. }
  119. static void clearCache() {
  120. _cacheData.clear();
  121. }
  122. static void clearSaved() {
  123. _pre.clear();
  124. }
  125. static void clearAll() {
  126. updateToken(null);
  127. updateUserInfo(null);
  128. updateAccount(null, null);
  129. clearCache();
  130. Log.i("clear all data");
  131. }
  132. static Future<String> loadCache() async {
  133. Directory tempDir = await getTemporaryDirectory();
  134. double value = await _getDirSize(tempDir);
  135. // tempDir.list(followLinks: false, recursive: true).listen((file) {
  136. // print(file.path);
  137. // });
  138. return _renderSize(value);
  139. }
  140. static _renderSize(double value) {
  141. if (null == value) {
  142. return 0;
  143. }
  144. List<String> unitArr = List()
  145. ..add('B')
  146. ..add('K')
  147. ..add('M')
  148. ..add('G');
  149. int index = 0;
  150. while (value > 1024) {
  151. index++;
  152. value = value / 1024;
  153. }
  154. String size = value.toStringAsFixed(2);
  155. return size + unitArr[index];
  156. }
  157. static Future<double> _getDirSize(FileSystemEntity file) async {
  158. if (file is File) {
  159. int length = await file.length();
  160. return double.parse(length.toString());
  161. }
  162. if (file is Directory) {
  163. final List<FileSystemEntity> children = file.listSync();
  164. double total = 0;
  165. if (children != null)
  166. for (final FileSystemEntity child in children)
  167. total += await _getDirSize(child);
  168. return total;
  169. }
  170. return 0;
  171. }
  172. static Future<String> clearFiles() async {
  173. Directory tempDir = await getTemporaryDirectory();
  174. await delDir(tempDir);
  175. return loadCache();
  176. }
  177. static Future delDir(FileSystemEntity file) async {
  178. if (file is Directory) {
  179. final List<FileSystemEntity> children = file.listSync();
  180. for (final FileSystemEntity child in children) {
  181. await delDir(child);
  182. }
  183. } else {
  184. await file.delete();
  185. }
  186. }
  187. }
  188. extension VersionExtension on Version {
  189. bool operator < (Version b) {
  190. var strA = version.split('.');
  191. var strB = b.version.split('.');
  192. if(int.parse(strA[0]) < int.parse(strB[0])) return true;
  193. else if(strA[0] == strB[0]) {
  194. if (int.parse(strA[1]) < int.parse(strB[1])) return true;
  195. else if(strA[1] == strB[1]) {
  196. if (int.parse(strA[2]) < int.parse(strB[2])) return true;
  197. }
  198. }
  199. return false;
  200. }
  201. bool operator > (Version b) {
  202. var strA = version.split('.');
  203. var strB = b.version.split('.');
  204. if(int.parse(strA[0]) > int.parse(strB[0])) return true;
  205. else if(strA[0] == strB[0]) {
  206. if (int.parse(strA[1]) > int.parse(strB[1])) return true;
  207. else if(strA[1] == strB[1]) {
  208. if (int.parse(strA[2]) > int.parse(strB[2])) return true;
  209. }
  210. }
  211. return false;
  212. }
  213. }