release_schedule/lib/model/movie.dart

155 lines
4.1 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:release_schedule/model/dates.dart';
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;
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
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;
}
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 {
return _releaseDates;
}
List<String>? get genres {
return _genres;
}
2023-11-16 12:51:45 +01:00
List<TitleInLanguage>? get titles {
return _titles;
}
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(
title: movie.title,
releaseDate: movie.releaseDate,
2023-11-08 14:43:59 +01:00
releaseDates: movie.releaseDates,
genres: movie.genres,
titles: movie.titles);
2023-11-08 14:43:59 +01:00
}
void setDetails(
{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,
List<String>? genres,
List<TitleInLanguage>? titles}) {
if (title != null) {
_title = title;
}
if (releaseDate != null) {
_releaseDate = releaseDate;
}
2024-01-08 12:57:36 +01:00
if (bookmarked != null) {
_bookmarked = bookmarked;
}
if (releaseDates != null) {
_releaseDates = releaseDates;
}
if (genres != null) {
_genres = genres;
}
if (titles != null) {
_titles = titles;
}
_hasDetails = true;
notifyListeners();
}
@override
String toString() {
return "$title (${_releaseDate.toString()}${_genres?.isNotEmpty ?? false ? "; ${_genres?.join(", ")}" : ""})";
2023-11-08 14:43:59 +01:00
}
2024-01-08 12:57:36 +01:00
bool same(MovieData other) {
2024-01-09 14:48:36 +01:00
return title == other.title &&
releaseDate.dateWithPrecision == other.releaseDate.dateWithPrecision;
2024-01-08 12:57:36 +01:00
}
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-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,
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);
}
}
typedef TitleInLanguage = ({String title, String language});
2024-01-09 14:48:36 +01:00
class DateWithPrecisionAndCountry {
final DateWithPrecision dateWithPrecision;
final String country;
DateWithPrecisionAndCountry(
DateTime date, DatePrecision precision, this.country)
: dateWithPrecision = DateWithPrecision(date, precision);
DateWithPrecisionAndCountry.fromJsonEncodable(List<dynamic> json)
: dateWithPrecision = DateWithPrecision.fromJsonEncodable(json),
country = json[2];
toJsonEncodable() {
return dateWithPrecision.toJsonEncodable() + [country];
}
@override
String toString() {
return "${dateWithPrecision.toString()} ($country)";
}
}