import 'dart:async'; import 'package:flutter/material.dart'; import 'package:selfprivacy/config/localization.dart'; import 'package:selfprivacy/config/preferences_repository/datasources/preferences_datasource.dart'; class PreferencesRepository { const PreferencesRepository({ required this.dataSource, required this.getSupportedLocales, required this.getDelegateLocale, required this.setDelegateLocale, required this.resetDelegateLocale, }); final PreferencesDataSource dataSource; /// easy localizations don't expose type of localization provider, /// so it needs to be this crutchy (I could've created one more class-wrapper, /// containing needed functions, but perceive it as boilerplate, because we /// don't need additional encapsulation level here) final FutureOr Function(Locale) setDelegateLocale; final FutureOr Function() resetDelegateLocale; final FutureOr> Function() getSupportedLocales; final FutureOr Function() getDelegateLocale; Future getSystemThemeModeFlag() async => (await dataSource.getSystemThemeModeFlag()) ?? true; Future setSystemThemeModeFlag(final bool newValue) async => dataSource.setSystemThemeModeFlag(newValue); Future getDarkThemeModeFlag() async => (await dataSource.getDarkThemeModeFlag()) ?? false; Future setDarkThemeModeFlag(final bool newValue) async => dataSource.setDarkThemeModeFlag(newValue); Future setSystemModeFlag(final bool newValue) async => dataSource.setSystemThemeModeFlag(newValue); Future> supportedLocales() async => getSupportedLocales(); Future getActiveLocale() async { Locale? chosenLocale; final String? storedLocaleCode = await dataSource.getLocale(); if (storedLocaleCode != null) { chosenLocale = Locale(storedLocaleCode); } // when it's null fallback on delegate locale chosenLocale ??= Localization.systemLocale; return chosenLocale; } Future setActiveLocale(final Locale newLocale) async { await dataSource.setLocale(newLocale.toString()); } Future resetActiveLocale() async { await dataSource.setLocale(null); } /// true when we need to show onboarding Future getShouldShowOnboarding() async => dataSource.getOnboardingFlag(); /// true when we need to show onboarding Future setShouldShowOnboarding(final bool newValue) => dataSource.setOnboardingFlag(newValue); }