import 'package:flutter/material.dart'; import 'package:release_schedule/api/movie_api.dart'; import 'package:release_schedule/api/wikidata_movie_api.dart'; import 'package:release_schedule/model/movie.dart'; T? firstWhereOrNull(List list, bool Function(T element) test) { try { return list.firstWhere(test); } catch (e) { return null; } } final movieManager = MovieManager(WikidataMovieApi()); class MovieManager extends ChangeNotifier { final List movies = List.empty(growable: true); final MovieApi api; MovieManager(this.api); List addMovies(List additionalMovies) { List actualMovies = []; bool added = false; for (var movie in additionalMovies) { CustomMovieData? existing = firstWhereOrNull(movies, (element) => movie.same(element)); if (existing == null) { movies.add(movie); added = true; actualMovies.add(movie); } else { actualMovies.add(existing); } } if (added) { notifyListeners(); } return actualMovies; } /// Only search locally cached movies. localSearch(String search) {} /// Online search for movies. Future> search(String search) async { List movies = await api.searchForMovies(search); return addMovies(movies); } expandDetails(List movies) { api.addMovieDetails(movies); } loadUpcomingMovies() async { List movies = await api.getUpcomingMovies(); addMovies(movies); } } class LiveSearch extends ChangeNotifier { String searchTerm = ""; List searchResults = []; Duration minTimeBetweenRequests = const Duration(milliseconds: 500); Duration minTimeAfterChangeToRequest = const Duration(milliseconds: 200); final MovieManager manager; LiveSearch(this.manager); updateSearch(String search) { searchTerm = search; } }