-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: replace StreamBuilder with ConsumerWidget
- Loading branch information
Showing
6 changed files
with
279 additions
and
60 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
177
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.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.