Skip to content

Commit

Permalink
fix: fetch reply and renote in draft (#285)
Browse files Browse the repository at this point in the history
* refactor: add NotesNotifier.show

* fix: fetch summary target note

* refactor: change arguments of `confirmPost`

* fix: fetch reply and renote before confirm post
  • Loading branch information
poppingmoon authored Jul 11, 2024
1 parent b9cbd74 commit fc2175f
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 66 deletions.
7 changes: 2 additions & 5 deletions lib/provider/api/page_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,8 @@ class PageNotifier extends _$PageNotifier {
unawaited(
Future.wait(
noteIds.map(
(noteId) async {
final note =
await _misskey.notes.show(NotesShowRequest(noteId: noteId));
ref.read(notesNotifierProvider(account).notifier).add(note);
},
(noteId) =>
ref.read(notesNotifierProvider(account).notifier).show(noteId),
),
),
);
Expand Down
2 changes: 1 addition & 1 deletion lib/provider/api/page_provider.g.dart

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

8 changes: 3 additions & 5 deletions lib/provider/api/post_notifier_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,9 @@ class PostNotifier extends _$PostNotifier {
return remoteNoteId;
}
try {
final note = await ref
.read(misskeyProvider(account))
.notes
.show(NotesShowRequest(noteId: remoteNoteId));
ref.read(notesNotifierProvider(account).notifier).add(note);
await ref
.read(notesNotifierProvider(account).notifier)
.show(remoteNoteId);
} catch (_) {}
return remoteNoteId;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/provider/api/post_notifier_provider.g.dart

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

6 changes: 6 additions & 0 deletions lib/provider/notes_notifier_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ class NotesNotifier extends _$NotesNotifier {
}
}

Future<Note> show(String noteId) async {
final note = await _misskey.notes.show(NotesShowRequest(noteId: noteId));
add(note);
return note;
}

void remove(String noteId) {
state = {
...state,
Expand Down
2 changes: 1 addition & 1 deletion lib/provider/notes_notifier_provider.g.dart

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

64 changes: 40 additions & 24 deletions lib/view/dialog/post_confirmation_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,53 @@ import 'package:misskey_dart/misskey_dart.dart';
import '../../extension/notes_create_request_extension.dart';
import '../../i18n/strings.g.dart';
import '../../model/account.dart';
import '../../model/post_file.dart';
import '../../provider/api/attaches_notifier_provider.dart';
import '../../provider/api/channel_notifier_provider.dart';
import '../../provider/api/i_notifier_provider.dart';
import '../../provider/api/post_notifier_provider.dart';
import '../../provider/note_provider.dart';
import '../../provider/notes_notifier_provider.dart';
import '../../util/future_with_dialog.dart';
import '../widget/note_widget.dart';

Future<bool> confirmPost(
BuildContext context,
Account account, {
String? noteId,
NotesCreateRequest? request,
WidgetRef ref,
Account account,
NotesCreateRequest request, {
List<DriveFile>? files,
}) async {
if (request.replyId case final replyId?) {
final reply = ref.read(noteProvider(account, replyId));
if (reply == null) {
final reply = await futureWithDialog(
ref.context,
ref.read(notesNotifierProvider(account).notifier).show(replyId),
);
if (reply == null) {
return false;
}
}
}
if (request.renoteId case final renoteId?) {
final renote = ref.read(noteProvider(account, renoteId));
if (renote == null) {
if (!ref.context.mounted) return false;
final renote = await futureWithDialog(
ref.context,
ref.read(notesNotifierProvider(account).notifier).show(renoteId),
);
if (renote == null) {
return false;
}
}
}
if (!ref.context.mounted) {
return false;
}
final result = await showDialog<bool>(
context: context,
context: ref.context,
builder: (context) => PostConfirmationDialog(
account: account,
noteId: noteId,
request: request,
files: files,
files: files ?? [],
),
);
return result ?? false;
Expand All @@ -36,26 +62,16 @@ class PostConfirmationDialog extends ConsumerWidget {
const PostConfirmationDialog({
super.key,
required this.account,
this.noteId,
this.request,
this.files,
required this.request,
required this.files,
});

final Account account;
final String? noteId;
final NotesCreateRequest? request;
final List<DriveFile>? files;
final NotesCreateRequest request;
final List<DriveFile> files;

@override
Widget build(BuildContext context, WidgetRef ref) {
final NotesCreateRequest request = this.request ??
ref.watch(postNotifierProvider(account, noteId: noteId));
final files = this.files ??
ref
.watch(attachesNotifierProvider(account, noteId: noteId))
.map((file) => file is DrivePostFile ? file.file : null)
.nonNulls
.toList();
final i = ref.watch(iNotifierProvider(account)).valueOrNull;
final channel = request.channelId != null
? ref
Expand Down
13 changes: 3 additions & 10 deletions lib/view/page/note_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:go_router/go_router.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:misskey_dart/misskey_dart.dart' hide Clip;

import '../../i18n/strings.g.dart';
import '../../model/account.dart';
import '../../model/pagination_state.dart';
import '../../model/tab_settings.dart';
import '../../provider/api/i_notifier_provider.dart';
import '../../provider/api/meta_notifier_provider.dart';
import '../../provider/api/misskey_provider.dart';
import '../../provider/api/timeline_notes_after_note_notifier_provider.dart';
import '../../provider/api/timeline_notes_notifier_provider.dart';
import '../../provider/general_settings_notifier_provider.dart';
Expand Down Expand Up @@ -140,17 +138,12 @@ class NotePage extends HookConsumerWidget {
title: Text(t.misskey.note),
actions: [
IconButton(
tooltip: t.misskey.reload,
onPressed: () async {
final note = await futureWithDialog(
await futureWithDialog(
context,
ref
.read(misskeyProvider(account))
.notes
.show(NotesShowRequest(noteId: noteId)),
ref.read(notesNotifierProvider(account).notifier).show(noteId),
);
if (note != null) {
ref.read(notesNotifierProvider(account).notifier).add(note);
}
},
icon: const Icon(Icons.refresh),
),
Expand Down
9 changes: 5 additions & 4 deletions lib/view/page/post_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class PostPage extends HookConsumerWidget {
WidgetRef ref,
Account account,
) async {
final text = ref.read(postNotifierProvider(account, noteId: noteId)).text;
final request = ref.read(postNotifierProvider(account, noteId: noteId));
final attaches =
ref.read(attachesNotifierProvider(account, noteId: noteId));
final hasFiles = attaches.isNotEmpty;
Expand All @@ -58,9 +58,10 @@ class PostPage extends HookConsumerWidget {
if (needsUpload ||
(ref.read(generalSettingsNotifierProvider).confirmBeforePost)) {
final confirmed = await confirmPost(
ref.context,
ref,
account,
noteId: noteId,
request,
files: files,
);
if (!confirmed) return;
if (!ref.context.mounted) return;
Expand All @@ -73,7 +74,7 @@ class PostPage extends HookConsumerWidget {
);
if (!ref.context.mounted) return;
if (result != null) {
if (text != null) {
if (request.text case final text?) {
final nodes = const MfmParser().parse(text);
final hashtags = nodes
.extract((node) => node is MfmHashTag)
Expand Down
10 changes: 1 addition & 9 deletions lib/view/widget/note_fallback_widget.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:misskey_dart/misskey_dart.dart';

import '../../model/account.dart';
import '../../provider/api/misskey_provider.dart';
import '../../provider/notes_notifier_provider.dart';
import 'error_message.dart';

Expand All @@ -20,13 +18,7 @@ class NoteFallbackWidget extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
return FutureBuilder(
future: Future(() async {
final note = await ref
.read(misskeyProvider(account))
.notes
.show(NotesShowRequest(noteId: noteId));
ref.read(notesNotifierProvider(account).notifier).add(note);
}),
future: ref.read(notesNotifierProvider(account).notifier).show(noteId),
builder: (context, snapshot) {
if (snapshot.hasError) {
return ErrorMessage(
Expand Down
6 changes: 5 additions & 1 deletion lib/view/widget/note_summary.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:hooks_riverpod/hooks_riverpod.dart';
import '../../i18n/strings.g.dart';
import '../../model/account.dart';
import '../../provider/note_provider.dart';
import '../../provider/notes_notifier_provider.dart';
import 'mfm.dart';

class NoteSummary extends ConsumerWidget {
Expand All @@ -22,7 +23,10 @@ class NoteSummary extends ConsumerWidget {
Widget build(BuildContext context, WidgetRef ref) {
final note = ref.watch(noteProvider(account, noteId));
if (note == null) {
return const SizedBox.shrink();
return FutureBuilder(
future: ref.read(notesNotifierProvider(account).notifier).show(noteId),
builder: (context, snapshot) => const SizedBox.shrink(),
);
}
return InkWell(
onTap: onTap,
Expand Down
5 changes: 3 additions & 2 deletions lib/view/widget/post_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@ class PostForm extends HookConsumerWidget {
(ref.read(generalSettingsNotifierProvider).confirmBeforePost)) {
final request = ref.read(postNotifierProvider(account, noteId: noteId));
final confirmed = await confirmPost(
ref.context,
ref,
account,
request: request,
request,
files: files,
);
if (!confirmed) return;
if (!ref.context.mounted) return;
Expand Down
5 changes: 2 additions & 3 deletions lib/view/widget/renote_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,9 @@ class RenoteSheet extends HookConsumerWidget {
.read(generalSettingsNotifierProvider)
.confirmBeforePost) {
final confirmed = await confirmPost(
context,
ref,
account,
request: request,
files: [],
request,
);
if (!confirmed) return;
}
Expand Down

0 comments on commit fc2175f

Please sign in to comment.