feat: Also update total episode count on refresh

This commit is contained in:
2026-07-26 18:42:32 +02:00
parent 04d64c3178
commit 58ea71e694
3 changed files with 41 additions and 25 deletions

View File

@@ -75,6 +75,7 @@ abstract class Anime with _$Anime {
required String title,
required bool isAiring,
required String? broadcastDay,
required int? episodes,
}) = _Anime;
factory Anime.fromJson(Map<String, Object?> json) {
@@ -88,6 +89,7 @@ abstract class Anime with _$Anime {
return Anime(
title: (json['title']! as Map<String, dynamic>)['romaji']! as String,
isAiring: json['status'] == 'RELEASING',
episodes: json['episodes'] as int?,
broadcastDay: airingDayOfTheWeek,
);
}

View File

@@ -570,7 +570,7 @@ as String?,
/// @nodoc
mixin _$Anime {
String get title; bool get isAiring; String? get broadcastDay;
String get title; bool get isAiring; String? get broadcastDay; int? get episodes;
/// Create a copy of Anime
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@@ -581,16 +581,16 @@ $AnimeCopyWith<Anime> get copyWith => _$AnimeCopyWithImpl<Anime>(this as Anime,
@override
bool operator ==(Object other) {
return identical(this, other) || (other.runtimeType == runtimeType&&other is Anime&&(identical(other.title, title) || other.title == title)&&(identical(other.isAiring, isAiring) || other.isAiring == isAiring)&&(identical(other.broadcastDay, broadcastDay) || other.broadcastDay == broadcastDay));
return identical(this, other) || (other.runtimeType == runtimeType&&other is Anime&&(identical(other.title, title) || other.title == title)&&(identical(other.isAiring, isAiring) || other.isAiring == isAiring)&&(identical(other.broadcastDay, broadcastDay) || other.broadcastDay == broadcastDay)&&(identical(other.episodes, episodes) || other.episodes == episodes));
}
@override
int get hashCode => Object.hash(runtimeType,title,isAiring,broadcastDay);
int get hashCode => Object.hash(runtimeType,title,isAiring,broadcastDay,episodes);
@override
String toString() {
return 'Anime(title: $title, isAiring: $isAiring, broadcastDay: $broadcastDay)';
return 'Anime(title: $title, isAiring: $isAiring, broadcastDay: $broadcastDay, episodes: $episodes)';
}
@@ -601,7 +601,7 @@ abstract mixin class $AnimeCopyWith<$Res> {
factory $AnimeCopyWith(Anime value, $Res Function(Anime) _then) = _$AnimeCopyWithImpl;
@useResult
$Res call({
String title, bool isAiring, String? broadcastDay
String title, bool isAiring, String? broadcastDay, int? episodes
});
@@ -618,12 +618,13 @@ class _$AnimeCopyWithImpl<$Res>
/// Create a copy of Anime
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline') @override $Res call({Object? title = null,Object? isAiring = null,Object? broadcastDay = freezed,}) {
@pragma('vm:prefer-inline') @override $Res call({Object? title = null,Object? isAiring = null,Object? broadcastDay = freezed,Object? episodes = freezed,}) {
return _then(_self.copyWith(
title: null == title ? _self.title : title // ignore: cast_nullable_to_non_nullable
as String,isAiring: null == isAiring ? _self.isAiring : isAiring // ignore: cast_nullable_to_non_nullable
as bool,broadcastDay: freezed == broadcastDay ? _self.broadcastDay : broadcastDay // ignore: cast_nullable_to_non_nullable
as String?,
as String?,episodes: freezed == episodes ? _self.episodes : episodes // ignore: cast_nullable_to_non_nullable
as int?,
));
}
@@ -708,10 +709,10 @@ return $default(_that);case _:
/// }
/// ```
@optionalTypeArgs TResult maybeWhen<TResult extends Object?>(TResult Function( String title, bool isAiring, String? broadcastDay)? $default,{required TResult orElse(),}) {final _that = this;
@optionalTypeArgs TResult maybeWhen<TResult extends Object?>(TResult Function( String title, bool isAiring, String? broadcastDay, int? episodes)? $default,{required TResult orElse(),}) {final _that = this;
switch (_that) {
case _Anime() when $default != null:
return $default(_that.title,_that.isAiring,_that.broadcastDay);case _:
return $default(_that.title,_that.isAiring,_that.broadcastDay,_that.episodes);case _:
return orElse();
}
@@ -729,10 +730,10 @@ return $default(_that.title,_that.isAiring,_that.broadcastDay);case _:
/// }
/// ```
@optionalTypeArgs TResult when<TResult extends Object?>(TResult Function( String title, bool isAiring, String? broadcastDay) $default,) {final _that = this;
@optionalTypeArgs TResult when<TResult extends Object?>(TResult Function( String title, bool isAiring, String? broadcastDay, int? episodes) $default,) {final _that = this;
switch (_that) {
case _Anime():
return $default(_that.title,_that.isAiring,_that.broadcastDay);case _:
return $default(_that.title,_that.isAiring,_that.broadcastDay,_that.episodes);case _:
throw StateError('Unexpected subclass');
}
@@ -749,10 +750,10 @@ return $default(_that.title,_that.isAiring,_that.broadcastDay);case _:
/// }
/// ```
@optionalTypeArgs TResult? whenOrNull<TResult extends Object?>(TResult? Function( String title, bool isAiring, String? broadcastDay)? $default,) {final _that = this;
@optionalTypeArgs TResult? whenOrNull<TResult extends Object?>(TResult? Function( String title, bool isAiring, String? broadcastDay, int? episodes)? $default,) {final _that = this;
switch (_that) {
case _Anime() when $default != null:
return $default(_that.title,_that.isAiring,_that.broadcastDay);case _:
return $default(_that.title,_that.isAiring,_that.broadcastDay,_that.episodes);case _:
return null;
}
@@ -764,12 +765,13 @@ return $default(_that.title,_that.isAiring,_that.broadcastDay);case _:
class _Anime implements Anime {
const _Anime({required this.title, required this.isAiring, required this.broadcastDay});
const _Anime({required this.title, required this.isAiring, required this.broadcastDay, required this.episodes});
@override final String title;
@override final bool isAiring;
@override final String? broadcastDay;
@override final int? episodes;
/// Create a copy of Anime
/// with the given fields replaced by the non-null parameter values.
@@ -781,16 +783,16 @@ _$AnimeCopyWith<_Anime> get copyWith => __$AnimeCopyWithImpl<_Anime>(this, _$ide
@override
bool operator ==(Object other) {
return identical(this, other) || (other.runtimeType == runtimeType&&other is _Anime&&(identical(other.title, title) || other.title == title)&&(identical(other.isAiring, isAiring) || other.isAiring == isAiring)&&(identical(other.broadcastDay, broadcastDay) || other.broadcastDay == broadcastDay));
return identical(this, other) || (other.runtimeType == runtimeType&&other is _Anime&&(identical(other.title, title) || other.title == title)&&(identical(other.isAiring, isAiring) || other.isAiring == isAiring)&&(identical(other.broadcastDay, broadcastDay) || other.broadcastDay == broadcastDay)&&(identical(other.episodes, episodes) || other.episodes == episodes));
}
@override
int get hashCode => Object.hash(runtimeType,title,isAiring,broadcastDay);
int get hashCode => Object.hash(runtimeType,title,isAiring,broadcastDay,episodes);
@override
String toString() {
return 'Anime(title: $title, isAiring: $isAiring, broadcastDay: $broadcastDay)';
return 'Anime(title: $title, isAiring: $isAiring, broadcastDay: $broadcastDay, episodes: $episodes)';
}
@@ -801,7 +803,7 @@ abstract mixin class _$AnimeCopyWith<$Res> implements $AnimeCopyWith<$Res> {
factory _$AnimeCopyWith(_Anime value, $Res Function(_Anime) _then) = __$AnimeCopyWithImpl;
@override @useResult
$Res call({
String title, bool isAiring, String? broadcastDay
String title, bool isAiring, String? broadcastDay, int? episodes
});
@@ -818,12 +820,13 @@ class __$AnimeCopyWithImpl<$Res>
/// Create a copy of Anime
/// with the given fields replaced by the non-null parameter values.
@override @pragma('vm:prefer-inline') $Res call({Object? title = null,Object? isAiring = null,Object? broadcastDay = freezed,}) {
@override @pragma('vm:prefer-inline') $Res call({Object? title = null,Object? isAiring = null,Object? broadcastDay = freezed,Object? episodes = freezed,}) {
return _then(_Anime(
title: null == title ? _self.title : title // ignore: cast_nullable_to_non_nullable
as String,isAiring: null == isAiring ? _self.isAiring : isAiring // ignore: cast_nullable_to_non_nullable
as bool,broadcastDay: freezed == broadcastDay ? _self.broadcastDay : broadcastDay // ignore: cast_nullable_to_non_nullable
as String?,
as String?,episodes: freezed == episodes ? _self.episodes : episodes // ignore: cast_nullable_to_non_nullable
as int?,
));
}

View File

@@ -44,29 +44,36 @@ class CalendarBloc extends Bloc<CalendarEvent, CalendarState> {
String? broadcastDay;
bool airing;
int? episodes;
try {
switch (anime.source) {
case TrackingDataSource.mal:
final apiData = await Jikan().getAnime(int.parse(anime.id));
airing = apiData.airing;
broadcastDay = apiData.broadcast?.split(' ').first;
episodes = apiData.episodes;
case TrackingDataSource.anilist:
final apiData = await GetIt.I.get<AniListClient>().getAnimeById(
anime.id,
);
airing = apiData.isAiring;
broadcastDay = apiData.broadcastDay;
episodes = apiData.episodes;
}
} catch (ex) {
print('API request for anime ${anime.id} failed: $ex');
airing = false;
continue;
}
print('Anime "${anime.title}": airing=$airing');
print('Anime "${anime.title}": airing=$airing; episodes=$episodes');
if (!airing) {
al.add(
AnimeUpdatedEvent(
anime.copyWith(airing: false, broadcastDay: null),
anime.copyWith(
airing: false,
broadcastDay: null,
episodesTotal: episodes,
),
commit: true,
),
);
@@ -74,13 +81,17 @@ class CalendarBloc extends Bloc<CalendarEvent, CalendarState> {
print('Updating Anime "${anime.title}": broadcastDay=$broadcastDay');
al.add(
AnimeUpdatedEvent(
anime.copyWith(airing: true, broadcastDay: broadcastDay),
anime.copyWith(
airing: true,
broadcastDay: broadcastDay,
episodesTotal: episodes,
),
commit: true,
),
);
}
// Prevent hammering Jikan
// Prevent hammering Jikan/AniList
await Future<void>.delayed(const Duration(milliseconds: 500));
}