import 'package:flutter/widgets.dart'; import 'package:shared_preferences/shared_preferences.dart'; /// Prefix of the persisted list-pane widths, one key per destination. const listPaneWidthStoragePrefix = 'pipelines.next.listPaneWidth.v1.'; /// The persistence seam for list-pane widths the operator dragged, in logical /// pixels per destination. Both directions may fail: a null read means /// "nothing saved", a false write means "this session only". abstract interface class ListPaneWidthStore { Future read(String destination); /// Saves [width], or forgets the destination's width when it is null. Future write(String destination, double? width); } /// `shared_preferences` storage under [listPaneWidthStoragePrefix]. class SharedPreferencesListPaneWidthStore implements ListPaneWidthStore { const SharedPreferencesListPaneWidthStore(); @override Future read(String destination) async { try { return (await SharedPreferences.getInstance()).getDouble( '$listPaneWidthStoragePrefix$destination', ); } catch (_) { return null; } } @override Future write(String destination, double? width) async { try { final prefs = await SharedPreferences.getInstance(); final key = '$listPaneWidthStoragePrefix$destination'; return width == null ? await prefs.remove(key) : await prefs.setDouble(key, width); } catch (_) { return false; } } } /// An in-memory store for tests, fixtures and panes outside a shell. class MemoryListPaneWidthStore implements ListPaneWidthStore { MemoryListPaneWidthStore([Map? values]) : values = values ?? {}; final Map values; @override Future read(String destination) async => values[destination]; @override Future write(String destination, double? width) async { if (width == null) { values.remove(destination); } else { values[destination] = width; } return true; } } /// The list-pane widths the operator chose, per destination; a destination /// without one uses the default policy (`listDetailListWidth`). /// /// A drag [preview]s widths live and [commit]s once; a window too small for /// a saved width clamps it when laying out, never here, so the saved value /// survives a small window. class ListPaneWidths extends ChangeNotifier { ListPaneWidths({ListPaneWidthStore? store}) : _store = store ?? const SharedPreferencesListPaneWidthStore(); final ListPaneWidthStore _store; final Map _widths = {}; final Set _loaded = {}; bool _disposed = false; /// The chosen width for [destination], null for the default policy. The /// first ask loads the saved width and notifies when it arrives. double? widthFor(String destination) { if (_loaded.add(destination)) _load(destination); return _widths[destination]; } Future _load(String destination) async { final saved = await _store.read(destination); // A drag before the read finished wins over the saved value. if (_disposed || saved == null || _widths.containsKey(destination)) { return; } _widths[destination] = saved; notifyListeners(); } /// Shows [width] for [destination] without saving it. void preview(String destination, double width) { _loaded.add(destination); if (_widths[destination] == width) return; _widths[destination] = width; notifyListeners(); } /// Saves the destination's current width. Future commit(String destination) async { await _store.write(destination, _widths[destination]); } /// Shows and saves [width] for [destination]. Future set(String destination, double width) { preview(destination, width); return commit(destination); } /// Returns [destination] to the default policy and forgets its width. Future reset(String destination) async { _loaded.add(destination); if (_widths.remove(destination) != null) notifyListeners(); await _store.write(destination, null); } @override void dispose() { _disposed = true; super.dispose(); } } /// Provides the shell's [ListPaneWidths] to the list-detail panes below it. class ListPaneWidthsScope extends InheritedNotifier { const ListPaneWidthsScope({ super.key, required ListPaneWidths widths, required super.child, }) : super(notifier: widths); static ListPaneWidths? maybeOf(BuildContext context) => context .dependOnInheritedWidgetOfExactType() ?.notifier; }