Skip to content

Commit

Permalink
refactor: replace StreamBuilder with ConsumerWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasn committed Jun 6, 2024
1 parent 1ea8d4e commit d6eef1f
Show file tree
Hide file tree
Showing 7 changed files with 332 additions and 120 deletions.
2 changes: 1 addition & 1 deletion lib/features/journal/state/entry_controller.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions lib/features/journal/state/journal_card_controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import 'dart:async';

import 'package:lotti/classes/journal_entities.dart';
import 'package:lotti/database/database.dart';
import 'package:lotti/get_it.dart';
import 'package:lotti/services/db_notification.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'journal_card_controller.g.dart';

@riverpod
class JournalCardController extends _$JournalCardController {
JournalCardController() {
listen();
}

late final String entryId;
StreamSubscription<({DatabaseType type, String id})>? _updateSubscription;
final JournalDb _journalDb = getIt<JournalDb>();
final UpdateNotifications _updateNotifications = getIt<UpdateNotifications>();

void listen() {
_updateSubscription =
_updateNotifications.updateStream.listen((event) async {
if (event.id == entryId) {
final latest = await _fetch();
if (latest != state.value) {
state = AsyncData(latest);
}
}
});
}

@override
Future<JournalEntity?> build({required String id}) async {
entryId = id;
ref.onDispose(() => _updateSubscription?.cancel());
final entry = await _fetch();
return entry;
}

Future<JournalEntity?> _fetch() async {
return _journalDb.journalEntityById(entryId);
}
}
177 changes: 177 additions & 0 deletions lib/features/journal/state/journal_card_controller.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d6eef1f

Please sign in to comment.