@@ -47,7 +47,6 @@ public sealed partial class Conversation : UserControl, INotifyPropertyChanged
47
47
public VirtualizedCollection Collection ;
48
48
private CoreWindowActivationState ActivationState = CoreWindowActivationState . Deactivated ;
49
49
private int LastMarkReadRequest ;
50
- private string SelectedFileToken ;
51
50
private StorageFile SelectedFile ;
52
51
53
52
private string _ThreadDisplayName ;
@@ -219,10 +218,6 @@ public void Load(SignalConversation conversation)
219
218
{
220
219
LoadDraft ( ) ;
221
220
}
222
- else
223
- { // Set the current draft in the new conversation object.
224
- SaveDraftInCurrentConversation ( ) ;
225
- }
226
221
UserInputBar . FocusTextBox ( ) ;
227
222
DisposeCurrentThread ( ) ;
228
223
UpdateHeader ( conversation ) ;
@@ -357,7 +352,7 @@ private async void UserInputBar_OnSendMessageButtonClicked()
357
352
var filePicker = new FileOpenPicker ( ) ;
358
353
filePicker . FileTypeFilter . Add ( "*" ) ; // Without this the file picker throws an exception, this is not documented
359
354
var file = await filePicker . PickSingleFileAsync ( ) ;
360
- SetSelectedFile ( file , true ) ;
355
+ SetSelectedFile ( file ) ;
361
356
if ( SelectedFile != null )
362
357
{
363
358
UserInputBar . FocusTextBox ( ) ;
@@ -484,7 +479,7 @@ await Task.Run(() =>
484
479
485
480
private void AddedAttachmentDisplay_OnCancelAttachmentButtonClicked ( )
486
481
{
487
- SetSelectedFile ( null , true ) ;
482
+ SetSelectedFile ( null ) ;
488
483
}
489
484
490
485
private void UpdateSendButtonIcon ( )
@@ -501,35 +496,64 @@ private void UpdateSendButtonIcon()
501
496
502
497
private void ResetInput ( )
503
498
{
504
- SetSelectedFile ( null , true ) ;
499
+ SetSelectedFile ( null ) ;
505
500
UserInputBar . InputText = string . Empty ;
506
501
UpdateSendButtonIcon ( ) ;
507
- SaveDraftInCurrentConversation ( ) ;
508
502
}
509
503
510
- public void SaveDraftInCurrentConversation ( )
504
+ /// <summary>
505
+ /// Saves the selected file in StorageApplicationPermissions store and saves conversation in database.
506
+ /// </summary>
507
+ /// <returns>Task that saves conversation in database.</returns>
508
+ public async Task SaveCurrentConversationInDatabase ( )
511
509
{
512
510
if ( SignalConversation != null )
513
511
{
514
512
SignalConversation . Draft = UserInputBar . InputText ;
515
- SignalConversation . DraftFileTokens = SelectedFileToken ;
513
+ if ( SelectedFile == null )
514
+ {
515
+ if ( ! string . IsNullOrEmpty ( SignalConversation . DraftFileTokens ) &&
516
+ StorageApplicationPermissions . FutureAccessList . ContainsItem ( SignalConversation . DraftFileTokens ) )
517
+ {
518
+ StorageApplicationPermissions . FutureAccessList . Remove ( SignalConversation . DraftFileTokens ) ;
519
+ }
520
+ SignalConversation . DraftFileTokens = null ;
521
+ }
522
+ else
523
+ {
524
+ if ( string . IsNullOrEmpty ( SignalConversation . DraftFileTokens ) ||
525
+ ! StorageApplicationPermissions . FutureAccessList . ContainsItem ( SignalConversation . DraftFileTokens ) )
526
+ {
527
+ SignalConversation . DraftFileTokens = StorageApplicationPermissions . FutureAccessList . Add ( SelectedFile ) ;
528
+ }
529
+ else
530
+ {
531
+ // Just reuse the old key
532
+ StorageApplicationPermissions . FutureAccessList . AddOrReplace ( SignalConversation . DraftFileTokens , SelectedFile ) ;
533
+ }
534
+ }
535
+ // SignalConversation can change while starting Thread.
536
+ SignalConversation conversationToSave = SignalConversation ;
537
+ await Task . Run ( ( ) =>
538
+ {
539
+ SignalDBContext . InsertOrUpdateConversationLocked ( conversationToSave ) ;
540
+ } ) ;
516
541
}
517
542
}
518
543
519
544
private async Task LoadDraft ( )
520
545
{
521
546
UserInputBar . InputText = SignalConversation . Draft ?? string . Empty ;
522
547
UserInputBar . SetCursorPositionToEnd ( ) ;
523
- SetSelectedFile ( null , false ) ;
548
+ SetSelectedFile ( null ) ;
524
549
try
525
550
{
526
551
StorageFile file = ! string . IsNullOrWhiteSpace ( SignalConversation . DraftFileTokens ) &&
527
552
StorageApplicationPermissions . FutureAccessList . ContainsItem ( SignalConversation . DraftFileTokens ) ?
528
553
await StorageApplicationPermissions . FutureAccessList . GetFileAsync ( SignalConversation . DraftFileTokens ) : null ;
529
554
if ( file != null )
530
555
{
531
- SelectedFileToken = SignalConversation . DraftFileTokens ;
532
- SetSelectedFile ( file , false ) ;
556
+ SetSelectedFile ( file ) ;
533
557
}
534
558
}
535
559
catch ( Exception e )
@@ -539,62 +563,21 @@ private async Task LoadDraft()
539
563
}
540
564
541
565
/// <summary>
542
- /// Set file as attatchment in UI and save it in StorageApplicationPermissions store and save it as draft in database.
543
- /// StorageApplicationPermissions stores the file to access it even after restart of app.
566
+ /// Set file as attatchment in UI.
544
567
/// </summary>
545
568
/// <param name="file">Attatchment to set in UI and save</param>
546
- /// <param name="save">If true save the file in StorageApplicationPermissions store and in database. If false only set in UI</param>
547
- private void SetSelectedFile ( StorageFile file , bool save )
569
+ private void SetSelectedFile ( StorageFile file )
548
570
{
549
- try
550
- {
551
- SelectedFile = file ;
552
- if ( save )
553
- {
554
- if ( file == null )
555
- {
556
- if ( ! string . IsNullOrEmpty ( SelectedFileToken ) )
557
- {
558
- StorageApplicationPermissions . FutureAccessList . Remove ( SelectedFileToken ) ;
559
- }
560
- SelectedFileToken = null ;
561
- }
562
- else
563
- {
564
- if ( string . IsNullOrEmpty ( SelectedFileToken ) )
565
- {
566
- SelectedFileToken = StorageApplicationPermissions . FutureAccessList . Add ( file ) ;
567
- }
568
- else
569
- {
570
- // Just reuse the old key
571
- StorageApplicationPermissions . FutureAccessList . AddOrReplace ( SelectedFileToken , file ) ;
572
- }
573
- }
574
- // Save in Datebase as soon as possible to avoid loosing token
575
- SaveDraftInCurrentConversation ( ) ;
576
- Task . Run ( ( ) =>
577
- {
578
- SignalDBContext . InsertOrUpdateConversationLocked ( SignalConversation ) ;
579
- } ) ;
580
- }
581
- }
582
- catch ( Exception e )
571
+ SelectedFile = file ;
572
+ if ( SelectedFile == null )
583
573
{
584
- Logger . LogError ( "SetSelectedFile() failed: {0} \n {1}" , e . Message , e . StackTrace ) ;
574
+ AddedAttachmentDisplay . HideAttachment ( ) ;
585
575
}
586
- finally
576
+ else
587
577
{
588
- if ( SelectedFile == null )
589
- {
590
- AddedAttachmentDisplay . HideAttachment ( ) ;
591
- }
592
- else
593
- {
594
- AddedAttachmentDisplay . ShowAttachment ( SelectedFile . Name ) ;
595
- }
596
- UpdateSendButtonIcon ( ) ;
578
+ AddedAttachmentDisplay . ShowAttachment ( SelectedFile . Name ) ;
597
579
}
580
+ UpdateSendButtonIcon ( ) ;
598
581
}
599
582
600
583
private async void Grid_KeyDown ( object sender , KeyRoutedEventArgs e )
@@ -609,7 +592,7 @@ private async void Grid_KeyDown(object sender, KeyRoutedEventArgs e)
609
592
{
610
593
var pastedFiles = await dataPackageView . GetStorageItemsAsync ( ) ;
611
594
var pastedFile = pastedFiles [ 0 ] ;
612
- SetSelectedFile ( pastedFile as StorageFile , true ) ;
595
+ SetSelectedFile ( pastedFile as StorageFile ) ;
613
596
}
614
597
else if ( dataPackageView . Contains ( StandardDataFormats . Bitmap ) )
615
598
{
@@ -629,7 +612,7 @@ private async void Grid_KeyDown(object sender, KeyRoutedEventArgs e)
629
612
pixels . DetachPixelData ( ) ) ;
630
613
await encoder . FlushAsync ( ) ;
631
614
}
632
- SetSelectedFile ( tmpFile , true ) ;
615
+ SetSelectedFile ( tmpFile ) ;
633
616
}
634
617
}
635
618
}
@@ -648,7 +631,7 @@ private async void Grid_Drop(object sender, DragEventArgs e)
648
631
{
649
632
var storageItems = await e . DataView . GetStorageItemsAsync ( ) ;
650
633
var storageItem = storageItems [ 0 ] ;
651
- SetSelectedFile ( storageItem as StorageFile , true ) ;
634
+ SetSelectedFile ( storageItem as StorageFile ) ;
652
635
}
653
636
}
654
637
0 commit comments