frosty/lib/screens/home/search/stores/search_store.dart

84 lines
2.3 KiB
Dart
Raw Normal View History

2021-11-14 04:21:39 -05:00
import 'package:flutter/material.dart';
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';
class SearchStore = _SearchStoreBase with _$SearchStore;
abstract class _SearchStoreBase with Store {
final textController = TextEditingController();
final AuthStore authStore;
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-02 19:12:39 -05:00
var _channelSearchResults = <ChannelQuery>[];
@readonly
var _categorySearchResults = <CategoryTwitch>[];
2021-12-23 00:26:33 -05:00
_SearchStoreBase({required this.authStore}) {
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-02 19:12:39 -05:00
final channelResults = await Twitch.searchChannels(query: query, headers: authStore.headersTwitch);
channelResults.sort((c1, c2) => c2.isLive ? 1 : -1);
_channelSearchResults = channelResults;
2021-11-14 04:21:39 -05:00
2022-01-02 19:12:39 -05:00
final categoryResults = await Twitch.searchCategories(query: query, headers: authStore.headersTwitch);
if (categoryResults != null) {
_categorySearchResults = categoryResults.data;
}
2021-11-14 04:21:39 -05:00
}
2022-01-02 19:12:39 -05:00
Future<Channel?> searchChannel(String query) async {
2021-12-28 19:12:55 -05:00
try {
2022-01-02 19:12:39 -05:00
final user = await Twitch.getUser(userLogin: query, headers: authStore.headersTwitch);
2021-12-28 19:12:55 -05:00
return await Twitch.getChannel(userId: user!.id, headers: authStore.headersTwitch);
} catch (e) {
debugPrint(e.toString());
rethrow;
2021-11-14 04:21:39 -05:00
}
}
2022-01-02 19:12:39 -05:00
@action
2021-12-06 02:31:30 -05:00
void clearSearch() {
textController.clear();
FocusManager.instance.primaryFocus?.unfocus();
2022-01-02 19:12:39 -05:00
_channelSearchResults = <ChannelQuery>[];
_categorySearchResults = <CategoryTwitch>[];
2021-12-06 02:31:30 -05:00
}
2021-11-14 04:21:39 -05:00
void dispose() {
textController.dispose();
}
}