Skip to content

Commit 4b6231b

Browse files
Extract methods for readability
1 parent 4fb2e3d commit 4b6231b

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

Flow.Launcher.Infrastructure/PinyinAlphabet.cs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,27 @@ public PinyinAlphabet()
3131
if (e.PropertyName == nameof(Settings.UseDoublePinyin) ||
3232
e.PropertyName == nameof(Settings.DoublePinyinSchema))
3333
{
34-
LoadDoublePinyinTable();
35-
_pinyinCache.Clear();
34+
Reload();
3635
}
3736
};
3837
}
3938

39+
public void Reload()
40+
{
41+
LoadDoublePinyinTable();
42+
_pinyinCache.Clear();
43+
}
44+
45+
private void CreateDoublePinyinTableFromStream(Stream jsonStream)
46+
{
47+
Dictionary<string, Dictionary<string, string>> table = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, string>>>(jsonStream);
48+
if (!table.TryGetValue(_settings.DoublePinyinSchema, out var value))
49+
{
50+
throw new InvalidOperationException("DoublePinyinSchema is invalid or double pinyin table is broken.");
51+
}
52+
currentDoublePinyinTable = new ReadOnlyDictionary<string, string>(value);
53+
}
54+
4055
private void LoadDoublePinyinTable()
4156
{
4257
if (_settings.UseDoublePinyin)
@@ -45,12 +60,7 @@ private void LoadDoublePinyinTable()
4560
try
4661
{
4762
using var fs = File.OpenRead(tablePath);
48-
Dictionary<string, Dictionary<string, string>> table = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, string>>>(fs);
49-
if (!table.TryGetValue(_settings.DoublePinyinSchema, out var value))
50-
{
51-
throw new InvalidOperationException("DoublePinyinSchema is invalid.");
52-
}
53-
currentDoublePinyinTable = new ReadOnlyDictionary<string, string>(value);
63+
CreateDoublePinyinTableFromStream(fs);
5464
}
5565
catch (System.Exception e)
5666
{
@@ -73,7 +83,7 @@ public bool ShouldTranslate(string stringToTranslate)
7383

7484
public (string translation, TranslationMapping map) Translate(string content)
7585
{
76-
if (!_settings.ShouldUsePinyin)
86+
if (!_settings.ShouldUsePinyin || !WordsHelper.HasChinese(content))
7787
return (content, null);
7888

7989
return _pinyinCache.TryGetValue(content, out var value)
@@ -83,11 +93,6 @@ public bool ShouldTranslate(string stringToTranslate)
8393

8494
private (string translation, TranslationMapping map) BuildCacheFromContent(string content)
8595
{
86-
if (!WordsHelper.HasChinese(content))
87-
{
88-
return (content, null);
89-
}
90-
9196
var resultList = WordsHelper.GetPinyinList(content);
9297

9398
var resultBuilder = new StringBuilder();

0 commit comments

Comments
 (0)