controller.dart 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. import '../../../../common/models/index.dart';
  4. import '../../../../common/routers/index.dart';
  5. import '../../../../common/services/index.dart';
  6. import '../../../../common/utils/index.dart';
  7. import '../../../../common/widgets/index.dart';
  8. import 'index.dart';
  9. class SettingsCommonController extends GetxController {
  10. SettingsCommonController();
  11. final state = SettingsCommonState();
  12. /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  13. @override
  14. void onReady() {
  15. loadCacheSize();
  16. state.languageName = LanguageListExt.getCurrentLanguage().getDisplayName();
  17. super.onReady();
  18. }
  19. void openLogs() {
  20. Get.toNamed(O2OARoutes.homeSettingsLogs);
  21. }
  22. ///
  23. /// 清除缓存
  24. ///
  25. void clickClearCache() {
  26. O2UI.showConfirm(Get.context, 'settings_clear_cache_clear_confirm_msg'.trArgs([(state.cacheSize)]) , okPressed: () {
  27. _clearCache();
  28. });
  29. }
  30. void _clearCache() async {
  31. await O2FilePathUtil.clearCache();
  32. await O2FlutterMethodChannelUtils().clearCacheNative();
  33. Loading.toast('settings_clear_cache_clear_success'.tr);
  34. loadCacheSize();
  35. }
  36. ///
  37. /// 获取缓存目录大小
  38. ///
  39. Future<void> loadCacheSize() async {
  40. int size = await O2FilePathUtil.cacheSizeTotal();
  41. if (size > 1024 * 1024 * 1024) { // G
  42. final s = (size / (1024 * 1024 * 1024));
  43. state.cacheSize = "${s.toStringAsFixed(2)} G";
  44. } else if (size > 1024 * 1024 ) { // M
  45. final s = (size / (1024 * 1024));
  46. state.cacheSize = "${s.toStringAsFixed(2)} M";
  47. } else if (size > 1024) {
  48. final s = (size / 1024);
  49. state.cacheSize = "${s.toStringAsFixed(2)} K";
  50. } else {
  51. state.cacheSize = "$size";
  52. }
  53. }
  54. // 切换语言
  55. changeLanguage() {
  56. final context = Get.context;
  57. if (context == null) {
  58. return ;
  59. }
  60. O2UI.showBottomSheetWithCancel(context, LanguageList.values.map((e) => ListTile(
  61. title: Text(e.getDisplayName()),
  62. onTap: () {
  63. Navigator.pop(context);
  64. updateLanguage(e);
  65. },
  66. )).toList());
  67. }
  68. updateLanguage(LanguageList lan) async {
  69. Get.updateLocale(lan.getLocale());
  70. SharedPreferenceService.to.putString(SharedPreferenceService.languageSaveKey, lan.getKey());
  71. state.languageName = lan.getDisplayName();
  72. }
  73. }