Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,10 @@ class MultiShoppingFr : Fragment() {
dialogRemoveCheckedItems()
}

R.id.item_shopping_remove_duplicates -> {
dialogRemoveDuplicateItems()
}

R.id.item_shopping_uncheck_all -> {
//uncheck all shopping items
activeShoppingFr.shoppingListInstance.uncheckAll()
Expand Down Expand Up @@ -447,7 +451,8 @@ class MultiShoppingFr : Fragment() {
dialogAddItemBinding = DialogAddItemBinding.inflate(layoutInflater)

//Initialize dialogBuilder and set its title
val myBuilder = myActivity.let { AlertDialog.Builder(it).setView(dialogAddItemBinding.root) }
val myBuilder =
myActivity.let { AlertDialog.Builder(it).setView(dialogAddItemBinding.root) }

val titleDialogBinding = TitleDialogBinding.inflate(layoutInflater)
titleDialogBinding.tvDialogTitle.text = myActivity.getString(R.string.shoppingAddItemTitle)
Expand Down Expand Up @@ -815,6 +820,16 @@ class MultiShoppingFr : Fragment() {
myActivity.dialogConfirm(titleId, action)
}

private fun dialogRemoveDuplicateItems() {
val titleId = R.string.shoppingDialogRemoveCheckedDuplicates
val action: () -> Unit = {
activeShoppingFr.shoppingListInstance.removeCheckedDuplicateItems()
activeShoppingFr.myAdapter.notifyDataSetChanged()
updateShoppingMenu()
}
myActivity.dialogConfirm(titleId, action)
}

fun updateExpandAllIcon() {
myMenu.findItem(R.id.item_shopping_expand_all)?.isVisible =
activeShoppingFr.shoppingListInstance.somethingsCollapsed() && !(SettingsManager.getSetting(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class ShoppingList(private var wrapper: ShoppingListWrapper?) :
fun setWrapper(newWrapper: ShoppingListWrapper) {
this.wrapper = newWrapper
}

/**
* Adds a given ShoppingElement to this list, according to its given tag.
* If no element of the given tag existed before, the list generate a new sublist,
Expand Down Expand Up @@ -212,6 +213,36 @@ class ShoppingList(private var wrapper: ShoppingListWrapper?) :
save()
}

/**
* Removes duplicates from checked items, either if there is
* a unchecked item with the same title or an checked item
* with the same title
*/
fun removeCheckedDuplicateItems() {
removeCheckedItemsIfAlsoUnchecked()
this.forEach { e ->
val checkedItems =
e.second.filter { item -> item.checked && e.second.indexOf(item) != 0 }
e.second.removeAll(checkedItems.toSet())
e.second.addAll(checkedItems.distinct())
}
save()
}

/**
* Removes all checked items from shopping list
* if there is an uncheck item with the same name
*/
private fun removeCheckedItemsIfAlsoUnchecked() {
this.forEach { e ->
val unchecked = e.second.filter { item -> !item.checked }.map { item -> item.name }
unchecked.forEach { name ->
e.second.removeAll { item -> item.checked && item.name.equals(name) }
}
}
save()
}

/**
* Tries to fetch the length of the sublist with given tag if the sublist exists.
* @param tag The tag the sublist is supposed to have.
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/menu/menu_shopping.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
android:id="@+id/item_shopping_remove_checked"
android:title="@string/shoppingOptionDeleteChecked"
app:showAsAction="never" />
<item
android:id="@+id/item_shopping_remove_duplicates"
android:title="@string/shoppingDialogRemoveCheckedDuplicates"
app:showAsAction="never" />
<item
android:id="@+id/item_shopping_undo"
android:icon="@drawable/ic_action_undo"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -309,5 +309,7 @@
<string name="settingsNotesSortFoldersTop">Ordner nach oben sortieren</string>
<string name="aboutSupportThisProject">PocketPlan ist werbefrei und generiert daher keine Einnahmen. Wenn du die Entwicklung dieses Projekts unterstützen willst, kannst du dies über den folgenden Link tun.</string>
<string name="aboutSupportThis">Unterstütze dieses Projekt</string>
<string name="shoppingDialogRemoveCheckedDuplicates">Abgehakte Duplikate löschen?</string>
<string name="shoppingOptionRemoveCheckedDuplicates">Abgehakte Duplikate löschen</string>

</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
<string name="shoppingDialogClearList">Clear list?</string>
<string name="shoppingDialogDeleteTitle">Delete list?</string>
<string name="shoppingDialogRemoveChecked">Remove checked items?</string>
<string name="shoppingDialogRemoveCheckedDuplicates">Remove duplicate checked items?</string>
<string name="shoppingDialogRenameList">Rename List</string>
<string name="shoppingEditItemTitle">Edit item</string>
<string name="shoppingItemTitle" translatable="false">%1$s%2$s %3$s</string>
Expand All @@ -197,6 +198,7 @@
<string name="shoppingOptionClearList">Clear list</string>
<string name="shoppingOptionCollapseAll">Collapse all</string>
<string name="shoppingOptionDeleteChecked">Remove checked items</string>
<string name="shoppingOptionRemoveCheckedDuplicates">Remove duplicate checked items</string>
<string name="shoppingOptionDeleteList">Delete List</string>
<string name="shoppingOptionExpandAll">Expand all</string>
<string name="shoppingOptionRenameList">Rename list</string>
Expand Down