mirror of
				https://github.com/KevinMidboe/immich.git
				synced 2025-10-29 17:40:28 +00:00 
			
		
		
		
	Refactor abstract class to separate file
This commit is contained in:
		@@ -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<List<AlbumResponseDto>> {
 | 
			
		||||
 
 | 
			
		||||
@@ -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<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();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class AssetCacheService extends JsonCache<List<AssetResponseDto>> {
 | 
			
		||||
  AssetCacheService() : super("asset_cache");
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										49
									
								
								mobile/lib/shared/services/json_cache.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								mobile/lib/shared/services/json_cache.dart
									
									
									
									
									
										Normal file
									
								
							@@ -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();
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user