Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ targets:
drift_dev:
options:
databases:
thunder: lib/src/core/database/database.dart
schema_dir: lib/src/core/database/schemas/
thunder: lib/src/foundation/persistence/database/database.dart
schema_dir: lib/src/foundation/persistence/database/schemas/
4 changes: 2 additions & 2 deletions lib/src/app/bootstrap/bootstrap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ Future<void> bootstrap() async {
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);

// Initialize preferences and database
await UserPreferences.instance.initialize();
await performSharedPreferencesMigration();
initializeDatabase();
await performDatabaseIntegrityChecks();
await UserPreferences.instance.initialize();
await performSharedPreferencesMigration();

final account = await fetchActiveProfile();

Expand Down
3 changes: 2 additions & 1 deletion lib/src/app/bootstrap/preferences_migration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'package:thunder/packages/ui/ui.dart' show NameColor;
/// Performs migrations for shared preferences.
Future<void> performSharedPreferencesMigration() async {
final prefs = UserPreferences.instance.preferences;
final draftRepository = DraftRepositoryImpl(database: database);

// Migrate the openInExternalBrowser setting, if found.
bool? legacyOpenInExternalBrowser = prefs.getBool(LocalSettings.openLinksInExternalBrowser.name);
Expand Down Expand Up @@ -91,7 +92,7 @@ Future<void> performSharedPreferencesMigration() async {
body: (draftPost?['text'] ?? draftComment?['text']) as String?,
);

Draft.upsertDraft(draft);
await draftRepository.upsertDraft(draft);

// If we've gotten this far without exception, it's safe to delete the shared pref eky
prefs.remove(draftKey);
Expand Down
10 changes: 7 additions & 3 deletions lib/src/app/shell/navigation/navigation_post.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ Future<void> navigateToComment(BuildContext context, ThunderComment comment) asy

Future<ThunderComment?> navigateToCreateCommentPage(
BuildContext context, {
Account? account,
ThunderPost? post,
ThunderComment? comment,
ThunderComment? parentComment,
Expand Down Expand Up @@ -183,6 +184,7 @@ Future<ThunderComment?> navigateToCreateCommentPage(
BlocProvider<ProfileBloc>.value(value: profileBloc),
],
child: CreateCommentPage(
account: account ?? profileBloc.state.account,
post: post,
comment: comment,
parentComment: parentComment,
Expand All @@ -198,6 +200,7 @@ Future<ThunderComment?> navigateToCreateCommentPage(

Future<void> navigateToCreatePostPage(
BuildContext context, {
Account? account,
String? title,
String? text,
File? image,
Expand All @@ -211,13 +214,13 @@ Future<void> navigateToCreatePostPage(
}) async {
try {
final l10n = AppLocalizations.of(context)!;
final account = context.read<ProfileBloc>().state.account;
final effectiveAccount = account ?? context.read<ProfileBloc>().state.account;

FeedBloc? feedBloc;
PostBloc? postBloc;
ThunderBloc thunderBloc = context.read<ThunderBloc>();
ProfileBloc profileBloc = context.read<ProfileBloc>();
CreatePostCubit createPostCubit = createCreatePostCubit(account);
CreatePostCubit createPostCubit = createCreatePostCubit(effectiveAccount);

final themeCubit = context.read<ThemePreferencesCubit>();
final bool reduceAnimations = themeCubit.state.reduceAnimations;
Expand Down Expand Up @@ -250,13 +253,14 @@ Future<void> navigateToCreatePostPage(
builder: (navigatorContext) {
return MultiBlocProvider(
providers: [
feedBloc != null ? BlocProvider<FeedBloc>.value(value: feedBloc) : BlocProvider(create: (context) => createFeedBloc(account)),
feedBloc != null ? BlocProvider<FeedBloc>.value(value: feedBloc) : BlocProvider(create: (context) => createFeedBloc(effectiveAccount)),
if (postBloc != null) BlocProvider<PostBloc>.value(value: postBloc),
BlocProvider<ThunderBloc>.value(value: thunderBloc),
BlocProvider<ProfileBloc>.value(value: profileBloc),
BlocProvider<CreatePostCubit>.value(value: createPostCubit),
],
child: CreatePostPage(
account: effectiveAccount,
title: title,
text: text,
image: image,
Expand Down
68 changes: 68 additions & 0 deletions lib/src/app/shell/pages/thunder_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import 'package:thunder/src/features/notification/application/state/notification
import 'package:thunder/src/features/settings/api.dart';
import 'package:thunder/src/features/feed/api.dart';
import 'package:thunder/src/features/comment/api.dart';
import 'package:thunder/src/features/drafts/drafts.dart';
import 'package:thunder/src/app/shell/widgets/bottom_nav_bar.dart';
import 'package:thunder/src/app/shell/navigation/link_navigation_utils.dart';
import 'package:thunder/src/features/inbox/inbox.dart';
Expand Down Expand Up @@ -57,6 +58,7 @@ class _ThunderState extends State<Thunder> {
bool hasShownUpdateDialog = false;
bool hasShownChangelogDialog = false;
bool hasShownPageView = false;
bool hasAttemptedDraftRestore = false;

bool _isFabOpen = false;

Expand All @@ -70,6 +72,8 @@ class _ThunderState extends State<Thunder> {

bool errorMessageLoading = false;

final DraftRepository _draftRepository = DraftRepositoryImpl(database: database);

@override
void initState() {
super.initState();
Expand Down Expand Up @@ -150,6 +154,68 @@ class _ThunderState extends State<Thunder> {
}
}

void _restoreDraftSession(ProfileState profileState) {
if (hasAttemptedDraftRestore) {
return;
}

hasAttemptedDraftRestore = true;

WidgetsBinding.instance.addPostFrameCallback((_) async {
await Future<void>.delayed(const Duration(milliseconds: 400));

final launchedFromExternalIntent = currentIntent == ANDROID_INTENT_ACTION_VIEW || currentIntent == 'android.intent.action.SEND' || currentIntent == 'android.intent.action.SEND_MULTIPLE';

if (!mounted || launchedFromExternalIntent) {
return;
}

if (Navigator.of(context).canPop()) {
return;
}

await restoreActiveDraftSession(
repository: _draftRepository,
account: profileState.account,
onPostCreateRestore: (account, communityId, community) async {
if (!mounted || Navigator.of(context).canPop()) {
return;
}

await navigateToCreatePostPage(context, account: account, communityId: communityId, community: community);
},
onPostEditRestore: (account, post) async {
if (!mounted || Navigator.of(context).canPop()) {
return;
}

await navigateToCreatePostPage(context, account: account, post: post);
},
onCommentCreateFromPostRestore: (account, post) async {
if (!mounted || Navigator.of(context).canPop()) {
return;
}

await navigateToCreateCommentPage(context, account: account, post: post);
},
onCommentCreateFromCommentRestore: (account, comment) async {
if (!mounted || Navigator.of(context).canPop()) {
return;
}

await navigateToCreateCommentPage(context, account: account, parentComment: comment);
},
onCommentEditRestore: (account, comment) async {
if (!mounted || Navigator.of(context).canPop()) {
return;
}

await navigateToCreateCommentPage(context, account: account, comment: comment);
},
);
});
}

FutureOr<bool> _handleBackButtonPress(bool stopDefaultButtonEvent, RouteInfo info) async {
final bool topOfNavigationStack = ModalRoute.of(context)?.isCurrent ?? false;

Expand Down Expand Up @@ -370,6 +436,8 @@ class _ThunderState extends State<Thunder> {
);
case ProfileStatus.contentWarning:
case ProfileStatus.success:
_restoreDraftSession(state);

Version? version = thunderBlocState.version;
bool showInAppUpdateNotification = thunderBlocState.showInAppUpdateNotification;

Expand Down
Loading