@@ -30,6 +30,7 @@ class ChatInputNotifier extends FamilyNotifier<ChatInputState, String> {
3030
3131 static final _logger = Logger ('ChatInputNotifier' );
3232 late final String _groupId;
33+ late String _accountHexPubkey;
3334 final ImagePickerService _imagePickerService;
3435 final DraftMessageService _draftMessageService;
3536 final Duration _draftSaveDelay;
@@ -44,16 +45,54 @@ class ChatInputNotifier extends FamilyNotifier<ChatInputState, String> {
4445 @override
4546 ChatInputState build (String groupId) {
4647 _groupId = groupId;
48+ final accountPubkey = ref.read (activePubkeyProvider);
49+ _accountHexPubkey = PubkeyFormatter (pubkey: accountPubkey).toHex () ?? '' ;
50+
51+ ref.listen <String ?>(activePubkeyProvider, (previous, next) {
52+ if (previous != next) {
53+ final newAccountHexPubkey = PubkeyFormatter (pubkey: next).toHex () ?? '' ;
54+ if (_accountHexPubkey != newAccountHexPubkey) {
55+ _draftSaveTimer? .cancel ();
56+ _accountHexPubkey = newAccountHexPubkey;
57+ }
58+ }
59+ });
60+
4761 ref.onDispose (() {
4862 _draftSaveTimer? .cancel ();
4963 });
5064 return const ChatInputState ();
5165 }
5266
67+ Future <void > handleAccountSwitch ({
68+ required String ? oldPubkey,
69+ required String currentText,
70+ }) async {
71+ if (oldPubkey == null || currentText.isEmpty) return ;
72+
73+ final oldAccountHexPubkey = PubkeyFormatter (pubkey: oldPubkey).toHex ();
74+ if (oldAccountHexPubkey != null && oldAccountHexPubkey.isNotEmpty) {
75+ await _draftMessageService.saveDraft (
76+ accountId: oldAccountHexPubkey,
77+ chatId: _groupId,
78+ message: currentText,
79+ );
80+ }
81+ }
82+
5383 Future <String ?> loadDraft () async {
5484 state = state.copyWith (isLoadingDraft: true );
5585 try {
56- final draft = await _draftMessageService.loadDraft (chatId: _groupId);
86+ final accountPubkey = ref.read (activePubkeyProvider);
87+ final accountHexPubkey = PubkeyFormatter (pubkey: accountPubkey).toHex () ?? '' ;
88+ if (accountHexPubkey.isEmpty) return null ;
89+
90+ _accountHexPubkey = accountHexPubkey;
91+
92+ final draft = await _draftMessageService.loadDraft (
93+ accountId: accountHexPubkey,
94+ chatId: _groupId,
95+ );
5796 return draft;
5897 } finally {
5998 state = state.copyWith (isLoadingDraft: false );
@@ -74,7 +113,17 @@ class ChatInputNotifier extends FamilyNotifier<ChatInputState, String> {
74113 }
75114
76115 Future <void > _saveDraft (String text) async {
77- await _draftMessageService.saveDraft (chatId: _groupId, message: text);
116+ final accountPubkey = ref.read (activePubkeyProvider);
117+ final accountHexPubkey = PubkeyFormatter (pubkey: accountPubkey).toHex () ?? '' ;
118+ if (accountHexPubkey.isEmpty) return ;
119+
120+ _accountHexPubkey = accountHexPubkey;
121+
122+ await _draftMessageService.saveDraft (
123+ accountId: accountHexPubkey,
124+ chatId: _groupId,
125+ message: text,
126+ );
78127 }
79128
80129 void hideMediaSelector () {
@@ -218,7 +267,17 @@ class ChatInputNotifier extends FamilyNotifier<ChatInputState, String> {
218267
219268 Future <void > clear () async {
220269 _draftSaveTimer? .cancel ();
221- await _draftMessageService.clearDraft (chatId: _groupId);
270+
271+ final accountPubkey = ref.read (activePubkeyProvider);
272+ final accountHexPubkey = PubkeyFormatter (pubkey: accountPubkey).toHex () ?? '' ;
273+ if (accountHexPubkey.isNotEmpty) {
274+ _accountHexPubkey = accountHexPubkey;
275+
276+ await _draftMessageService.clearDraft (
277+ accountId: accountHexPubkey,
278+ chatId: _groupId,
279+ );
280+ }
222281 state = state.copyWith (
223282 showMediaSelector: false ,
224283 isLoadingDraft: false ,
0 commit comments