Skip to content

Commit

Permalink
fix edit/reply draft state bug
Browse files Browse the repository at this point in the history
  • Loading branch information
gtalha07 committed Aug 20, 2024
1 parent 3ad62c6 commit b3a339e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 31 deletions.
4 changes: 2 additions & 2 deletions app/lib/features/chat/providers/chat_providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final autoDownloadMediaProvider =

// keep track of text controller values across rooms.
final chatInputProvider =
StateNotifierProvider<ChatInputNotifier, ChatInputState>(
StateNotifierProvider.autoDispose<ChatInputNotifier, ChatInputState>(
(ref) => ChatInputNotifier(),
);

Expand Down Expand Up @@ -137,7 +137,7 @@ final chatMessagesProvider =
return [...messages, ...moreMessages];
});

final isAuthorOfSelectedMessage = StateProvider<bool>((ref) {
final isAuthorOfSelectedMessage = StateProvider.autoDispose<bool>((ref) {
final chatInputState = ref.watch(chatInputProvider);
final myUserId = ref.watch(myUserIdStrProvider);
return chatInputState.selectedMessage?.author.id == myUserId;
Expand Down
49 changes: 20 additions & 29 deletions app/lib/features/chat/widgets/custom_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import 'package:skeletonizer/skeletonizer.dart';

final _log = Logger('a3::chat::custom_input');

final _allowEdit = StateProvider.family<bool, String>(
final _allowEdit = StateProvider.family.autoDispose<bool, String>(
(ref, roomId) => ref.watch(
chatInputProvider
.select((state) => state.sendingState == SendingState.preparing),
Expand Down Expand Up @@ -208,16 +208,6 @@ class __ChatInputState extends ConsumerState<_ChatInput> {
});
}

// @override
// void didUpdateWidget(covariant _ChatInput oldWidget) {
// super.didUpdateWidget(oldWidget);
// // useful lifecycle method for desktop UI where room selection is done in side-view.
// // re-builds when roomId update occurs and loads the room composer draft state.
// if (widget.roomId != oldWidget.roomId) {
// loadDraft();
// }
// }

@override
void dispose() {
textController.dispose();
Expand Down Expand Up @@ -251,7 +241,6 @@ class __ChatInputState extends ConsumerState<_ChatInput> {
final eventId = draft.eventId()!;
final draftType = draft.draftType();
final inputNotifier = ref.read(chatInputProvider.notifier);
inputNotifier.unsetSelectedMessage();
final m = ref
.read(chatMessagesProvider(widget.roomId))
.firstWhere((x) => x.id == eventId);
Expand All @@ -261,7 +250,7 @@ class __ChatInputState extends ConsumerState<_ChatInput> {
inputNotifier.setReplyToMessage(m);
}
}
textController.text = draft.htmlText() ?? draft.plainText();
textController.text = draft.plainText();
_log.info('compose draft loaded for room: ${widget.roomId}');
}
}
Expand Down Expand Up @@ -607,7 +596,7 @@ class __ChatInputState extends ConsumerState<_ChatInput> {
),
const Spacer(),
GestureDetector(
onTap: () {
onTap: () async {
inputNotifier.unsetSelectedMessage();
// frame delay to keep focus connected with keyboard.
WidgetsBinding.instance.addPostFrameCallback((_) {
Expand Down Expand Up @@ -641,7 +630,9 @@ class __ChatInputState extends ConsumerState<_ChatInput> {
),
const Spacer(),
GestureDetector(
onTap: () {
onTap: () async {
final convo = await ref.read(chatProvider(widget.roomId).future);
await convo?.saveMsgDraft('', null, 'new', null);
textController.clear();
inputNotifier.unsetSelectedMessage();
// frame delay to keep focus connected with keyboard..
Expand Down Expand Up @@ -742,7 +733,7 @@ class _TextInputWidgetConsumerState extends ConsumerState<_TextInputWidget> {
@override
void initState() {
super.initState();
ref.listenManual(chatInputProvider, (prev, next) {
ref.listenManual(chatInputProvider, (prev, next) async {
if (next.selectedMessageState == SelectedMessageState.edit &&
(prev?.selectedMessageState != next.selectedMessageState ||
next.selectedMessage != prev?.selectedMessage)) {
Expand All @@ -751,21 +742,21 @@ class _TextInputWidgetConsumerState extends ConsumerState<_TextInputWidget> {
if (next.selectedMessage != null) {
widget.controller.text = parseEditMsg(next.selectedMessage!);
// save edit UI state also by pointing reference event id.
saveDraft(widget.controller.text, next.selectedMessage!.id);
await saveDraft(widget.controller.text, next.selectedMessage!.id);
// frame delay to keep focus connected with keyboard.
WidgetsBinding.instance.addPostFrameCallback((_) {
widget.chatFocus.requestFocus();
});
// WidgetsBinding.instance.addPostFrameCallback((_) {
widget.chatFocus.requestFocus();
// });
}
} else if (next.selectedMessageState == SelectedMessageState.replyTo &&
(next.selectedMessage != prev?.selectedMessage ||
prev?.selectedMessageState != next.selectedMessageState)) {
// save reply UI state also by pointing reference event id.
saveDraft(widget.controller.text, next.selectedMessage!.id);
await saveDraft(widget.controller.text, next.selectedMessage!.id);
// frame delay to keep focus connected with keyboard..
WidgetsBinding.instance.addPostFrameCallback((_) {
widget.chatFocus.requestFocus();
});
// WidgetsBinding.instance.addPostFrameCallback((_) {
widget.chatFocus.requestFocus();
// });
}
});
}
Expand Down Expand Up @@ -819,14 +810,14 @@ class _TextInputWidgetConsumerState extends ConsumerState<_TextInputWidget> {
final selectedMessageState =
ref.read(chatInputProvider).selectedMessageState;
if (selectedMessageState == SelectedMessageState.edit) {
/// FIXME: how html can be passed down here?
res = await chat.saveMsgDraft(text, text, 'edit', eventId);
res = await chat.saveMsgDraft(text, null, 'edit', eventId);
} else if (selectedMessageState == SelectedMessageState.replyTo) {
/// FIXME: how html can be passed down here?
res = await chat.saveMsgDraft(text, text, 'reply', eventId);
res = await chat.saveMsgDraft(text, null, 'reply', eventId);
}
} else {
res = await chat.saveMsgDraft(text, null, 'new', null);
}
res = await chat.saveMsgDraft(text, text, 'new', null);

_log.info('compose message state stored for room? $res|${widget.roomId}');
}
}
Expand Down

0 comments on commit b3a339e

Please sign in to comment.