70 lines
1.7 KiB
Dart
70 lines
1.7 KiB
Dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import '../data/models/note.dart';
|
|
import 'api_client_provider.dart';
|
|
|
|
final notesProvider =
|
|
AsyncNotifierProvider<NotesNotifier, List<Note>>(NotesNotifier.new);
|
|
|
|
class NotesNotifier extends AsyncNotifier<List<Note>> {
|
|
@override
|
|
Future<List<Note>> build() async {
|
|
return ref.watch(notesRepositoryProvider).getAll();
|
|
}
|
|
|
|
Future<Note> create(
|
|
String title,
|
|
String body, {
|
|
List<String> tags = const [],
|
|
int? projectId,
|
|
String noteType = 'note',
|
|
}) async {
|
|
final note = await ref.read(notesRepositoryProvider).create(
|
|
title, body,
|
|
tags: tags,
|
|
projectId: projectId,
|
|
noteType: noteType,
|
|
);
|
|
state = AsyncData([...state.value ?? [], note]);
|
|
return note;
|
|
}
|
|
|
|
Future<Note> updateNote(
|
|
int id,
|
|
String title,
|
|
String body, {
|
|
List<String> tags = const [],
|
|
int? projectId,
|
|
bool clearProject = false,
|
|
String noteType = 'note',
|
|
}) async {
|
|
final updated = await ref.read(notesRepositoryProvider).update(
|
|
id,
|
|
title,
|
|
body,
|
|
tags: tags,
|
|
projectId: projectId,
|
|
clearProject: clearProject,
|
|
noteType: noteType,
|
|
);
|
|
state = AsyncData([
|
|
for (final n in state.value ?? [])
|
|
if (n.id == id) updated else n,
|
|
]);
|
|
return updated;
|
|
}
|
|
|
|
Future<void> delete(int id) async {
|
|
await ref.read(notesRepositoryProvider).delete(id);
|
|
state = AsyncData([
|
|
for (final n in state.value ?? [])
|
|
if (n.id != id) n,
|
|
]);
|
|
}
|
|
}
|
|
|
|
final noteDetailProvider =
|
|
FutureProvider.family<Note, int>((ref, id) async {
|
|
return ref.watch(notesRepositoryProvider).getOne(id);
|
|
});
|