From 1f2b99dfce7b40dd848c724dcf4baebbbd9150eb Mon Sep 17 00:00:00 2001 From: Tommy Chow Date: Tue, 4 Jan 2022 04:08:00 -0500 Subject: [PATCH] Combine top and categories stores into one --- lib/main.dart | 10 +-- lib/screens/home/home.dart | 11 +-- lib/screens/home/stores/list_store.dart | 72 ++++++++++--------- lib/screens/home/stores/list_store.g.dart | 30 +++++--- lib/screens/home/streams_list.dart | 4 +- .../home/top/categories/categories.dart | 4 +- .../home/top/categories/category_card.dart | 2 +- .../home/top/categories/category_streams.dart | 4 +- lib/screens/home/top/top_section.dart | 10 ++- 9 files changed, 78 insertions(+), 69 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index a73f133e..eaac56f2 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -92,18 +92,14 @@ class MyApp extends StatelessWidget { title: 'Frosty', theme: settingsStore.useOledTheme ? oledTheme : defaultTheme, home: Home( - topStreamsStore: ListStore( + topSectionStore: ListStore( authStore: authStore, - listType: StreamListType.top, - ), - categoriesStore: ListStore( - authStore: authStore, - listType: StreamListType.categories, + listType: ListType.top, ), followedStreamsStore: authStore.isLoggedIn ? ListStore( authStore: authStore, - listType: StreamListType.followed, + listType: ListType.followed, ) : null, searchStore: SearchStore(authStore: authStore), diff --git a/lib/screens/home/home.dart b/lib/screens/home/home.dart index 4d5adbdf..38eeb3d7 100644 --- a/lib/screens/home/home.dart +++ b/lib/screens/home/home.dart @@ -11,15 +11,13 @@ import 'package:frosty/screens/settings/stores/settings_store.dart'; import 'package:provider/provider.dart'; class Home extends StatefulWidget { - final ListStore topStreamsStore; - final ListStore categoriesStore; + final ListStore topSectionStore; final SearchStore searchStore; final ListStore? followedStreamsStore; const Home({ Key? key, - required this.topStreamsStore, - required this.categoriesStore, + required this.topSectionStore, required this.searchStore, required this.followedStreamsStore, }) : super(key: key); @@ -77,10 +75,7 @@ class _HomeState extends State { index: _selectedIndex, children: [ if (authStore.isLoggedIn) StreamsList(store: widget.followedStreamsStore!), - TopSection( - topStreamsStore: widget.topStreamsStore, - categoriesStore: widget.categoriesStore, - ), + TopSection(topSectionStore: widget.topSectionStore), Search(searchStore: widget.searchStore), ], ); diff --git a/lib/screens/home/stores/list_store.dart b/lib/screens/home/stores/list_store.dart index 2ea1acf7..5015f328 100644 --- a/lib/screens/home/stores/list_store.dart +++ b/lib/screens/home/stores/list_store.dart @@ -21,17 +21,20 @@ abstract class _ListStoreBase with Store { bool _isLoading = false; /// The pagination cursor for the streams. - String? _currentCursor; + String? _streamsCursor; - StreamListType listType; + /// The pagination cursor for the categories. + String? _categoriesCursor; final CategoryTwitch? categoryInfo; + final ListType listType; + /// The authentication store. final AuthStore authStore; /// Returns whether or not there are more streams and loading status for pagination. - bool get hasMore => _isLoading == false && _currentCursor != null; + bool get hasMore => _isLoading == false && _streamsCursor != null; _ListStoreBase({ required this.authStore, @@ -39,59 +42,55 @@ abstract class _ListStoreBase with Store { this.categoryInfo, }) { switch (listType) { - case StreamListType.followed: - if (listType == StreamListType.followed && authStore.isLoggedIn) { - getData(); - } + case ListType.followed: + if (listType == ListType.followed && authStore.isLoggedIn) getStreams(); break; - case StreamListType.top: - case StreamListType.category: - getData(); + case ListType.top: + getStreams(); + getCategories(); break; - case StreamListType.categories: - getGames(); + case ListType.category: + getStreams(); break; } } /// Fetches the streams based on the type and current cursor. @action - Future getData() async { + Future getStreams() async { _isLoading = true; final StreamsTwitch? newStreams; switch (listType) { - case StreamListType.followed: + case ListType.followed: newStreams = await Twitch.getFollowedStreams( id: authStore.user.details!.id, headers: authStore.headersTwitch, - cursor: _currentCursor, + cursor: _streamsCursor, ); break; - case StreamListType.top: + case ListType.top: newStreams = await Twitch.getTopStreams( headers: authStore.headersTwitch, - cursor: _currentCursor, + cursor: _streamsCursor, ); break; - case StreamListType.category: + case ListType.category: newStreams = await Twitch.getStreamsUnderGame( gameId: categoryInfo!.id, headers: authStore.headersTwitch, - cursor: _currentCursor, + cursor: _streamsCursor, ); break; - case StreamListType.categories: - return getGames(); } if (newStreams != null) { - if (_currentCursor == null) { + if (_streamsCursor == null) { _streams = newStreams.data.asObservable(); } else { _streams.addAll(newStreams.data); } - _currentCursor = newStreams.pagination['cursor']; + _streamsCursor = newStreams.pagination['cursor']; } _isLoading = false; @@ -99,15 +98,17 @@ abstract class _ListStoreBase with Store { // Fetches the top categories based on the current cursor. @action - Future getGames() async { - final result = await Twitch.getTopGames(headers: authStore.headersTwitch, cursor: _currentCursor); + Future getCategories() async { + _isLoading = true; + + final result = await Twitch.getTopGames(headers: authStore.headersTwitch, cursor: _categoriesCursor); if (result != null) { - if (_currentCursor == null) { + if (_categoriesCursor == null) { _categories = result.data.asObservable(); } else { _categories.addAll(result.data); } - _currentCursor = result.pagination['cursor']; + _categoriesCursor = result.pagination['cursor']; } _isLoading = false; @@ -115,16 +116,23 @@ abstract class _ListStoreBase with Store { /// Resets the cursor and then fetches the streams. @action - Future refresh() { - _currentCursor = null; + Future refreshStreams() { + _streamsCursor = null; - return getData(); + return getStreams(); + } + + /// Resets the cursor and then fetches the categories. + @action + Future refreshCategories() { + _categoriesCursor = null; + + return getCategories(); } } -enum StreamListType { +enum ListType { followed, top, category, - categories, } diff --git a/lib/screens/home/stores/list_store.g.dart b/lib/screens/home/stores/list_store.g.dart index 1e2f6a31..17b926ec 100644 --- a/lib/screens/home/stores/list_store.g.dart +++ b/lib/screens/home/stores/list_store.g.dart @@ -60,29 +60,41 @@ mixin _$ListStore on _ListStoreBase, Store { }); } - final _$getDataAsyncAction = AsyncAction('_ListStoreBase.getData'); + final _$getStreamsAsyncAction = AsyncAction('_ListStoreBase.getStreams'); @override - Future getData() { - return _$getDataAsyncAction.run(() => super.getData()); + Future getStreams() { + return _$getStreamsAsyncAction.run(() => super.getStreams()); } - final _$getGamesAsyncAction = AsyncAction('_ListStoreBase.getGames'); + final _$getCategoriesAsyncAction = + AsyncAction('_ListStoreBase.getCategories'); @override - Future getGames() { - return _$getGamesAsyncAction.run(() => super.getGames()); + Future getCategories() { + return _$getCategoriesAsyncAction.run(() => super.getCategories()); } final _$_ListStoreBaseActionController = ActionController(name: '_ListStoreBase'); @override - Future refresh() { + Future refreshStreams() { final _$actionInfo = _$_ListStoreBaseActionController.startAction( - name: '_ListStoreBase.refresh'); + name: '_ListStoreBase.refreshStreams'); try { - return super.refresh(); + return super.refreshStreams(); + } finally { + _$_ListStoreBaseActionController.endAction(_$actionInfo); + } + } + + @override + Future refreshCategories() { + final _$actionInfo = _$_ListStoreBaseActionController.startAction( + name: '_ListStoreBase.refreshCategories'); + try { + return super.refreshCategories(); } finally { _$_ListStoreBaseActionController.endAction(_$actionInfo); } diff --git a/lib/screens/home/streams_list.dart b/lib/screens/home/streams_list.dart index 0ac727c3..009271da 100644 --- a/lib/screens/home/streams_list.dart +++ b/lib/screens/home/streams_list.dart @@ -14,7 +14,7 @@ class StreamsList extends StatelessWidget { @override Widget build(BuildContext context) { return RefreshIndicator( - onRefresh: store.refresh, + onRefresh: store.refreshStreams, child: Observer( builder: (_) { if (store.streams.isEmpty && store.isLoading) { @@ -24,7 +24,7 @@ class StreamsList extends StatelessWidget { itemCount: store.streams.length, itemBuilder: (context, index) { if (index > store.streams.length / 2 && store.hasMore) { - store.getData(); + store.getStreams(); } return Observer( builder: (context) => StreamCard( diff --git a/lib/screens/home/top/categories/categories.dart b/lib/screens/home/top/categories/categories.dart index 27c70ef3..055ad349 100644 --- a/lib/screens/home/top/categories/categories.dart +++ b/lib/screens/home/top/categories/categories.dart @@ -14,7 +14,7 @@ class Categories extends StatelessWidget { @override Widget build(BuildContext context) { return RefreshIndicator( - onRefresh: store.refresh, + onRefresh: store.refreshCategories, child: Observer( builder: (_) { return GridView.builder( @@ -24,7 +24,7 @@ class Categories extends StatelessWidget { itemCount: store.categories.length, itemBuilder: (context, index) { if (index > store.categories.length / 2 && store.hasMore) { - store.getGames(); + store.getCategories(); } return CategoryCard(category: store.categories[index]); }, diff --git a/lib/screens/home/top/categories/category_card.dart b/lib/screens/home/top/categories/category_card.dart index 94bad921..de1420d9 100644 --- a/lib/screens/home/top/categories/category_card.dart +++ b/lib/screens/home/top/categories/category_card.dart @@ -21,7 +21,7 @@ class CategoryCard extends StatelessWidget { builder: (context) => CategoryStreams( store: ListStore( authStore: context.read(), - listType: StreamListType.category, + listType: ListType.category, categoryInfo: category, ), ), diff --git a/lib/screens/home/top/categories/category_streams.dart b/lib/screens/home/top/categories/category_streams.dart index faec1747..841fbc79 100644 --- a/lib/screens/home/top/categories/category_streams.dart +++ b/lib/screens/home/top/categories/category_streams.dart @@ -15,7 +15,7 @@ class CategoryStreams extends StatelessWidget { Widget build(BuildContext context) { return Scaffold( body: RefreshIndicator( - onRefresh: store.refresh, + onRefresh: store.refreshStreams, child: Observer( builder: (context) { return CustomScrollView( @@ -44,7 +44,7 @@ class CategoryStreams extends StatelessWidget { delegate: SliverChildBuilderDelegate( (context, index) { if (index > store.streams.length / 2 && store.hasMore) { - store.getData(); + store.getStreams(); } return Observer( builder: (context) => StreamCard( diff --git a/lib/screens/home/top/top_section.dart b/lib/screens/home/top/top_section.dart index 8866d318..804b086e 100644 --- a/lib/screens/home/top/top_section.dart +++ b/lib/screens/home/top/top_section.dart @@ -4,13 +4,11 @@ import 'package:frosty/screens/home/streams_list.dart'; import 'package:frosty/screens/home/top/categories/categories.dart'; class TopSection extends StatelessWidget { - final ListStore topStreamsStore; - final ListStore categoriesStore; + final ListStore topSectionStore; const TopSection({ Key? key, - required this.topStreamsStore, - required this.categoriesStore, + required this.topSectionStore, }) : super(key: key); @override @@ -28,8 +26,8 @@ class TopSection extends StatelessWidget { Expanded( child: TabBarView( children: [ - StreamsList(store: topStreamsStore), - Categories(store: categoriesStore), + StreamsList(store: topSectionStore), + Categories(store: topSectionStore), ], ), ),