add author selection menu
This commit is contained in:
@@ -7,10 +7,12 @@ import 'package:frontend/repository/quote_repository.dart';
|
||||
|
||||
class QuoteProvider extends ChangeNotifier {
|
||||
List<Quote> _quotes = [];
|
||||
Set<String> _authors = {};
|
||||
bool _loading = false;
|
||||
String? _error;
|
||||
|
||||
List<Quote> get quotes => _quotes;
|
||||
Set<String> get authors => _authors;
|
||||
bool get loading => _loading;
|
||||
String? get error => _error;
|
||||
|
||||
@@ -20,6 +22,13 @@ class QuoteProvider extends ChangeNotifier {
|
||||
readQuotes(); // auto-load on creation
|
||||
}
|
||||
|
||||
void populateAuthors() {
|
||||
_authors = {};
|
||||
for (var i = 0; i < quotes.length; i++) {
|
||||
_authors.add(quotes[i].author);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> readQuotes() async {
|
||||
_loading = true;
|
||||
_error = null;
|
||||
@@ -31,6 +40,8 @@ class QuoteProvider extends ChangeNotifier {
|
||||
_error = e.toString();
|
||||
}
|
||||
|
||||
populateAuthors();
|
||||
|
||||
_loading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
@@ -38,6 +49,7 @@ class QuoteProvider extends ChangeNotifier {
|
||||
Future<void> createQuote(Quote quote) async {
|
||||
final newQuote = await repo.createQuote(quote);
|
||||
_quotes.add(newQuote);
|
||||
populateAuthors();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@@ -50,11 +62,20 @@ class QuoteProvider extends ChangeNotifier {
|
||||
|
||||
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 {
|
||||
} finally {
|
||||
_loading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
@@ -64,6 +85,7 @@ class QuoteProvider extends ChangeNotifier {
|
||||
await repo.deleteQuote(quote);
|
||||
|
||||
_quotes.removeWhere((q) => q.id == quote.id);
|
||||
populateAuthors();
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user