Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 42 additions & 20 deletions app/lib/src/dashboard/widgets/upcoming_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:intl/intl.dart';

import '../../screens/calendar_groups.dart';
import '../../screens/calendar_screen.dart';
import '../dashboard_widget_card.dart';
import '../dashboard_widget_kind.dart';
Expand Down Expand Up @@ -54,7 +55,10 @@ class DashboardUpcomingWidget extends ConsumerWidget {
return !d.isBefore(windowStart) && d.isBefore(windowEnd);
}).toList()
..sort((CalendarEvent a, CalendarEvent b) => a.date.compareTo(b.date));
final List<CalendarEvent> top = upcoming.take(5).toList();
// A series' episodes on one day are one row, so a season dropped at once
// does not fill the widget; every count below is in rows.
final List<CalendarGroup> groups = groupCalendarEvents(upcoming);
final List<CalendarGroup> top = groups.take(5).toList();

Widget body;
if (upcoming.isEmpty && anyLoading) {
Expand All @@ -81,8 +85,8 @@ class DashboardUpcomingWidget extends ConsumerWidget {
} else {
final List<Widget> rows = <Widget>[];
DateTime? lastDay;
for (final CalendarEvent e in top) {
final DateTime d = e.date.toLocal();
for (final CalendarGroup g in top) {
final DateTime d = g.date.toLocal();
final DateTime day = DateTime(d.year, d.month, d.day);
if (lastDay == null || day != lastDay) {
lastDay = day;
Expand Down Expand Up @@ -118,13 +122,12 @@ class DashboardUpcomingWidget extends ConsumerWidget {
} else {
rows.add(const SizedBox(height: Insets.sm));
}
rows.add(_ReleaseBanner(event: e));
rows.add(_ReleaseBanner(group: g));
}
if (upcoming.length > top.length) {
if (groups.length > top.length) {
rows.add(Padding(
padding: const EdgeInsets.only(top: Insets.sm),
child:
DashboardIdleRow(text: '+${upcoming.length - top.length} more'),
child: DashboardIdleRow(text: '+${groups.length - top.length} more'),
));
}
body = Column(
Expand All @@ -137,11 +140,10 @@ class DashboardUpcomingWidget extends ConsumerWidget {
kind: DashboardWidgetKind.upcoming,
accent: cs.secondary,
onTap: () => context.goNamed(AtriumRoutes.calendarName),
trailing: upcoming.isNotEmpty
trailing: groups.isNotEmpty
? DashboardPill(
icon: Icons.calendar_today_outlined,
label:
'${upcoming.length} release${upcoming.length == 1 ? '' : 's'}',
label: '${groups.length} release${groups.length == 1 ? '' : 's'}',
color: cs.primary,
)
: null,
Expand All @@ -164,12 +166,13 @@ class DashboardUpcomingWidget extends ConsumerWidget {
/// poster thumb, title + meta line, and a trailing badge - a check for a
/// grabbed release, an airtime chip for an episode still to air.
class _ReleaseBanner extends StatelessWidget {
const _ReleaseBanner({required this.event});
const _ReleaseBanner({required this.group});

final CalendarEvent event;
final CalendarGroup group;

@override
Widget build(BuildContext context) {
final CalendarEvent event = group.first;
final ThemeData theme = Theme.of(context);
final ColorScheme cs = theme.colorScheme;
final String? backdrop = event.backdropUrl;
Expand All @@ -186,9 +189,10 @@ class _ReleaseBanner extends StatelessWidget {
hasArt ? onArt.withValues(alpha: 0.78) : cs.onSurfaceVariant;
// Episodes carry a real airtime; movie release dates do not.
final String? airTime = event is SonarrCalendarEvent
? DateFormat.jm().format(event.date.toLocal())
? DateFormat.jm().format(group.date.toLocal())
: null;


return ClipRRect(
borderRadius: BorderRadius.circular(16),
child: SizedBox(
Expand Down Expand Up @@ -254,20 +258,38 @@ class _ReleaseBanner extends StatelessWidget {
),
),
const SizedBox(height: 3),
Text(
event.subtitle,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: theme.textTheme.bodySmall
?.copyWith(color: subColor),
Row(
children: <Widget>[
Flexible(
child: Text(
event.subtitle,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: theme.textTheme.bodySmall
?.copyWith(color: subColor),
),
),
// Its own text, so a long episode title is cut
// before the count is.
if (group.more > 0) ...<Widget>[
const SizedBox(width: Insets.xs),
Text(
'+${group.more} more',
style: theme.textTheme.bodySmall?.copyWith(
color: subColor,
fontWeight: FontWeight.w700,
),
),
],
],
),
],
),
),
const SizedBox(width: Insets.sm),
_StatusBadge(
hasArt: hasArt,
hasFile: event.hasFile,
hasFile: group.allHaveFiles,
airTime: airTime,
onArt: onArt,
scrim: scrim,
Expand Down
94 changes: 94 additions & 0 deletions app/lib/src/screens/calendar_groups.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import 'calendar_screen.dart';

/// Where a release stands, as the calendar's badge and accent show it.
enum CalendarStatus { downloaded, upcoming, missing }

/// [event]'s own status at [now]: downloaded with a file, upcoming until it
/// airs, missing once it has aired without one.
CalendarStatus calendarStatusOf(CalendarEvent event, DateTime now) {
if (event.hasFile) return CalendarStatus.downloaded;
return event.date.isAfter(now)
? CalendarStatus.upcoming
: CalendarStatus.missing;
}

/// One calendar row: a release, or every episode of one series that airs on
/// the same day on the same instance (a season dropped at once).
class CalendarGroup {
CalendarGroup._(this.id, this.events);

/// Stable across rebuilds and refreshes: the instance, the series and the
/// day for a group of episodes, the item itself otherwise. Rows are keyed
/// by it so an opened row stays with its own group.
final Object id;

/// Oldest episode first; a single release is a group of one.
final List<CalendarEvent> events;

/// The event the row is drawn from.
CalendarEvent get first => events.first;

/// How many more the row stands for, shown as "+N more".
int get more => events.length - 1;

/// The earliest air time in the group.
DateTime get date => events
.map((CalendarEvent e) => e.date)
.reduce((DateTime a, DateTime b) => b.isBefore(a) ? b : a);

/// Monitored when any of them is, since Sonarr will grab that one.
bool get monitored => events.any((CalendarEvent e) => e.monitored);

bool get allHaveFiles => events.every((CalendarEvent e) => e.hasFile);

/// Missing if any has aired without a file, so a gap is never hidden
/// behind a happier badge; then upcoming if any is still to air;
/// downloaded only when every one has its file.
CalendarStatus status(DateTime now) {
final Iterable<CalendarStatus> all =
events.map((CalendarEvent e) => calendarStatusOf(e, now));
if (all.contains(CalendarStatus.missing)) return CalendarStatus.missing;
if (all.contains(CalendarStatus.upcoming)) return CalendarStatus.upcoming;
return CalendarStatus.downloaded;
}
}

/// Folds [events] into calendar rows, in time order.
///
/// Episodes of the same series on the same instance and the same local day
/// share a row. Movies and albums keep one row each. An episode that arrives
/// twice, as it can from two months' calendars meeting at a boundary, counts
/// once.
List<CalendarGroup> groupCalendarEvents(Iterable<CalendarEvent> events) {
final Map<Object, List<CalendarEvent>> byKey = <Object, List<CalendarEvent>>{};
final Set<(String, int)> seenEpisodes = <(String, int)>{};
for (final CalendarEvent e in events) {
final DateTime local = e.date.toLocal();
final DateTime day = DateTime(local.year, local.month, local.day);
final Object key = switch (e) {
SonarrCalendarEvent() => ('episodes', e.instance.id, e.episode.seriesId, day),
RadarrCalendarEvent() => ('movie', e.instance.id, e.movie.id, day),
LidarrCalendarEvent() => ('album', e.instance.id, e.album.id, day),
};
if (e is SonarrCalendarEvent &&
!seenEpisodes.add((e.instance.id, e.episode.id))) {
continue;
}
byKey.putIfAbsent(key, () => <CalendarEvent>[]).add(e);
}
final List<CalendarGroup> groups = <CalendarGroup>[
for (final MapEntry<Object, List<CalendarEvent>> entry in byKey.entries)
CalendarGroup._(entry.key, entry.value..sort(_episodeOrder)),
]..sort((CalendarGroup a, CalendarGroup b) => a.date.compareTo(b.date));
return groups;
}

int _episodeOrder(CalendarEvent a, CalendarEvent b) {
if (a is SonarrCalendarEvent && b is SonarrCalendarEvent) {
final int bySeason =
a.episode.seasonNumber.compareTo(b.episode.seasonNumber);
if (bySeason != 0) return bySeason;
return a.episode.episodeNumber.compareTo(b.episode.episodeNumber);
}
return a.date.compareTo(b.date);
}
Loading
Loading