From 36174338589b405e0d4fb917f34a208f08461535 Mon Sep 17 00:00:00 2001 From: Matthias Rupp Date: Wed, 19 Oct 2022 22:03:54 +0200 Subject: [PATCH] Refactor abstract class to separate file --- .../album/services/album_cache.service.dart | 2 +- .../home/services/asset_cache.service.dart | 50 +------------------ mobile/lib/shared/services/json_cache.dart | 49 ++++++++++++++++++ 3 files changed, 51 insertions(+), 50 deletions(-) create mode 100644 mobile/lib/shared/services/json_cache.dart diff --git a/mobile/lib/modules/album/services/album_cache.service.dart b/mobile/lib/modules/album/services/album_cache.service.dart index 4db41a3775..0e16056585 100644 --- a/mobile/lib/modules/album/services/album_cache.service.dart +++ b/mobile/lib/modules/album/services/album_cache.service.dart @@ -2,7 +2,7 @@ import 'package:collection/collection.dart'; import 'package:flutter/foundation.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; -import 'package:immich_mobile/modules/home/services/asset_cache.service.dart'; +import 'package:immich_mobile/shared/services/json_cache.dart'; import 'package:openapi/api.dart'; class BaseAlbumCacheService extends JsonCache> { diff --git a/mobile/lib/modules/home/services/asset_cache.service.dart b/mobile/lib/modules/home/services/asset_cache.service.dart index 0ba669d4c1..9675938b3b 100644 --- a/mobile/lib/modules/home/services/asset_cache.service.dart +++ b/mobile/lib/modules/home/services/asset_cache.service.dart @@ -1,57 +1,9 @@ -import 'dart:convert'; -import 'dart:io'; - import 'package:collection/collection.dart'; import 'package:flutter/foundation.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; -import 'package:http/http.dart'; +import 'package:immich_mobile/shared/services/json_cache.dart'; import 'package:openapi/api.dart'; -import 'package:path_provider/path_provider.dart'; -abstract class JsonCache { - final String cacheFileName; - - JsonCache(this.cacheFileName); - - Future _getCacheFile() async { - final basePath = await getTemporaryDirectory(); - final basePathName = basePath.path; - - final file = File("$basePathName/$cacheFileName.bin"); - - return file; - } - - Future isValid() async { - final file = await _getCacheFile(); - return await file.exists(); - } - - Future invalidate() async { - final file = await _getCacheFile(); - await file.delete(); - } - - Future putRawData(dynamic data) async { - final jsonString = json.encode(data); - final file = await _getCacheFile(); - - if (!await file.exists()) { - await file.create(); - } - - await file.writeAsString(jsonString); - } - - dynamic readRawData() async { - final file = await _getCacheFile(); - final data = await file.readAsString(); - return json.decode(data); - } - - void put(T data); - Future get(); -} class AssetCacheService extends JsonCache> { AssetCacheService() : super("asset_cache"); diff --git a/mobile/lib/shared/services/json_cache.dart b/mobile/lib/shared/services/json_cache.dart new file mode 100644 index 0000000000..b8a403abba --- /dev/null +++ b/mobile/lib/shared/services/json_cache.dart @@ -0,0 +1,49 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:path_provider/path_provider.dart'; + +abstract class JsonCache { + final String cacheFileName; + + JsonCache(this.cacheFileName); + + Future _getCacheFile() async { + final basePath = await getTemporaryDirectory(); + final basePathName = basePath.path; + + final file = File("$basePathName/$cacheFileName.bin"); + + return file; + } + + Future isValid() async { + final file = await _getCacheFile(); + return await file.exists(); + } + + Future invalidate() async { + final file = await _getCacheFile(); + await file.delete(); + } + + Future putRawData(dynamic data) async { + final jsonString = json.encode(data); + final file = await _getCacheFile(); + + if (!await file.exists()) { + await file.create(); + } + + await file.writeAsString(jsonString); + } + + dynamic readRawData() async { + final file = await _getCacheFile(); + final data = await file.readAsString(); + return json.decode(data); + } + + void put(T data); + Future get(); +} \ No newline at end of file