mc_gallery/lib/features/core/services/local_storage_service.dart

48 lines
1.2 KiB
Dart

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();
}