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';
|
2024-01-10 23:50:43 +01:00
|
|
|
import 'package:release_schedule/model/dates.dart';
|
2023-11-16 13:12:55 +01:00
|
|
|
import 'package:release_schedule/model/delayed_function_caller.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';
|
2024-01-08 21:48:13 +01:00
|
|
|
import 'package:release_schedule/model/search.dart';
|
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;
|
2024-01-08 21:48:13 +01:00
|
|
|
late final DelayedFunctionCaller cacheUpdater;
|
2023-11-08 14:43:59 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2023-11-11 15:05:11 +01:00
|
|
|
Future<void> _loadCache() async {
|
2023-11-08 14:43:59 +01:00
|
|
|
addMovies(await cache.retrieve());
|
|
|
|
}
|
|
|
|
|
2023-11-11 15:05:11 +01:00
|
|
|
void _moviesModified({bool withoutAddingOrRemoving = false}) {
|
2024-01-08 21:48:13 +01:00
|
|
|
cacheUpdater.call();
|
2023-11-08 14:43:59 +01:00
|
|
|
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 =
|
2024-01-09 21:02:18 +01:00
|
|
|
movies.where((element) => movie.same(element)).firstOrNull;
|
2023-11-06 10:38:26 +01:00
|
|
|
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-11 15:08:08 +01:00
|
|
|
_resortMovies();
|
2023-11-08 14:43:59 +01:00
|
|
|
});
|
2023-11-06 10:38:26 +01:00
|
|
|
added = true;
|
|
|
|
actualMovies.add(movie);
|
|
|
|
} else {
|
2024-01-08 12:57:36 +01:00
|
|
|
existing.updateWithNewIgnoringUserControlled(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-11 15:05:11 +01:00
|
|
|
void _insertMovie(MovieData movie) {
|
2023-11-08 14:59:23 +01:00
|
|
|
int min = 0;
|
|
|
|
int max = movies.length - 1;
|
2024-01-10 23:50:43 +01:00
|
|
|
DateWithPrecision? movieDate = movie.releaseDate?.dateWithPrecision;
|
2024-01-08 11:56:35 +01:00
|
|
|
while (min <= max) {
|
|
|
|
int center = (min + max) ~/ 2;
|
2024-01-10 23:50:43 +01:00
|
|
|
DateWithPrecision? centerDate =
|
|
|
|
movies[center].releaseDate?.dateWithPrecision;
|
|
|
|
int diff = movieDate != null && centerDate != null
|
|
|
|
? movieDate.compareTo(centerDate)
|
|
|
|
: 0;
|
|
|
|
if (movieDate == null || centerDate != null && diff < 0) {
|
2023-11-08 14:59:23 +01:00
|
|
|
max = center - 1;
|
|
|
|
} else {
|
|
|
|
min = center + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
movies.insert(min, movie);
|
|
|
|
}
|
|
|
|
|
2023-11-11 15:08:08 +01:00
|
|
|
void _resortMovies() {
|
|
|
|
for (int i = 0; i < movies.length; i++) {
|
|
|
|
var temp = movies[i];
|
2024-01-10 23:50:43 +01:00
|
|
|
DateWithPrecision? tempDate = temp.releaseDate?.dateWithPrecision;
|
2023-11-11 15:08:08 +01:00
|
|
|
int j = i - 1;
|
2024-01-10 23:50:43 +01:00
|
|
|
for (; j >= 0; j--) {
|
|
|
|
DateWithPrecision? date = movies[j].releaseDate?.dateWithPrecision;
|
|
|
|
if (date == null || tempDate != null && date.compareTo(tempDate) <= 0) {
|
|
|
|
break;
|
|
|
|
}
|
2023-11-11 15:08:08 +01:00
|
|
|
movies[j + 1] = movies[j];
|
|
|
|
}
|
|
|
|
movies[j + 1] = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-11 15:05:11 +01:00
|
|
|
void removeMoviesWhere(bool Function(MovieData movie) test) {
|
2023-11-08 14:43:59 +01:00
|
|
|
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.
|
2024-01-08 21:48:13 +01:00
|
|
|
List<MovieData> localSearch(String search) {
|
|
|
|
var results = searchList(
|
|
|
|
movies,
|
|
|
|
search,
|
|
|
|
(movie) => [
|
2024-01-10 23:50:43 +01:00
|
|
|
movie.title ?? "",
|
|
|
|
...(movie.titles?.value?.map((title) => title.text) ?? []),
|
2024-01-08 21:48:13 +01:00
|
|
|
]);
|
|
|
|
return results;
|
|
|
|
}
|
2023-11-06 10:38:26 +01:00
|
|
|
|
|
|
|
/// Online search for movies.
|
2024-01-08 21:48:13 +01:00
|
|
|
Future<List<MovieData>> onlineSearch(String search) async {
|
2023-11-08 14:43:59 +01:00
|
|
|
List<MovieData> movies = await api.searchForMovies(search);
|
2023-11-06 10:38:26 +01:00
|
|
|
return addMovies(movies);
|
|
|
|
}
|
|
|
|
|
2023-11-11 15:05:11 +01:00
|
|
|
Future<void> 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
|
|
|
}
|
|
|
|
}
|