95 lines
2.4 KiB
Dart
95 lines
2.4 KiB
Dart
import '../state/app_interaction_state.dart';
|
|
|
|
abstract class UserStateRepository {
|
|
Future<bool> isLoggedIn();
|
|
Future<void> login(LoginMethod method, {String? phone});
|
|
Future<void> logout();
|
|
Future<String?> getPhone();
|
|
Future<LoginMethod?> getLoginMethod();
|
|
|
|
Future<Set<String>> getFavorites();
|
|
Future<void> toggleFavorite(String reportId);
|
|
|
|
Future<Set<String>> getSavedListens();
|
|
Future<void> toggleSavedListen(String reportId);
|
|
|
|
Future<List<String>> getHistory();
|
|
Future<void> addHistory(String reportId);
|
|
|
|
Future<Map<String, double>> getAudioProgress();
|
|
Future<void> saveAudioProgress(String audioId, double seconds);
|
|
}
|
|
|
|
class MemoryUserStateRepository implements UserStateRepository {
|
|
bool _loggedIn = false;
|
|
String? _phone;
|
|
LoginMethod? _loginMethod;
|
|
final Set<String> _favorites = {};
|
|
final Set<String> _savedListens = {};
|
|
final List<String> _history = [];
|
|
final Map<String, double> _audioProgress = {};
|
|
|
|
@override
|
|
Future<bool> isLoggedIn() async => _loggedIn;
|
|
|
|
@override
|
|
Future<void> login(LoginMethod method, {String? phone}) async {
|
|
_loggedIn = true;
|
|
_loginMethod = method;
|
|
_phone = phone;
|
|
}
|
|
|
|
@override
|
|
Future<void> logout() async {
|
|
_loggedIn = false;
|
|
_phone = null;
|
|
_loginMethod = null;
|
|
}
|
|
|
|
@override
|
|
Future<String?> getPhone() async => _phone;
|
|
|
|
@override
|
|
Future<LoginMethod?> getLoginMethod() async => _loginMethod;
|
|
|
|
@override
|
|
Future<Set<String>> getFavorites() async => {..._favorites};
|
|
|
|
@override
|
|
Future<void> toggleFavorite(String reportId) async {
|
|
if (!_favorites.add(reportId)) {
|
|
_favorites.remove(reportId);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<Set<String>> getSavedListens() async => {..._savedListens};
|
|
|
|
@override
|
|
Future<void> toggleSavedListen(String reportId) async {
|
|
if (!_savedListens.add(reportId)) {
|
|
_savedListens.remove(reportId);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<List<String>> getHistory() async => [..._history];
|
|
|
|
@override
|
|
Future<void> addHistory(String reportId) async {
|
|
_history.remove(reportId);
|
|
_history.insert(0, reportId);
|
|
if (_history.length > 40) {
|
|
_history.removeRange(40, _history.length);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<Map<String, double>> getAudioProgress() async => {..._audioProgress};
|
|
|
|
@override
|
|
Future<void> saveAudioProgress(String audioId, double seconds) async {
|
|
_audioProgress[audioId] = seconds < 0 ? 0 : seconds;
|
|
}
|
|
}
|