import 'package:flutter/foundation.dart'; import '../domain/diff_search.dart'; import '../domain/file_change.dart'; /// The diff page's search: whether it is open, its query, every match across /// the viewed files and which one is current. class DiffSearchController extends ChangeNotifier { bool _open = false; String _query = ''; List _matches = const []; int? _current; bool get open => _open; String get query => _open ? _query : ''; List get matches => _matches; /// The current match, or null when there is none. DiffMatch? get current => _current == null ? null : _matches[_current!]; /// The current match's position counted from 1, or 0. int get position => _current == null ? 0 : _current! + 1; void show() { if (_open) return; _open = true; notifyListeners(); } void close() { if (!_open) return; _open = false; notifyListeners(); } /// Searches [files] for [query], starting at the first match in [fromFile] /// or after it. void search(List files, String query, {required int fromFile}) { _open = true; _query = query; _matches = diffMatches(files, query); _current = firstMatchFrom(_matches, fromFile); notifyListeners(); } /// Moves to the next (+1) or previous (-1) match, wrapping around. Returns /// false when there is no match to move to. bool step(int delta) { if (!_open || _matches.isEmpty) return false; final from = _current ?? (delta > 0 ? -1 : 0); _current = (from + delta) % _matches.length; notifyListeners(); return true; } }