From 89edbcacfab612898f832aab938c1202fb345e61 Mon Sep 17 00:00:00 2001 From: Fynn Petersen-Frey <10599762+fyfrey@users.noreply.github.com> Date: Sat, 20 May 2023 05:06:39 +0200 Subject: [PATCH] chore(mobile): remove obsolete files (#2482) --- .../models/asset_selection_state.model.dart | 77 ------- .../login/models/login_response.model.dart | 110 --------- .../ui/cache_settings/cache_settings.dart | 150 ------------- .../cache_settings_slider_pref.dart | 63 ------ .../experimental_settings.dart | 48 ---- .../models/image_viewer_page_data.model.dart | 11 - .../upload_profile_image_repsonse.model.dart | 57 ----- mobile/lib/shared/services/cache.service.dart | 82 ------- .../utils/immich_cache_info_repository.dart | 210 ------------------ 9 files changed, 808 deletions(-) delete mode 100644 mobile/lib/modules/album/models/asset_selection_state.model.dart delete mode 100644 mobile/lib/modules/login/models/login_response.model.dart delete mode 100644 mobile/lib/modules/settings/ui/cache_settings/cache_settings.dart delete mode 100644 mobile/lib/modules/settings/ui/cache_settings/cache_settings_slider_pref.dart delete mode 100644 mobile/lib/modules/settings/ui/experimental_settings/experimental_settings.dart delete mode 100644 mobile/lib/shared/models/image_viewer_page_data.model.dart delete mode 100644 mobile/lib/shared/models/upload_profile_image_repsonse.model.dart delete mode 100644 mobile/lib/shared/services/cache.service.dart delete mode 100644 mobile/lib/utils/immich_cache_info_repository.dart diff --git a/mobile/lib/modules/album/models/asset_selection_state.model.dart b/mobile/lib/modules/album/models/asset_selection_state.model.dart deleted file mode 100644 index 084d86be59..0000000000 --- a/mobile/lib/modules/album/models/asset_selection_state.model.dart +++ /dev/null @@ -1,77 +0,0 @@ -import 'package:collection/collection.dart'; -import 'package:immich_mobile/shared/models/asset.dart'; - -class AssetSelectionState { - final Set selectedMonths; - final Set selectedNewAssetsForAlbum; - final Set selectedAdditionalAssetsForAlbum; - final Set selectedAssetsInAlbumViewer; - final bool isMultiselectEnable; - - /// Indicate the asset selection page is navigated from existing album - final bool isAlbumExist; - AssetSelectionState({ - required this.selectedMonths, - required this.selectedNewAssetsForAlbum, - required this.selectedAdditionalAssetsForAlbum, - required this.selectedAssetsInAlbumViewer, - required this.isMultiselectEnable, - required this.isAlbumExist, - }); - - AssetSelectionState copyWith({ - Set? selectedMonths, - Set? selectedNewAssetsForAlbum, - Set? selectedAdditionalAssetsForAlbum, - Set? selectedAssetsInAlbumViewer, - bool? isMultiselectEnable, - bool? isAlbumExist, - }) { - return AssetSelectionState( - selectedMonths: selectedMonths ?? this.selectedMonths, - selectedNewAssetsForAlbum: - selectedNewAssetsForAlbum ?? this.selectedNewAssetsForAlbum, - selectedAdditionalAssetsForAlbum: selectedAdditionalAssetsForAlbum ?? - this.selectedAdditionalAssetsForAlbum, - selectedAssetsInAlbumViewer: - selectedAssetsInAlbumViewer ?? this.selectedAssetsInAlbumViewer, - isMultiselectEnable: isMultiselectEnable ?? this.isMultiselectEnable, - isAlbumExist: isAlbumExist ?? this.isAlbumExist, - ); - } - - @override - String toString() { - return 'AssetSelectionState(selectedMonths: $selectedMonths, selectedNewAssetsForAlbum: $selectedNewAssetsForAlbum, selectedAdditionalAssetsForAlbum: $selectedAdditionalAssetsForAlbum, selectedAssetsInAlbumViewer: $selectedAssetsInAlbumViewer, isMultiselectEnable: $isMultiselectEnable, isAlbumExist: $isAlbumExist)'; - } - - @override - bool operator ==(Object other) { - if (identical(this, other)) return true; - final setEquals = const DeepCollectionEquality().equals; - - return other is AssetSelectionState && - setEquals(other.selectedMonths, selectedMonths) && - setEquals(other.selectedNewAssetsForAlbum, selectedNewAssetsForAlbum) && - setEquals( - other.selectedAdditionalAssetsForAlbum, - selectedAdditionalAssetsForAlbum, - ) && - setEquals( - other.selectedAssetsInAlbumViewer, - selectedAssetsInAlbumViewer, - ) && - other.isMultiselectEnable == isMultiselectEnable && - other.isAlbumExist == isAlbumExist; - } - - @override - int get hashCode { - return selectedMonths.hashCode ^ - selectedNewAssetsForAlbum.hashCode ^ - selectedAdditionalAssetsForAlbum.hashCode ^ - selectedAssetsInAlbumViewer.hashCode ^ - isMultiselectEnable.hashCode ^ - isAlbumExist.hashCode; - } -} diff --git a/mobile/lib/modules/login/models/login_response.model.dart b/mobile/lib/modules/login/models/login_response.model.dart deleted file mode 100644 index 6e825eff67..0000000000 --- a/mobile/lib/modules/login/models/login_response.model.dart +++ /dev/null @@ -1,110 +0,0 @@ -import 'dart:convert'; - -class LogInReponse { - final String accessToken; - final String userId; - final String userEmail; - final String firstName; - final String lastName; - final String profileImagePath; - final bool isAdmin; - final bool shouldChangePassword; - - LogInReponse({ - required this.accessToken, - required this.userId, - required this.userEmail, - required this.firstName, - required this.lastName, - required this.profileImagePath, - required this.isAdmin, - required this.shouldChangePassword, - }); - - LogInReponse copyWith({ - String? accessToken, - String? userId, - String? userEmail, - String? firstName, - String? lastName, - String? profileImagePath, - bool? isAdmin, - bool? shouldChangePassword, - }) { - return LogInReponse( - accessToken: accessToken ?? this.accessToken, - userId: userId ?? this.userId, - userEmail: userEmail ?? this.userEmail, - firstName: firstName ?? this.firstName, - lastName: lastName ?? this.lastName, - profileImagePath: profileImagePath ?? this.profileImagePath, - isAdmin: isAdmin ?? this.isAdmin, - shouldChangePassword: shouldChangePassword ?? this.shouldChangePassword, - ); - } - - Map toMap() { - final result = {}; - - result.addAll({'accessToken': accessToken}); - result.addAll({'userId': userId}); - result.addAll({'userEmail': userEmail}); - result.addAll({'firstName': firstName}); - result.addAll({'lastName': lastName}); - result.addAll({'profileImagePath': profileImagePath}); - result.addAll({'isAdmin': isAdmin}); - result.addAll({'shouldChangePassword': shouldChangePassword}); - - return result; - } - - factory LogInReponse.fromMap(Map map) { - return LogInReponse( - accessToken: map['accessToken'] ?? '', - userId: map['userId'] ?? '', - userEmail: map['userEmail'] ?? '', - firstName: map['firstName'] ?? '', - lastName: map['lastName'] ?? '', - profileImagePath: map['profileImagePath'] ?? '', - isAdmin: map['isAdmin'] ?? false, - shouldChangePassword: map['shouldChangePassword'] ?? false, - ); - } - - String toJson() => json.encode(toMap()); - - factory LogInReponse.fromJson(String source) => - LogInReponse.fromMap(json.decode(source)); - - @override - String toString() { - return 'LogInReponse(accessToken: $accessToken, userId: $userId, userEmail: $userEmail, firstName: $firstName, lastName: $lastName, profileImagePath: $profileImagePath, isAdmin: $isAdmin, shouldChangePassword: $shouldChangePassword)'; - } - - @override - bool operator ==(Object other) { - if (identical(this, other)) return true; - - return other is LogInReponse && - other.accessToken == accessToken && - other.userId == userId && - other.userEmail == userEmail && - other.firstName == firstName && - other.lastName == lastName && - other.profileImagePath == profileImagePath && - other.isAdmin == isAdmin && - other.shouldChangePassword == shouldChangePassword; - } - - @override - int get hashCode { - return accessToken.hashCode ^ - userId.hashCode ^ - userEmail.hashCode ^ - firstName.hashCode ^ - lastName.hashCode ^ - profileImagePath.hashCode ^ - isAdmin.hashCode ^ - shouldChangePassword.hashCode; - } -} diff --git a/mobile/lib/modules/settings/ui/cache_settings/cache_settings.dart b/mobile/lib/modules/settings/ui/cache_settings/cache_settings.dart deleted file mode 100644 index 00701a29e5..0000000000 --- a/mobile/lib/modules/settings/ui/cache_settings/cache_settings.dart +++ /dev/null @@ -1,150 +0,0 @@ -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 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: ElevatedButton( - onPressed: clearCache, - child: const Text( - "cache_settings_clear_cache_button", - style: TextStyle( - fontWeight: FontWeight.bold, - fontSize: 12, - ), - ).tr(), - ), - ) - ], - ); - } -} diff --git a/mobile/lib/modules/settings/ui/cache_settings/cache_settings_slider_pref.dart b/mobile/lib/modules/settings/ui/cache_settings/cache_settings_slider_pref.dart deleted file mode 100644 index da9e8b030e..0000000000 --- a/mobile/lib/modules/settings/ui/cache_settings/cache_settings_slider_pref.dart +++ /dev/null @@ -1,63 +0,0 @@ -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 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(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, - ), - ], - ); - } -} diff --git a/mobile/lib/modules/settings/ui/experimental_settings/experimental_settings.dart b/mobile/lib/modules/settings/ui/experimental_settings/experimental_settings.dart deleted file mode 100644 index f6db82494f..0000000000 --- a/mobile/lib/modules/settings/ui/experimental_settings/experimental_settings.dart +++ /dev/null @@ -1,48 +0,0 @@ -import 'package:easy_localization/easy_localization.dart'; -import 'package:flutter/material.dart'; -import 'package:hooks_riverpod/hooks_riverpod.dart'; - -class ExperimentalSettings extends HookConsumerWidget { - const ExperimentalSettings({ - Key? key, - }) : super(key: key); - - @override - Widget build(BuildContext context, WidgetRef ref) { - return ExpansionTile( - textColor: Theme.of(context).primaryColor, - title: const Text( - 'experimental_settings_title', - style: TextStyle( - fontWeight: FontWeight.bold, - ), - ).tr(), - subtitle: const Text( - 'experimental_settings_subtitle', - style: TextStyle( - fontSize: 13, - ), - ).tr(), - children: const [ - // SwitchListTile.adaptive( - // activeColor: Theme.of(context).primaryColor, - // title: const Text( - // "experimental_settings_new_asset_list_title", - // style: TextStyle( - // fontSize: 12, - // fontWeight: FontWeight.bold, - // ), - // ).tr(), - // subtitle: const Text( - // "experimental_settings_new_asset_list_subtitle", - // style: TextStyle( - // fontSize: 12, - // ), - // ).tr(), - // value: useExperimentalAssetGrid.value, - // onChanged: changeUseExperimentalAssetGrid, - // ), - ], - ); - } -} diff --git a/mobile/lib/shared/models/image_viewer_page_data.model.dart b/mobile/lib/shared/models/image_viewer_page_data.model.dart deleted file mode 100644 index 8d101b5e8f..0000000000 --- a/mobile/lib/shared/models/image_viewer_page_data.model.dart +++ /dev/null @@ -1,11 +0,0 @@ -class ImageViewerPageData { - final String heroTag; - final String imageUrl; - final String thumbnailUrl; - - ImageViewerPageData({ - required this.heroTag, - required this.imageUrl, - required this.thumbnailUrl, - }); -} diff --git a/mobile/lib/shared/models/upload_profile_image_repsonse.model.dart b/mobile/lib/shared/models/upload_profile_image_repsonse.model.dart deleted file mode 100644 index fb25d356ed..0000000000 --- a/mobile/lib/shared/models/upload_profile_image_repsonse.model.dart +++ /dev/null @@ -1,57 +0,0 @@ -import 'dart:convert'; - -class UploadProfileImageResponse { - final String userId; - final String profileImagePath; - UploadProfileImageResponse({ - required this.userId, - required this.profileImagePath, - }); - - UploadProfileImageResponse copyWith({ - String? userId, - String? profileImagePath, - }) { - return UploadProfileImageResponse( - userId: userId ?? this.userId, - profileImagePath: profileImagePath ?? this.profileImagePath, - ); - } - - Map toMap() { - final result = {}; - - result.addAll({'userId': userId}); - result.addAll({'profileImagePath': profileImagePath}); - - return result; - } - - factory UploadProfileImageResponse.fromMap(Map map) { - return UploadProfileImageResponse( - userId: map['userId'] ?? '', - profileImagePath: map['profileImagePath'] ?? '', - ); - } - - String toJson() => json.encode(toMap()); - - factory UploadProfileImageResponse.fromJson(String source) => - UploadProfileImageResponse.fromMap(json.decode(source)); - - @override - String toString() => - 'UploadProfileImageReponse(userId: $userId, profileImagePath: $profileImagePath)'; - - @override - bool operator ==(Object other) { - if (identical(this, other)) return true; - - return other is UploadProfileImageResponse && - other.userId == userId && - other.profileImagePath == profileImagePath; - } - - @override - int get hashCode => userId.hashCode ^ profileImagePath.hashCode; -} diff --git a/mobile/lib/shared/services/cache.service.dart b/mobile/lib/shared/services/cache.service.dart deleted file mode 100644 index 72ca31f7d6..0000000000 --- a/mobile/lib/shared/services/cache.service.dart +++ /dev/null @@ -1,82 +0,0 @@ -// ignore: depend_on_referenced_packages -import 'package:flutter_cache_manager/flutter_cache_manager.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'; -import 'package:immich_mobile/utils/immich_cache_info_repository.dart'; - -enum CacheType { - // Shared cache for asset thumbnails in various modules - thumbnail, - imageViewerPreview, - imageViewerFull, - albumThumbnail, - sharedAlbumThumbnail; -} - -final cacheServiceProvider = Provider( - (ref) => CacheService(ref.watch(appSettingsServiceProvider)), -); - -class CacheService { - final AppSettingsService _settingsService; - final _cacheRepositoryInstances = {}; - - CacheService(this._settingsService); - - BaseCacheManager getCache(CacheType type) { - return _getDefaultCache( - type.name, - _getCacheSize(type) + 1, - getCacheRepo(type), - ); - } - - ImmichCacheRepository getCacheRepo(CacheType type) { - if (!_cacheRepositoryInstances.containsKey(type)) { - final repo = ImmichCacheInfoRepository( - "cache_${type.name}", - "cacheKeys_${type.name}", - ); - _cacheRepositoryInstances[type] = repo; - } - - return _cacheRepositoryInstances[type]!; - } - - Future emptyAllCaches() async { - for (var type in CacheType.values) { - await getCache(type).emptyCache(); - } - } - - int _getCacheSize(CacheType type) { - switch (type) { - case CacheType.thumbnail: - return _settingsService.getSetting(AppSettingsEnum.thumbnailCacheSize); - case CacheType.imageViewerPreview: - case CacheType.imageViewerFull: - return _settingsService.getSetting(AppSettingsEnum.imageCacheSize); - case CacheType.sharedAlbumThumbnail: - case CacheType.albumThumbnail: - return _settingsService - .getSetting(AppSettingsEnum.albumThumbnailCacheSize); - default: - return 200; - } - } - - BaseCacheManager _getDefaultCache( - String cacheName, - int size, - CacheInfoRepository repo, - ) { - return CacheManager( - Config( - cacheName, - maxNrOfCacheObjects: size, - repo: repo, - ), - ); - } -} diff --git a/mobile/lib/utils/immich_cache_info_repository.dart b/mobile/lib/utils/immich_cache_info_repository.dart deleted file mode 100644 index 699713a6bd..0000000000 --- a/mobile/lib/utils/immich_cache_info_repository.dart +++ /dev/null @@ -1,210 +0,0 @@ -// ignore_for_file: depend_on_referenced_packages, implementation_imports - -import 'dart:io'; -import 'dart:math'; - -import 'package:flutter_cache_manager/flutter_cache_manager.dart'; -import 'package:flutter_cache_manager/src/storage/cache_object.dart'; -import 'package:hive/hive.dart'; -import 'package:path/path.dart' as p; -import 'package:path_provider/path_provider.dart'; - -// Implementation of a CacheInfoRepository based on Hive -abstract class ImmichCacheRepository extends CacheInfoRepository { - int getNumberOfCachedObjects(); - int getCacheSize(); -} - -class ImmichCacheInfoRepository extends ImmichCacheRepository { - final String hiveBoxName; - final String keyLookupHiveBoxName; - - // To circumvent some of the limitations of a non-relational key-value database, - // we use two hive boxes per cache. - // [cacheObjectLookupBox] maps ids to cache objects. - // [keyLookupHiveBox] maps keys to ids. - // The lookup of a cache object by key therefore involves two steps: - // id = keyLookupHiveBox[key] - // object = cacheObjectLookupBox[id] - late Box> cacheObjectLookupBox; - late Box keyLookupHiveBox; - - ImmichCacheInfoRepository(this.hiveBoxName, this.keyLookupHiveBoxName); - - @override - Future close() async { - await cacheObjectLookupBox.close(); - return true; - } - - @override - Future delete(int id) async { - if (cacheObjectLookupBox.containsKey(id)) { - await cacheObjectLookupBox.delete(id); - return 1; - } - return 0; - } - - @override - Future deleteAll(Iterable ids) async { - int deleted = 0; - for (var id in ids) { - if (cacheObjectLookupBox.containsKey(id)) { - deleted++; - await cacheObjectLookupBox.delete(id); - } - } - return deleted; - } - - @override - Future deleteDataFile() async { - await cacheObjectLookupBox.clear(); - await keyLookupHiveBox.clear(); - } - - @override - Future exists() async { - return cacheObjectLookupBox.isNotEmpty && keyLookupHiveBox.isNotEmpty; - } - - @override - Future get(String key) async { - if (!keyLookupHiveBox.containsKey(key)) { - return null; - } - int id = keyLookupHiveBox.get(key)!; - if (!cacheObjectLookupBox.containsKey(id)) { - keyLookupHiveBox.delete(key); - return null; - } - return _deserialize(cacheObjectLookupBox.get(id)!); - } - - @override - Future> getAllObjects() async { - return cacheObjectLookupBox.values.map(_deserialize).toList(); - } - - @override - Future> getObjectsOverCapacity(int capacity) async { - if (cacheObjectLookupBox.length <= capacity) { - return List.empty(); - } - var values = cacheObjectLookupBox.values.map(_deserialize).toList(); - values.sort((CacheObject a, CacheObject b) { - final aTouched = a.touched ?? DateTime.fromMicrosecondsSinceEpoch(0); - final bTouched = b.touched ?? DateTime.fromMicrosecondsSinceEpoch(0); - - return aTouched.compareTo(bTouched); - }); - return values.skip(capacity).take(10).toList(); - } - - @override - Future> getOldObjects(Duration maxAge) async { - return cacheObjectLookupBox.values - .map(_deserialize) - .where((CacheObject element) { - DateTime touched = - element.touched ?? DateTime.fromMicrosecondsSinceEpoch(0); - return touched.isBefore(DateTime.now().subtract(maxAge)); - }).toList(); - } - - @override - Future insert( - CacheObject cacheObject, { - bool setTouchedToNow = true, - }) async { - int newId = keyLookupHiveBox.length == 0 - ? 0 - : keyLookupHiveBox.values.reduce(max) + 1; - cacheObject = cacheObject.copyWith(id: newId); - - keyLookupHiveBox.put(cacheObject.key, newId); - cacheObjectLookupBox.put(newId, cacheObject.toMap()); - - return cacheObject; - } - - @override - Future open() async { - cacheObjectLookupBox = await Hive.openBox(hiveBoxName); - keyLookupHiveBox = await Hive.openBox(keyLookupHiveBoxName); - - // The cache might have cleared by the operating system. - // This could create inconsistencies between the file system cache and database. - // To check whether the cache was cleared, a file within the cache directory - // is created for each database. If the file is absent, the cache was cleared and therefore - // the database has to be cleared as well. - if (!await _checkAndCreateAnchorFile()) { - await cacheObjectLookupBox.clear(); - await keyLookupHiveBox.clear(); - } - - return cacheObjectLookupBox.isOpen; - } - - @override - Future update( - CacheObject cacheObject, { - bool setTouchedToNow = true, - }) async { - if (cacheObject.id != null) { - cacheObjectLookupBox.put(cacheObject.id, cacheObject.toMap()); - return 1; - } - return 0; - } - - @override - Future updateOrInsert(CacheObject cacheObject) { - if (cacheObject.id == null) { - return insert(cacheObject); - } else { - return update(cacheObject); - } - } - - @override - int getNumberOfCachedObjects() { - return cacheObjectLookupBox.length; - } - - @override - int getCacheSize() { - final cacheElementsWithSize = - cacheObjectLookupBox.values.map(_deserialize).map((e) => e.length ?? 0); - - if (cacheElementsWithSize.isEmpty) { - return 0; - } - - return cacheElementsWithSize.reduce((value, element) => value + element); - } - - CacheObject _deserialize(Map serData) { - Map converted = {}; - - serData.forEach((key, value) { - converted[key.toString()] = value; - }); - - return CacheObject.fromMap(converted); - } - - Future _checkAndCreateAnchorFile() async { - final tmpDir = await getTemporaryDirectory(); - final cacheFile = File(p.join(tmpDir.path, "$hiveBoxName.tmp")); - - if (await cacheFile.exists()) { - return true; - } - - await cacheFile.create(); - - return false; - } -}