working on filtering
This commit is contained in:
@@ -7,17 +7,20 @@ import 'package:frontend/repository/quote_repository.dart';
|
||||
|
||||
class QuoteProvider extends ChangeNotifier {
|
||||
List<Quote> _quotes = [];
|
||||
List<Quote> _filteredQuotes = [];
|
||||
Set<String> _authors = {};
|
||||
bool _loading = false;
|
||||
String? _error;
|
||||
|
||||
List<Quote> get quotes => _quotes;
|
||||
List<Quote> get filteredQuotes => _filteredQuotes;
|
||||
Set<String> get authors => _authors;
|
||||
bool get loading => _loading;
|
||||
String? get error => _error;
|
||||
|
||||
final QuoteRepository repo = QuoteRepository();
|
||||
|
||||
int filterBy = 0; // 0 = by author, 1 = by quote
|
||||
var filterController = TextEditingController(text: '');
|
||||
|
||||
void clearFilterController() {
|
||||
@@ -35,6 +38,25 @@ class QuoteProvider extends ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
void filterQuotes() {
|
||||
_filteredQuotes = [];
|
||||
for (var quote in _quotes) {
|
||||
switch (filterBy) {
|
||||
case 0:
|
||||
if (quote.author.toLowerCase() == filterController.text.toLowerCase()) {
|
||||
_filteredQuotes.add(quote);
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
if (quote.body.toLowerCase().contains(filterController.text.toLowerCase())) {
|
||||
_filteredQuotes.add(quote);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> readQuotes() async {
|
||||
_loading = true;
|
||||
_error = null;
|
||||
@@ -42,6 +64,7 @@ class QuoteProvider extends ChangeNotifier {
|
||||
|
||||
try {
|
||||
_quotes = await repo.readQuotes();
|
||||
_filteredQuotes = _quotes;
|
||||
} catch (e) {
|
||||
_error = e.toString();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user