global.dart 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // 提供五套可选主题色
  2. import 'dart:convert';
  3. import 'package:dio/dio.dart';
  4. import 'package:e2ee_chat/common/api.dart';
  5. import 'package:e2ee_chat/model/profile.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:permission_handler/permission_handler.dart';
  8. import 'package:shared_preferences/shared_preferences.dart';
  9. import '../presenter/login.dart';
  10. const _themes = <MaterialColor>[
  11. Colors.blue,
  12. Colors.cyan,
  13. Colors.teal,
  14. Colors.green,
  15. Colors.red,
  16. ];
  17. void debug(Object? o) {
  18. if (!Global.isRelease) {
  19. print(o);
  20. }
  21. }
  22. class Global {
  23. static GlobalKey<NavigatorState> navigatorKey = GlobalKey();
  24. static final Image defaultAvatar = Image.asset(
  25. "images/avatar.png",
  26. width: 80,
  27. );
  28. static final int profileId = 1;
  29. static Profile profile = Profile();
  30. // 可选的主题列表
  31. static List<MaterialColor> get themes => _themes;
  32. // 是否为release版
  33. static bool get isRelease => bool.fromEnvironment("dart.vm.product");
  34. static final String emptyToken = "token";
  35. static get locales => [
  36. const Locale('zh'), // 中文简体
  37. const Locale('en'), // 美国英语
  38. //其它Locales
  39. ];
  40. static Future<bool> checkPermission() async {
  41. bool status = await Permission.storage.isGranted;
  42. return status ? true : await Permission.storage.request().isGranted;
  43. }
  44. static listenIsLogin() async {
  45. while (true) {
  46. if (!profile.isLogin && (profile.isLogout || !await LoginPresenter().login())) {
  47. debug("offline detected! navigate to login route");
  48. await Global.navigatorKey.currentState?.pushNamed("login");
  49. }
  50. await Future.delayed(Duration(seconds: 1));
  51. }
  52. }
  53. //初始化全局信息,会在APP启动时执行
  54. static Future<void> init() async {
  55. debug('Init begin');
  56. bool status = await checkPermission();
  57. assert(status, "permission error");
  58. final prefs = await SharedPreferences.getInstance();
  59. String? json = prefs.getString("profile");
  60. profile = json!=null ? Profile.fromJson(jsonDecode(json)) : Profile();
  61. profile.isLogin = false;
  62. debug('Init profile: username: ${profile.username}, isLogout: ${profile.isLogout}');
  63. await Api.init();
  64. listenIsLogin();
  65. /*
  66. debug('Init login');
  67. try {
  68. await UserModel().login(); // must await, or can not catch error
  69. } on DioError catch(e) {
  70. debug('Init login failed');
  71. }
  72. */
  73. debug('Global init end');
  74. }
  75. // 持久化Profile信息
  76. static saveProfile() async {
  77. //TODO: save profile
  78. // debug('save profile: username: ${profile.username}, isLogout: ${profile.isLogout}');
  79. // var box = store.box<Profile>();
  80. // box.removeAll();
  81. // profile.id = 1;
  82. // box.put(profile);
  83. }
  84. }