import 'controllers/reader_settings_controller.dart' show ReaderSettingsController; import 'controllers/subscriptions_controller.dart'; import 'pages/mews_urls.dart'; /// Applies the change an action address encodes, e.g. mews:do/set-theme. /// Returns whether anything changed, so the navigator can skip a rerender that /// would show the same page. /// /// An unknown verb or a missing argument does nothing and reports nothing: the /// only links carrying these addresses are the ones the client builds, so a /// miss is a bug rather than something to explain to the reader. class InternalActions { InternalActions({ required ReaderSettingsController settings, required SubscriptionsController subscriptions, }) : _settings = settings, _subscriptions = subscriptions; final ReaderSettingsController _settings; final SubscriptionsController _subscriptions; Future apply(String url) async { final verb = MewsUrls.verbOf(url); if (verb == null) return false; final args = Uri.parse(url).queryParameters; return switch (verb) { 'set-font' => _settings.setFont(args['value'] ?? ''), 'set-size' => _setSize(args['step']), 'set-theme' => _settings.setThemeMode(args['value'] ?? ''), 'set-colors' => _settings.setVariant(args['value'] ?? ''), 'mark-read' => _markRead(args, read: true), 'mark-unread' => _markRead(args, read: false), 'refresh-subscriptions' => _refreshAll(), 'refresh-subscription' => _refreshOne(args['url']), _ => false, }; } Future _setSize(String? step) => switch (step) { 'up' => _settings.increaseSize(), 'down' => _settings.decreaseSize(), _ => Future.value(false), }; Future _markRead(Map args, {required bool read}) async { final url = args['url']; if (url == null || url.isEmpty) return false; final site = args['site']; if (read) { await _subscriptions.markRead(url, siteUrl: site); } else { await _subscriptions.markUnread(url, siteUrl: site); } return true; } /// The check redraws the page itself as it runs, so the caller need not. Future _refreshAll() async { await _subscriptions.refreshAll(); return false; } Future _refreshOne(String? siteUrl) async { if (siteUrl == null || siteUrl.isEmpty) return false; await _subscriptions.refreshOne(siteUrl); return false; } }