/// 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 supermarkets = List.from(kDefaultSupermarkets); int catalogGeneration = 0; Future 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 setDarkMode(bool enabled) async { darkMode = enabled; notifyListeners(); try { await repository.setDarkMode(enabled); } catch (_) {} } Future 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 updateSupermarket(Supermarket store) async { await repository.updateSupermarket(store); supermarkets = [ for (final item in supermarkets) if (item.id == store.id) store else item, ]; notifyListeners(); } Future deleteSupermarket(int id) async { await repository.deleteSupermarket(id); supermarkets = [ for (final item in supermarkets) if (item.id != id) item, ]; notifyListeners(); } Future reload() async { await load(); catalogGeneration++; notifyListeners(); } } class SettingsScope extends InheritedNotifier { const SettingsScope({ super.key, required AppSettings settings, required super.child, }) : super(notifier: settings); static AppSettings of(BuildContext context) { final scope = context.dependOnInheritedWidgetOfExactType(); assert(scope != null, 'SettingsScope not found'); return scope!.notifier!; } static AppSettings? maybeOf(BuildContext context) { return context.dependOnInheritedWidgetOfExactType()?.notifier; } }