2853b18b9e
System default plus Inter, IBM Plex Sans, Source Sans 3 and Atkinson Hyperlegible Next (OFL 1.1, bundled as on-demand assets and loaded with FontLoader, so the web fetches only the previewed/chosen files). Stored in shared_preferences, applied live to Material and Cupertino text themes; RobotoMono styles unchanged. Licences registered with LicenseRegistry.
61 lines
1.6 KiB
Dart
61 lines
1.6 KiB
Dart
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import 'app_font.dart';
|
|
|
|
/// The device's font choice. It lives in this device's (or browser's) own
|
|
/// preferences and is never sent to a server, so a phone and a browser signed
|
|
/// in to the same server keep their own choice.
|
|
const String appFontKey = 'pipelines.next.font.v1';
|
|
|
|
/// The persistence seam for the font choice.
|
|
abstract interface class AppFontStore {
|
|
Future<AppFont> load();
|
|
|
|
/// Whether the choice is actually stored.
|
|
Future<bool> save(AppFont font);
|
|
}
|
|
|
|
/// `shared_preferences` storage. An unavailable or unreadable store means the
|
|
/// default; a failed save returns false rather than throwing.
|
|
class SharedPreferencesAppFontStore implements AppFontStore {
|
|
const SharedPreferencesAppFontStore();
|
|
|
|
@override
|
|
Future<AppFont> load() async {
|
|
try {
|
|
final preferences = await SharedPreferences.getInstance();
|
|
return AppFont.fromId(preferences.getString(appFontKey));
|
|
} catch (_) {
|
|
return AppFont.system;
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<bool> save(AppFont font) async {
|
|
try {
|
|
final preferences = await SharedPreferences.getInstance();
|
|
return await preferences.setString(appFontKey, font.id);
|
|
} catch (_) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// An in-memory store for tests and fixtures.
|
|
class MemoryAppFontStore implements AppFontStore {
|
|
MemoryAppFontStore([this.font = AppFont.system]);
|
|
|
|
AppFont font;
|
|
bool failSaves = false;
|
|
|
|
@override
|
|
Future<AppFont> load() async => font;
|
|
|
|
@override
|
|
Future<bool> save(AppFont font) async {
|
|
if (failSaves) return false;
|
|
this.font = font;
|
|
return true;
|
|
}
|
|
}
|