refactor: change ordering of classes and functions
parent
5c11b931c0
commit
bbe2c6a718
|
@ -2,11 +2,6 @@ import 'dart:async';
|
||||||
|
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
|
|
||||||
class RateLimitStatus {
|
|
||||||
int consecutiveCount = 0;
|
|
||||||
Duration timeout = const Duration(seconds: 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
class ApiManager {
|
class ApiManager {
|
||||||
String baseUrl;
|
String baseUrl;
|
||||||
Future<void> ongoingRequest = Future.value();
|
Future<void> ongoingRequest = Future.value();
|
||||||
|
@ -59,3 +54,8 @@ class ApiManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class RateLimitStatus {
|
||||||
|
int consecutiveCount = 0;
|
||||||
|
Duration timeout = const Duration(seconds: 1);
|
||||||
|
}
|
||||||
|
|
|
@ -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) {
|
String _durationApproximatedInWords(Duration duration) {
|
||||||
int seconds = duration.inSeconds;
|
int seconds = duration.inSeconds;
|
||||||
int minutes = duration.inMinutes;
|
int minutes = duration.inMinutes;
|
||||||
|
@ -36,21 +54,3 @@ String _durationApproximatedInWords(Duration duration) {
|
||||||
}
|
}
|
||||||
return centuries > 1 ? "$centuries centuries" : "a century";
|
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);
|
|
||||||
}
|
|
||||||
|
|
|
@ -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();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,63 +1,6 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:intl/intl.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<dynamic> 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 {
|
class MovieData extends ChangeNotifier {
|
||||||
String _title;
|
String _title;
|
||||||
DateWithPrecisionAndCountry _releaseDate;
|
DateWithPrecisionAndCountry _releaseDate;
|
||||||
|
@ -187,3 +130,60 @@ class MovieData extends ChangeNotifier {
|
||||||
: null);
|
: 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<dynamic> 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,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -3,37 +3,10 @@ import 'dart:async';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:release_schedule/api/movie_api.dart';
|
import 'package:release_schedule/api/movie_api.dart';
|
||||||
import 'package:release_schedule/api/wikidata_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/local_movie_storage.dart';
|
||||||
import 'package:release_schedule/model/movie.dart';
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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(),
|
final movieManager = MovieManager(WikidataMovieApi(),
|
||||||
LocalMovieStorageGetStorage(WikidataMovieData.fromEncodable));
|
LocalMovieStorageGetStorage(WikidataMovieData.fromEncodable));
|
||||||
|
|
||||||
|
@ -161,16 +134,10 @@ class MovieManager extends ChangeNotifier {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class LiveSearch<CustomMovieData extends MovieData> extends ChangeNotifier {
|
T? firstWhereOrNull<T>(List<T> list, bool Function(T element) test) {
|
||||||
String searchTerm = "";
|
try {
|
||||||
List<CustomMovieData> searchResults = [];
|
return list.firstWhere(test);
|
||||||
Duration minTimeBetweenRequests = const Duration(milliseconds: 500);
|
} catch (e) {
|
||||||
Duration minTimeAfterChangeToRequest = const Duration(milliseconds: 200);
|
return null;
|
||||||
final MovieManager manager;
|
|
||||||
|
|
||||||
LiveSearch(this.manager);
|
|
||||||
|
|
||||||
void updateSearch(String search) {
|
|
||||||
searchTerm = search;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue