Refactor APIs to use a persistent client

This commit is contained in:
tommyxchow 2022-01-14 14:56:54 -05:00
parent 91d70fffae
commit 34263fbf94
19 changed files with 256 additions and 127 deletions

View file

@ -13,6 +13,8 @@ class SearchStore = _SearchStoreBase with _$SearchStore;
abstract class _SearchStoreBase with Store {
final AuthStore authStore;
final TwitchApi twitchApi;
@readonly
var _searchHistory = ObservableList<String>();
@ -22,7 +24,7 @@ abstract class _SearchStoreBase with Store {
@readonly
ObservableFuture<CategoriesTwitch?>? _categoryFuture;
_SearchStoreBase({required this.authStore}) {
_SearchStoreBase({required this.authStore, required this.twitchApi}) {
init();
}
@ -46,26 +48,30 @@ abstract class _SearchStoreBase with Store {
_searchHistory.remove(query);
_searchHistory.insert(0, query);
_channelFuture = Twitch.searchChannels(
_channelFuture = twitchApi
.searchChannels(
query: query,
headers: authStore.headersTwitch,
).then(
)
.then(
(channels) {
channels.sort((c1, c2) => c2.isLive ? 1 : -1);
return channels;
},
).asObservable();
_categoryFuture = Twitch.searchCategories(
query: query,
headers: authStore.headersTwitch,
).asObservable();
_categoryFuture = twitchApi
.searchCategories(
query: query,
headers: authStore.headersTwitch,
)
.asObservable();
}
Future<Channel?> searchChannel(String query) async {
try {
final user = await Twitch.getUser(userLogin: query, headers: authStore.headersTwitch);
return await Twitch.getChannel(userId: user!.id, headers: authStore.headersTwitch);
final user = await twitchApi.getUser(userLogin: query, headers: authStore.headersTwitch);
return await twitchApi.getChannel(userId: user!.id, headers: authStore.headersTwitch);
} catch (e) {
debugPrint(e.toString());
rethrow;