Skip to content
This repository has been archived by the owner on May 2, 2022. It is now read-only.

Commit

Permalink
[Somewhere Desktop] FIx an issue when editing and focus is still on N…
Browse files Browse the repository at this point in the history
…T (Notebook Tab) tags textbox nd use shortcut to create a new note, the tags are not saved

- Fix an issue when closing the window there is an exception with disposed Commands object when active item/note saving is executed after **Windows Closing** event
  • Loading branch information
chaojian-zhang committed Aug 26, 2019
1 parent 78c7069 commit e7ad2a2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
7 changes: 4 additions & 3 deletions SomewhereDesktop/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@
</DockPanel>
<!-- Tabs -->
<Grid>
<!-- Items Tab -->
<!-- Inventory Tab -->
<DockPanel x:Name="InventoryPanel" Visibility="Visible">
<!-- Items List Region -->
<Border Style="{StaticResource Panel}" MaxWidth="800">
<Border Style="{StaticResource Panel}" MaxWidth="700">
<DockPanel LastChildFill="True">
<!-- Name Serach -->
<DockPanel DockPanel.Dock="Top" LastChildFill="True">
Expand All @@ -142,6 +142,7 @@
<Button Content="Reverse" Click="ItemSortingReverseButton_Click" Width="100" DockPanel.Dock="Right"/>
<ComboBox ItemsSource="{Binding ItemsSortingOptions}" SelectedItem="{Binding SelectedItemSorting}"/>
</DockPanel>
<!-- Items list view -->
<ListView ItemsSource="{Binding Items}" SelectionMode="Single"
SelectedItem="{Binding ActiveItem}" MouseDoubleClick="ItemsList_DoubleClick">
<ListView.ItemTemplate>
Expand Down Expand Up @@ -276,7 +277,7 @@
</DockPanel>
</Border>
<!-- Notes List -->
<Border Style="{StaticResource Panel}">
<Border Style="{StaticResource Panel}" MaxWidth="700">
<DockPanel>
<Button DockPanel.Dock="Bottom" Content="Create Note" Command="local:CustomCommands.Create"
DataContext="{x:Static local:CustomCommands.Create}" ToolTip="{Binding Text}"/>
Expand Down
52 changes: 31 additions & 21 deletions SomewhereDesktop/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ public FileItemObjectModel ActiveItem
public string ActiveItemRemarkMeta
{
get => (ActiveItem != null && ActiveItem.Meta != null) ? Commands.ExtractRemarkMeta(ActiveItem.Meta).Remark : null;
set { ActiveItem.Meta = Commands.ReplaceOrInitializeMetaAttribute(ActiveItem.Meta, "Remark", value); NotifyPropertyChanged(); CommitActiveItemChange(); ActiveItem.BroadcastPropertyChange(); }
set { ActiveItem.Meta = Commands.ReplaceOrInitializeMetaAttribute(ActiveItem.Meta, "Remark", value); NotifyPropertyChanged(); TryCommitActiveItemChange(); ActiveItem.BroadcastPropertyChange(); }
}
public string ActiveItemEntryDate
{
Expand All @@ -418,12 +418,12 @@ public int ActiveItemID
public string ActiveItemName
{
get => ActiveItem?.Name;
set { ActiveItem.Name = value; NotifyPropertyChanged(); CommitActiveItemChange(); ActiveItem.BroadcastPropertyChange(); RefreshTypeFilters(); if(ActiveItem == ActiveNote) NotifyPropertyChanged("ActiveNoteName"); }
set { ActiveItem.Name = value; NotifyPropertyChanged(); TryCommitActiveItemChange(); ActiveItem.BroadcastPropertyChange(); RefreshTypeFilters(); if(ActiveItem == ActiveNote) NotifyPropertyChanged("ActiveNoteName"); }
}
public string ActiveItemTags
{
get => ActiveItem?.Tags;
set { ActiveItem.Tags = value; NotifyPropertyChanged(); CommitActiveItemChange(); ActiveItem.BroadcastPropertyChange(); RefreshTags(); FilterItems(); if(ActiveItem == ActiveNote) NotifyPropertyChanged("ActiveNoteTags"); }
set { ActiveItem.Tags = value; NotifyPropertyChanged(); TryCommitActiveItemChange(); ActiveItem.BroadcastPropertyChange(); RefreshTags(); FilterItems(); if(ActiveItem == ActiveNote) NotifyPropertyChanged("ActiveNoteTags"); }
}
#endregion

Expand Down Expand Up @@ -473,17 +473,17 @@ public int ActiveNoteID
public string ActiveNoteName
{
get => ActiveNote?.Name;
set { ActiveNote.Name = value; NotifyPropertyChanged(); CommitActiveNoteChange(); ActiveNote.BroadcastPropertyChange(); RefreshTypeFilters(); if(ActiveNote == ActiveItem) NotifyPropertyChanged("ActiveItemName"); }
set { ActiveNote.Name = value; NotifyPropertyChanged(); TryCommitActiveNoteChange(); ActiveNote.BroadcastPropertyChange(); RefreshTypeFilters(); if(ActiveNote == ActiveItem) NotifyPropertyChanged("ActiveItemName"); }
}
public string ActiveNoteTags
{
get => ActiveNote?.Tags;
set { ActiveNote.Tags = value; NotifyPropertyChanged(); CommitActiveNoteChange(); ActiveNote.BroadcastPropertyChange(); RefreshTags(); FilterItems(); if(ActiveNote == ActiveItem) NotifyPropertyChanged("ActiveItemTags"); }
set { ActiveNote.Tags = value; NotifyPropertyChanged(); TryCommitActiveNoteChange(); ActiveNote.BroadcastPropertyChange(); RefreshTags(); FilterItems(); if(ActiveNote == ActiveItem) NotifyPropertyChanged("ActiveItemTags"); }
}
public string ActiveNoteContent
{
get => ActiveNote?.Content;
set { ActiveNote.Content = value; NotifyPropertyChanged(); CommitActiveNoteChange(); ActiveNote.BroadcastPropertyChange(); if (ActiveNote == ActiveItem) UpdateItemPreview(); }
set { ActiveNote.Content = value; NotifyPropertyChanged(); TryCommitActiveNoteChange(); ActiveNote.BroadcastPropertyChange(); if (ActiveNote == ActiveItem) UpdateItemPreview(); }
}
public bool IsNoteFieldEditEnabled
=> ActiveNote != null;
Expand Down Expand Up @@ -544,8 +544,8 @@ private void CloseWindowCommand_CanExecute(object sender, CanExecuteRoutedEventA
private void CloseWindowCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
// In case things are not saved, commit change for safety and avoid data loss
CommitActiveItemChange();
CommitActiveNoteChange();
TryCommitActiveItemChange();
TryCommitActiveNoteChange();
this.Close();
}
private void ShowShortcutsCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
Expand All @@ -570,8 +570,8 @@ private void MaximizeWindowCommand_CanExecute(object sender, CanExecuteRoutedEve
private void MaximizeWindowCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
// Safety
CommitActiveItemChange();
CommitActiveNoteChange();
TryCommitActiveItemChange();
TryCommitActiveNoteChange();

if (this.WindowState == WindowState.Normal)
this.WindowState = WindowState.Maximized;
Expand All @@ -582,8 +582,8 @@ private void HideWindowCommand_CanExecute(object sender, CanExecuteRoutedEventAr
private void HideWindowCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
// Safety
CommitActiveItemChange();
CommitActiveNoteChange();
TryCommitActiveItemChange();
TryCommitActiveNoteChange();
this.WindowState = WindowState.Minimized;
}
private void RefreshCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
Expand All @@ -599,8 +599,8 @@ private void SaveCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
=> e.CanExecute = ActiveItem != null || ActiveNote != null;
private void SaveCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
CommitActiveItemChange();
CommitActiveNoteChange();
TryCommitActiveItemChange();
TryCommitActiveNoteChange();
InfoText = "Item saved.";
}
private void GotoActiveItemEditContentCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
Expand All @@ -616,6 +616,10 @@ private void CreateNoteCommand_CanExecute(object sender, CanExecuteRoutedEventAr
=> e.CanExecute = true;
private void CreateNoteCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
// Save old note by changing focus
NotenameTextBox.Focus();
NoteContentTextBox.Focus();
NotenameTextBox.Focus();
// Add to database
int id = Commands.AddFile(null, string.Empty); // Give some default empty content so it's not considered a binary file
// Add to view collection
Expand Down Expand Up @@ -914,16 +918,20 @@ private void Window_MouseDown(object sender, MouseButtonEventArgs e)
=> this.DragMove();
private void Window_Closing(object sender, CancelEventArgs e)
{
// Safety
TryCommitActiveItemChange();
TryCommitActiveNoteChange();

// Dispose resources
Commands?.Dispose();
Popup?.Close();
LastWorker?.Dispose();
Commands?.Dispose(); Commands = null;
Popup?.Close(); Popup = null;
LastWorker?.Dispose(); LastWorker = null;
}
#endregion

#region Searching Routines and Auxliary
private int WorkerCount { get; } = 0;
private PopupSelectionWindow Popup { get; }
private PopupSelectionWindow Popup { get; set; }
private BackgroundWorker LastWorker { get; set; }
private ConcurrentBag<string> SearchKeywords { get; }
/// <summary>
Expand Down Expand Up @@ -975,10 +983,11 @@ private string GetArgumentValue(string arg)
}
private string this[string arg]
=> GetArgumentValue(arg);
private void CommitActiveItemChange()
private void TryCommitActiveItemChange()
{
if (Commands == null) return;
// Commit to database for active item
if(ActiveItem != null)
if (ActiveItem != null)
{
try
{
Expand All @@ -998,8 +1007,9 @@ private void CommitActiveItemChange()
}
}
}
private void CommitActiveNoteChange()
private void TryCommitActiveNoteChange()
{
if (Commands == null) return;
// Commit to database for active note item
if (ActiveNote != null)
{
Expand Down
4 changes: 3 additions & 1 deletion TODO List.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
This list is for myself.

```
im * flatten (for reference images)
im * flatten (for reference images) (non-home folder)
im HomeFolder clean (default copy) - entires and files (first copy/cut all contents, second make a one-by-one item transition from original home, third double check everything exists, finally generate a report as a **file**; No need to delete original home folder)
Move from SD by changing name (path) e.g. for files under a folder
For MD shortcut, allow ALT key to unformat brackets.
[Practical Tests and Usage]
Expand Down

0 comments on commit e7ad2a2

Please sign in to comment.