From 728597fcdcd9699c1af55138dcd33022c33011f0 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Fri, 3 Apr 2020 14:27:39 +1100 Subject: [PATCH] Allow model class to be filtered - Each subclass should provide a custom filtering function --- lib/inventree/model.dart | 19 +++++++++++++++++++ lib/inventree/stock.dart | 9 +++++++++ lib/widget/location_display.dart | 23 ++++++++++++++++++++++- lib/widget/stock_display.dart | 1 - 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/lib/inventree/model.dart b/lib/inventree/model.dart index 825f315c..8bf3bee0 100644 --- a/lib/inventree/model.dart +++ b/lib/inventree/model.dart @@ -142,6 +142,25 @@ class InvenTreeModel { // TODO - Define a 'save' / 'update' function + // Override this function for each sub-class + bool matchAgainstString(String filter) => false; + + // Filter this item against a list of provided filters + // Each filter must be matched + // Used for (e.g.) filtering returned results + bool filter(String filterString) { + + List filters = filterString.trim().toLowerCase().split(" "); + + for (var f in filters) { + if (!matchAgainstString(f)) { + return false; + } + } + + return true; + } + } diff --git a/lib/inventree/stock.dart b/lib/inventree/stock.dart index fd616767..493c71e5 100644 --- a/lib/inventree/stock.dart +++ b/lib/inventree/stock.dart @@ -44,6 +44,15 @@ class InvenTreeStockLocation extends InvenTreeModel { var loc = InvenTreeStockLocation.fromJson(json); return loc; + } + @override + bool matchAgainstString(String filter) { + + if (name.toLowerCase().contains(filter)) return true; + + if (description.toLowerCase().contains(filter)) return true; + + return false; } } \ No newline at end of file diff --git a/lib/widget/location_display.dart b/lib/widget/location_display.dart index dd549667..5143973c 100644 --- a/lib/widget/location_display.dart +++ b/lib/widget/location_display.dart @@ -28,6 +28,17 @@ class _LocationDisplayState extends State { List _sublocations = List(); + String _locationFilter = ''; + + List get sublocations { + + if (_locationFilter.isEmpty || _sublocations.isEmpty) { + return _sublocations; + } else { + return _sublocations.where((loc) => loc.filter(_locationFilter)).toList(); + } + } + List _items = List(); String get _title { @@ -86,7 +97,17 @@ class _LocationDisplayState extends State { textAlign: TextAlign.left, style: TextStyle(fontWeight: FontWeight.bold), ), - Expanded(child: SublocationList(_sublocations)), + TextField( + decoration: InputDecoration( + hintText: "Filter locations", + ), + onChanged: (text) { + setState(() { + _locationFilter = text.trim().toLowerCase(); + }); + }, + ), + Expanded(child: SublocationList(sublocations)), Divider(), Text( "Stock Items - ${_items.length}", diff --git a/lib/widget/stock_display.dart b/lib/widget/stock_display.dart index dc62faa5..55d16a5b 100644 --- a/lib/widget/stock_display.dart +++ b/lib/widget/stock_display.dart @@ -27,7 +27,6 @@ class _StockItemDisplayState extends State { if (item == null) { return "Stock Item"; } else { - print(item.jsondata); return "Item: x ${item.partName}"; } }