2023-11-06 10:38:26 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2023-11-16 12:51:45 +01:00
|
|
|
import 'package:intl/intl.dart';
|
2023-11-06 10:38:26 +01:00
|
|
|
|
|
|
|
class MovieData extends ChangeNotifier {
|
2023-11-08 14:43:59 +01:00
|
|
|
String _title;
|
2023-11-16 12:51:45 +01:00
|
|
|
DateWithPrecisionAndCountry _releaseDate;
|
2024-01-08 12:57:36 +01:00
|
|
|
bool _bookmarked = false;
|
2023-11-11 14:50:20 +01:00
|
|
|
|
2023-11-06 10:38:26 +01:00
|
|
|
bool _hasDetails = false;
|
2023-11-16 12:51:45 +01:00
|
|
|
List<DateWithPrecisionAndCountry>? _releaseDates;
|
2023-11-08 14:43:59 +01:00
|
|
|
List<String>? _genres;
|
2023-11-16 12:51:45 +01:00
|
|
|
List<TitleInLanguage>? _titles;
|
2023-11-08 14:43:59 +01:00
|
|
|
List<Review>? _reviews;
|
|
|
|
|
2023-11-16 12:51:45 +01:00
|
|
|
MovieData(this._title, this._releaseDate);
|
2023-11-11 15:05:11 +01:00
|
|
|
|
2023-11-08 14:43:59 +01:00
|
|
|
String get title {
|
|
|
|
return _title;
|
|
|
|
}
|
|
|
|
|
2023-11-16 12:51:45 +01:00
|
|
|
DateWithPrecisionAndCountry get releaseDate {
|
2023-11-08 14:43:59 +01:00
|
|
|
return _releaseDate;
|
|
|
|
}
|
2023-11-06 10:38:26 +01:00
|
|
|
|
2024-01-08 12:57:36 +01:00
|
|
|
bool get bookmarked {
|
|
|
|
return _bookmarked;
|
|
|
|
}
|
|
|
|
|
2023-11-16 12:51:45 +01:00
|
|
|
List<DateWithPrecisionAndCountry>? get releaseDates {
|
2023-11-06 10:38:26 +01:00
|
|
|
return _releaseDates;
|
|
|
|
}
|
|
|
|
|
|
|
|
List<String>? get genres {
|
|
|
|
return _genres;
|
|
|
|
}
|
|
|
|
|
2023-11-16 12:51:45 +01:00
|
|
|
List<TitleInLanguage>? get titles {
|
2023-11-06 10:38:26 +01:00
|
|
|
return _titles;
|
|
|
|
}
|
|
|
|
|
|
|
|
List<Review>? get reviews {
|
|
|
|
return _reviews;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool get hasDetails {
|
|
|
|
return _hasDetails;
|
|
|
|
}
|
|
|
|
|
2024-01-08 12:57:36 +01:00
|
|
|
/// Updates the information with that of a new version of the movie
|
|
|
|
/// but ignores fields that are user controlled, like whether the movie was bookmarked.
|
|
|
|
void updateWithNewIgnoringUserControlled(MovieData movie) {
|
2023-11-08 14:43:59 +01:00
|
|
|
setDetails(
|
2023-11-11 14:50:20 +01:00
|
|
|
title: movie.title,
|
|
|
|
releaseDate: movie.releaseDate,
|
2023-11-08 14:43:59 +01:00
|
|
|
releaseDates: movie.releaseDates,
|
|
|
|
genres: movie.genres,
|
|
|
|
titles: movie.titles,
|
|
|
|
reviews: movie.reviews);
|
|
|
|
}
|
|
|
|
|
2023-11-06 10:38:26 +01:00
|
|
|
void setDetails(
|
2023-11-11 14:50:20 +01:00
|
|
|
{String? title,
|
2023-11-16 12:51:45 +01:00
|
|
|
DateWithPrecisionAndCountry? releaseDate,
|
2024-01-08 12:57:36 +01:00
|
|
|
bool? bookmarked,
|
2023-11-16 12:51:45 +01:00
|
|
|
List<DateWithPrecisionAndCountry>? releaseDates,
|
2023-11-06 10:38:26 +01:00
|
|
|
List<String>? genres,
|
2023-11-16 12:51:45 +01:00
|
|
|
List<TitleInLanguage>? titles,
|
2023-11-06 10:38:26 +01:00
|
|
|
List<Review>? reviews}) {
|
2023-11-11 14:50:20 +01:00
|
|
|
if (title != null) {
|
|
|
|
_title = title;
|
|
|
|
}
|
|
|
|
if (releaseDate != null) {
|
|
|
|
_releaseDate = releaseDate;
|
|
|
|
}
|
2024-01-08 12:57:36 +01:00
|
|
|
if (bookmarked != null) {
|
|
|
|
_bookmarked = bookmarked;
|
|
|
|
}
|
2023-11-06 10:38:26 +01:00
|
|
|
if (releaseDates != null) {
|
|
|
|
_releaseDates = releaseDates;
|
|
|
|
}
|
|
|
|
if (genres != null) {
|
|
|
|
_genres = genres;
|
|
|
|
}
|
|
|
|
if (titles != null) {
|
|
|
|
_titles = titles;
|
|
|
|
}
|
|
|
|
if (reviews != null) {
|
|
|
|
_reviews = reviews;
|
|
|
|
}
|
|
|
|
_hasDetails = true;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
String toString() {
|
2023-11-16 12:51:45 +01:00
|
|
|
return "$title (${_releaseDate.toString()}${_genres?.isNotEmpty ?? true ? "; ${_genres?.join(", ")}" : ""})";
|
2023-11-08 14:43:59 +01:00
|
|
|
}
|
|
|
|
|
2024-01-08 12:57:36 +01:00
|
|
|
bool same(MovieData other) {
|
|
|
|
return title == other.title && releaseDate.date == other.releaseDate.date;
|
|
|
|
}
|
|
|
|
|
2023-11-08 14:43:59 +01:00
|
|
|
Map toJsonEncodable() {
|
|
|
|
List? releaseDatesByCountry =
|
2023-11-16 12:51:45 +01:00
|
|
|
_releaseDates?.map((e) => e.toJsonEncodable()).toList();
|
|
|
|
List? titlesByCountry = _titles?.map((e) => [e.title, e.language]).toList();
|
2023-11-08 14:43:59 +01:00
|
|
|
return {
|
|
|
|
"title": title,
|
2023-11-16 12:51:45 +01:00
|
|
|
"releaseDate": _releaseDate.toJsonEncodable(),
|
2024-01-08 12:57:36 +01:00
|
|
|
"bookmarked": _bookmarked,
|
2023-11-08 14:43:59 +01:00
|
|
|
"releaseDates": releaseDatesByCountry,
|
|
|
|
"genres": genres,
|
|
|
|
"titles": titlesByCountry,
|
2023-11-16 14:38:33 +01:00
|
|
|
"reviews": reviews?.map((review) => review.toJsonEncodable()).toList(),
|
2023-11-08 14:43:59 +01:00
|
|
|
};
|
2023-11-06 10:38:26 +01:00
|
|
|
}
|
|
|
|
|
2023-11-08 14:43:59 +01:00
|
|
|
MovieData.fromJsonEncodable(Map json)
|
|
|
|
: _title = json["title"],
|
2023-11-16 12:51:45 +01:00
|
|
|
_releaseDate =
|
|
|
|
DateWithPrecisionAndCountry.fromJsonEncodable(json["releaseDate"]) {
|
2023-11-08 14:43:59 +01:00
|
|
|
setDetails(
|
2024-01-08 12:57:36 +01:00
|
|
|
bookmarked: json["bookmarked"] as bool,
|
2023-11-16 12:51:45 +01:00
|
|
|
genres: (json["genres"] as List<dynamic>?)
|
|
|
|
?.map((genre) => genre as String)
|
|
|
|
.toList(),
|
2023-11-08 14:43:59 +01:00
|
|
|
releaseDates: json["releaseDates"] != null
|
2023-11-16 14:38:33 +01:00
|
|
|
? (json["releaseDates"] as List<dynamic>)
|
2023-11-16 12:51:45 +01:00
|
|
|
.map((release) =>
|
|
|
|
DateWithPrecisionAndCountry.fromJsonEncodable(release))
|
2023-11-08 14:43:59 +01:00
|
|
|
.toList()
|
|
|
|
: null,
|
|
|
|
reviews: json["reviews"] != null
|
2023-11-16 14:38:33 +01:00
|
|
|
? (json["reviews"] as List<dynamic>)
|
2023-11-08 14:43:59 +01:00
|
|
|
.map((review) => Review.fromJsonEncodable(review))
|
|
|
|
.toList()
|
|
|
|
: null,
|
|
|
|
titles: json["titles"] != null
|
|
|
|
? (json["titles"] as List<dynamic>)
|
2023-11-16 12:51:45 +01:00
|
|
|
.map((title) =>
|
|
|
|
(title: title[0], language: title[1]) as TitleInLanguage)
|
2023-11-08 14:43:59 +01:00
|
|
|
.toList()
|
|
|
|
: null);
|
|
|
|
}
|
2023-11-06 10:38:26 +01:00
|
|
|
}
|
2023-11-16 13:12:55 +01:00
|
|
|
|
|
|
|
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) {
|
2023-11-16 14:38:33 +01:00
|
|
|
DatePrecision.decade =>
|
|
|
|
"${DateFormat("yyyy").format(date).substring(0, 3)}0s",
|
|
|
|
DatePrecision.year => date.year.toString(),
|
2023-11-16 13:12:55 +01:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|