question

EduardoGomez-1870 avatar image
0 Votes"
EduardoGomez-1870 asked DaisyTian-1203 commented

Save automatically every X seconds

I have a simple note-taking app, where the user can write notes on different notebooks.

I also have a Save Button, that store each note in Microsoft Azure Storage, and firebase, but also want to save automatically every x seconds

  private async void VM_SelectedNoteChanged(object sender, EventArgs e) {
             NoteContent.Document.Blocks.Clear();
             if (vM.SelectedNote != null) {
                 string downloadPath = $"{vM.SelectedNote.Id}.rtf";
                 if (!string.IsNullOrEmpty(vM.SelectedNote.FileLocation)) {
                     await new BlobClient
                         (new Uri(vM.SelectedNote.FileLocation)).DownloadToAsync(downloadPath);
                     using (FileStream fs = new(downloadPath, FileMode.Open)) {
                         var contents = new TextRange(NoteContent.Document.ContentStart,
                         NoteContent.Document.ContentEnd);
                         contents.Load(fs, DataFormats.Rtf);
                     }
                 }
             }
         }
    
         private async void SpeechRecognizer_Recognized(object sender, SpeechRecognitionEventArgs e) {
             if (e.Result.Reason == ResultReason.RecognizedSpeech) {
                 await Dispatcher.InvokeAsync(() => NoteContent.AppendText($"{e.Result.Text}"));
    
             }
         }
    
         protected override void OnActivated(EventArgs e) {
             base.OnActivated(e);
    
             if (string.IsNullOrEmpty(App.UserId)) {
                 LoginWindow loginWindow = new();
                 loginWindow.ShowDialog();
                 vM.GetNoteBooksAsync();
             }
         }
         private void NoteContent_TextChanged(object sender, TextChangedEventArgs e) {
             FlowDocument doc = NoteContent.Document;
             int count = new TextRange(doc.ContentStart, doc.ContentEnd).Text.Length;
             NumOfCharacters.Text = $"Characters: {count}";
         }
         private async void SpeechBton_Click(object sender, RoutedEventArgs e) {
    
             var toggle = sender as System.Windows.Controls.Primitives.ToggleButton;
             switch (toggle.Tag) {
                 case "0":
                     if (!isRecognizing) {
                         await recog.StartContinuousRecognitionAsync();
    
                         isRecognizing = true;
                         SpeechBton.IsChecked = true;
                     } else {
                         await recog.StopContinuousRecognitionAsync();
    
                         isRecognizing = false;
                         SpeechBton.IsChecked = false;
                     }
                     break;
                 case "1":
                     bool isChecked = toggle.IsChecked ?? false;
                     if (isChecked) {
                         NoteContent.Selection.ApplyPropertyValue(FontWeightProperty, FontWeights.Bold);
                     } else {
                         NoteContent.Selection.ApplyPropertyValue(FontWeightProperty, FontWeights.Normal);
                     }
                     break;
                 case "2":
                     bool isEnable = toggle.IsChecked ?? false;
                     if (isEnable) {
                         NoteContent.Selection.ApplyPropertyValue(FontStyleProperty, FontStyles.Italic);
                     } else {
                         NoteContent.Selection.ApplyPropertyValue(FontStyleProperty, FontStyles.Normal);
                     }
                     break;
                 case "3":
                     bool isPressed = toggle.IsChecked ?? false;
                     if (isPressed) {
                         NoteContent.Selection.ApplyPropertyValue(Inline.TextDecorationsProperty, TextDecorations.Underline);
                     } else {
    
                         (NoteContent.Selection.GetPropertyValue(Inline.TextDecorationsProperty) as TextDecorationCollection)
                             .TryRemove(TextDecorations.Underline, out TextDecorationCollection decorations);
                         NoteContent.Selection.ApplyPropertyValue(Inline.TextDecorationsProperty, decorations);
                     }
                     break;
             }
         }
         private void NoteContent_SelectionChanged(object sender, RoutedEventArgs e) {
    
             var selectionFontWeight = NoteContent.Selection.GetPropertyValue(TextElement.FontWeightProperty);
             boldBton.IsChecked = (selectionFontWeight != DependencyProperty.UnsetValue)
                 && selectionFontWeight.Equals(FontWeights.Bold);
    
             var selectionFontStyle = NoteContent.Selection.GetPropertyValue(TextElement.FontStyleProperty);
             italicBton.IsChecked = (selectionFontStyle != DependencyProperty.UnsetValue)
                 && selectionFontStyle.Equals(FontStyles.Italic);
    
             var selectionFontDecoration = NoteContent.Selection.GetPropertyValue(Inline.TextDecorationsProperty);
             UndelineBton.IsChecked = (selectionFontDecoration != DependencyProperty.UnsetValue)
                 && selectionFontDecoration.Equals(TextDecorations.Underline);
    
             FontsComboBox.SelectedItem = NoteContent.Selection.GetPropertyValue(FontFamilyProperty);
             SizeConboBox.Text = (NoteContent.Selection.GetPropertyValue(FontSizeProperty)).ToString();
         }
    
         private void FontsComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {
             if (FontsComboBox.SelectedItem != null) {
                 NoteContent.Selection.ApplyPropertyValue(FontFamilyProperty, FontsComboBox.SelectedItem);
             }
         }
    
         private void SizeConboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {
             NoteContent.Selection.ApplyPropertyValue(FontSizeProperty, SizeConboBox.Text);
         }
         private async void SaveBton_Click(object sender, RoutedEventArgs e) {
    
             var fileName = $"{vM.SelectedNote.Id}";
             var rtfFile = Path.Combine(Environment.CurrentDirectory, fileName);
             vM.SelectedNote.FileLocation = rtfFile;
    
             using (FileStream fs = new(rtfFile, FileMode.Create)) {
                 var contents = new TextRange(NoteContent.Document.ContentStart,
                 NoteContent.Document.ContentEnd);
                 contents.Save(fs, DataFormats.Rtf);
             }
    
             vM.SelectedNote.FileLocation = await UpdateFileAsync(rtfFile, fileName);
             await Database.UpdateAsync(vM.SelectedNote);
         }
    
         private static async Task<string> UpdateFileAsync(string rtfFile, string fileName) {
    
             var connectionString = App.azureStorageKey;
             var containerName = "notes";
    
             var containerClient = new BlobContainerClient(connectionString, containerName);
    
             var blob = containerClient.GetBlobClient(fileName);
             await blob.UploadAsync(rtfFile, overwrite: true);
    
             return $"https://safestoragewpf.blob.core.windows.net/notes/{fileName}";
         }
     }
 }
windows-wpf
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@EduardoGomez-1870
Is DispatcherTimer what you want? You could you it like below:

 DispatcherTimer timer1 = new DispatcherTimer();
 timer1.Interval = new TimeSpan(1000);  //Put your TimeSpan
 timer1.Tick += new EventHandler(timer1_Tick);
 timer1.Start();

If I misunderstand your quesion, please point out.



0 Votes 0 ·

Yes I try that., but the question is where do I put it? I want the user, to be able to save the note automatically

For example, the app start, the user logs in or create a new account.

When the user logs in, he/she will be presented with a screen, where there is a ribbon, that lets you create a new Notebook.

When the user creates a new Notebook, he need to create a note, in order for him to start writing in the RichTextBox.

I have several buttons, to Bold, Italic ,Save, etc., the user can save the the note by clicking on the content, but I also want to save the note an x in an x amount of time,

here is my code

https://github.com/eduardoagr/Safe

email: admi*.com
password: q
*y

0 Votes 0 ·

@EduardoGomez-1870
You could add a Loaded event and put timer in it for NotesWindow.xaml.cs. This will start the timer to save every time the page loads.

0 Votes 0 ·
Show more comments

0 Answers