Skip to content

Commit

Permalink
Merge pull request #1765 from matthiasn/refactor/journal_card_upgrade
Browse files Browse the repository at this point in the history
refactor: journal card controller
  • Loading branch information
matthiasn committed Jun 6, 2024
2 parents 1ea8d4e + c6dc001 commit ea6f199
Show file tree
Hide file tree
Showing 9 changed files with 421 additions and 170 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.

14 changes: 0 additions & 14 deletions lib/widgets/journal/editor/editor_widget.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// ignore_for_file: avoid_dynamic_calls

import 'dart:async';

import 'package:flex_color_scheme/flex_color_scheme.dart';
Expand All @@ -9,7 +7,6 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:lotti/features/journal/state/entry_controller.dart';
import 'package:lotti/l10n/app_localizations_context.dart';
import 'package:lotti/themes/theme.dart';
import 'package:lotti/utils/platform.dart';
import 'package:lotti/widgets/journal/editor/editor_styles.dart';
import 'package:lotti/widgets/journal/editor/editor_toolbar.dart';

Expand Down Expand Up @@ -40,20 +37,9 @@ class EditorWidget extends ConsumerWidget {
final controller = notifier.controller;
final focusNode = notifier.focusNode;

final isFocused = entryState.value?.isFocused ?? false;
final shouldShowEditorToolBar =
entryState.value?.shouldShowEditorToolBar ?? false;

if (isFocused && isMobile) {
Future.microtask(() {
Scrollable.ensureVisible(
context,
duration: const Duration(milliseconds: 400),
curve: Curves.easeInOutQuint,
);
});
}

return Card(
color: Theme.of(context).colorScheme.surface.brighten(),
elevation: 0,
Expand Down
Loading

0 comments on commit ea6f199

Please sign in to comment.