From 67c2db01b952632c9fd3a4b76a9feb100cea5410 Mon Sep 17 00:00:00 2001 From: rampaa Date: Sat, 16 Dec 2023 11:32:58 +0300 Subject: [PATCH] Minor --- JL.Core/Dicts/DictUtils.cs | 229 ++++++++++++++---------------- JL.Core/Freqs/FreqUtils.cs | 64 ++++----- JL.Windows/GUI/MainWindow.xaml.cs | 57 +++++++- 3 files changed, 192 insertions(+), 158 deletions(-) diff --git a/JL.Core/Dicts/DictUtils.cs b/JL.Core/Dicts/DictUtils.cs index 33acb3ff..6952d697 100644 --- a/JL.Core/Dicts/DictUtils.cs +++ b/JL.Core/Dicts/DictUtils.cs @@ -589,29 +589,28 @@ public static async Task LoadDictionaries() })); } - else + else if (dict.Contents.Count > 0 && (!dict.Active || useDB)) { - if (dict.Contents.Count > 0 && (!dict.Active || useDB)) + dict.Ready = false; + if (useDB && !dbExists) { - if (useDB && !dbExists) - { - tasks.Add(Task.Run(() => - { - JmdictDBManager.CreateDB(dict.Name); - JmdictDBManager.InsertRecordsToDB(dict); - dict.Contents.Clear(); - dict.Contents.TrimExcess(); - })); - } - else + tasks.Add(Task.Run(() => { + JmdictDBManager.CreateDB(dict.Name); + JmdictDBManager.InsertRecordsToDB(dict); dict.Contents.Clear(); dict.Contents.TrimExcess(); - } - - dictCleared = true; + dict.Ready = true; + })); } - dict.Ready = true; + else + { + dict.Contents.Clear(); + dict.Contents.TrimExcess(); + dict.Ready = true; + } + + dictCleared = true; } } @@ -646,30 +645,28 @@ public static async Task LoadDictionaries() })); } - else + else if (dict.Contents.Count > 0 && (!dict.Active || useDB)) { - if (dict.Contents.Count > 0 && (!dict.Active || useDB)) + dict.Ready = false; + if (useDB && !dbExists) { - if (useDB && !dbExists) - { - tasks.Add(Task.Run(() => - { - JmnedictDBManager.CreateDB(dict.Name); - JmnedictDBManager.InsertRecordsToDB(dict); - dict.Contents.Clear(); - dict.Contents.TrimExcess(); - })); - } - else + tasks.Add(Task.Run(() => { + JmnedictDBManager.CreateDB(dict.Name); + JmnedictDBManager.InsertRecordsToDB(dict); dict.Contents.Clear(); dict.Contents.TrimExcess(); - } - - dictCleared = true; + dict.Ready = true; + })); + } + else + { + dict.Contents.Clear(); + dict.Contents.TrimExcess(); + dict.Ready = true; } - dict.Ready = true; + dictCleared = true; } } @@ -711,30 +708,26 @@ public static async Task LoadDictionaries() })); } - else + else if (dict.Contents.Count > 0 && (!dict.Active || useDB)) { - if (dict.Contents.Count > 0 && (!dict.Active || useDB)) + dict.Ready = false; + if (useDB && !dbExists) { - if (useDB && !dbExists) - { - tasks.Add(Task.Run(() => - { - KanjidicDBManager.CreateDB(dict.Name); - KanjidicDBManager.InsertRecordsToDB(dict); - dict.Contents.Clear(); - dict.Contents.TrimExcess(); - })); - } - else + tasks.Add(Task.Run(() => { + KanjidicDBManager.CreateDB(dict.Name); + KanjidicDBManager.InsertRecordsToDB(dict); dict.Contents.Clear(); dict.Contents.TrimExcess(); - } - - dictCleared = true; + dict.Ready = true; + })); + } + else + { + dict.Contents.Clear(); + dict.Contents.TrimExcess(); + dict.Ready = true; } - - dict.Ready = true; } } @@ -837,29 +830,28 @@ public static async Task LoadDictionaries() })); } - else + else if (dict.Contents.Count > 0 && (!dict.Active || useDB)) { - if (dict.Contents.Count > 0 && (!dict.Active || useDB)) + dict.Ready = false; + if (useDB && !dbExists) { - if (useDB && !dbExists) - { - tasks.Add(Task.Run(() => - { - EpwingYomichanDBManager.CreateDB(dict.Name); - EpwingYomichanDBManager.InsertRecordsToDB(dict); - dict.Contents.Clear(); - dict.Contents.TrimExcess(); - })); - } - else + tasks.Add(Task.Run(() => { + EpwingYomichanDBManager.CreateDB(dict.Name); + EpwingYomichanDBManager.InsertRecordsToDB(dict); dict.Contents.Clear(); dict.Contents.TrimExcess(); - } - - dictCleared = true; + dict.Ready = true; + })); } - dict.Ready = true; + else + { + dict.Contents.Clear(); + dict.Contents.TrimExcess(); + dict.Ready = true; + } + + dictCleared = true; } break; @@ -913,29 +905,28 @@ public static async Task LoadDictionaries() })); } - else + else if (dict.Contents.Count > 0 && (!dict.Active || useDB)) { - if (dict.Contents.Count > 0 && (!dict.Active || useDB)) + dict.Ready = false; + if (useDB && !dbExists) { - if (useDB && !dbExists) - { - tasks.Add(Task.Run(() => - { - YomichanKanjiDBManager.CreateDB(dict.Name); - YomichanKanjiDBManager.InsertRecordsToDB(dict); - dict.Contents.Clear(); - dict.Contents.TrimExcess(); - })); - } - else + tasks.Add(Task.Run(() => { + YomichanKanjiDBManager.CreateDB(dict.Name); + YomichanKanjiDBManager.InsertRecordsToDB(dict); dict.Contents.Clear(); dict.Contents.TrimExcess(); - } - - dictCleared = true; + dict.Ready = true; + })); } - dict.Ready = true; + else + { + dict.Contents.Clear(); + dict.Contents.TrimExcess(); + dict.Ready = true; + } + + dictCleared = true; } break; @@ -1081,29 +1072,28 @@ dict.Type is DictType.CustomNameDictionary })); } - else + else if (dict.Contents.Count > 0 && (!dict.Active || useDB)) { - if (dict.Contents.Count > 0 && (!dict.Active || useDB)) + dict.Ready = false; + if (useDB && !dbExists) { - if (useDB && !dbExists) - { - tasks.Add(Task.Run(() => - { - EpwingNazekaDBManager.CreateDB(dict.Name); - EpwingNazekaDBManager.InsertRecordsToDB(dict); - dict.Contents.Clear(); - dict.Contents.TrimExcess(); - })); - } - else + tasks.Add(Task.Run(() => { + EpwingNazekaDBManager.CreateDB(dict.Name); + EpwingNazekaDBManager.InsertRecordsToDB(dict); dict.Contents.Clear(); dict.Contents.TrimExcess(); - } - - dictCleared = true; + dict.Ready = true; + })); } - dict.Ready = true; + else + { + dict.Contents.Clear(); + dict.Contents.TrimExcess(); + dict.Ready = true; + } + + dictCleared = true; } break; @@ -1158,29 +1148,28 @@ dict.Type is DictType.CustomNameDictionary })); } - else + else if (dict.Contents.Count > 0 && (!dict.Active || useDB)) { - if (dict.Contents.Count > 0 && (!dict.Active || useDB)) + dict.Ready = false; + if (useDB && !dbExists) { - if (useDB && !dbExists) - { - tasks.Add(Task.Run(() => - { - YomichanPitchAccentDBManager.CreateDB(dict.Name); - YomichanPitchAccentDBManager.InsertRecordsToDB(dict); - dict.Contents.Clear(); - dict.Contents.TrimExcess(); - })); - } - else + tasks.Add(Task.Run(() => { + YomichanPitchAccentDBManager.CreateDB(dict.Name); + YomichanPitchAccentDBManager.InsertRecordsToDB(dict); dict.Contents.Clear(); dict.Contents.TrimExcess(); - } - - dictCleared = true; + dict.Ready = true; + })); } - dict.Ready = true; + else + { + dict.Contents.Clear(); + dict.Contents.TrimExcess(); + dict.Ready = true; + } + + dictCleared = true; } break; diff --git a/JL.Core/Freqs/FreqUtils.cs b/JL.Core/Freqs/FreqUtils.cs index 266e0c98..af05f45a 100644 --- a/JL.Core/Freqs/FreqUtils.cs +++ b/JL.Core/Freqs/FreqUtils.cs @@ -129,27 +129,25 @@ public static async Task LoadFrequencies() })); } - else + else if (freq.Contents.Count > 0 && (!freq.Active || useDB)) { - if (freq.Contents.Count > 0 && (!freq.Active || useDB)) + freq.Ready = false; + if (useDB && !dbExists) { - if (useDB && !dbExists) - { - FreqDBManager.CreateDB(freq.Name); - FreqDBManager.InsertRecordsToDB(freq); - freq.Contents.Clear(); - freq.Contents.TrimExcess(); - } - else - { - freq.Contents.Clear(); - freq.Contents.TrimExcess(); - } - - freqCleared = true; + FreqDBManager.CreateDB(freq.Name); + FreqDBManager.InsertRecordsToDB(freq); + freq.Contents.Clear(); + freq.Contents.TrimExcess(); + freq.Ready = true; + } + else + { + freq.Contents.Clear(); + freq.Contents.TrimExcess(); + freq.Ready = true; } - freq.Ready = true; + freqCleared = true; } break; @@ -206,27 +204,25 @@ public static async Task LoadFrequencies() })); } - else + else if (freq.Contents.Count > 0 && (!freq.Active || useDB)) { - if (freq.Contents.Count > 0 && (!freq.Active || useDB)) + freq.Ready = false; + if (useDB && !dbExists) { - if (useDB && !dbExists) - { - FreqDBManager.CreateDB(freq.Name); - FreqDBManager.InsertRecordsToDB(freq); - freq.Contents.Clear(); - freq.Contents.TrimExcess(); - } - else - { - freq.Contents.Clear(); - freq.Contents.TrimExcess(); - } - - freqCleared = true; + FreqDBManager.CreateDB(freq.Name); + FreqDBManager.InsertRecordsToDB(freq); + freq.Contents.Clear(); + freq.Contents.TrimExcess(); + freq.Ready = true; + } + else + { + freq.Contents.Clear(); + freq.Contents.TrimExcess(); + freq.Ready = true; } - freq.Ready = true; + freqCleared = true; } break; diff --git a/JL.Windows/GUI/MainWindow.xaml.cs b/JL.Windows/GUI/MainWindow.xaml.cs index 12781f82..4cf04f67 100644 --- a/JL.Windows/GUI/MainWindow.xaml.cs +++ b/JL.Windows/GUI/MainWindow.xaml.cs @@ -556,7 +556,19 @@ public async Task HandleHotKey(KeyGesture keyGesture, KeyEventArgs? e) { handled = true; - if (DictUtils.SingleDictTypeDicts[DictType.CustomNameDictionary].Ready && DictUtils.SingleDictTypeDicts[DictType.ProfileCustomNameDictionary].Ready) + bool customNameDictReady = false; + if (DictUtils.SingleDictTypeDicts.TryGetValue(DictType.CustomNameDictionary, out Dict? customNameDict)) + { + customNameDictReady = customNameDict.Ready; + } + + bool profileCustomNameDictReady = false; + if (DictUtils.SingleDictTypeDicts.TryGetValue(DictType.ProfileCustomNameDictionary, out Dict? profileCustomNameDict)) + { + profileCustomNameDictReady = profileCustomNameDict.Ready; + } + + if (customNameDictReady && profileCustomNameDictReady) { ShowAddNameWindow(); } @@ -566,7 +578,19 @@ public async Task HandleHotKey(KeyGesture keyGesture, KeyEventArgs? e) { handled = true; - if (DictUtils.SingleDictTypeDicts[DictType.CustomWordDictionary].Ready && DictUtils.SingleDictTypeDicts[DictType.ProfileCustomWordDictionary].Ready) + bool customWordDictReady = false; + if (DictUtils.SingleDictTypeDicts.TryGetValue(DictType.CustomWordDictionary, out Dict? customWordDict)) + { + customWordDictReady = customWordDict.Ready; + } + + bool profileCustomWordDictReady = false; + if (DictUtils.SingleDictTypeDicts.TryGetValue(DictType.ProfileCustomWordDictionary, out Dict? profileCustomWordDict)) + { + profileCustomWordDictReady = profileCustomWordDict.Ready; + } + + if (customWordDictReady && profileCustomWordDictReady) { ShowAddWordWindow(); } @@ -1278,8 +1302,33 @@ private void MainTextBox_ContextMenuOpening(object sender, ContextMenuEventArgs ManageFrequenciesMenuItem.IsEnabled = FreqUtils.FreqsReady; - AddNameMenuItem.IsEnabled = DictUtils.SingleDictTypeDicts[DictType.CustomNameDictionary].Ready && DictUtils.SingleDictTypeDicts[DictType.ProfileCustomNameDictionary].Ready; - AddWordMenuItem.IsEnabled = DictUtils.SingleDictTypeDicts[DictType.CustomWordDictionary].Ready && DictUtils.SingleDictTypeDicts[DictType.ProfileCustomWordDictionary].Ready; + bool customNameDictReady = false; + if (DictUtils.SingleDictTypeDicts.TryGetValue(DictType.CustomNameDictionary, out Dict? customNameDict)) + { + customNameDictReady = customNameDict.Ready; + } + + bool profileCustomNameDictReady = false; + if (DictUtils.SingleDictTypeDicts.TryGetValue(DictType.ProfileCustomNameDictionary, out Dict? profileCustomNameDict)) + { + profileCustomNameDictReady = profileCustomNameDict.Ready; + } + + AddNameMenuItem.IsEnabled = customNameDictReady && profileCustomNameDictReady; + + bool customWordDictReady = false; + if (DictUtils.SingleDictTypeDicts.TryGetValue(DictType.CustomWordDictionary, out Dict? customWordDict)) + { + customWordDictReady = customWordDict.Ready; + } + + bool profileCustomWordDictReady = false; + if (DictUtils.SingleDictTypeDicts.TryGetValue(DictType.ProfileCustomWordDictionary, out Dict? profileCustomWordDict)) + { + profileCustomWordDictReady = profileCustomWordDict.Ready; + } + + AddWordMenuItem.IsEnabled = customWordDictReady && profileCustomWordDictReady; int charIndex = MainTextBox.GetCharacterIndexFromPoint(Mouse.GetPosition(MainTextBox), ConfigManager.HorizontallyCenterMainWindowText); ContextMenuIsOpening = charIndex >= MainTextBox.SelectionStart && charIndex <= MainTextBox.SelectionStart + MainTextBox.SelectionLength;