2023-11-08 14:43:59 +01:00
|
|
|
import 'package:get_storage/get_storage.dart';
|
|
|
|
import 'package:release_schedule/model/movie.dart';
|
|
|
|
|
2024-01-09 21:02:18 +01:00
|
|
|
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 = [];
|
2024-01-09 21:02:18 +01:00
|
|
|
@override
|
2023-11-08 14:43:59 +01:00
|
|
|
update(List<MovieData> movies) {
|
|
|
|
_storedMovies = movies;
|
|
|
|
}
|
|
|
|
|
2024-01-09 21:02:18 +01:00
|
|
|
@override
|
2023-11-08 14:43:59 +01:00
|
|
|
Future<List<MovieData>> retrieve() async {
|
|
|
|
return _storedMovies;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-09 21:02:18 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|