2023-11-08 14:43:59 +01:00
|
|
|
import 'dart:async';
|
|
|
|
|
2023-11-06 10:38:26 +01:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:release_schedule/api/movie_api.dart';
|
|
|
|
import 'package:release_schedule/api/wikidata_movie_api.dart';
|
2023-11-08 14:43:59 +01:00
|
|
|
import 'package:release_schedule/model/local_movie_storage.dart';
|
2023-11-06 10:38:26 +01:00
|
|
|
import 'package:release_schedule/model/movie.dart';
|
|
|
|
|
|
|
|
T? firstWhereOrNull<T>(List<T> list, bool Function(T element) test) {
|
|
|
|
try {
|
|
|
|
return list.firstWhere(test);
|
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-08 14:43:59 +01:00
|
|
|
class DelayedFunctionCaller {
|
|
|
|
final Function function;
|
|
|
|
final Duration duration;
|
|
|
|
Timer? _timer;
|
|
|
|
|
|
|
|
DelayedFunctionCaller(this.function, this.duration);
|
|
|
|
|
|
|
|
void call() {
|
|
|
|
// If a timer is already active, return.
|
|
|
|
if (_timer != null && _timer!.isActive) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a timer that calls the function after the specified duration.
|
|
|
|
_timer = Timer(duration, () {
|
|
|
|
function();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2023-11-06 10:38:26 +01:00
|
|
|
|
2023-11-08 14:43:59 +01:00
|
|
|
final movieManager = MovieManager(WikidataMovieApi(),
|
|
|
|
LocalMovieStorageGetStorage(WikidataMovieData.fromEncodable));
|
2023-11-06 10:38:26 +01:00
|
|
|
|
2023-11-08 14:43:59 +01:00
|
|
|
class MovieManager extends ChangeNotifier {
|
|
|
|
final List<MovieData> movies = List.empty(growable: true);
|
|
|
|
final LocalMovieStorage cache;
|
|
|
|
final MovieApi api;
|
|
|
|
bool loading = false;
|
|
|
|
DelayedFunctionCaller? cacheUpdater;
|
|
|
|
bool cacheLoaded = false;
|
2023-11-06 10:38:26 +01:00
|
|
|
|
2023-11-08 14:43:59 +01:00
|
|
|
MovieManager(this.api, this.cache) {
|
|
|
|
cacheUpdater = DelayedFunctionCaller(() {
|
|
|
|
cache.update(movies);
|
|
|
|
}, const Duration(seconds: 3));
|
|
|
|
|
|
|
|
_loadCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
_loadCache() async {
|
|
|
|
addMovies(await cache.retrieve());
|
|
|
|
}
|
|
|
|
|
|
|
|
_moviesModified({bool withoutAddingOrRemoving = false}) {
|
|
|
|
cacheUpdater?.call();
|
|
|
|
if (!withoutAddingOrRemoving) {
|
|
|
|
// only notify listeners if movies are added or removed
|
|
|
|
// if they are modified in place they will notify listeners themselves
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
List<MovieData> addMovies(List<MovieData> additionalMovies) {
|
|
|
|
List<MovieData> actualMovies = [];
|
2023-11-06 10:38:26 +01:00
|
|
|
bool added = false;
|
|
|
|
for (var movie in additionalMovies) {
|
2023-11-08 14:43:59 +01:00
|
|
|
MovieData? existing =
|
2023-11-06 10:38:26 +01:00
|
|
|
firstWhereOrNull(movies, (element) => movie.same(element));
|
|
|
|
if (existing == null) {
|
2023-11-08 14:59:23 +01:00
|
|
|
_insertMovie(movie);
|
2023-11-08 14:43:59 +01:00
|
|
|
movie.addListener(() {
|
|
|
|
_moviesModified(withoutAddingOrRemoving: true);
|
|
|
|
});
|
2023-11-06 10:38:26 +01:00
|
|
|
added = true;
|
|
|
|
actualMovies.add(movie);
|
|
|
|
} else {
|
2023-11-08 14:59:23 +01:00
|
|
|
existing.updateWithNew(movie);
|
2023-11-06 10:38:26 +01:00
|
|
|
actualMovies.add(existing);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (added) {
|
2023-11-08 14:43:59 +01:00
|
|
|
_moviesModified();
|
2023-11-06 10:38:26 +01:00
|
|
|
}
|
|
|
|
return actualMovies;
|
|
|
|
}
|
|
|
|
|
2023-11-08 14:59:23 +01:00
|
|
|
_insertMovie(MovieData movie) {
|
|
|
|
int min = 0;
|
|
|
|
int max = movies.length - 1;
|
|
|
|
while (min - 1 < max) {
|
|
|
|
int center = ((min + max) / 2).floor();
|
|
|
|
int diff = movie.releaseDate.compareTo(movies[center].releaseDate);
|
|
|
|
if (diff < 0) {
|
|
|
|
max = center - 1;
|
|
|
|
} else {
|
|
|
|
min = center + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
movies.insert(min, movie);
|
|
|
|
}
|
|
|
|
|
2023-11-08 14:43:59 +01:00
|
|
|
removeMoviesWhere(bool Function(MovieData movie) test) {
|
|
|
|
bool removedMovies = false;
|
|
|
|
for (int i = movies.length - 1; i >= 0; i--) {
|
|
|
|
bool remove = test(movies[i]);
|
|
|
|
if (remove) {
|
|
|
|
removedMovies = true;
|
|
|
|
movies.removeAt(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (removedMovies) {
|
|
|
|
_moviesModified();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-06 10:38:26 +01:00
|
|
|
/// Only search locally cached movies.
|
|
|
|
localSearch(String search) {}
|
|
|
|
|
|
|
|
/// Online search for movies.
|
2023-11-08 14:43:59 +01:00
|
|
|
Future<List<MovieData>> search(String search) async {
|
|
|
|
List<MovieData> movies = await api.searchForMovies(search);
|
2023-11-06 10:38:26 +01:00
|
|
|
return addMovies(movies);
|
|
|
|
}
|
|
|
|
|
2023-11-08 14:43:59 +01:00
|
|
|
expandDetails(List<MovieData> movies) {
|
2023-11-06 10:38:26 +01:00
|
|
|
api.addMovieDetails(movies);
|
|
|
|
}
|
|
|
|
|
|
|
|
loadUpcomingMovies() async {
|
2023-11-08 14:43:59 +01:00
|
|
|
try {
|
|
|
|
loading = true;
|
|
|
|
notifyListeners();
|
2023-11-11 14:50:20 +01:00
|
|
|
List<MovieData> movies = await api
|
|
|
|
.getUpcomingMovies(DateTime.now().subtract(const Duration(days: 7)));
|
2023-11-08 14:43:59 +01:00
|
|
|
addMovies(movies);
|
|
|
|
} finally {
|
|
|
|
loading = false;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
2023-11-06 10:38:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class LiveSearch<CustomMovieData extends MovieData> extends ChangeNotifier {
|
|
|
|
String searchTerm = "";
|
|
|
|
List<CustomMovieData> searchResults = [];
|
|
|
|
Duration minTimeBetweenRequests = const Duration(milliseconds: 500);
|
|
|
|
Duration minTimeAfterChangeToRequest = const Duration(milliseconds: 200);
|
|
|
|
final MovieManager manager;
|
|
|
|
|
|
|
|
LiveSearch(this.manager);
|
|
|
|
|
|
|
|
updateSearch(String search) {
|
|
|
|
searchTerm = search;
|
|
|
|
}
|
|
|
|
}
|