Favourites

This commit is contained in:
Mguy13 2022-12-25 01:55:53 +01:00
parent 6a84a9bef0
commit 1747ab0245
23 changed files with 469 additions and 67 deletions

View file

@ -1,14 +1,14 @@
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:hive_flutter/hive_flutter.dart';
import '/l10n/generated/l10n.dart';
import '/locator.dart';
abstract class AppSetup {
// TODO: When locator is properly refactored we should not have to use these stub methods for testing
static Future<void> initialise() async {
WidgetsFlutterBinding.ensureInitialized();
@ -19,6 +19,8 @@ abstract class AppSetup {
DeviceOrientation.portraitDown,
]);
await Hive.initFlutter();
await Locator.setup();
}

View file

@ -0,0 +1,26 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_svg/flutter_svg.dart';
abstract class ConstMedia {
static const String favStarFilled = 'assets/icons/star_filled.svg';
static const String favStarOutline = 'assets/icons/star_outline.svg';
static SvgPicture buildIcon(
String iconReference, {
Color? color,
double? width,
double? height,
BoxFit fit = BoxFit.contain,
Clip clipBehavior = Clip.hardEdge,
Alignment alignment = Alignment.center,
}) =>
SvgPicture.asset(
iconReference,
color: color,
width: width,
height: height,
fit: fit,
clipBehavior: clipBehavior,
alignment: alignment,
);
}

View file

@ -1,6 +1,17 @@
import 'dart:collection';
extension MapExtensions<A, B> on Map<A, B> {
Map<A, B> get deepCopy => {...this};
/// Returns the values of a [Map] at given [keys] indices.
Iterable<B> valuesByKeys({required Iterable<A> keys}) => keys.map((final key) => this[key]!);
}
extension LinkedHashMapExtensions<A, B> on LinkedHashMap<A, B> {
/// Updated the value at [valueIndex] to [newValue], in addition to preserving the order.
void updateValueAt({
required int valueIndex,
required B newValue,
}) =>
this[keys.toList()[valueIndex]] = newValue;
}

View file

@ -0,0 +1,5 @@
import 'package:flutter/cupertino.dart';
extension ValueNotifierBoolExtensions on ValueNotifier<bool> {
void flipValue() => value = !value;
}

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

View file

@ -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;