From 1f30bfa71cc710d4d91cc63437039ab93c9a63da Mon Sep 17 00:00:00 2001 From: Simon Sarasova Date: Sun, 11 Aug 2024 12:31:40 +0000 Subject: [PATCH] Improved the helpers package. --- Contributors.md | 2 +- gui/buildProfileGui_General.go | 2 +- gui/buildProfileGui_Physical.go | 4 +-- gui/contactsGui.go | 6 ++--- gui/desiresGui_General.go | 8 +++--- gui/desiresGui_Mental.go | 5 ++-- gui/desiresGui_Physical.go | 4 +-- gui/manageGeneticsGui.go | 2 +- gui/matchesGui.go | 4 +-- internal/helpers/helpers.go | 27 ++----------------- .../moderation/reviewStorage/reviewStorage.go | 2 +- internal/myContacts/myContacts.go | 2 +- internal/myDatastores/myList/myList.go | 2 +- 13 files changed, 24 insertions(+), 46 deletions(-) diff --git a/Contributors.md b/Contributors.md index fded102..d72e5f6 100644 --- a/Contributors.md +++ b/Contributors.md @@ -9,4 +9,4 @@ Many other people have written code for modules which are imported by Seekia. Th Name | Date Of First Commit | Number Of Commits --- | --- | --- -Simon Sarasova | June 13, 2023 | 276 \ No newline at end of file +Simon Sarasova | June 13, 2023 | 277 \ No newline at end of file diff --git a/gui/buildProfileGui_General.go b/gui/buildProfileGui_General.go index 1de186e..45b1efa 100644 --- a/gui/buildProfileGui_General.go +++ b/gui/buildProfileGui_General.go @@ -2451,7 +2451,7 @@ func setBuildMateProfilePage_Tags(window fyne.Window, previousPage func()){ deleteButton := widget.NewButtonWithIcon("", theme.DeleteIcon(), func(){ - newList, deletedAny := helpers.DeleteAllMatchingItemsFromStringList(myTagsList, tagName) + newList, deletedAny := helpers.DeleteAllMatchingItemsFromList(myTagsList, tagName) if (deletedAny == false){ setErrorEncounteredPage(window, errors.New("Cannot delete tag: tag not found."), currentPage) return diff --git a/gui/buildProfileGui_Physical.go b/gui/buildProfileGui_Physical.go index 14c1aa2..bb4eb90 100644 --- a/gui/buildProfileGui_Physical.go +++ b/gui/buildProfileGui_Physical.go @@ -2445,7 +2445,7 @@ func setBuildMateProfilePage_EyeColor(window fyne.Window, previousPage func()){ getNewAttributeList := func()[]string{ if (newChoice == false){ - newList, _ := helpers.DeleteAllMatchingItemsFromStringList(currentEyeColorList, colorName) + newList, _ := helpers.DeleteAllMatchingItemsFromList(currentEyeColorList, colorName) return newList } @@ -2613,7 +2613,7 @@ func setBuildMateProfilePage_HairColor(window fyne.Window, previousPage func()){ getNewAttributeList := func()[]string{ if (newChoice == false){ - newList, _ := helpers.DeleteAllMatchingItemsFromStringList(currentHairColorList, colorName) + newList, _ := helpers.DeleteAllMatchingItemsFromList(currentHairColorList, colorName) return newList } diff --git a/gui/contactsGui.go b/gui/contactsGui.go index 654ac85..f85442f 100644 --- a/gui/contactsGui.go +++ b/gui/contactsGui.go @@ -482,7 +482,7 @@ func setAddContactFromIdentityHashPage(window fyne.Window, userIdentityHash [16] newContactCategoriesList := helpers.AddItemToStringListAndAvoidDuplicate(currentContactCategoriesList, categoryName) contactCategoriesListBinding.Set(newContactCategoriesList) } else { - newContactCategoriesList, _ := helpers.DeleteAllMatchingItemsFromStringList(currentContactCategoriesList, categoryName) + newContactCategoriesList, _ := helpers.DeleteAllMatchingItemsFromList(currentContactCategoriesList, categoryName) contactCategoriesListBinding.Set(newContactCategoriesList) } } @@ -785,8 +785,8 @@ func setEditContactCategoriesPage(window fyne.Window, contactIdentityHash [16]by newCategoriesList := append(currentContactCategoriesList, categoryName) return newCategoriesList } - - newCategoriesList, _ := helpers.DeleteAllMatchingItemsFromStringList(currentContactCategoriesList, categoryName) + + newCategoriesList, _ := helpers.DeleteAllMatchingItemsFromList(currentContactCategoriesList, categoryName) return newCategoriesList } diff --git a/gui/desiresGui_General.go b/gui/desiresGui_General.go index 8592a48..344e6b4 100644 --- a/gui/desiresGui_General.go +++ b/gui/desiresGui_General.go @@ -275,7 +275,7 @@ func setChooseDesiresPage_ProfileLanguage(window fyne.Window, previousPage func( deleteLanguageButton := widget.NewButtonWithIcon("", theme.DeleteIcon(), func(){ - newDesiredLanguagesList, _ := helpers.DeleteAllMatchingItemsFromStringList(currentDesiredChoicesList, languageIdentifierBase64) + newDesiredLanguagesList, _ := helpers.DeleteAllMatchingItemsFromList(currentDesiredChoicesList, languageIdentifierBase64) if (len(newDesiredLanguagesList) == 0){ @@ -499,7 +499,7 @@ func setChooseDesiresPage_Country(window fyne.Window, previousPage func()){ deleteCountryButton := widget.NewButtonWithIcon("", theme.DeleteIcon(), func(){ - newDesiredCountriesList, _ := helpers.DeleteAllMatchingItemsFromStringList(currentDesiredChoicesList, countryIdentifierBase64) + newDesiredCountriesList, _ := helpers.DeleteAllMatchingItemsFromList(currentDesiredChoicesList, countryIdentifierBase64) if (len(newDesiredCountriesList) == 0){ @@ -805,7 +805,7 @@ func setChooseDesiresPage_SearchTerms(window fyne.Window, previousPage func()){ deleteTermButton := widget.NewButtonWithIcon("", theme.DeleteIcon(), func(){ - newDesiredTermsList, _ := helpers.DeleteAllMatchingItemsFromStringList(currentDesiredChoicesList, termNameBase64) + newDesiredTermsList, _ := helpers.DeleteAllMatchingItemsFromList(currentDesiredChoicesList, termNameBase64) if (len(newDesiredTermsList) == 0){ @@ -1287,7 +1287,7 @@ func getDesireEditor_Choice(window fyne.Window, currentPage func(), desireName s return emptyList } - newAttributeList, _ := helpers.DeleteAllMatchingItemsFromStringList(currentDesiredChoicesList, "Other") + newAttributeList, _ := helpers.DeleteAllMatchingItemsFromList(currentDesiredChoicesList, "Other") return newAttributeList } diff --git a/gui/desiresGui_Mental.go b/gui/desiresGui_Mental.go index 5872422..22d734a 100644 --- a/gui/desiresGui_Mental.go +++ b/gui/desiresGui_Mental.go @@ -109,7 +109,7 @@ func setChooseDesiresPage_Language(window fyne.Window, previousPage func()){ if (response == false){ - newList, _ := helpers.DeleteAllMatchingItemsFromStringList(currentDesiredChoicesList, "Other") + newList, _ := helpers.DeleteAllMatchingItemsFromList(currentDesiredChoicesList, "Other") return newList } @@ -195,7 +195,8 @@ func setChooseDesiresPage_Language(window fyne.Window, previousPage func()){ languageNameLabel := getBoldLabelCentered(translate(languageName)) deleteLanguageButton := widget.NewButtonWithIcon("", theme.DeleteIcon(), func(){ - newDesiredLanguagesList, _ := helpers.DeleteAllMatchingItemsFromStringList(currentDesiredChoicesList, languageNameBase64) + + newDesiredLanguagesList, _ := helpers.DeleteAllMatchingItemsFromList(currentDesiredChoicesList, languageNameBase64) if (len(newDesiredLanguagesList) == 0){ diff --git a/gui/desiresGui_Physical.go b/gui/desiresGui_Physical.go index a59143c..85a01db 100644 --- a/gui/desiresGui_Physical.go +++ b/gui/desiresGui_Physical.go @@ -1305,7 +1305,7 @@ func setChooseDesiresPage_23andMe_Haplogroup(window fyne.Window, maternalOrPater return emptyList } - newAttributeList, _ := helpers.DeleteAllMatchingItemsFromStringList(currentDesiredChoicesList, "Other") + newAttributeList, _ := helpers.DeleteAllMatchingItemsFromList(currentDesiredChoicesList, "Other") return newAttributeList } @@ -1393,7 +1393,7 @@ func setChooseDesiresPage_23andMe_Haplogroup(window fyne.Window, maternalOrPater deleteHaplogroupButton := widget.NewButtonWithIcon("", theme.DeleteIcon(), func(){ - newDesiredHaplogroupsList, _ := helpers.DeleteAllMatchingItemsFromStringList(currentDesiredChoicesList, haplogroupNameBase64) + newDesiredHaplogroupsList, _ := helpers.DeleteAllMatchingItemsFromList(currentDesiredChoicesList, haplogroupNameBase64) if (len(newDesiredHaplogroupsList) == 0){ diff --git a/gui/manageGeneticsGui.go b/gui/manageGeneticsGui.go index f95713a..7d8df79 100644 --- a/gui/manageGeneticsGui.go +++ b/gui/manageGeneticsGui.go @@ -1329,7 +1329,7 @@ func setCreateCouplePage(window fyne.Window, previousPage func()){ return } - newList, deletedAny := helpers.DeleteAllMatchingItemsFromStringList(existingList, personIdentifier) + newList, deletedAny := helpers.DeleteAllMatchingItemsFromList(existingList, personIdentifier) if (deletedAny == false){ setErrorEncounteredPage(window, errors.New("Person not found when trying to delete person from chosen people list."), currentPage) return diff --git a/gui/matchesGui.go b/gui/matchesGui.go index 2852e67..f15e5a7 100644 --- a/gui/matchesGui.go +++ b/gui/matchesGui.go @@ -762,7 +762,7 @@ func setBrowseMatchesPage(window fyne.Window, previousPage func()){ currentAttributesList := strings.Split(currentAttributesListString, ",") - displayAttributesListPruned, _ := helpers.DeleteAllMatchingItemsFromStringList(currentAttributesList, currentSortByAttribute) + displayAttributesListPruned, _ := helpers.DeleteAllMatchingItemsFromList(currentAttributesList, currentSortByAttribute) newDisplayAttributesList := []string{currentSortByAttribute} @@ -1390,7 +1390,7 @@ func setCustomizeMatchDisplayPage(window fyne.Window, previousPage func()){ deleteAttributeButton := widget.NewButtonWithIcon("", theme.DeleteIcon(), func(){ - newAttributesList, _ := helpers.DeleteAllMatchingItemsFromStringList(customDisplayAttributesList, attributeName) + newAttributesList, _ := helpers.DeleteAllMatchingItemsFromList(customDisplayAttributesList, attributeName) if (len(newAttributesList) == 0){ diff --git a/internal/helpers/helpers.go b/internal/helpers/helpers.go index a35b0eb..37b6236 100644 --- a/internal/helpers/helpers.go +++ b/internal/helpers/helpers.go @@ -1098,34 +1098,11 @@ func DeleteIndexFromStringList(inputList []string, indexToDelete int)([]string, //Outputs: // -[]string: New list // -bool: Deleted any items -func DeleteAllMatchingItemsFromStringList(inputList []string, itemToDelete string)([]string, bool){ +func DeleteAllMatchingItemsFromList[E comparable](inputList []E, itemToDelete E)([]E, bool){ listCopy := slices.Clone(inputList) - deletionFunction := func(input string)bool{ - if (input == itemToDelete){ - return true - } - return false - } - - newList := slices.DeleteFunc(listCopy, deletionFunction) - - if (len(newList) == len(inputList)){ - return newList, false - } - - return newList, true -} - -//Outputs: -// -[]string: New list -// -bool: Deleted any items -func DeleteAllMatchingItemsFromProfileHashList(inputList [][28]byte, itemToDelete [28]byte)([][28]byte, bool){ - - listCopy := slices.Clone(inputList) - - deletionFunction := func(input [28]byte)bool{ + deletionFunction := func(input E)bool{ if (input == itemToDelete){ return true } diff --git a/internal/moderation/reviewStorage/reviewStorage.go b/internal/moderation/reviewStorage/reviewStorage.go index 0eda66d..f6d3406 100644 --- a/internal/moderation/reviewStorage/reviewStorage.go +++ b/internal/moderation/reviewStorage/reviewStorage.go @@ -776,7 +776,7 @@ func GetProfileVerdictMaps(profileHash [28]byte, profileNetworkType byte, integr } // We can omit the current profileHash, because we are already checking it - attributeProfileHashesList, _ := helpers.DeleteAllMatchingItemsFromProfileHashList(attributeProfilesList, profileHash) + attributeProfileHashesList, _ := helpers.DeleteAllMatchingItemsFromList(attributeProfilesList, profileHash) return attributeProfileHashesList, nil } diff --git a/internal/myContacts/myContacts.go b/internal/myContacts/myContacts.go index 8ba7acb..55c3bd2 100644 --- a/internal/myContacts/myContacts.go +++ b/internal/myContacts/myContacts.go @@ -467,7 +467,7 @@ func DeleteContactCategory(identityType string, categoryToDeleteName string)erro contactCategoriesListBase64 := strings.Split(contactCategoriesListString, "+") - newCategoriesList, deletedAny := helpers.DeleteAllMatchingItemsFromStringList(contactCategoriesListBase64, categoryToDeleteNameBase64) + newCategoriesList, deletedAny := helpers.DeleteAllMatchingItemsFromList(contactCategoriesListBase64, categoryToDeleteNameBase64) if (deletedAny == false){ newContactsMapList = append(newContactsMapList, contactMap) continue diff --git a/internal/myDatastores/myList/myList.go b/internal/myDatastores/myList/myList.go index 20cb988..b37c15c 100644 --- a/internal/myDatastores/myList/myList.go +++ b/internal/myDatastores/myList/myList.go @@ -163,7 +163,7 @@ func (listObject *MyList) DeleteListItem(item string)error{ currentList := listObject.memoryList - newList, anyDeleted := helpers.DeleteAllMatchingItemsFromStringList(currentList, item) + newList, anyDeleted := helpers.DeleteAllMatchingItemsFromList(currentList, item) if (anyDeleted == false){ listObject.memoryMutex.Unlock() return nil