81 lines
2.0 KiB
Dart
81 lines
2.0 KiB
Dart
import '../state/app_interaction_state.dart';
|
|
|
|
abstract class UserStateRepository {
|
|
Future<bool> isLoggedIn();
|
|
Future<void> login(LoginMethod method);
|
|
Future<void> logout();
|
|
|
|
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;
|
|
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) async {
|
|
_loggedIn = true;
|
|
}
|
|
|
|
@override
|
|
Future<void> logout() async {
|
|
_loggedIn = false;
|
|
}
|
|
|
|
@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;
|
|
}
|
|
}
|