cache_settings.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import 'package:easy_localization/easy_localization.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_hooks/flutter_hooks.dart';
  4. import 'package:hooks_riverpod/hooks_riverpod.dart';
  5. import 'package:immich_mobile/modules/settings/services/app_settings.service.dart';
  6. import 'package:immich_mobile/modules/settings/ui/cache_settings/cache_settings_slider_pref.dart';
  7. import 'package:immich_mobile/shared/services/cache.service.dart';
  8. import 'package:immich_mobile/utils/bytes_units.dart';
  9. class CacheSettings extends HookConsumerWidget {
  10. const CacheSettings({
  11. Key? key,
  12. }) : super(key: key);
  13. @override
  14. Widget build(BuildContext context, WidgetRef ref) {
  15. final CacheService cacheService = ref.watch(cacheServiceProvider);
  16. final clearCacheState = useState(false);
  17. Future<void> clearCache() async {
  18. await cacheService.emptyAllCaches();
  19. clearCacheState.value = true;
  20. }
  21. Widget cacheStatisticsRow(String name, CacheType type) {
  22. final cacheSize = useState(0);
  23. final cacheAssets = useState(0);
  24. if (!clearCacheState.value) {
  25. final repo = cacheService.getCacheRepo(type);
  26. repo.open().then((_) {
  27. cacheSize.value = repo.getCacheSize();
  28. cacheAssets.value = repo.getNumberOfCachedObjects();
  29. });
  30. } else {
  31. cacheSize.value = 0;
  32. cacheAssets.value = 0;
  33. }
  34. return Container(
  35. margin: const EdgeInsets.only(left: 20, bottom: 10),
  36. child: Column(
  37. crossAxisAlignment: CrossAxisAlignment.start,
  38. children: [
  39. Text(
  40. name,
  41. style: const TextStyle(
  42. fontWeight: FontWeight.bold,
  43. ),
  44. ),
  45. const Text(
  46. "cache_settings_statistics_assets",
  47. style: TextStyle(color: Colors.grey),
  48. ).tr(
  49. args: ["${cacheAssets.value}", formatBytes(cacheSize.value)],
  50. ),
  51. ],
  52. ),
  53. );
  54. }
  55. return ExpansionTile(
  56. expandedCrossAxisAlignment: CrossAxisAlignment.start,
  57. textColor: Theme.of(context).primaryColor,
  58. title: const Text(
  59. 'cache_settings_title',
  60. style: TextStyle(
  61. fontWeight: FontWeight.bold,
  62. ),
  63. ).tr(),
  64. subtitle: const Text(
  65. 'cache_settings_subtitle',
  66. style: TextStyle(
  67. fontSize: 13,
  68. ),
  69. ).tr(),
  70. children: [
  71. const CacheSettingsSliderPref(
  72. setting: AppSettingsEnum.thumbnailCacheSize,
  73. translationKey: "cache_settings_thumbnail_size",
  74. min: 1000,
  75. max: 20000,
  76. divisions: 19,
  77. ),
  78. const CacheSettingsSliderPref(
  79. setting: AppSettingsEnum.imageCacheSize,
  80. translationKey: "cache_settings_image_cache_size",
  81. min: 0,
  82. max: 1000,
  83. divisions: 20,
  84. ),
  85. const CacheSettingsSliderPref(
  86. setting: AppSettingsEnum.albumThumbnailCacheSize,
  87. translationKey: "cache_settings_album_thumbnails",
  88. min: 0,
  89. max: 1000,
  90. divisions: 20,
  91. ),
  92. ListTile(
  93. title: const Text(
  94. "cache_settings_statistics_title",
  95. style: TextStyle(
  96. fontSize: 12,
  97. fontWeight: FontWeight.bold,
  98. ),
  99. ).tr(),
  100. ),
  101. cacheStatisticsRow(
  102. "cache_settings_statistics_thumbnail".tr(), CacheType.thumbnail),
  103. cacheStatisticsRow(
  104. "cache_settings_statistics_album".tr(), CacheType.albumThumbnail),
  105. cacheStatisticsRow("cache_settings_statistics_shared".tr(),
  106. CacheType.sharedAlbumThumbnail),
  107. cacheStatisticsRow(
  108. "cache_settings_statistics_full".tr(), CacheType.imageViewerFull),
  109. ListTile(
  110. title: const Text(
  111. "cache_settings_clear_cache_button_title",
  112. style: TextStyle(
  113. fontSize: 12,
  114. fontWeight: FontWeight.bold,
  115. ),
  116. ).tr(),
  117. ),
  118. Container(
  119. alignment: Alignment.center,
  120. child: TextButton(
  121. onPressed: clearCache,
  122. child: Text(
  123. "cache_settings_clear_cache_button",
  124. style: TextStyle(
  125. color: Theme.of(context).primaryColor,
  126. ),
  127. ).tr(),
  128. ),
  129. )
  130. ],
  131. );
  132. }
  133. }