120 lines
2.9 KiB
Dart
120 lines
2.9 KiB
Dart
// gets all quotes from the database and returns them in a list
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:frontend/model/quote.dart';
|
|
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() {
|
|
filterController.text = '';
|
|
}
|
|
|
|
QuoteProvider() {
|
|
readQuotes(); // auto-load on creation
|
|
}
|
|
|
|
void populateAuthors() {
|
|
_authors = {};
|
|
for (var i = 0; i < quotes.length; i++) {
|
|
_authors.add(quotes[i].author);
|
|
}
|
|
}
|
|
|
|
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;
|
|
notifyListeners();
|
|
|
|
try {
|
|
_quotes = await repo.readQuotes();
|
|
_filteredQuotes = _quotes;
|
|
} catch (e) {
|
|
_error = e.toString();
|
|
}
|
|
|
|
populateAuthors();
|
|
|
|
_loading = false;
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> createQuote(Quote quote) async {
|
|
final newQuote = await repo.createQuote(quote);
|
|
_quotes.add(newQuote);
|
|
populateAuthors();
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> updateQuote(Quote quote) async {
|
|
try {
|
|
_loading = true;
|
|
notifyListeners();
|
|
|
|
final updated = await repo.updateQuote(quote);
|
|
|
|
final index = _quotes.indexWhere((q) => q.id == quote.id);
|
|
if (index != -1) {
|
|
final oldAuthor = _quotes[index].author;
|
|
final newList = List<Quote>.from(_quotes);
|
|
newList[index] = updated;
|
|
_quotes = newList;
|
|
Map<String, int> authorMap = {};
|
|
for (final q in quotes) {
|
|
authorMap[q.author] = (authorMap[q.author] ?? 0) + 1;
|
|
}
|
|
if (authorMap[oldAuthor] == 1) {
|
|
_authors.remove(oldAuthor);
|
|
}
|
|
_authors.add(updated.author);
|
|
}
|
|
} finally {
|
|
_loading = false;
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
Future<void> deleteQuote(Quote quote) async {
|
|
await repo.deleteQuote(quote);
|
|
|
|
_quotes.removeWhere((q) => q.id == quote.id);
|
|
populateAuthors();
|
|
notifyListeners();
|
|
}
|
|
} |