2021-11-14 04:21:39 -05:00
|
|
|
import 'package:frosty/api/twitch_api.dart';
|
2021-11-21 23:29:47 -05:00
|
|
|
import 'package:frosty/core/auth/auth_store.dart';
|
2022-01-02 19:12:39 -05:00
|
|
|
import 'package:frosty/models/category.dart';
|
2021-11-14 04:21:39 -05:00
|
|
|
import 'package:frosty/models/channel.dart';
|
|
|
|
|
import 'package:mobx/mobx.dart';
|
2021-12-23 00:26:33 -05:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2021-11-14 04:21:39 -05:00
|
|
|
|
|
|
|
|
part 'search_store.g.dart';
|
|
|
|
|
|
2022-05-12 04:58:03 -04:00
|
|
|
class SearchStore = SearchStoreBase with _$SearchStore;
|
2021-11-14 04:21:39 -05:00
|
|
|
|
2022-05-12 04:58:03 -04:00
|
|
|
abstract class SearchStoreBase with Store {
|
2021-11-14 04:21:39 -05:00
|
|
|
final AuthStore authStore;
|
|
|
|
|
|
2022-01-14 14:56:54 -05:00
|
|
|
final TwitchApi twitchApi;
|
|
|
|
|
|
2021-12-06 02:31:30 -05:00
|
|
|
@readonly
|
2021-12-23 00:26:33 -05:00
|
|
|
var _searchHistory = ObservableList<String>();
|
2021-11-14 04:21:39 -05:00
|
|
|
|
2021-12-23 00:26:33 -05:00
|
|
|
@readonly
|
2022-01-05 09:38:05 -05:00
|
|
|
ObservableFuture<List<ChannelQuery>>? _channelFuture;
|
2022-01-02 19:12:39 -05:00
|
|
|
|
|
|
|
|
@readonly
|
2022-01-05 09:38:05 -05:00
|
|
|
ObservableFuture<CategoriesTwitch?>? _categoryFuture;
|
2021-12-23 00:26:33 -05:00
|
|
|
|
2022-05-12 04:58:03 -04:00
|
|
|
SearchStoreBase({required this.authStore, required this.twitchApi}) {
|
2021-12-23 00:26:33 -05:00
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@action
|
|
|
|
|
Future<void> init() async {
|
|
|
|
|
// Retrieve the instance that will allow us to retrieve local search history.
|
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
|
|
|
|
|
|
_searchHistory = prefs.getStringList('search_history')?.asObservable() ?? ObservableList<String>();
|
|
|
|
|
|
|
|
|
|
autorun((_) {
|
|
|
|
|
if (_searchHistory.length > 8) _searchHistory.removeLast();
|
|
|
|
|
prefs.setStringList('search_history', _searchHistory);
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-11-14 04:21:39 -05:00
|
|
|
|
|
|
|
|
@action
|
|
|
|
|
Future<void> handleQuery(String query) async {
|
2021-12-23 00:26:33 -05:00
|
|
|
if (query.isEmpty) return;
|
|
|
|
|
|
|
|
|
|
_searchHistory.remove(query);
|
|
|
|
|
_searchHistory.insert(0, query);
|
|
|
|
|
|
2022-01-14 14:56:54 -05:00
|
|
|
_channelFuture = twitchApi
|
|
|
|
|
.searchChannels(
|
2022-01-05 09:38:05 -05:00
|
|
|
query: query,
|
|
|
|
|
headers: authStore.headersTwitch,
|
2022-01-14 14:56:54 -05:00
|
|
|
)
|
|
|
|
|
.then(
|
2022-01-05 09:38:05 -05:00
|
|
|
(channels) {
|
|
|
|
|
channels.sort((c1, c2) => c2.isLive ? 1 : -1);
|
|
|
|
|
return channels;
|
|
|
|
|
},
|
|
|
|
|
).asObservable();
|
|
|
|
|
|
2022-01-14 14:56:54 -05:00
|
|
|
_categoryFuture = twitchApi
|
|
|
|
|
.searchCategories(
|
|
|
|
|
query: query,
|
|
|
|
|
headers: authStore.headersTwitch,
|
|
|
|
|
)
|
|
|
|
|
.asObservable();
|
2021-11-14 04:21:39 -05:00
|
|
|
}
|
|
|
|
|
|
2022-01-15 13:16:43 -05:00
|
|
|
Future<Channel> searchChannel(String query) async {
|
|
|
|
|
final user = await twitchApi.getUser(userLogin: query, headers: authStore.headersTwitch);
|
|
|
|
|
return await twitchApi.getChannel(userId: user.id, headers: authStore.headersTwitch);
|
2021-11-14 04:21:39 -05:00
|
|
|
}
|
|
|
|
|
}
|