Files
pipelines/app/lib/ui/theme/app_design_controller.dart
T

148 lines
4.7 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart' show kThemeAnimationDuration;
import 'package:shared_preferences/shared_preferences.dart';
import 'app_design.dart';
import 'app_design_tokens.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<String?> load();
/// Whether the choice is actually stored.
Future<bool> 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<String?> load() async {
try {
return (await SharedPreferences.getInstance()).getString(appDesignKey);
} catch (_) {
return null;
}
}
@override
Future<bool> 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<String?> load() async => id;
@override
Future<bool> 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) _shown(shown);
}
/// 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 or one that
/// asks to snap ([AppDesignTokens.snapSwitch]). 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.
/// A design whose switches snap: a dense one, or one whose tokens ask to
/// ([AppDesignTokens.snapSwitch], High contrast).
static bool _snaps(AppDesign design) =>
design.dense ||
(design.light().extension<AppDesignTokens>()?.snapSwitch ?? false);
Duration get themeAnimationDuration =>
_snapTheme ? Duration.zero : kThemeAnimationDuration;
bool _snapTheme = false;
/// The shown design changed from [before]: decide the hosts' blend and
/// tell them.
void _shown(AppDesign before) {
_snapTheme = _snaps(before) || _snaps(design);
notifyListeners();
}
/// 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<void> 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;
final shown = this.design;
_design = design;
if (_preview == null) _shown(shown);
}
/// Applies [design] now and stores it.
Future<void> select(AppDesign design) async {
_revision++;
final revision = _revision;
final shown = this.design;
_preview = null;
_design = design;
if (design != shown) _shown(shown);
final saved = await _store.save(design.id);
if (revision != _revision || saved != _saveFailed) return;
_saveFailed = !saved;
notifyListeners();
}
}