2021-12-21 23:20:38 -05:00
|
|
|
import 'package:frosty/api/twitch_api.dart';
|
|
|
|
|
import 'package:frosty/models/user.dart';
|
|
|
|
|
import 'package:mobx/mobx.dart';
|
|
|
|
|
|
|
|
|
|
part 'user_store.g.dart';
|
|
|
|
|
|
2022-05-12 04:58:03 -04:00
|
|
|
class UserStore = UserStoreBase with _$UserStore;
|
2021-12-21 23:20:38 -05:00
|
|
|
|
2022-05-12 04:58:03 -04:00
|
|
|
abstract class UserStoreBase with Store {
|
2022-01-14 14:56:54 -05:00
|
|
|
final TwitchApi twitchApi;
|
|
|
|
|
|
2021-12-21 23:20:38 -05:00
|
|
|
/// The current user's info.
|
|
|
|
|
@readonly
|
|
|
|
|
UserTwitch? _details;
|
|
|
|
|
|
|
|
|
|
/// The user's list of blocked users.
|
|
|
|
|
@readonly
|
|
|
|
|
var _blockedUsers = ObservableList<UserBlockedTwitch>();
|
|
|
|
|
|
2022-03-23 23:28:31 -04:00
|
|
|
ReactionDisposer? _disposeReaction;
|
2022-01-03 05:51:43 -05:00
|
|
|
|
2022-05-12 04:58:03 -04:00
|
|
|
UserStoreBase({required this.twitchApi});
|
2022-01-14 14:56:54 -05:00
|
|
|
|
2021-12-21 23:20:38 -05:00
|
|
|
@action
|
|
|
|
|
Future<void> init({required Map<String, String> headers}) async {
|
|
|
|
|
// Get and update the current user's info.
|
2022-01-14 14:56:54 -05:00
|
|
|
_details = await twitchApi.getUserInfo(headers: headers);
|
2021-12-21 23:20:38 -05:00
|
|
|
|
|
|
|
|
// Get and update the current user's list of blocked users.
|
2022-08-05 03:24:35 -04:00
|
|
|
// Don't use await because having a huge list of blocked users will block the UI.
|
|
|
|
|
if (_details?.id != null) {
|
|
|
|
|
twitchApi.getUserBlockedList(id: _details!.id, headers: headers).then((blockedUsers) => _blockedUsers = blockedUsers.asObservable());
|
|
|
|
|
}
|
2022-01-03 05:51:43 -05:00
|
|
|
|
2022-03-23 23:28:31 -04:00
|
|
|
_disposeReaction = autorun((_) => _blockedUsers.sort((a, b) => a.userLogin.compareTo(b.userLogin)));
|
2022-01-03 05:51:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@action
|
2022-02-09 18:50:17 -05:00
|
|
|
Future<void> block({
|
|
|
|
|
required String targetId,
|
|
|
|
|
required String displayName,
|
|
|
|
|
required Map<String, String> headers,
|
|
|
|
|
}) async {
|
2022-01-14 14:56:54 -05:00
|
|
|
final success = await twitchApi.blockUser(userId: targetId, headers: headers);
|
2022-02-09 18:50:17 -05:00
|
|
|
|
|
|
|
|
if (success) _blockedUsers.add(UserBlockedTwitch(targetId, displayName, displayName));
|
2021-12-21 23:20:38 -05:00
|
|
|
}
|
|
|
|
|
|
2022-01-03 05:51:43 -05:00
|
|
|
@action
|
|
|
|
|
Future<void> unblock({required String targetId, required Map<String, String> headers}) async {
|
2022-01-14 14:56:54 -05:00
|
|
|
final success = await twitchApi.unblockUser(userId: targetId, headers: headers);
|
2022-01-03 05:51:43 -05:00
|
|
|
if (success) await refreshBlockedUsers(headers: headers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@action
|
|
|
|
|
Future<void> refreshBlockedUsers({required Map<String, String> headers}) async =>
|
2022-01-14 14:56:54 -05:00
|
|
|
_blockedUsers = (await twitchApi.getUserBlockedList(id: _details!.id, headers: headers)).asObservable();
|
2022-01-03 05:51:43 -05:00
|
|
|
|
2021-12-21 23:20:38 -05:00
|
|
|
@action
|
|
|
|
|
void dispose() {
|
|
|
|
|
_details = null;
|
|
|
|
|
_blockedUsers.clear();
|
2022-03-23 23:28:31 -04:00
|
|
|
if (_disposeReaction != null) _disposeReaction!();
|
2021-12-21 23:20:38 -05:00
|
|
|
}
|
|
|
|
|
}
|