Add doc comments to channel list store

This commit is contained in:
Tommy Chow 2021-10-05 03:10:30 -04:00
parent 514e3ebb6a
commit a6efcfdf15
2 changed files with 58 additions and 48 deletions

View file

@ -8,27 +8,24 @@ part 'channel_list_store.g.dart';
class ChannelListStore = _ChannelListBase with _$ChannelListStore;
abstract class _ChannelListBase with Store {
/// The list of the fetched top channels
@observable
ObservableList<Channel> topChannels = ObservableList<Channel>();
ObservableList<Channel> _topChannels = ObservableList<Channel>();
/// The list of the fetched followed channels
@observable
ObservableList<Channel> followedChannels = ObservableList<Channel>();
ObservableList<Channel> _followedChannels = ObservableList<Channel>();
bool isLoading = false;
/// The loading status for pagination.
bool _isLoading = false;
String? topChannelsCurrentCursor;
/// The pagination cursor for top channels.
String? _topChannelsCurrentCursor;
String? followedChannelsCurrentCursor;
ObservableList<Channel> channels({required ChannelCategory category}) {
switch (category) {
case ChannelCategory.top:
return topChannels;
case ChannelCategory.followed:
return followedChannels;
}
}
/// The pagination cursor for followed channels.
String? _followedChannelsCurrentCursor;
/// The authentication store.
final AuthStore auth;
_ChannelListBase({required this.auth}) {
@ -39,62 +36,76 @@ abstract class _ChannelListBase with Store {
}
}
bool hasMore({required ChannelCategory category}) {
/// Returns the appropriate channels the [category].
ObservableList<Channel> channels({required ChannelCategory category}) {
switch (category) {
case ChannelCategory.top:
return isLoading == false && topChannelsCurrentCursor != null;
return _topChannels;
case ChannelCategory.followed:
return isLoading == false && followedChannelsCurrentCursor != null;
return _followedChannels;
}
}
/// Returns whether or not there are more channels for the [category].
bool hasMore({required ChannelCategory category}) {
switch (category) {
case ChannelCategory.top:
return _isLoading == false && _topChannelsCurrentCursor != null;
case ChannelCategory.followed:
return _isLoading == false && _followedChannelsCurrentCursor != null;
}
}
/// Resets the cursor and then fetches the channels for the [category].
@action
Future<void> refresh({required ChannelCategory category}) async {
switch (category) {
case ChannelCategory.top:
topChannelsCurrentCursor = null;
_topChannelsCurrentCursor = null;
break;
case ChannelCategory.followed:
followedChannelsCurrentCursor = null;
_followedChannelsCurrentCursor = null;
break;
}
await getChannels(category: category);
}
/// Fetches the channels for the [category] based on the current cursor.
@action
Future<void> getChannels({required ChannelCategory category}) async {
isLoading = true;
_isLoading = true;
switch (category) {
case ChannelCategory.top:
final newTopChannels = await Twitch.getTopChannels(headers: auth.headersTwitch, cursor: topChannelsCurrentCursor);
final newTopChannels = await Twitch.getTopChannels(headers: auth.headersTwitch, cursor: _topChannelsCurrentCursor);
if (newTopChannels != null) {
if (topChannelsCurrentCursor == null) {
topChannels = ObservableList.of(newTopChannels['channels']);
if (_topChannelsCurrentCursor == null) {
_topChannels = ObservableList.of(newTopChannels['channels']);
} else {
topChannels.addAll(newTopChannels['channels']);
_topChannels.addAll(newTopChannels['channels']);
}
topChannelsCurrentCursor = newTopChannels['cursor'];
_topChannelsCurrentCursor = newTopChannels['cursor'];
}
break;
case ChannelCategory.followed:
final newFollowedChannels = await Twitch.getFollowedChannels(id: auth.user!.id, headers: auth.headersTwitch, cursor: followedChannelsCurrentCursor);
final newFollowedChannels = await Twitch.getFollowedChannels(id: auth.user!.id, headers: auth.headersTwitch, cursor: _followedChannelsCurrentCursor);
if (newFollowedChannels != null) {
if (followedChannelsCurrentCursor == null) {
followedChannels = ObservableList.of(newFollowedChannels['channels']);
if (_followedChannelsCurrentCursor == null) {
_followedChannels = ObservableList.of(newFollowedChannels['channels']);
} else {
followedChannels.addAll(newFollowedChannels['channels']);
_followedChannels.addAll(newFollowedChannels['channels']);
}
followedChannelsCurrentCursor = newFollowedChannels['cursor'];
_followedChannelsCurrentCursor = newFollowedChannels['cursor'];
}
break;
}
isLoading = false;
_isLoading = false;
}
}
/// The type of category for channels.
enum ChannelCategory { top, followed }

View file

@ -9,34 +9,34 @@ part of 'channel_list_store.dart';
// 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 _$ChannelListStore on _ChannelListBase, Store {
final _$topChannelsAtom = Atom(name: '_ChannelListBase.topChannels');
final _$_topChannelsAtom = Atom(name: '_ChannelListBase._topChannels');
@override
ObservableList<Channel> get topChannels {
_$topChannelsAtom.reportRead();
return super.topChannels;
ObservableList<Channel> get _topChannels {
_$_topChannelsAtom.reportRead();
return super._topChannels;
}
@override
set topChannels(ObservableList<Channel> value) {
_$topChannelsAtom.reportWrite(value, super.topChannels, () {
super.topChannels = value;
set _topChannels(ObservableList<Channel> value) {
_$_topChannelsAtom.reportWrite(value, super._topChannels, () {
super._topChannels = value;
});
}
final _$followedChannelsAtom =
Atom(name: '_ChannelListBase.followedChannels');
final _$_followedChannelsAtom =
Atom(name: '_ChannelListBase._followedChannels');
@override
ObservableList<Channel> get followedChannels {
_$followedChannelsAtom.reportRead();
return super.followedChannels;
ObservableList<Channel> get _followedChannels {
_$_followedChannelsAtom.reportRead();
return super._followedChannels;
}
@override
set followedChannels(ObservableList<Channel> value) {
_$followedChannelsAtom.reportWrite(value, super.followedChannels, () {
super.followedChannels = value;
set _followedChannels(ObservableList<Channel> value) {
_$_followedChannelsAtom.reportWrite(value, super._followedChannels, () {
super._followedChannels = value;
});
}
@ -58,8 +58,7 @@ mixin _$ChannelListStore on _ChannelListBase, Store {
@override
String toString() {
return '''
topChannels: ${topChannels},
followedChannels: ${followedChannels}
''';
}
}