added categories, import export, settings
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
/// App-wide settings: dark mode and the user supermarket list.
|
||||
library;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../models/supermarket.dart';
|
||||
import '../utils/supermarkets.dart';
|
||||
import 'product_repository.dart';
|
||||
|
||||
class AppSettings extends ChangeNotifier {
|
||||
AppSettings(this.repository);
|
||||
|
||||
final ProductRepository repository;
|
||||
|
||||
bool darkMode = false;
|
||||
List<Supermarket> supermarkets = List<Supermarket>.from(kDefaultSupermarkets);
|
||||
int catalogGeneration = 0;
|
||||
|
||||
Future<void> load() async {
|
||||
try {
|
||||
darkMode = await repository.getDarkMode();
|
||||
supermarkets = await repository.getSupermarkets();
|
||||
notifyListeners();
|
||||
} catch (_) {
|
||||
// Widget tests and first-run without a plugin keep the defaults.
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> setDarkMode(bool enabled) async {
|
||||
darkMode = enabled;
|
||||
notifyListeners();
|
||||
try {
|
||||
await repository.setDarkMode(enabled);
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
Future<void> addSupermarket(String name, int colorValue) async {
|
||||
final trimmed = name.trim();
|
||||
if (trimmed.isEmpty) return;
|
||||
final store = await repository.addSupermarket(
|
||||
name: trimmed,
|
||||
colorValue: colorValue,
|
||||
);
|
||||
supermarkets = [...supermarkets, store];
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> updateSupermarket(Supermarket store) async {
|
||||
await repository.updateSupermarket(store);
|
||||
supermarkets = [
|
||||
for (final item in supermarkets)
|
||||
if (item.id == store.id) store else item,
|
||||
];
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> deleteSupermarket(int id) async {
|
||||
await repository.deleteSupermarket(id);
|
||||
supermarkets = [
|
||||
for (final item in supermarkets)
|
||||
if (item.id != id) item,
|
||||
];
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> reload() async {
|
||||
await load();
|
||||
catalogGeneration++;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
class SettingsScope extends InheritedNotifier<AppSettings> {
|
||||
const SettingsScope({
|
||||
super.key,
|
||||
required AppSettings settings,
|
||||
required super.child,
|
||||
}) : super(notifier: settings);
|
||||
|
||||
static AppSettings of(BuildContext context) {
|
||||
final scope = context.dependOnInheritedWidgetOfExactType<SettingsScope>();
|
||||
assert(scope != null, 'SettingsScope not found');
|
||||
return scope!.notifier!;
|
||||
}
|
||||
|
||||
static AppSettings? maybeOf(BuildContext context) {
|
||||
return context.dependOnInheritedWidgetOfExactType<SettingsScope>()?.notifier;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user