Commit 3f678637 authored by SusanneFischer's avatar SusanneFischer
Browse files

undo delete, sort selected items by name

parent 592b3210
Pipeline #3593 failed with stages
in 0 seconds
......@@ -10,7 +10,7 @@ import de.asta.hochschule.trier.verleih.util.GlideApp
class RentalItemQuantityAdapter(
private var objects: ArrayList<RentalObject>?,
private val removeItem: (RentalObject?) -> Unit
private val removeItem: (RentalObject?, Int) -> Unit
) :
RecyclerView.Adapter<RentalItemQuantityAdapter.ViewHolder>() {
......@@ -32,6 +32,18 @@ class RentalItemQuantityAdapter(
return objects?.size ?: 0
}
fun removeObject(rentalObject: RentalObject?, position: Int) {
objects?.remove(rentalObject)
notifyItemRemoved(position)
}
fun addObject(rentalObject: RentalObject?, position: Int) {
if (rentalObject != null) {
objects?.add(position, rentalObject)
notifyItemInserted(position)
}
}
fun resetObjects(resetObjects: ArrayList<RentalObject>): RentalItemQuantityAdapter {
objects = resetObjects
notifyDataSetChanged()
......@@ -40,7 +52,7 @@ class RentalItemQuantityAdapter(
class ViewHolder(
private val itemBinding: RowItemQuantityOverviewBinding,
private val removeItem: (RentalObject?) -> Unit
private val removeItem: (RentalObject?, Int) -> Unit
) :
RecyclerView.ViewHolder(itemBinding.root) {
fun bind(rentalObject: RentalObject?) {
......@@ -53,7 +65,7 @@ class RentalItemQuantityAdapter(
.into(itemBinding.itemCircleImageView)
itemBinding.itemDeleteButton.setOnClickListener {
removeItem.invoke(rentalObject)
removeItem.invoke(rentalObject, adapterPosition)
}
}
}
......
......@@ -4,6 +4,7 @@ import android.os.Bundle
import android.view.*
import androidx.fragment.app.*
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.snackbar.*
import de.asta.hochschule.trier.verleih.R
import de.asta.hochschule.trier.verleih.databinding.FragmentNewRentalItemsQuanityBinding
import de.asta.hochschule.trier.verleih.rental.adapter.RentalItemQuantityAdapter
......@@ -30,11 +31,34 @@ class NewRentalItemsQuantityFragment : Fragment(R.layout.fragment_new_rental_ite
super.onViewCreated(view, savedInstanceState)
binding.itemsRecyclerview.layoutManager = LinearLayoutManager(context)
adapter = RentalItemQuantityAdapter(viewModel.objectsLiveData.value) {
if (it != null) {
viewModel.removeRentalObject(it)
adapter =
RentalItemQuantityAdapter(viewModel.objectsLiveData.value) { rentalObject, position ->
// remove object from recyclerview only
adapter?.removeObject(rentalObject, position)
// undo snackbar
val snackbar = Snackbar.make(
binding.root,
"${rentalObject?.name} ${getString(R.string.deleted)}",
Snackbar.LENGTH_LONG
)
snackbar.setAction(R.string.undo) {
// re-add object to recyclerview
adapter?.addObject(rentalObject, position)
}
snackbar.addCallback(object : BaseTransientBottomBar.BaseCallback<Snackbar>() {
override fun onDismissed(transientBottomBar: Snackbar?, event: Int) {
if (event == Snackbar.Callback.DISMISS_EVENT_TIMEOUT) {
// now remove object permanently from viewmodel
if (rentalObject != null) {
viewModel.removeRentalObject(rentalObject)
}
}
super.onDismissed(transientBottomBar, event)
}
})
snackbar.show()
}
}
binding.itemsRecyclerview.adapter = adapter
viewModel.objectsLiveData.observe(requireActivity(), { objects ->
......
......@@ -15,12 +15,14 @@ class NewRentalViewModel : ViewModel() {
fun addRentalObject(rentalObject: RentalObject) {
val list = getObjectList()
list?.add(rentalObject)
list?.sortBy { it.name }
mutableObjects.value = list
}
fun removeRentalObject(rentalObject: RentalObject) {
val list = getObjectList()
list?.remove(rentalObject)
list?.sortBy { it.name }
mutableObjects.value = list
}
......
......@@ -31,4 +31,6 @@
<string name="rental_date_range_select">Verleihzeitraum auswählen</string>
<string name="rental_pickup_time_select">Abholzeit auswählen</string>
<string name="rental_return_time_select">Rückgabezeit auswählen</string>
<string name="deleted">gelöscht</string>
<string name="undo">Rückgängig</string>
</resources>
\ No newline at end of file
......@@ -31,4 +31,6 @@
<string name="rental_date_range_select">Verleihzeitraum auswählen</string>
<string name="rental_pickup_time_select">Abholzeit auswählen</string>
<string name="rental_return_time_select">Rückgabezeit auswählen</string>
<string name="deleted">gelöscht</string>
<string name="undo">Rückgängig</string>
</resources>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment