From 69a4432696f0c3535a386faf84481cb77726c572 Mon Sep 17 00:00:00 2001 From: tommyxchow Date: Tue, 21 Dec 2021 23:20:38 -0500 Subject: [PATCH] Move user-related info into its own store --- lib/core/auth/auth_store.dart | 28 +++---- lib/core/auth/auth_store.g.dart | 34 --------- lib/core/user/user_store.dart | 32 ++++++++ lib/core/user/user_store.g.dart | 73 +++++++++++++++++++ .../followed_streams_store.dart | 3 +- 5 files changed, 116 insertions(+), 54 deletions(-) create mode 100644 lib/core/user/user_store.dart create mode 100644 lib/core/user/user_store.g.dart diff --git a/lib/core/auth/auth_store.dart b/lib/core/auth/auth_store.dart index c73ab824..44080cf8 100644 --- a/lib/core/auth/auth_store.dart +++ b/lib/core/auth/auth_store.dart @@ -3,7 +3,7 @@ import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:flutter_web_auth/flutter_web_auth.dart'; import 'package:frosty/api/twitch_api.dart'; import 'package:frosty/constants.dart'; -import 'package:frosty/models/user.dart'; +import 'package:frosty/core/user/user_store.dart'; import 'package:mobx/mobx.dart'; part 'auth_store.g.dart'; @@ -17,18 +17,13 @@ abstract class _AuthBase with Store { /// Whether the token is valid or not. var _tokenIsValid = false; + /// The store containing information relevant to the current user. + final user = UserStore(); + /// The current token. @readonly String? _token; - /// The current user's info. - @readonly - UserTwitch? _user; - - /// The user's list of blocked users. - @readonly - var _blockedUsers = ObservableList(); - /// Whether the user is logged in or not. @readonly var _isLoggedIn = false; @@ -54,11 +49,8 @@ abstract class _AuthBase with Store { await _storage.write(key: 'DEFAULT_TOKEN', value: _token); } } else { - // Get and update the current user's info. - _user = await Twitch.getUserInfo(headers: headersTwitch); - - // Get and update the current user's list of blocked users. - if (_user?.id != null) _blockedUsers = (await Twitch.getUserBlockedList(id: _user!.id, headers: headersTwitch)).asObservable(); + // Initialize the user store. + await user.init(headers: headersTwitch); _isLoggedIn = true; } @@ -102,10 +94,8 @@ abstract class _AuthBase with Store { // Store the user token. await _storage.write(key: 'USER_TOKEN', value: _token); - // Retrieve the user info and set it - _user = await Twitch.getUserInfo(headers: headersTwitch); - - if (_user?.id != null) _blockedUsers = (await Twitch.getUserBlockedList(id: _user!.id, headers: headersTwitch)).asObservable(); + // Initialize the user with the new token. + user.init(headers: headersTwitch); // Set the login status to logged in. _isLoggedIn = true; @@ -122,7 +112,7 @@ abstract class _AuthBase with Store { _token = null; // Clear the user info. - _user = null; + user.dispose(); // Set the login status to logged out. _isLoggedIn = false; diff --git a/lib/core/auth/auth_store.g.dart b/lib/core/auth/auth_store.g.dart index 2b5747f4..e0d1873e 100644 --- a/lib/core/auth/auth_store.g.dart +++ b/lib/core/auth/auth_store.g.dart @@ -34,40 +34,6 @@ mixin _$AuthStore on _AuthBase, Store { }); } - final _$_userAtom = Atom(name: '_AuthBase._user'); - - UserTwitch? get user { - _$_userAtom.reportRead(); - return super._user; - } - - @override - UserTwitch? get _user => user; - - @override - set _user(UserTwitch? value) { - _$_userAtom.reportWrite(value, super._user, () { - super._user = value; - }); - } - - final _$_blockedUsersAtom = Atom(name: '_AuthBase._blockedUsers'); - - ObservableList get blockedUsers { - _$_blockedUsersAtom.reportRead(); - return super._blockedUsers; - } - - @override - ObservableList get _blockedUsers => blockedUsers; - - @override - set _blockedUsers(ObservableList value) { - _$_blockedUsersAtom.reportWrite(value, super._blockedUsers, () { - super._blockedUsers = value; - }); - } - final _$_isLoggedInAtom = Atom(name: '_AuthBase._isLoggedIn'); bool get isLoggedIn { diff --git a/lib/core/user/user_store.dart b/lib/core/user/user_store.dart new file mode 100644 index 00000000..ddb015e2 --- /dev/null +++ b/lib/core/user/user_store.dart @@ -0,0 +1,32 @@ +import 'package:frosty/api/twitch_api.dart'; +import 'package:frosty/models/user.dart'; +import 'package:mobx/mobx.dart'; + +part 'user_store.g.dart'; + +class UserStore = _UserStoreBase with _$UserStore; + +abstract class _UserStoreBase with Store { + /// The current user's info. + @readonly + UserTwitch? _details; + + /// The user's list of blocked users. + @readonly + var _blockedUsers = ObservableList(); + + @action + Future init({required Map headers}) async { + // Get and update the current user's info. + _details = await Twitch.getUserInfo(headers: headers); + + // Get and update the current user's list of blocked users. + if (_details?.id != null) _blockedUsers = (await Twitch.getUserBlockedList(id: _details!.id, headers: headers)).asObservable(); + } + + @action + void dispose() { + _details = null; + _blockedUsers.clear(); + } +} diff --git a/lib/core/user/user_store.g.dart b/lib/core/user/user_store.g.dart new file mode 100644 index 00000000..3dc253db --- /dev/null +++ b/lib/core/user/user_store.g.dart @@ -0,0 +1,73 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'user_store.dart'; + +// ************************************************************************** +// StoreGenerator +// ************************************************************************** + +// ignore_for_file: non_constant_identifier_names, unnecessary_brace_in_string_interps, unnecessary_lambdas, prefer_expression_function_bodies, lines_longer_than_80_chars, avoid_as, avoid_annotating_with_dynamic + +mixin _$UserStore on _UserStoreBase, Store { + final _$_detailsAtom = Atom(name: '_UserStoreBase._details'); + + UserTwitch? get details { + _$_detailsAtom.reportRead(); + return super._details; + } + + @override + UserTwitch? get _details => details; + + @override + set _details(UserTwitch? value) { + _$_detailsAtom.reportWrite(value, super._details, () { + super._details = value; + }); + } + + final _$_blockedUsersAtom = Atom(name: '_UserStoreBase._blockedUsers'); + + ObservableList get blockedUsers { + _$_blockedUsersAtom.reportRead(); + return super._blockedUsers; + } + + @override + ObservableList get _blockedUsers => blockedUsers; + + @override + set _blockedUsers(ObservableList value) { + _$_blockedUsersAtom.reportWrite(value, super._blockedUsers, () { + super._blockedUsers = value; + }); + } + + final _$initAsyncAction = AsyncAction('_UserStoreBase.init'); + + @override + Future init({required Map headers}) { + return _$initAsyncAction.run(() => super.init(headers: headers)); + } + + final _$_UserStoreBaseActionController = + ActionController(name: '_UserStoreBase'); + + @override + void dispose() { + final _$actionInfo = _$_UserStoreBaseActionController.startAction( + name: '_UserStoreBase.dispose'); + try { + return super.dispose(); + } finally { + _$_UserStoreBaseActionController.endAction(_$actionInfo); + } + } + + @override + String toString() { + return ''' + + '''; + } +} diff --git a/lib/screens/stream_list/streams_followed/followed_streams_store.dart b/lib/screens/stream_list/streams_followed/followed_streams_store.dart index 0aa877ea..b84d453c 100644 --- a/lib/screens/stream_list/streams_followed/followed_streams_store.dart +++ b/lib/screens/stream_list/streams_followed/followed_streams_store.dart @@ -34,7 +34,8 @@ abstract class _FollowedStreamsStoreBase with Store { Future getFollowedStreams() async { _isLoading = true; - final newFollowedStreams = await Twitch.getFollowedStreams(id: authStore.user!.id, headers: authStore.headersTwitch, cursor: _followedStreamsCurrentCursor); + final newFollowedStreams = + await Twitch.getFollowedStreams(id: authStore.user.details!.id, headers: authStore.headersTwitch, cursor: _followedStreamsCurrentCursor); if (newFollowedStreams != null) { if (_followedStreamsCurrentCursor == null) {