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 = final List<MangaTrackingData> _mangas =
List<MangaTrackingData>.empty(growable: true); List<MangaTrackingData>.empty(growable: true);
List<AnimeTrackingData> get unfilteredAnime => _animes;
List<AnimeTrackingData> _getFilteredAnime({ List<AnimeTrackingData> _getFilteredAnime({
MediumTrackingState? trackingState, MediumTrackingState? trackingState,
}) { }) {

View File

@ -28,7 +28,7 @@ class CalendarBloc extends Bloc<CalendarEvent, CalendarState> {
); );
final al = GetIt.I.get<AnimeListBloc>(); final al = GetIt.I.get<AnimeListBloc>();
final animes = al.state.animes.where((anime) => anime.airing); final animes = al.unfilteredAnime.where((anime) => anime.airing);
emit( emit(
state.copyWith( state.copyWith(
refreshing: true, refreshing: true,
@ -40,21 +40,33 @@ class CalendarBloc extends Bloc<CalendarEvent, CalendarState> {
for (final anime in animes) { for (final anime in animes) {
emit(state.copyWith(refreshingCount: state.refreshingCount + 1)); emit(state.copyWith(refreshingCount: state.refreshingCount + 1));
Anime apiData; String? broadcastDay;
bool airing;
try { try {
apiData = await Jikan().getAnime(int.parse(anime.id)); final apiData = await Jikan().getAnime(int.parse(anime.id));
} catch (_) { airing = apiData.airing;
print('API request for anime ${anime.id} failed'); broadcastDay = apiData.broadcast?.split(' ').first;
continue; } 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( al.add(
AnimeUpdatedEvent( AnimeUpdatedEvent(
anime.copyWith(airing: false, broadcastDay: null), anime.copyWith(airing: false, broadcastDay: null),
commit: true, 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 // 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:anitrack/src/ui/widgets/image.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:get_it/get_it.dart';
enum Weekday { enum Weekday {
monday, 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}); const CalendarPage({super.key});
static MaterialPageRoute<dynamic> get route => MaterialPageRoute<dynamic>( 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( List<Widget> _renderWeekdayList(
BuildContext context, BuildContext context,
Weekday day, Weekday day,
@ -73,11 +79,12 @@ class CalendarPage extends StatelessWidget {
assert(data[day]!.isNotEmpty, 'There should be at least one anime'); assert(data[day]!.isNotEmpty, 'There should be at least one anime');
return [ return [
Padding( SliverToBoxAdapter(
child: Padding(
padding: const EdgeInsets.only( padding: const EdgeInsets.only(
left: 16, left: 0,
right: 16, right: 16,
top: 16, top: 20,
bottom: 4, bottom: 4,
), ),
child: Text( child: Text(
@ -85,11 +92,8 @@ class CalendarPage extends StatelessWidget {
style: Theme.of(context).textTheme.titleLarge, style: Theme.of(context).textTheme.titleLarge,
), ),
), ),
Padding( ),
padding: const EdgeInsets.symmetric(horizontal: 16), SliverGrid.builder(
child: GridView.builder(
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount( gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3, crossAxisCount: 3,
mainAxisSpacing: 8, mainAxisSpacing: 8,
@ -115,19 +119,13 @@ class CalendarPage extends StatelessWidget {
); );
}, },
), ),
),
]; ];
} }
@override @override
Widget build(BuildContext context) { 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>>{}; final airingAnimeMap = <Weekday, List<AnimeTrackingData>>{};
for (final anime in state.animes) { for (final anime in GetIt.I.get<AnimeListBloc>().unfilteredAnime) {
if (!anime.airing) continue; if (!anime.airing) continue;
final Weekday day; final Weekday day;
@ -161,7 +159,16 @@ class CalendarPage extends StatelessWidget {
airingAnimeMap.addOrSet(day, anime); 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( appBar: AppBar(
title: Text(t.calendar.calendar), title: Text(t.calendar.calendar),
actions: [ actions: [
@ -177,12 +184,12 @@ class CalendarPage extends StatelessWidget {
body: Stack( body: Stack(
children: [ children: [
Positioned( Positioned(
left: 0, left: 8,
right: 0, right: 8,
top: 0, top: 0,
bottom: 0, bottom: 0,
child: ListView( child: CustomScrollView(
children: [ slivers: [
// Render all available weekdays // Render all available weekdays
..._renderWeekdayList( ..._renderWeekdayList(
context, context,
@ -227,9 +234,11 @@ class CalendarPage extends StatelessWidget {
// Provide a nice bottom padding, while keeping the elastic effect attached // Provide a nice bottom padding, while keeping the elastic effect attached
// to the bottom-most edge. // to the bottom-most edge.
const SizedBox( const SliverToBoxAdapter(
child: SizedBox(
height: 16, height: 16,
), ),
),
], ],
), ),
), ),
@ -239,6 +248,8 @@ class CalendarPage extends StatelessWidget {
top: 0, top: 0,
bottom: 0, bottom: 0,
child: BlocBuilder<CalendarBloc, CalendarState>( child: BlocBuilder<CalendarBloc, CalendarState>(
buildWhen: (previous, current) =>
previous.refreshing != current.refreshing,
builder: (context, state) { builder: (context, state) {
if (!state.refreshing) { if (!state.refreshing) {
return const SizedBox(); return const SizedBox();
@ -296,8 +307,6 @@ class CalendarPage extends StatelessWidget {
), ),
], ],
), ),
);
},
), ),
); );
} }