import 'package:shared_preferences/shared_preferences.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: the chosen option's stable id /// (a bundled `AppFont.id` or an added `CustomFont.id`). abstract interface class AppFontStore { /// 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 unavailable or unreadable store means the /// default; a failed save returns false rather than throwing. class SharedPreferencesAppFontStore implements AppFontStore { const SharedPreferencesAppFontStore(); @override Future load() async { try { final preferences = await SharedPreferences.getInstance(); return preferences.getString(appFontKey); } catch (_) { return null; } } @override Future save(String id) async { try { final preferences = await SharedPreferences.getInstance(); return await preferences.setString(appFontKey, id); } catch (_) { return false; } } } /// An in-memory store for tests and fixtures. class MemoryAppFontStore implements AppFontStore { MemoryAppFontStore([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; } }