Commit fdc66ef7 authored by SusanneFischer's avatar SusanneFischer
Browse files

cleaned up some code

parent c7bbef9b
......@@ -55,7 +55,7 @@ class RentalItemChoiceAdapter(
private fun loadObjectPicture(holder: ViewHolder, pictureName: String?) {
val path =
"${Constants.PATH_OBJ_PICTURE.childName}$pictureName${Constants.JPG_EXT.childName}"
"${Constants.PATH_OBJ_PIC_BIG.childName}$pictureName${Constants.JPG_EXT.childName}"
val storageRef = FirebaseStorage.getInstance().reference.child(path)
GlideApp.with(holder.itemView.context).load(storageRef)
.placeholder(R.drawable.placeholder)
......
......@@ -6,13 +6,13 @@ import com.google.firebase.storage.FirebaseStorage
import de.asta.hochschule.trier.verleih.R
import de.asta.hochschule.trier.verleih.databinding.RowItemQuantityOverviewBinding
import de.asta.hochschule.trier.verleih.rental.model.RentalObject
import de.asta.hochschule.trier.verleih.util.GlideApp
import de.asta.hochschule.trier.verleih.util.*
class RentalItemQuantityAdapter(
private var objects: ArrayList<RentalObject>?,
private var components: MutableMap<String, MutableMap<String, Int>?>?,
private val removeItem: (RentalObject?, MutableMap<String, Int>?, Int) -> Unit,
private val updateQuantity: (RentalObject, Pair<String, Int>, Int, Int) -> Unit
private val removeItem: (RentalObject?, Int) -> Unit,
private val updateQuantity: (RentalObject, Pair<String, Int>, Int) -> Unit
) :
RecyclerView.Adapter<RentalItemQuantityAdapter.ViewHolder>() {
......@@ -50,19 +50,19 @@ class RentalItemQuantityAdapter(
}
fun resetData(
objs: ArrayList<RentalObject>?,
comps: MutableMap<String, MutableMap<String, Int>?>?
objects: ArrayList<RentalObject>?,
components: MutableMap<String, MutableMap<String, Int>?>?
): RentalItemQuantityAdapter {
objects = objs
components = comps
this.objects = objects
this.components = components
notifyDataSetChanged()
return this
}
class ViewHolder(
private val itemBinding: RowItemQuantityOverviewBinding,
private val removeItem: (RentalObject?, MutableMap<String, Int>?, Int) -> Unit,
private val updateQuantity: (RentalObject, Pair<String, Int>, Int, Int) -> Unit
private val removeItem: (RentalObject?, Int) -> Unit,
private val updateQuantity: (RentalObject, Pair<String, Int>, Int) -> Unit
) :
RecyclerView.ViewHolder(itemBinding.root) {
......@@ -71,16 +71,24 @@ class RentalItemQuantityAdapter(
fun bind(obj: RentalObject?, objComponents: MutableMap<String, Int>?) {
itemBinding.itemTitle.text = obj?.name
val storageRef =
FirebaseStorage.getInstance().reference.child("objects/round/${obj?.picture_name}.png")
GlideApp.with(itemView.context).load(storageRef)
.placeholder(R.drawable.placeholder)
.into(itemBinding.itemCircleImageView)
loadObjectPicture(obj?.picture_name)
setupRecyclerView(obj, objComponents)
itemBinding.itemDeleteButton.setOnClickListener {
removeItem.invoke(obj, objComponents, adapterPosition)
removeItem.invoke(obj, adapterPosition)
}
}
private fun loadObjectPicture(pictureName: String?) {
val path =
"${Constants.PATH_OBJ_PIC_ROUND.childName}$pictureName${Constants.PNG_EXT.childName}"
val storageRef = FirebaseStorage.getInstance().reference.child(path)
GlideApp.with(itemView.context).load(storageRef)
.placeholder(R.drawable.placeholder)
.into(itemBinding.itemCircleImageView)
}
private fun setupRecyclerView(obj: RentalObject?, objComponents: MutableMap<String, Int>?) {
itemBinding.itemQuantityRecyclerView.layoutManager =
LinearLayoutManager(itemView.context)
adapter = RentalItemQuantitySelectionAdapter(objComponents, obj, updateQuantity)
......@@ -88,7 +96,4 @@ class RentalItemQuantityAdapter(
}
}
companion object {
private const val TAG = "RentalItemQuantityAdapter"
}
}
\ No newline at end of file
......@@ -11,7 +11,7 @@ import de.asta.hochschule.trier.verleih.rental.model.RentalObject
class RentalItemQuantitySelectionAdapter(
private var objComponents: MutableMap<String, Int>?,
private var obj: RentalObject?,
private val updateQuantity: (RentalObject, Pair<String, Int>, Int, Int) -> Unit
private val updateQuantity: (RentalObject, Pair<String, Int>, Int) -> Unit
) :
RecyclerView.Adapter<RentalItemQuantitySelectionAdapter.ViewHolder>() {
......@@ -35,17 +35,9 @@ class RentalItemQuantitySelectionAdapter(
return objComponents?.size ?: 0
}
fun updateComponents(
updatedComponent: Pair<String, Int>,
position: Int
) {
objComponents?.replace(updatedComponent.first, updatedComponent.second)
notifyItemChanged(position)
}
class ViewHolder(
private val itemBinding: RowItemQuantitySelectBinding,
private val changeQuantity: (RentalObject, Pair<String, Int>, Int, Int) -> Unit
private val changeQuantity: (RentalObject, Pair<String, Int>, Int) -> Unit
) :
RecyclerView.ViewHolder(itemBinding.root) {
@SuppressLint("SetTextI18n")
......@@ -67,19 +59,19 @@ class RentalItemQuantitySelectionAdapter(
setupChips(-1, +1, null, null, component, obj, maxQuantity)
}
maxQuantity in 6..25 -> {
setupChips(-5, -1, +1, +5, component, obj, maxQuantity)
setupChips(-1, -5, +5, +1, component, obj, maxQuantity)
}
maxQuantity in 26..50 -> {
setupChips(-10, -5, +5, +10, component, obj, maxQuantity)
setupChips(-5, -10, +10, +5, component, obj, maxQuantity)
}
maxQuantity in 51..250 -> {
setupChips(-50, -10, +10, +50, component, obj, maxQuantity)
setupChips(-10, -50, +50, +10, component, obj, maxQuantity)
}
maxQuantity in 251..500 -> {
setupChips(-100, -50, +50, +100, component, obj, maxQuantity)
setupChips(-50, -100, +100, +50, component, obj, maxQuantity)
}
else -> {
setupChips(-200, -100, +100, +200, component, obj, maxQuantity)
setupChips(-100, -200, +200, +100, component, obj, maxQuantity)
}
}
}
......@@ -96,16 +88,8 @@ class RentalItemQuantitySelectionAdapter(
obj: RentalObject?,
maxQuantity: Int?
) {
itemBinding.chip3.visibility = if (val3 == null) {
View.GONE
} else {
View.VISIBLE
}
itemBinding.chip4.visibility = if (val4 == null) {
View.GONE
} else {
View.VISIBLE
}
setupChipVisibility(itemBinding.chip3, val3)
setupChipVisibility(itemBinding.chip4, val4)
itemBinding.chip1.text = "$val1"
setupChipListener(itemBinding.chip1, val1, component, obj)
......@@ -127,6 +111,14 @@ class RentalItemQuantitySelectionAdapter(
disableChips(val1, val2, val3, val4, maxQuantity, component?.second)
}
private fun setupChipVisibility(chip: Chip, value: Int?) {
chip.visibility = if (value == null) {
View.GONE
} else {
View.VISIBLE
}
}
private fun disableChips(
val1: Int,
val2: Int,
......@@ -150,7 +142,7 @@ class RentalItemQuantitySelectionAdapter(
) {
chip.setOnClickListener {
if (component != null && obj != null) {
changeQuantity(obj, component, component.second.plus(value), adapterPosition)
changeQuantity(obj, component, component.second.plus(value))
}
}
}
......
......@@ -5,13 +5,13 @@ import android.view.*
import android.widget.Toast
import androidx.fragment.app.*
import androidx.recyclerview.widget.GridLayoutManager
import com.firebase.ui.database.*
import com.firebase.ui.database.FirebaseRecyclerOptions
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.firebase.database.FirebaseDatabase
import de.asta.hochschule.trier.verleih.R
import de.asta.hochschule.trier.verleih.databinding.*
import de.asta.hochschule.trier.verleih.rental.adapter.RentalItemChoiceAdapter
import de.asta.hochschule.trier.verleih.rental.model.*
import de.asta.hochschule.trier.verleih.rental.model.RentalObject
import de.asta.hochschule.trier.verleih.rental.viewmodel.NewRentalViewModel
import de.asta.hochschule.trier.verleih.util.Constants
......@@ -51,19 +51,11 @@ class NewRentalItemsChoiceFragment : Fragment(R.layout.fragment_new_rental_items
}
private fun setupRecyclerView(bottomSheetDialog: BottomSheetDialog?) {
val parser = SnapshotParser { snapshot ->
val obj = snapshot.getValue(RentalObject::class.java)
if (obj != null) {
obj.id = snapshot.key
return@SnapshotParser obj
}
return@SnapshotParser RentalObject()
}
val query = FirebaseDatabase.getInstance().reference.child(Constants.OBJECTS.childName)
.orderByChild(Constants.NAME.childName)
val options =
FirebaseRecyclerOptions.Builder<RentalObject>()
.setQuery(query, parser)
.setQuery(query, RentalObject::class.java)
.build()
adapter =
......@@ -79,7 +71,7 @@ class NewRentalItemsChoiceFragment : Fragment(R.layout.fragment_new_rental_items
bottomSheetBinding.itemDescriptionText.text = model.description
bottomSheetDialog?.show()
})
binding.itemsRecyclerview.layoutManager = GridLayoutManager(this.context, 3)
binding.itemsRecyclerview.layoutManager = GridLayoutManager(this.context, RV_SPAN_COUNT)
binding.itemsRecyclerview.adapter = adapter
}
......@@ -93,4 +85,8 @@ class NewRentalItemsChoiceFragment : Fragment(R.layout.fragment_new_rental_items
adapter.stopListening()
}
companion object {
private const val RV_SPAN_COUNT = 3
}
}
\ No newline at end of file
......@@ -9,14 +9,13 @@ 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
import de.asta.hochschule.trier.verleih.rental.model.RentalObject
import de.asta.hochschule.trier.verleih.rental.viewmodel.NewRentalViewModel
class NewRentalItemsQuantityFragment : Fragment(R.layout.fragment_new_rental_items_quanity) {
private lateinit var binding: FragmentNewRentalItemsQuanityBinding
private val viewModel: NewRentalViewModel by activityViewModels()
private var adapter: RentalItemQuantityAdapter? = null
override fun onCreateView(
......@@ -31,42 +30,7 @@ class NewRentalItemsQuantityFragment : Fragment(R.layout.fragment_new_rental_ite
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.itemsRecyclerview.layoutManager = LinearLayoutManager(context)
adapter =
RentalItemQuantityAdapter(
viewModel.objectsLiveData.value,
viewModel.rentalObjectsLiveData.value,
{ obj, objComponents, position ->
// remove object from recyclerview only
adapter?.removeObject(obj, position)
// undo SnackBar
val snackBar = Snackbar.make(
binding.root,
"${obj?.name} ${getString(R.string.deleted)}",
Snackbar.LENGTH_LONG
)
snackBar.setAction(R.string.undo) {
// re-add object to recyclerview
adapter?.addObject(obj, 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 (obj != null) {
viewModel.removeRentalObject(obj)
}
}
super.onDismissed(transientBottomBar, event)
}
})
snackBar.show()
}
) { o, component, quantity, position ->
viewModel.updateQuantity(o, component, quantity)
}
binding.itemsRecyclerview.adapter = adapter
setupRecyclerView()
viewModel.objectsLiveData.observe(requireActivity(), { objects ->
adapter = adapter?.resetData(objects, viewModel.rentalObjectsLiveData.value)
......@@ -82,7 +46,40 @@ class NewRentalItemsQuantityFragment : Fragment(R.layout.fragment_new_rental_ite
})
}
companion object {
private const val TAG = "NewRentalItemsQuantityFragment"
private fun setupRecyclerView() {
binding.itemsRecyclerview.layoutManager = LinearLayoutManager(context)
adapter =
RentalItemQuantityAdapter(
viewModel.objectsLiveData.value,
viewModel.rentalObjectsLiveData.value,
{ obj, position ->
adapter?.removeObject(obj, position)
showUndoSnackBar(obj, position)
}
) { o, component, quantity ->
viewModel.updateQuantity(o, component, quantity)
}
binding.itemsRecyclerview.adapter = adapter
}
private fun showUndoSnackBar(obj: RentalObject?, position: Int) {
val snackBar = Snackbar.make(
binding.root,
"${obj?.name} ${getString(R.string.deleted)}",
Snackbar.LENGTH_LONG
)
snackBar.setAction(R.string.undo) {
adapter?.addObject(obj, position)
}
snackBar.addCallback(object : BaseTransientBottomBar.BaseCallback<Snackbar>() {
override fun onDismissed(transientBottomBar: Snackbar?, event: Int) {
if (event == Snackbar.Callback.DISMISS_EVENT_TIMEOUT) {
viewModel.removeRentalObject(obj)
}
super.onDismissed(transientBottomBar, event)
}
})
snackBar.show()
}
}
\ No newline at end of file
......@@ -89,14 +89,14 @@ class NewRentalViewModel : ViewModel() {
}
}
fun removeRentalObject(rentalObject: RentalObject) {
fun removeRentalObject(rentalObject: RentalObject?) {
val list = getObjectList()
list?.remove(list.find { it.name == rentalObject.name })
list?.remove(list.find { it.name == rentalObject?.name })
list?.sortBy { it.name }
mutableObjects.value = list
val rentalObjects = getRentalObjects()
rentalObjects?.remove(rentalObject.picture_name)
rentalObjects?.remove(rentalObject?.picture_name)
mutableRentalObjects.value = rentalObjects
}
......
......@@ -5,6 +5,8 @@ enum class Constants(val childName: String) {
OBJECTS("objects"),
NAME("name"),
PICTURE_NAME("picture_name"),
PATH_OBJ_PICTURE("objects/big/"),
JPG_EXT(".jpg")
PATH_OBJ_PIC_BIG("objects/big/"),
PATH_OBJ_PIC_ROUND("objects/round/"),
JPG_EXT(".jpg"),
PNG_EXT(".png")
}
\ No newline at end of file
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