diff --git a/lib/api/api_manager.dart b/lib/api/api_manager.dart index b040e2d..27dfe8b 100644 --- a/lib/api/api_manager.dart +++ b/lib/api/api_manager.dart @@ -2,11 +2,6 @@ import 'dart:async'; import 'package:http/http.dart' as http; -class RateLimitStatus { - int consecutiveCount = 0; - Duration timeout = const Duration(seconds: 1); -} - class ApiManager { String baseUrl; Future ongoingRequest = Future.value(); @@ -59,3 +54,8 @@ class ApiManager { } } } + +class RateLimitStatus { + int consecutiveCount = 0; + Duration timeout = const Duration(seconds: 1); +} diff --git a/lib/model/date_format.dart b/lib/model/date_format.dart index 58958d0..53ee480 100644 --- a/lib/model/date_format.dart +++ b/lib/model/date_format.dart @@ -1,3 +1,21 @@ +String dateRelativeToNow(DateTime date) { + DateTime dateOnly = DateTime.utc(date.year, date.month, date.day); + DateTime now = DateTime.now().toUtc(); + DateTime today = DateTime.utc(now.year, now.month, now.day); + Duration diff = dateOnly.difference(today); + return _durationToRelativeTimeString(diff); +} + +String _durationToRelativeTimeString(Duration duration) { + if (duration.isNegative) { + return "${_durationApproximatedInWords(-duration)} ago"; + } else if (duration == Duration.zero) { + return "now"; + } else { + return "in ${_durationApproximatedInWords(duration)}"; + } +} + String _durationApproximatedInWords(Duration duration) { int seconds = duration.inSeconds; int minutes = duration.inMinutes; @@ -36,21 +54,3 @@ String _durationApproximatedInWords(Duration duration) { } return centuries > 1 ? "$centuries centuries" : "a century"; } - -String _durationToRelativeTimeString(Duration duration) { - if (duration.isNegative) { - return "${_durationApproximatedInWords(-duration)} ago"; - } else if (duration == Duration.zero) { - return "now"; - } else { - return "in ${_durationApproximatedInWords(duration)}"; - } -} - -String dateRelativeToNow(DateTime date) { - DateTime dateOnly = DateTime.utc(date.year, date.month, date.day); - DateTime now = DateTime.now().toUtc(); - DateTime today = DateTime.utc(now.year, now.month, now.day); - Duration diff = dateOnly.difference(today); - return _durationToRelativeTimeString(diff); -} diff --git a/lib/model/delayed_function_caller.dart b/lib/model/delayed_function_caller.dart new file mode 100644 index 0000000..d439537 --- /dev/null +++ b/lib/model/delayed_function_caller.dart @@ -0,0 +1,21 @@ +import 'dart:async'; + +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(); + }); + } +} diff --git a/lib/model/movie.dart b/lib/model/movie.dart index 60494fd..d9de369 100644 --- a/lib/model/movie.dart +++ b/lib/model/movie.dart @@ -1,63 +1,6 @@ import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; -class Review { - String score; - String by; - DateTime asOf; - int count; - - Review(this.score, this.by, this.asOf, this.count); - Review.fromJsonEncodable(Map json) - : score = json["score"], - by = json["by"], - asOf = DateTime.parse(json["asOf"]), - count = json["count"]; - - Map toJsonEncodable() { - return { - "score": score, - "by": by, - "asOf": asOf.toIso8601String(), - "count": count, - }; - } -} - -typedef TitleInLanguage = ({String title, String language}); - -class DateWithPrecisionAndCountry { - DateTime date; - DatePrecision precision; - String country; - - DateWithPrecisionAndCountry(this.date, this.precision, this.country); - - DateWithPrecisionAndCountry.fromJsonEncodable(List json) - : date = DateTime.parse(json[0]), - precision = DatePrecision.values - .firstWhere((element) => element.name == json[1]), - country = json[2]; - - toJsonEncodable() { - return [date.toIso8601String(), precision.name, country]; - } - - @override - String toString() { - String dateString = switch (precision) { - DatePrecision.decade || DatePrecision.year => date.year.toString(), - DatePrecision.month => DateFormat("MMMM yyyy").format(date), - DatePrecision.day => DateFormat("MMMM d, yyyy").format(date), - DatePrecision.hour => DateFormat("MMMM d, yyyy, HH").format(date), - DatePrecision.minute => DateFormat("MMMM d, yyyy, HH:mm").format(date) - }; - return "$dateString ($country)"; - } -} - -enum DatePrecision { decade, year, month, day, hour, minute } - class MovieData extends ChangeNotifier { String _title; DateWithPrecisionAndCountry _releaseDate; @@ -187,3 +130,60 @@ class MovieData extends ChangeNotifier { : null); } } + +enum DatePrecision { decade, year, month, day, hour, minute } + +typedef TitleInLanguage = ({String title, String language}); + +class DateWithPrecisionAndCountry { + DateTime date; + DatePrecision precision; + String country; + + DateWithPrecisionAndCountry(this.date, this.precision, this.country); + + DateWithPrecisionAndCountry.fromJsonEncodable(List json) + : date = DateTime.parse(json[0]), + precision = DatePrecision.values + .firstWhere((element) => element.name == json[1]), + country = json[2]; + + toJsonEncodable() { + return [date.toIso8601String(), precision.name, country]; + } + + @override + String toString() { + String dateString = switch (precision) { + DatePrecision.decade || DatePrecision.year => date.year.toString(), + DatePrecision.month => DateFormat("MMMM yyyy").format(date), + DatePrecision.day => DateFormat("MMMM d, yyyy").format(date), + DatePrecision.hour => DateFormat("MMMM d, yyyy, HH").format(date), + DatePrecision.minute => DateFormat("MMMM d, yyyy, HH:mm").format(date) + }; + return "$dateString ($country)"; + } +} + +class Review { + String score; + String by; + DateTime asOf; + int count; + + Review(this.score, this.by, this.asOf, this.count); + Review.fromJsonEncodable(Map json) + : score = json["score"], + by = json["by"], + asOf = DateTime.parse(json["asOf"]), + count = json["count"]; + + Map toJsonEncodable() { + return { + "score": score, + "by": by, + "asOf": asOf.toIso8601String(), + "count": count, + }; + } +} diff --git a/lib/model/movie_manager.dart b/lib/model/movie_manager.dart index aa348fd..07300ea 100644 --- a/lib/model/movie_manager.dart +++ b/lib/model/movie_manager.dart @@ -3,37 +3,10 @@ import 'dart:async'; 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/delayed_function_caller.dart'; import 'package:release_schedule/model/local_movie_storage.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; - } -} - -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(); - }); - } -} - final movieManager = MovieManager(WikidataMovieApi(), LocalMovieStorageGetStorage(WikidataMovieData.fromEncodable)); @@ -161,16 +134,10 @@ class MovieManager extends ChangeNotifier { } } -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); - - void updateSearch(String search) { - searchTerm = search; +T? firstWhereOrNull(List list, bool Function(T element) test) { + try { + return list.firstWhere(test); + } catch (e) { + return null; } }