-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Check for Duplicates In Mining Mode #109
Changes from 1 commit
e410754
850b34e
0d51e21
5b9e33b
a687c8d
4d42f6f
dbcc60a
c3483ce
25abc48
a5aee43
ddcb427
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -440,6 +440,77 @@ public static async Task MineToFile(LookupResult lookupResult, string currentTex | |
Utils.Logger.Information("Mined {PrimarySpelling}", lookupResult.PrimarySpelling); | ||
} | ||
|
||
public static async Task<bool> CheckDuplicate(LookupResult lookupResult) | ||
{ | ||
CoreConfigManager coreConfigManager = CoreConfigManager.Instance; | ||
if (!coreConfigManager.AnkiIntegration || !coreConfigManager.CheckForDuplicateCards) | ||
{ | ||
return false; | ||
} | ||
|
||
Dictionary<MineType, AnkiConfig>? ankiConfigDict = await AnkiConfig.ReadAnkiConfig().ConfigureAwait(false); | ||
if (ankiConfigDict is null) | ||
{ | ||
return false; | ||
} | ||
|
||
AnkiConfig? ankiConfig; | ||
if (DictUtils.s_wordDictTypes.Contains(lookupResult.Dict.Type)) | ||
{ | ||
ankiConfig = ankiConfigDict.GetValueOrDefault(MineType.Word); | ||
} | ||
else if (DictUtils.s_kanjiDictTypes.Contains(lookupResult.Dict.Type)) | ||
{ | ||
ankiConfig = ankiConfigDict.GetValueOrDefault(MineType.Kanji); | ||
} | ||
else if (DictUtils.s_nameDictTypes.Contains(lookupResult.Dict.Type)) | ||
{ | ||
ankiConfig = ankiConfigDict.GetValueOrDefault(MineType.Name); | ||
} | ||
else | ||
{ | ||
ankiConfig = ankiConfigDict.GetValueOrDefault(MineType.Other); | ||
} | ||
|
||
if (ankiConfig is null) | ||
{ | ||
return false; | ||
} | ||
|
||
Dictionary<string, JLField> userFields = ankiConfig.Fields; | ||
Dictionary<JLField, string> miningParams = new() | ||
{ | ||
[JLField.LocalTime] = DateTime.Now.ToString("s", CultureInfo.InvariantCulture), | ||
[JLField.DictionaryName] = lookupResult.Dict.Name, | ||
[JLField.MatchedText] = lookupResult.MatchedText, | ||
[JLField.DeconjugatedMatchedText] = lookupResult.DeconjugatedMatchedText, | ||
[JLField.PrimarySpelling] = lookupResult.PrimarySpelling, | ||
[JLField.PrimarySpellingWithOrthographyInfo] = lookupResult.PrimarySpellingOrthographyInfoList is not null | ||
? $"{lookupResult.PrimarySpelling} ({string.Join(", ", lookupResult.PrimarySpellingOrthographyInfoList)})" | ||
: lookupResult.PrimarySpelling | ||
}; | ||
|
||
Dictionary<string, string> fields = ConvertFields(userFields, miningParams); | ||
|
||
// Audio/Picture/Video shouldn't be set here | ||
// Otherwise AnkiConnect will place them under the "collection.media" folder even when it's a duplicate note | ||
Note note = new(ankiConfig.DeckName, ankiConfig.ModelName, fields, ankiConfig.Tags, null, null, null, null); | ||
if (!coreConfigManager.AllowDuplicateCards) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we have this check here? As is, this method will always return false if the user enabled the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Already removed in local. |
||
{ | ||
bool? canAddNote = await AnkiUtils.CanAddNote(note).ConfigureAwait(false); | ||
if (canAddNote is null) | ||
{ | ||
return false; | ||
} | ||
|
||
if (!canAddNote.Value) | ||
{ | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public static async Task Mine(LookupResult lookupResult, string currentText, string? formattedDefinitions, string? selectedDefinitions, int currentCharPosition) | ||
{ | ||
CoreConfigManager coreConfigManager = CoreConfigManager.Instance; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -695,6 +695,31 @@ private StackPanel PrepareResultStackPanel(LookupResult result, int index, int r | |
audioButton.Click += AudioButton_Click; | ||
|
||
_ = top.Children.Add(audioButton); | ||
|
||
if (!configManager.MineToFileInsteadOfAnki) | ||
rampaa marked this conversation as resolved.
Show resolved
Hide resolved
bpwhelan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
if (MiningUtils.CheckDuplicate(result).Result) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Accessing the bool duplicateCard = await MiningUtils.CheckDuplicate(result).ConfigureAwait(true);
if (duplicateCard) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the biggest thing that I'm having trouble with since this method is synchronous. Toying with something to do the check for duplicates after the window is loaded and toggling the visibility so there is 0 impact on loading the window. |
||
{ | ||
Utils.Frontend.Alert(AlertLevel.Error, $"{result.PrimarySpelling} is a duplicate card"); | ||
Button duplicate = new() | ||
{ | ||
Name = nameof(duplicate), | ||
Content = "⚠️", | ||
Foreground = configManager.DefinitionsColor, | ||
VerticalAlignment = VerticalAlignment.Top, | ||
Margin = new Thickness(3, 0, 0, 0), | ||
HorizontalAlignment = HorizontalAlignment.Left, | ||
Background = Brushes.Transparent, | ||
Cursor = Cursors.Arrow, | ||
BorderThickness = new Thickness(0), | ||
Padding = new Thickness(0), | ||
FontSize = 14, | ||
ToolTip = $"{result.PrimarySpelling} is already in anki deck." | ||
}; | ||
|
||
_ = top.Children.Add(duplicate); | ||
} | ||
} | ||
} | ||
|
||
if (result.AlternativeSpellings is not null && configManager.AlternativeSpellingsFontSize > 0) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably should use
GetMiningParameters
here. The implementation provided here just partially creates mining parameters but for all we know the user might haveSourceText
as their note type's first field (which is all Anki cares for when checking if a card is a duplicate AFAIK) and its value is missing here.Or better yet, we can check what's the first
Field
in the config and just create a note with that field to check whether it's a duplicate note.