Hello Microsoft
I have a problem, that I do not know how to solve I have debugged my app and I cannot get into the root of the problem
I am using firebase to develop a note-taking application, and also I am using Azure Storage
The problem is, as you can see, that I can see the notes of previous notebooks


and here is my database
I also have a problem updating my file, because Azure Cloud has a file already with that name
I will post the code that I think is the problem
This is my code behind for my rich text bok
private async void VM_SelectedNoteChanged(object sender, EventArgs e) {
NoteContent.Document.Blocks.Clear();
if (vM.SelectedNote != null) {
string downloadPath = $"{vM.SelectedNote.Id}.rtf";
if (vM.SelectedNote.FileLocation != null) {
await new BlobClient(new Uri(vM.SelectedNote.FileLocation)).DownloadToAsync(downloadPath);
}
if (!string.IsNullOrEmpty(vM.SelectedNote.FileLocation)) {
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 async Task<string> UpdateFileAsync(string rtfFile, string fileName) {
var connectionString = azureStorageKey;
var containerName = "notes";
var containerClient = new BlobContainerClient(connectionString, containerName);
var blob = containerClient.GetBlobClient(fileName);
await blob.UploadAsync(rtfFile);
return $"https://safestoragewpf.blob.core.windows.net/notes/{fileName}";
}
}
this is my ViewModel.
private Notebook _SelectedNoteBook;
public Notebook SelectedNoteBook {
get { return _SelectedNoteBook; }
set {
if (_SelectedNoteBook != value) {
_SelectedNoteBook = value;
RaisePropertyChanged();
GetNotes();
}
}
}
private bool _IsEnabled;
public bool IsEnabled {
get { return _IsEnabled; }
set {
if (_IsEnabled != value) {
_IsEnabled = value;
RaisePropertyChanged();
}
}
}
private Note _SelectedNote;
public Note SelectedNote {
get { return _SelectedNote; }
set {
if (_SelectedNote != value) {
_SelectedNote = value;
RaisePropertyChanged();
SelectedNoteChanged.Invoke(this, new EventArgs());
if (SelectedNote != null) {
IsEnabled = true;
} else {
IsEnabled = false;
}
}
}
}
private Visibility _IsVisible;
public Visibility IsVisible {
get { return _IsVisible; }
set {
if (_IsVisible != value) {
_IsVisible = value;
RaisePropertyChanged();
}
}
}
public NotesWindowVM() {
IsEnabled = false;
IsVisible = Visibility.Collapsed;
Notes = new ObservableCollection<Note>();
Notebooks = new ObservableCollection<Notebook>();
Fonts = System.Windows.Media.Fonts.SystemFontFamilies.OrderBy(f => f.Source);
FontSizes = new List<int>();
for (int i = 7; i < 72; i++) {
FontSizes.Add(i);
}
ExitCommand = new Command(() => {
Application.Current.Shutdown();
});
NewNotebookCommand = new Command(() => {
CreateNewNotebook();
});
RenameCommand = new Command(() => {
IsVisible = Visibility.Visible;
});
NewNoteCommand = new HelperCommand {
ExecuteDelegate = x => CreateNewNote(SelectedNoteBook.Id),
CanExecuteDelegate = x => SelectedNoteBook != null
};
CloseWidowCommand = new Command<ICloseable>(CloseWindow);
RenameNotebookCompleteCommand = new HelperCommand {
ExecuteDelegate = x => EditionCompltedNotebook(SelectedNoteBook),
CanExecuteDelegate = x => true
};
RenameNoteCompleteCommand = new HelperCommand {
ExecuteDelegate = x => EditionCompltedNote(SelectedNote),
CanExecuteDelegate = x => true
};
GetNoteBooksAsync();
}
private void CloseWindow(ICloseable obj) {
if (obj != null) {
LoginWindow loginWindow = new();
loginWindow.Show();
obj.Close();
}
}
private async void EditionCompltedNote(Note selectedNote) {
if (selectedNote != null) {
IsVisible = Visibility.Collapsed;
await Database.UpdateAsync(selectedNote);
GetNoteBooksAsync();
}
}
private async void EditionCompltedNotebook(Notebook notebook) {
if (notebook != null) {
IsVisible = Visibility.Collapsed;
await Database.UpdateAsync(notebook);
GetNotes();
}
}
private async void CreateNewNote(string NotebookId) {
Note note = new() {
NotebookId = NotebookId,
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now,
Title = "New Note"
};
await Database.InsertAsync(note);
GetNotes();
}
private async void CreateNewNotebook() {
Notebook notebook = new() {Name = $"Notebook", UserId = App.UserId };
await Database.InsertAsync(notebook);
GetNoteBooksAsync();
}
public async void GetNoteBooksAsync() {
var notebooks = await Database.ReadAsync<Notebook>();
if (notebooks != null) {
notebooks.Where(n => n.Id == App.UserId);
Notebooks.Clear();
foreach (var item in notebooks) {
Notebooks.Add(item);
}
}
}
private async void GetNotes() {
if (SelectedNoteBook != null) {
var notes = await Database.ReadAsync<Note>();
if (notes != null) {
notes.Where(n => n.NotebookId == SelectedNote.Id);
Notes.Clear();
foreach (var item in notes) {
Notes.Add(item);
}
}
}
}
public event EventHandler SelectedNoteChanged;
}
}
and my DB file
private static readonly string FirebadeDb = "https://safe-wpf-default-rtdb.europe-west1.firebasedatabase.app/";
public static async Task<bool> InsertAsync<T>(T Item) {
string jsonBody = JsonConvert.SerializeObject(Item);
var content = new StringContent(jsonBody, Encoding.UTF8, "application/json");
using (HttpClient client = new()) {
var res = await client.PostAsync($"{FirebadeDb}{Item.GetType().Name.ToLower()}.json", content);
if (res.IsSuccessStatusCode) {
return true;
} else {
return false;
}
}
}
public static async Task<List<T>> ReadAsync<T>() where T : HasId {
using (HttpClient client = new()) {
var res = await client.GetAsync($"{FirebadeDb}{typeof(T).Name.ToLower()}.json");
var JsonRes = await res.Content.ReadAsStringAsync();
if (res.IsSuccessStatusCode) {
var valuePairs = JsonConvert.DeserializeObject<Dictionary<string, T>>(JsonRes);
if (valuePairs != null) {
List<T> objts = new();
foreach (var item in valuePairs) {
item.Value.Id = item.Key;
objts.Add(item.Value);
}
return objts;
}
} else {
return null;
}
return null;
}
}
public static async Task<bool> UpdateAsync<T>(T Item) where T : HasId {
string jsonBody = JsonConvert.SerializeObject(Item);
var content = new StringContent(jsonBody, Encoding.UTF8, "application/json");
using (HttpClient client = new()) {
var res = await client.PatchAsync($"{FirebadeDb}{Item.GetType().Name.ToLower()}/{Item.Id}.json", content);
if (res.IsSuccessStatusCode) {
return true;
} else {
return false;
}
}
}
public static async Task<bool> DeleteAsync<T>(T Item) where T : HasId {
using (HttpClient client = new()) {
var res = await client.DeleteAsync($"{FirebadeDb}{Item.GetType().Name.ToLower()}/{Item.Id}.json");
if (res.IsSuccessStatusCode) {
return true;
} else {
return false;
}
}
}
}
All the code is on github.com
https://github.com/eduardoagr/Safe
I tried to clear all the notebooks and notes, but that did not work, because it will add again