import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart' show kThemeAnimationDuration; import 'package:shared_preferences/shared_preferences.dart'; import 'app_design.dart'; /// The device's design choice. It lives in this device's (or browser's) own /// preferences and is never sent to a server. const String appDesignKey = 'pipelines.design.v1'; /// The persistence seam for the design choice: an [AppDesign.id]. abstract interface class AppDesignStore { /// The stored id; null when nothing is stored or it cannot be read. Future load(); /// Whether the choice is actually stored. Future save(String id); } /// `shared_preferences` storage. An unreadable store means the default; a /// failed save returns false rather than throwing. class SharedPreferencesAppDesignStore implements AppDesignStore { const SharedPreferencesAppDesignStore(); @override Future load() async { try { return (await SharedPreferences.getInstance()).getString(appDesignKey); } catch (_) { return null; } } @override Future save(String id) async { try { return await (await SharedPreferences.getInstance()).setString( appDesignKey, id, ); } catch (_) { return false; } } } /// An in-memory store for tests and fixtures. class MemoryAppDesignStore implements AppDesignStore { MemoryAppDesignStore([this.id]); String? id; bool failSaves = false; @override Future load() async => id; @override Future save(String id) async { if (failSaves) return false; this.id = id; return true; } } /// The device's app-wide design. The theme hosts listen to it, so a choice /// rebuilds every theme in place without a restart. class AppDesignController extends ChangeNotifier { AppDesignController({AppDesignStore? store}) : _store = store ?? const SharedPreferencesAppDesignStore(); /// The app's own controller, shared by the theme hosts and Settings. static final AppDesignController instance = AppDesignController(); final AppDesignStore _store; AppDesign _design = AppDesign.defaultDesign; AppDesign? _preview; int _revision = 0; bool _saveFailed = false; /// The design shown: a [preview] while there is one, else the choice. AppDesign get design => _preview ?? _design; /// The stored choice, whatever a [preview] shows meanwhile. AppDesign get chosen => _design; /// Shows [design] app-wide without storing it (the design switcher's live /// preview); null returns to the choice. [select] ends a preview. void preview(AppDesign? design) { final shown = this.design; _preview = design; if (this.design != shown) notifyListeners(); } /// How long the theme hosts blend into the current design (their /// `themeAnimationDuration`): Material's blend between designs of the same /// density, none for a switch into or out of a dense design. A dense /// design's sizes (icons, controls) have no value in a comfortable theme to /// blend from, so a blend would pass every icon through size zero, and in /// the browser (skwasm) laying out the near-zero icon glyphs could hang. Duration get themeAnimationDuration => _snapTheme ? Duration.zero : kThemeAnimationDuration; bool _snapTheme = false; void _apply(AppDesign design) { _snapTheme = _design.dense || design.dense; _design = design; } /// The last choice could not be stored on this device. bool get saveFailed => _saveFailed; /// Reads the stored choice. A choice made while reading wins. Future restore() async { final revision = _revision; final id = await _store.load(); if (revision != _revision || id == null) return; final design = AppDesign.fromId(id); if (design == _design) return; <<<<<<< HEAD _apply(design); notifyListeners(); ======= _design = design; if (_preview == null) notifyListeners(); >>>>>>> main } /// Applies [design] now and stores it. Future select(AppDesign design) async { _revision++; final revision = _revision; <<<<<<< HEAD if (design != _design) { _apply(design); notifyListeners(); } ======= final shown = this.design; _preview = null; _design = design; if (design != shown) notifyListeners(); >>>>>>> main final saved = await _store.save(design.id); if (revision != _revision || saved != _saveFailed) return; _saveFailed = !saved; notifyListeners(); } }