Favourites
This commit is contained in:
parent
6a84a9bef0
commit
1747ab0245
23 changed files with 469 additions and 67 deletions
47
lib/features/core/services/local_storage_service.dart
Normal file
47
lib/features/core/services/local_storage_service.dart
Normal file
|
@ -0,0 +1,47 @@
|
|||
import 'package:hive/hive.dart';
|
||||
import 'package:mc_gallery/features/core/services/logging_service.dart';
|
||||
import 'package:mc_gallery/locator.dart';
|
||||
|
||||
class LocalStorageService {
|
||||
LocalStorageService() {
|
||||
_init();
|
||||
}
|
||||
|
||||
final LoggingService _loggingService = LoggingService.locate;
|
||||
|
||||
static const String _userBoxKey = 'userBoxKey';
|
||||
|
||||
late final Box<bool> _userBox;
|
||||
|
||||
Future<void> _init() async {
|
||||
_userBox = await Hive.openBox(_userBoxKey);
|
||||
|
||||
Locator.instance().signalReady(this);
|
||||
}
|
||||
|
||||
Iterable<bool> get storedFavouritesStates => _userBox.values;
|
||||
|
||||
void initNewFavourites({required Iterable<bool> newValues}) {
|
||||
_userBox.addAll(newValues);
|
||||
_loggingService.info('Adding new favourites value');
|
||||
}
|
||||
|
||||
void updateFavourite({
|
||||
required index,
|
||||
required bool newValue,
|
||||
}) {
|
||||
try {
|
||||
_userBox.putAt(index, newValue);
|
||||
_loggingService.good('Successfully updated favourite status at $index -> $newValue');
|
||||
} on Exception catch (ex, stackTrace) {
|
||||
_loggingService.handleException(ex, stackTrace);
|
||||
}
|
||||
}
|
||||
|
||||
void resetFavourites() {
|
||||
_userBox.clear();
|
||||
_loggingService.info('Cleared favourites table');
|
||||
}
|
||||
|
||||
static LocalStorageService get locate => Locator.locate();
|
||||
}
|
|
@ -25,7 +25,8 @@ class LoggingService {
|
|||
void Function(dynamic msg, [Object? exception, StackTrace? stackTrace]) get fine => _talker.fine;
|
||||
void Function(dynamic msg, [Object? exception, StackTrace? stackTrace]) get good => _talker.good;
|
||||
|
||||
void Function(dynamic msg, [Object? exception, StackTrace? stackTrace]) get info => _talker.info;
|
||||
void Function(dynamic msg, [Object? exception, StackTrace? stackTrace]) get info =>
|
||||
_talker.verbose;
|
||||
void Function(dynamic msg, [Object exception, StackTrace stackTrace]) get warning =>
|
||||
_talker.warning;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue