-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mobile): Cache assets and albums for faster loading speed
feat(mobile): Cache assets and albums for faster loading speed
- Loading branch information
Showing
7 changed files
with
220 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
mobile/lib/modules/album/services/album_cache.service.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
|
||
import 'package:collection/collection.dart'; | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||
import 'package:immich_mobile/shared/services/json_cache.dart'; | ||
import 'package:openapi/api.dart'; | ||
|
||
class BaseAlbumCacheService extends JsonCache<List<AlbumResponseDto>> { | ||
BaseAlbumCacheService(super.cacheFileName); | ||
|
||
@override | ||
void put(List<AlbumResponseDto> data) { | ||
putRawData(data.map((e) => e.toJson()).toList()); | ||
} | ||
|
||
@override | ||
Future<List<AlbumResponseDto>> get() async { | ||
try { | ||
final mapList = await readRawData() as List<dynamic>; | ||
|
||
final responseData = mapList | ||
.map((e) => AlbumResponseDto.fromJson(e)) | ||
.whereNotNull() | ||
.toList(); | ||
|
||
return responseData; | ||
} catch (e) { | ||
debugPrint(e.toString()); | ||
return []; | ||
} | ||
} | ||
} | ||
|
||
class AlbumCacheService extends BaseAlbumCacheService { | ||
AlbumCacheService() : super("album_cache"); | ||
} | ||
|
||
class SharedAlbumCacheService extends BaseAlbumCacheService { | ||
SharedAlbumCacheService() : super("shared_album_cache"); | ||
} | ||
|
||
final albumCacheServiceProvider = Provider( | ||
(ref) => AlbumCacheService(), | ||
); | ||
|
||
final sharedAlbumCacheServiceProvider = Provider( | ||
(ref) => SharedAlbumCacheService(), | ||
); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import 'package:collection/collection.dart'; | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||
import 'package:immich_mobile/shared/services/json_cache.dart'; | ||
import 'package:openapi/api.dart'; | ||
|
||
|
||
class AssetCacheService extends JsonCache<List<AssetResponseDto>> { | ||
AssetCacheService() : super("asset_cache"); | ||
|
||
@override | ||
void put(List<AssetResponseDto> data) { | ||
putRawData(data.map((e) => e.toJson()).toList()); | ||
} | ||
|
||
@override | ||
Future<List<AssetResponseDto>> get() async { | ||
try { | ||
final mapList = await readRawData() as List<dynamic>; | ||
|
||
final responseData = mapList | ||
.map((e) => AssetResponseDto.fromJson(e)) | ||
.whereNotNull() | ||
.toList(); | ||
|
||
return responseData; | ||
} catch (e) { | ||
debugPrint(e.toString()); | ||
|
||
return []; | ||
} | ||
} | ||
} | ||
|
||
final assetCacheServiceProvider = Provider( | ||
(ref) => AssetCacheService(), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:path_provider/path_provider.dart'; | ||
|
||
abstract class JsonCache<T> { | ||
final String cacheFileName; | ||
|
||
JsonCache(this.cacheFileName); | ||
|
||
Future<File> _getCacheFile() async { | ||
final basePath = await getTemporaryDirectory(); | ||
final basePathName = basePath.path; | ||
|
||
final file = File("$basePathName/$cacheFileName.bin"); | ||
|
||
return file; | ||
} | ||
|
||
Future<bool> isValid() async { | ||
final file = await _getCacheFile(); | ||
return await file.exists(); | ||
} | ||
|
||
Future<void> invalidate() async { | ||
final file = await _getCacheFile(); | ||
await file.delete(); | ||
} | ||
|
||
Future<void> 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<T> get(); | ||
} |