Line data Source code
1 : import 'dart:async';
2 :
3 : import 'package:flutter/widgets.dart';
4 : import 'package:fossui/src/components/toast/foss_toast.dart';
5 :
6 : /// A live toast in a [FossToastController]'s queue.
7 : class FossToastEntry {
8 : /// Creates an entry. Built by the controller, not directly.
9 2 : FossToastEntry({required this.id, required this.toast});
10 :
11 : /// Stable identity within the controller, returned by `show`.
12 : final int id;
13 :
14 : /// The current message; replaced by `update`.
15 : FossToast toast;
16 :
17 : // Auto-dismiss machinery, owned by the controller. The timer only runs while
18 : // the entry is visible and not pressed; [_remaining] carries the time left so
19 : // a pause resumes where it stopped, and [_total] is null when the toast
20 : // persists (loading, or a non-positive duration).
21 : Duration? _total;
22 : Duration _remaining = Duration.zero;
23 : Timer? _timer;
24 : bool _pressed = false;
25 : final Stopwatch _watch = Stopwatch();
26 : }
27 :
28 : /// Owns the queue of live toasts and their auto-dismiss timers. Shared by a
29 : /// `FossToaster` and read with `FossToastScope.of`. Notifies on every change.
30 : ///
31 : /// A toast's dismiss timer runs only while the toast is one of the
32 : /// [maxVisible] on screen and is not being pressed, so a queued toast cannot
33 : /// expire before it is ever seen, and a press holds it open.
34 : class FossToastController extends ChangeNotifier {
35 : /// The default auto-dismiss delay.
36 : static const Duration defaultDuration = Duration(milliseconds: 5000);
37 :
38 : /// The most toasts shown at once; older ones wait.
39 : static const int maxVisible = 3;
40 :
41 : final List<FossToastEntry> _entries = <FossToastEntry>[];
42 : int _nextId = 0;
43 :
44 : /// The live toasts, oldest first.
45 6 : List<FossToastEntry> get entries => List.unmodifiable(_entries);
46 :
47 : /// Enqueues [toast] and returns its id. Schedules auto-dismiss once the toast
48 : /// is visible, unless it is [FossToastVariant.loading] or its duration is
49 : /// zero.
50 2 : int show(FossToast toast) {
51 2 : final total = _totalFor(toast);
52 6 : final entry = FossToastEntry(id: _nextId++, toast: toast)
53 2 : .._total = total
54 2 : .._remaining = total ?? Duration.zero;
55 4 : _entries.add(entry);
56 2 : _reconcile();
57 2 : notifyListeners();
58 2 : return entry.id;
59 : }
60 :
61 : /// Replaces the message of [id] in place and restarts its timer. The
62 : /// loading-to-status flip. No-op if [id] is gone.
63 2 : void update(int id, FossToast toast) {
64 2 : final entry = _find(id);
65 : if (entry == null) return;
66 2 : _pause(entry);
67 2 : final total = _totalFor(toast);
68 : entry
69 2 : ..toast = toast
70 2 : .._total = total
71 2 : .._remaining = total ?? Duration.zero;
72 2 : _reconcile();
73 2 : notifyListeners();
74 : }
75 :
76 : /// Removes [id]. No-op if it is already gone.
77 2 : void dismiss(int id) {
78 2 : final entry = _find(id);
79 : if (entry == null) return;
80 2 : _pause(entry);
81 4 : _entries.remove(entry);
82 2 : _reconcile();
83 2 : notifyListeners();
84 : }
85 :
86 : /// Removes every toast.
87 1 : void clear() {
88 1 : _entries
89 2 : ..forEach(_pause)
90 1 : ..clear();
91 1 : notifyListeners();
92 : }
93 :
94 : /// Holds [id] open while it is pressed, resuming its countdown on release.
95 : /// No-op if [id] is gone or persists.
96 2 : void setPressed(int id, {required bool pressed}) {
97 2 : final entry = _find(id);
98 4 : if (entry == null || entry._pressed == pressed) return;
99 2 : entry._pressed = pressed;
100 2 : _reconcile();
101 : }
102 :
103 : // Runs a timer for every visible, unpressed, auto-dismiss entry, and pauses
104 : // the rest. Called after every change; idempotent.
105 2 : void _reconcile() {
106 6 : final firstVisible = _entries.length - maxVisible;
107 8 : for (var i = 0; i < _entries.length; i++) {
108 4 : final entry = _entries[i];
109 2 : final visible = i >= firstVisible;
110 4 : final shouldRun = entry._total != null && visible && !entry._pressed;
111 2 : if (shouldRun && entry._timer == null) {
112 2 : _resume(entry);
113 2 : } else if (!shouldRun && entry._timer != null) {
114 2 : _pause(entry);
115 : }
116 : }
117 : }
118 :
119 2 : void _resume(FossToastEntry entry) {
120 2 : entry._watch
121 2 : ..reset()
122 2 : ..start();
123 9 : entry._timer = Timer(entry._remaining, () => dismiss(entry.id));
124 : }
125 :
126 2 : void _pause(FossToastEntry entry) {
127 2 : if (entry._timer == null) return;
128 4 : entry._timer?.cancel();
129 2 : entry._timer = null;
130 4 : entry._watch.stop();
131 8 : entry._remaining -= entry._watch.elapsed;
132 4 : if (entry._remaining < Duration.zero) entry._remaining = Duration.zero;
133 : }
134 :
135 2 : Duration? _totalFor(FossToast toast) {
136 4 : if (toast.variant == FossToastVariant.loading) return null;
137 2 : final duration = toast.duration ?? defaultDuration;
138 2 : return duration <= Duration.zero ? null : duration;
139 : }
140 :
141 2 : FossToastEntry? _find(int id) {
142 4 : for (final entry in _entries) {
143 4 : if (entry.id == id) return entry;
144 : }
145 : return null;
146 : }
147 :
148 2 : @override
149 : void dispose() {
150 6 : _entries.forEach(_pause);
151 2 : super.dispose();
152 : }
153 : }
|