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

124 lines
3.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';
/// 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.standard;
int _revision = 0;
bool _saveFailed = false;
AppDesign get design => _design;
/// 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<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;
_apply(design);
notifyListeners();
}
/// Applies [design] now and stores it.
Future<void> select(AppDesign design) async {
_revision++;
final revision = _revision;
if (design != _design) {
_apply(design);
notifyListeners();
}
final saved = await _store.save(design.id);
if (revision != _revision || saved != _saveFailed) return;
_saveFailed = !saved;
notifyListeners();
}
}