global.dart 2.7 KB

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