import 'destination.dart'; /// The destinations the operator left, most recent first, for the system Back. /// /// Every destination change (a tap, a search result or notification opening /// another destination) goes through [record]: the destination being left moves /// to the top and the destination being opened leaves the stack, so each /// destination appears at most once and the current one is never in it. /// Settings is a destination of its own, so leaving it returns to whatever was /// selected before it. /// /// [takePrevious] steps back: it removes the top destination and returns it, and /// the next [record] that opens that destination does not push the one being /// left, so Back never builds a forward trail. class TabHistory { final List _tabs = []; AppDestination? _returningTo; /// The left destinations, most recent first. List get tabs => List.unmodifiable(_tabs); bool get isEmpty => _tabs.isEmpty; /// Notes a change of the shown destination from [from] to [to]. void record(AppDestination from, AppDestination to) { if (from == to) return; final returning = _returningTo == to; _returningTo = null; _tabs.remove(to); if (returning) return; _tabs ..remove(from) ..insert(0, from); } /// The destination Back returns to, removed from the stack; null when empty. AppDestination? takePrevious() { if (_tabs.isEmpty) return null; final previous = _tabs.removeAt(0); _returningTo = previous; return previous; } }