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 _userBox; Future _init() async { _userBox = await Hive.openBox(_userBoxKey); Locator.instance().signalReady(this); } Iterable get storedFavouritesStates => _userBox.values; void initNewFavourites({required Iterable 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(); }