Support for searching by account or issuer

This commit is contained in:
Neeraj Gupta 2023-02-05 22:33:37 +05:30
parent 2435c9ddef
commit 3b39a34045
No known key found for this signature in database
GPG key ID: 3C5A1684DC1729E1

View file

@ -34,29 +34,55 @@ class _HomePageState extends State<HomePage> {
);
bool _hasLoaded = false;
bool _isSettingsOpen = false;
final TextEditingController _textController = TextEditingController();
bool _showSearchBox = false;
String _searchText = "";
List<Code> _codes = [];
List<Code> _filteredCodes = [];
StreamSubscription<CodesUpdatedEvent>? _streamSubscription;
@override
void initState() {
super.initState();
_textController.addListener(_applyFiltering);
_loadCodes();
_streamSubscription = Bus.instance.on<CodesUpdatedEvent>().listen((event) {
_loadCodes();
});
super.initState();
}
void _loadCodes() {
CodeStore.instance.getAllCodes().then((codes) {
_codes = codes;
_filteredCodes = codes;
_hasLoaded = true;
setState(() {});
});
}
void _applyFiltering() {
if (_searchText.isNotEmpty) {
final String val = _searchText.toLowerCase();
_filteredCodes = _codes
.where(
(element) => (element.account.toLowerCase().contains(val) ||
element.issuer.toLowerCase().contains(val)),
)
.toList();
} else {
_filteredCodes = _codes;
}
if (mounted) {
setState(() {});
}
}
@override
void dispose() {
_streamSubscription?.cancel();
_textController.removeListener(_applyFiltering);
super.dispose();
}
@ -122,6 +148,26 @@ class _HomePageState extends State<HomePage> {
resizeToAvoidBottomInset: false,
appBar: AppBar(
title: const Text('ente Authenticator'),
actions: <Widget>[
IconButton(
icon: const Icon(Icons.search),
tooltip: 'Search',
onPressed: () {
setState(
() {
_showSearchBox = !_showSearchBox;
if (!_showSearchBox) {
_searchText = "";
_applyFiltering();
} else if (_textController.text.isNotEmpty) {
_searchText = _textController.text;
_applyFiltering();
}
},
);
},
),
],
),
floatingActionButton: !_hasLoaded ||
_codes.isEmpty ||
@ -134,14 +180,14 @@ class _HomePageState extends State<HomePage> {
Widget _getBody() {
if (_hasLoaded) {
if (_codes.isEmpty) {
if (_filteredCodes.isEmpty && _searchText.isEmpty) {
return _getEmptyState();
} else {
final list = ListView.builder(
itemBuilder: ((context, index) {
return CodeWidget(_codes[index]);
return CodeWidget(_filteredCodes[index]);
}),
itemCount: _codes.length,
itemCount: _filteredCodes.length,
);
if (!PreferenceService.instance.hasShownCoachMark()) {
return Stack(
@ -150,6 +196,47 @@ class _HomePageState extends State<HomePage> {
_getCoachMarkWidget(),
],
);
} else if (_showSearchBox) {
return Column(
children: [
Center(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 24.0),
child: TextField(
autofocus: _searchText.isEmpty,
controller: _textController,
onChanged: (val) {
_searchText = val;
_applyFiltering();
},
decoration: InputDecoration(
// prefixIcon: const Icon(Icons.search),
suffixIcon: IconButton(
icon: const Icon(Icons.clear),
onPressed: () {
_textController.clear();
_searchText = "";
_applyFiltering();
},
),
hintText: 'Search...',
border: InputBorder.none,
),
),
),
),
_filteredCodes.isNotEmpty
? Expanded(
child: ListView.builder(
itemBuilder: ((context, index) {
return CodeWidget(_filteredCodes[index]);
}),
itemCount: _filteredCodes.length,
),
)
: (const Text("No result")),
],
);
} else {
return list;
}