global.dart 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // 提供五套可选主题色
  2. import 'dart:convert';
  3. import 'package:dio/dio.dart';
  4. import 'package:e2ee_chat/entities/profile.dart';
  5. import 'package:e2ee_chat/common/api.dart';
  6. import 'package:e2ee_chat/models/user_model.dart';
  7. import 'package:e2ee_chat/objectbox.g.dart';
  8. import 'package:flutter/material.dart';
  9. import 'package:objectbox/objectbox.dart';
  10. import 'package:permission_handler/permission_handler.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 int profileId = 1;
  26. static Profile profile = Profile();
  27. // 可选的主题列表
  28. static List<MaterialColor> get themes => _themes;
  29. // 是否为release版
  30. static bool get isRelease => bool.fromEnvironment("dart.vm.product");
  31. static final String emptyToken = "token";
  32. static get locales => [
  33. const Locale('zh'), // 中文简体
  34. const Locale('en'), // 美国英语
  35. //其它Locales
  36. ];
  37. static Future<bool> checkPermission() async {
  38. bool status = await Permission.storage.isGranted;
  39. return status ? true : await Permission.storage.request().isGranted;
  40. }
  41. static listenIsLogin() async {
  42. while (true) {
  43. if (!profile.isLogin && (profile.isLogout || !await UserModel().login())) {
  44. debug("offline detected! navigate to login route");
  45. await Global.navigatorKey.currentState?.pushNamed("login");
  46. }
  47. await Future.delayed(Duration(seconds: 1));
  48. }
  49. }
  50. //初始化全局信息,会在APP启动时执行
  51. static init() async {
  52. debug('Init begin');
  53. bool status = await checkPermission();
  54. assert(status, "permission error");
  55. Store store = await openStore();
  56. var box = store.box<Profile>();
  57. profile = box.get(profileId) ?? Profile();
  58. store.close();
  59. profile.isLogin = false;
  60. debug('Init profile: id: ${profile.id}, username: ${profile.username}, token: ${await profile.token}, isLogout: ${profile.isLogout}');
  61. await Api.init();
  62. listenIsLogin();
  63. /*
  64. debug('Init login');
  65. try {
  66. await UserModel().login(); // must await, or can not catch error
  67. } on DioError catch(e) {
  68. debug('Init login failed');
  69. }
  70. */
  71. debug('Global init end');
  72. }
  73. // 持久化Profile信息
  74. static saveProfile() async {
  75. debug('save profile: username: ${profile.username}, isLogout: ${profile.isLogout}');
  76. Store store = await openStore();
  77. var box = store.box<Profile>();
  78. box.removeAll();
  79. box.put(profile);
  80. store.close();
  81. }
  82. }