setting_controller.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import 'package:get/get.dart';
  2. import 'package:package_info_plus/package_info_plus.dart';
  3. import '../../../../config/translations/strings_enum.dart';
  4. import '../../../../utils/awesome_notifications_helper.dart';
  5. import '../../../controllers/api_controller.dart';
  6. import '../../../data/sp/ix_sp.dart';
  7. import '../../../dialog/loading/loading_dialog.dart';
  8. class SettingController extends GetxController {
  9. final apiController = Get.find<ApiController>();
  10. // 自动重连开关
  11. final _autoReconnect = true.obs;
  12. bool get autoReconnect => _autoReconnect.value;
  13. set autoReconnect(bool value) => _autoReconnect.value = value;
  14. final _pushNotifications = false.obs;
  15. bool get pushNotifications => _pushNotifications.value;
  16. set pushNotifications(bool value) => _pushNotifications.value = value;
  17. final _version = ''.obs;
  18. String get version => _version.value;
  19. set version(String value) => _version.value = value;
  20. final _hasUpdate = false.obs;
  21. bool get hasUpdate => _hasUpdate.value;
  22. set hasUpdate(bool value) => _hasUpdate.value = value;
  23. final _themeMode = ''.obs;
  24. String get themeMode => _themeMode.value;
  25. set themeMode(String value) => _themeMode.value = value;
  26. @override
  27. void onInit() {
  28. super.onInit();
  29. initThemeMode();
  30. _initPushNotifications();
  31. _getVersion();
  32. }
  33. void initThemeMode() {
  34. themeMode = _getThemeModeText();
  35. }
  36. /// 获取当前主题模式文本
  37. String _getThemeModeText() {
  38. final mode = IXSP.getThemeMode();
  39. switch (mode) {
  40. case 'light':
  41. return Strings.lightMode.tr;
  42. case 'dark':
  43. return Strings.darkMode.tr;
  44. case 'system':
  45. default:
  46. return Strings.followSystem.tr;
  47. }
  48. }
  49. /// 获取版本信息
  50. void _getVersion() async {
  51. final packageInfo = await PackageInfo.fromPlatform();
  52. version = packageInfo.version;
  53. final code = int.tryParse(packageInfo.buildNumber) ?? 0;
  54. final upgrade = IXSP.getUpgrade();
  55. if (upgrade?.versionCode == code) {
  56. hasUpdate = false;
  57. } else {
  58. hasUpdate = upgrade?.upgradeType == 1;
  59. }
  60. }
  61. Future<void> _initPushNotifications() async {
  62. pushNotifications =
  63. await AwesomeNotificationsHelper.checkNotificationPermission();
  64. }
  65. Future<void> showNotificationConfigPage() async {
  66. await AwesomeNotificationsHelper.showNotificationConfigPage();
  67. _initPushNotifications();
  68. }
  69. // 处理退出登录
  70. Future<void> handleLogout() async {
  71. await LoadingDialog.show(
  72. context: Get.context!,
  73. loadingText: Strings.loggingOut.tr,
  74. successText: Strings.logoutSuccessful.tr,
  75. onRequest: () async {
  76. // 执行你的异步请求
  77. await apiController.logout();
  78. },
  79. onSuccess: () {
  80. // 成功后的操作
  81. Get.back();
  82. },
  83. );
  84. }
  85. // 处理删除账户
  86. Future<void> handleDeleteAccount() async {
  87. await LoadingDialog.show(
  88. context: Get.context!,
  89. loadingText: Strings.deletingAccount.tr,
  90. successText: Strings.deleteAccountSuccessful.tr,
  91. onRequest: () async {
  92. // 执行你的异步请求
  93. await apiController.deleteAccount();
  94. },
  95. onSuccess: () {
  96. // 成功后的操作
  97. Get.back();
  98. },
  99. );
  100. }
  101. }