fix: Multiple fixes

- Improve performance when a lot of items are "unknown". Thanks to https://github.com/dattran-pt19/group_grid_view
- Fix some behaviour in the refresh code
This commit is contained in:
PapaTutuWawa 2023-07-16 17:37:47 +02:00
parent 1481841009
commit f081bd7c43
3 changed files with 238 additions and 215 deletions

View File

@ -36,6 +36,8 @@ class AnimeListBloc extends Bloc<AnimeListEvent, AnimeListState> {
final List<MangaTrackingData> _mangas =
List<MangaTrackingData>.empty(growable: true);
List<AnimeTrackingData> get unfilteredAnime => _animes;
List<AnimeTrackingData> _getFilteredAnime({
MediumTrackingState? trackingState,
}) {

View File

@ -28,7 +28,7 @@ class CalendarBloc extends Bloc<CalendarEvent, CalendarState> {
);
final al = GetIt.I.get<AnimeListBloc>();
final animes = al.state.animes.where((anime) => anime.airing);
final animes = al.unfilteredAnime.where((anime) => anime.airing);
emit(
state.copyWith(
refreshing: true,
@ -40,21 +40,33 @@ class CalendarBloc extends Bloc<CalendarEvent, CalendarState> {
for (final anime in animes) {
emit(state.copyWith(refreshingCount: state.refreshingCount + 1));
Anime apiData;
String? broadcastDay;
bool airing;
try {
apiData = await Jikan().getAnime(int.parse(anime.id));
} catch (_) {
print('API request for anime ${anime.id} failed');
continue;
final apiData = await Jikan().getAnime(int.parse(anime.id));
airing = apiData.airing;
broadcastDay = apiData.broadcast?.split(' ').first;
} catch (ex) {
print('API request for anime ${anime.id} failed: $ex');
airing = false;
}
if (!apiData.airing) {
print('Anime "${anime.title}": airing=${airing}');
if (!airing) {
al.add(
AnimeUpdatedEvent(
anime.copyWith(airing: false, broadcastDay: null),
commit: true,
),
);
} else if (anime.broadcastDay != broadcastDay) {
print('Updating Anime "${anime.title}": broadcastDay=$broadcastDay');
al.add(
AnimeUpdatedEvent(
anime.copyWith(airing: true, broadcastDay: broadcastDay),
commit: true,
),
);
}
// Prevent hammering Jikan

View File

@ -9,6 +9,7 @@ import 'package:anitrack/src/ui/widgets/grid_item.dart';
import 'package:anitrack/src/ui/widgets/image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:get_it/get_it.dart';
enum Weekday {
monday,
@ -52,7 +53,7 @@ extension AddIfExists<K, V> on Map<K, List<V>> {
}
}
class CalendarPage extends StatelessWidget {
class CalendarPage extends StatefulWidget {
const CalendarPage({super.key});
static MaterialPageRoute<dynamic> get route => MaterialPageRoute<dynamic>(
@ -62,6 +63,11 @@ class CalendarPage extends StatelessWidget {
),
);
@override
CalendarPageState createState() => CalendarPageState();
}
class CalendarPageState extends State<CalendarPage> {
List<Widget> _renderWeekdayList(
BuildContext context,
Weekday day,
@ -73,11 +79,12 @@ class CalendarPage extends StatelessWidget {
assert(data[day]!.isNotEmpty, 'There should be at least one anime');
return [
Padding(
SliverToBoxAdapter(
child: Padding(
padding: const EdgeInsets.only(
left: 16,
left: 0,
right: 16,
top: 16,
top: 20,
bottom: 4,
),
child: Text(
@ -85,11 +92,8 @@ class CalendarPage extends StatelessWidget {
style: Theme.of(context).textTheme.titleLarge,
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: GridView.builder(
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
),
SliverGrid.builder(
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
mainAxisSpacing: 8,
@ -115,19 +119,13 @@ class CalendarPage extends StatelessWidget {
);
},
),
),
];
}
@override
Widget build(BuildContext context) {
return BlocBuilder<CalendarBloc, CalendarState>(
builder: (context, calendarState) =>
BlocBuilder<AnimeListBloc, AnimeListState>(
buildWhen: (previous, current) => !calendarState.refreshing,
builder: (context, state) {
final airingAnimeMap = <Weekday, List<AnimeTrackingData>>{};
for (final anime in state.animes) {
for (final anime in GetIt.I.get<AnimeListBloc>().unfilteredAnime) {
if (!anime.airing) continue;
final Weekday day;
@ -161,7 +159,16 @@ class CalendarPage extends StatelessWidget {
airingAnimeMap.addOrSet(day, anime);
}
return Scaffold(
return BlocListener<CalendarBloc, CalendarState>(
listenWhen: (previous, current) =>
previous.refreshing != current.refreshing,
listener: (context, state) {
// Force an update
if (!state.refreshing) {
setState(() {});
}
},
child: Scaffold(
appBar: AppBar(
title: Text(t.calendar.calendar),
actions: [
@ -177,12 +184,12 @@ class CalendarPage extends StatelessWidget {
body: Stack(
children: [
Positioned(
left: 0,
right: 0,
left: 8,
right: 8,
top: 0,
bottom: 0,
child: ListView(
children: [
child: CustomScrollView(
slivers: [
// Render all available weekdays
..._renderWeekdayList(
context,
@ -227,9 +234,11 @@ class CalendarPage extends StatelessWidget {
// Provide a nice bottom padding, while keeping the elastic effect attached
// to the bottom-most edge.
const SizedBox(
const SliverToBoxAdapter(
child: SizedBox(
height: 16,
),
),
],
),
),
@ -239,6 +248,8 @@ class CalendarPage extends StatelessWidget {
top: 0,
bottom: 0,
child: BlocBuilder<CalendarBloc, CalendarState>(
buildWhen: (previous, current) =>
previous.refreshing != current.refreshing,
builder: (context, state) {
if (!state.refreshing) {
return const SizedBox();
@ -296,8 +307,6 @@ class CalendarPage extends StatelessWidget {
),
],
),
);
},
),
);
}