release_schedule/lib/model/local_movie_storage.dart

54 lines
1.3 KiB
Dart
Raw Normal View History

2023-11-08 14:43:59 +01:00
import 'package:get_storage/get_storage.dart';
import 'package:release_schedule/model/movie.dart';
abstract class LocalMovieStorage {
void update(List<MovieData> movies);
Future<List<MovieData>> retrieve();
}
class InMemoryMovieStorage implements LocalMovieStorage {
2023-11-08 14:43:59 +01:00
List<MovieData> _storedMovies = [];
@override
2023-11-08 14:43:59 +01:00
update(List<MovieData> movies) {
_storedMovies = movies;
}
@override
2023-11-08 14:43:59 +01:00
Future<List<MovieData>> retrieve() async {
return _storedMovies;
}
}
class LocalMovieStorageGetStorage implements LocalMovieStorage {
2023-11-08 14:43:59 +01:00
Future<void>? initialized;
GetStorage? container;
MovieData Function(Map jsonEncodable) toMovieData;
LocalMovieStorageGetStorage(this.toMovieData) {
initialized = _init();
}
_init() async {
await GetStorage.init("movies");
container = GetStorage("movies");
}
@override
update(List<MovieData> movies) async {
await initialized;
container!.write(
"movies", movies.map((movie) => movie.toJsonEncodable()).toList());
}
@override
Future<List<MovieData>> retrieve() async {
await initialized;
dynamic movies = container!.read("movies");
if (movies == null) {
return [];
}
return (movies as List<dynamic>)
.map((encodable) => toMovieData(encodable))
.toList();
}
}