You've already forked immich
mirror of
https://github.com/immich-app/immich.git
synced 2025-07-16 07:24:40 +02:00
Better caching for mobile (#521)
* Use custom caches in all modules * Cache Settings * Fix wrong key * Create custom cache repository based on hive * Show cache usage in settings * Show cache sizes * Change settings ranges and default value * Handle cache clear by operating system * Resolve review comments
This commit is contained in:
@ -7,7 +7,10 @@ enum AppSettingsEnum<T> {
|
||||
tilesPerRow<int>("tilesPerRow", 4),
|
||||
uploadErrorNotificationGracePeriod<int>(
|
||||
"uploadErrorNotificationGracePeriod", 2),
|
||||
storageIndicator<bool>("storageIndicator", true);
|
||||
storageIndicator<bool>("storageIndicator", true),
|
||||
thumbnailCacheSize<int>("thumbnailCacheSize", 10000),
|
||||
imageCacheSize<int>("imageCacheSize", 350),
|
||||
albumThumbnailCacheSize<int>("albumThumbnailCacheSize", 200);
|
||||
|
||||
const AppSettingsEnum(this.hiveKey, this.defaultValue);
|
||||
|
||||
|
@ -0,0 +1,142 @@
|
||||
import 'package:easy_localization/easy_localization.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/modules/settings/services/app_settings.service.dart';
|
||||
import 'package:immich_mobile/modules/settings/ui/cache_settings/cache_settings_slider_pref.dart';
|
||||
import 'package:immich_mobile/shared/services/cache.service.dart';
|
||||
import 'package:immich_mobile/utils/bytes_units.dart';
|
||||
|
||||
class CacheSettings extends HookConsumerWidget {
|
||||
const CacheSettings({
|
||||
Key? key,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final CacheService cacheService = ref.watch(cacheServiceProvider);
|
||||
|
||||
final clearCacheState = useState(false);
|
||||
|
||||
Future<void> clearCache() async {
|
||||
await cacheService.emptyAllCaches();
|
||||
clearCacheState.value = true;
|
||||
}
|
||||
|
||||
Widget cacheStatisticsRow(String name, CacheType type) {
|
||||
final cacheSize = useState(0);
|
||||
final cacheAssets = useState(0);
|
||||
|
||||
if (!clearCacheState.value) {
|
||||
final repo = cacheService.getCacheRepo(type);
|
||||
|
||||
repo.open().then((_) {
|
||||
cacheSize.value = repo.getCacheSize();
|
||||
cacheAssets.value = repo.getNumberOfCachedObjects();
|
||||
});
|
||||
} else {
|
||||
cacheSize.value = 0;
|
||||
cacheAssets.value = 0;
|
||||
}
|
||||
|
||||
return Container(
|
||||
margin: const EdgeInsets.only(left: 20, bottom: 10),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
name,
|
||||
style: const TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const Text(
|
||||
"cache_settings_statistics_assets",
|
||||
style: TextStyle(color: Colors.grey),
|
||||
).tr(
|
||||
args: ["${cacheAssets.value}", formatBytes(cacheSize.value)],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return ExpansionTile(
|
||||
expandedCrossAxisAlignment: CrossAxisAlignment.start,
|
||||
textColor: Theme.of(context).primaryColor,
|
||||
title: const Text(
|
||||
'cache_settings_title',
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
).tr(),
|
||||
subtitle: const Text(
|
||||
'cache_settings_subtitle',
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
),
|
||||
).tr(),
|
||||
children: [
|
||||
const CacheSettingsSliderPref(
|
||||
setting: AppSettingsEnum.thumbnailCacheSize,
|
||||
translationKey: "cache_settings_thumbnail_size",
|
||||
min: 1000,
|
||||
max: 20000,
|
||||
divisions: 19,
|
||||
),
|
||||
const CacheSettingsSliderPref(
|
||||
setting: AppSettingsEnum.imageCacheSize,
|
||||
translationKey: "cache_settings_image_cache_size",
|
||||
min: 0,
|
||||
max: 1000,
|
||||
divisions: 20,
|
||||
),
|
||||
const CacheSettingsSliderPref(
|
||||
setting: AppSettingsEnum.albumThumbnailCacheSize,
|
||||
translationKey: "cache_settings_album_thumbnails",
|
||||
min: 0,
|
||||
max: 1000,
|
||||
divisions: 20,
|
||||
),
|
||||
ListTile(
|
||||
title: const Text(
|
||||
"cache_settings_statistics_title",
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
).tr(),
|
||||
),
|
||||
cacheStatisticsRow(
|
||||
"cache_settings_statistics_thumbnail".tr(), CacheType.thumbnail),
|
||||
cacheStatisticsRow(
|
||||
"cache_settings_statistics_album".tr(), CacheType.albumThumbnail),
|
||||
cacheStatisticsRow("cache_settings_statistics_shared".tr(),
|
||||
CacheType.sharedAlbumThumbnail),
|
||||
cacheStatisticsRow(
|
||||
"cache_settings_statistics_full".tr(), CacheType.imageViewerFull),
|
||||
ListTile(
|
||||
title: const Text(
|
||||
"cache_settings_clear_cache_button_title",
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
).tr(),
|
||||
),
|
||||
Container(
|
||||
alignment: Alignment.center,
|
||||
child: TextButton(
|
||||
onPressed: clearCache,
|
||||
child: Text(
|
||||
"cache_settings_clear_cache_button",
|
||||
style: TextStyle(
|
||||
color: Theme.of(context).primaryColor,
|
||||
),
|
||||
).tr(),
|
||||
),
|
||||
)
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
import 'package:easy_localization/easy_localization.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/modules/settings/providers/app_settings.provider.dart';
|
||||
import 'package:immich_mobile/modules/settings/services/app_settings.service.dart';
|
||||
|
||||
class CacheSettingsSliderPref extends HookConsumerWidget {
|
||||
final AppSettingsEnum<int> setting;
|
||||
final String translationKey;
|
||||
final int min;
|
||||
final int max;
|
||||
final int divisions;
|
||||
|
||||
const CacheSettingsSliderPref({
|
||||
Key? key,
|
||||
required this.setting,
|
||||
required this.translationKey,
|
||||
required this.min,
|
||||
required this.max,
|
||||
required this.divisions,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final appSettingService = ref.watch(appSettingsServiceProvider);
|
||||
|
||||
final itemsValue = useState(appSettingService.getSetting<int>(setting));
|
||||
|
||||
void sliderChanged(double value) {
|
||||
itemsValue.value = value.toInt();
|
||||
}
|
||||
|
||||
void sliderChangedEnd(double value) {
|
||||
appSettingService.setSetting(setting, value.toInt());
|
||||
}
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
ListTile(
|
||||
title: Text(
|
||||
translationKey,
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
).tr(args: ["${itemsValue.value.toInt()}"]),
|
||||
),
|
||||
Slider(
|
||||
onChangeEnd: sliderChangedEnd,
|
||||
onChanged: sliderChanged,
|
||||
value: itemsValue.value.toDouble(),
|
||||
min: min.toDouble(),
|
||||
max: max.toDouble(),
|
||||
divisions: divisions,
|
||||
label: "${itemsValue.value.toInt()}",
|
||||
activeColor: Theme.of(context).primaryColor,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ import 'package:easy_localization/easy_localization.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/modules/settings/ui/asset_list_settings/asset_list_settings.dart';
|
||||
import 'package:immich_mobile/modules/settings/ui/cache_settings/cache_settings.dart';
|
||||
import 'package:immich_mobile/modules/settings/ui/image_viewer_quality_setting/image_viewer_quality_setting.dart';
|
||||
import 'package:immich_mobile/modules/settings/ui/notification_setting/notification_setting.dart';
|
||||
import 'package:immich_mobile/modules/settings/ui/theme_setting/theme_setting.dart';
|
||||
@ -41,6 +42,7 @@ class SettingsPage extends HookConsumerWidget {
|
||||
const ImageViewerQualitySetting(),
|
||||
const ThemeSetting(),
|
||||
const AssetListSettings(),
|
||||
const CacheSettings(),
|
||||
if (Platform.isAndroid) const NotificationSetting(),
|
||||
],
|
||||
).toList(),
|
||||
|
Reference in New Issue
Block a user