Commit 33629251 authored by SusanneFischer's avatar SusanneFischer
Browse files

fixed item view, select/deselect items and add/remove them in ViewModel, show info bottom sheet

parent f978b849
Pipeline #3591 failed with stages
in 0 seconds
......@@ -9,10 +9,11 @@ import de.asta.hochschule.trier.verleih.R
import de.asta.hochschule.trier.verleih.databinding.RowItemChoiceBinding
import de.asta.hochschule.trier.verleih.rental.model.RentalObject
import de.asta.hochschule.trier.verleih.util.GlideApp
import java.util.*
class RentalItemChoiceAdapter(
options: FirebaseRecyclerOptions<RentalObject>
options: FirebaseRecyclerOptions<RentalObject>,
private val showBottomSheetDialog: (RentalObject) -> Unit,
private val selectItem: (RentalObject, Boolean) -> Unit
) : FirebaseRecyclerAdapter<
RentalObject, RentalItemChoiceAdapter.ViewHolder>(options) {
......@@ -26,21 +27,45 @@ class RentalItemChoiceAdapter(
}
override fun onBindViewHolder(holder: ViewHolder, position: Int, model: RentalObject) {
holder.itemBinding.itemNameText.text = model.name?.capitalize(Locale.getDefault())
holder.itemBinding.itemNameText.text = model.name
val width = holder.itemView.context.resources.displayMetrics.widthPixels / 3
val height = holder.itemView.context.resources.displayMetrics.heightPixels / 3
holder.itemBinding.itemImageView.layoutParams = ConstraintLayout.LayoutParams(width, height)
val size = (holder.itemView.context.resources.displayMetrics.widthPixels / 3) - (4 * 8)
holder.itemBinding.itemImageView.layoutParams = ConstraintLayout.LayoutParams(size, size)
val storageRef =
FirebaseStorage.getInstance().reference.child("objects/big/${model.picture_name}.jpg")
FirebaseStorage.getInstance().reference.child("objects/big/${model.picture_name}")
GlideApp.with(holder.itemView.context).load(storageRef)
.placeholder(R.drawable.placeholder)
.into(holder.itemBinding.itemImageView)
holder.itemBinding.itemImageView.setOnClickListener {
holder.isSelected = !holder.isSelected
if (holder.isSelected) {
selectItem.invoke(model, true)
holder.itemBinding.itemChoiceCard.setCardBackgroundColor(
holder.itemView.context.getColor(
R.color.colorSecondaryLight
)
)
} else {
selectItem.invoke(model, false)
holder.itemBinding.itemChoiceCard.setCardBackgroundColor(
holder.itemView.context.getColor(
R.color.surface
)
)
}
}
holder.itemBinding.itemInformationButton.setOnClickListener {
showBottomSheetDialog.invoke(model)
}
}
class ViewHolder(val itemBinding: RowItemChoiceBinding) :
RecyclerView.ViewHolder(itemBinding.root)
RecyclerView.ViewHolder(itemBinding.root) {
var isSelected = false
}
companion object {
private const val TAG = "RentalMainListAdapter"
......
......@@ -6,7 +6,7 @@ class Rental() {
var returndate: String? = null
var eventname: String? = null
var status: String? = null
var objects: Map<String, Map<String, Int>>? = null
var objects: MutableMap<String, MutableMap<String, Int>?>? = null
constructor(
timestamp: String?,
......@@ -14,7 +14,7 @@ class Rental() {
returndate: String?,
eventname: String?,
status: String?,
objects: Map<String, Map<String, Int>>?
objects: MutableMap<String, MutableMap<String, Int>?>?
) : this() {
this.timestamp = timestamp
this.pickupdate = pickupdate
......
......@@ -6,14 +6,14 @@ class RentalObject() {
var description: String? = null
var picture_name: String? = null
var quantity: Int? = null
var components: Map<String, Map<String, Any>>? = null
var components: MutableMap<String, MutableMap<String, Any>>? = null
constructor(
name: String?,
description: String?,
picture_name: String?,
quantity: Int?,
components: Map<String, Map<String, Any>>?
components: MutableMap<String, MutableMap<String, Any>>?
) : this() {
this.name = name
this.description = description
......
......@@ -2,20 +2,26 @@ package de.asta.hochschule.trier.verleih.rental.view
import android.os.Bundle
import android.view.*
import androidx.fragment.app.Fragment
import androidx.fragment.app.*
import androidx.recyclerview.widget.GridLayoutManager
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.FragmentNewRentalItemsChoiceBinding
import de.asta.hochschule.trier.verleih.databinding.*
import de.asta.hochschule.trier.verleih.rental.adapter.RentalItemChoiceAdapter
import de.asta.hochschule.trier.verleih.rental.model.RentalObject
import de.asta.hochschule.trier.verleih.rental.viewmodel.NewRentalViewModel
class NewRentalItemsChoiceFragment : Fragment(R.layout.fragment_new_rental_items_choice) {
private lateinit var binding: FragmentNewRentalItemsChoiceBinding
private lateinit var bottomSheetBinding: BottomSheetItemInfoBinding
private lateinit var adapter: RentalItemChoiceAdapter
private var bottomSheetDialog: BottomSheetDialog? = null
private val viewModel: NewRentalViewModel by activityViewModels()
override fun onCreateView(
inflater: LayoutInflater,
......@@ -23,19 +29,33 @@ class NewRentalItemsChoiceFragment : Fragment(R.layout.fragment_new_rental_items
savedInstanceState: Bundle?
): View {
binding = FragmentNewRentalItemsChoiceBinding.inflate(layoutInflater)
bottomSheetBinding = BottomSheetItemInfoBinding.inflate(layoutInflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
bottomSheetDialog = context?.let { BottomSheetDialog(it) }
bottomSheetDialog?.setContentView(bottomSheetBinding.root)
val query =
FirebaseDatabase.getInstance().reference.child("objects")//.orderByChild("name").startAt("A")
val options =
FirebaseRecyclerOptions.Builder<RentalObject>()
.setQuery(query, RentalObject::class.java)
.build()
adapter = RentalItemChoiceAdapter(options)
adapter = RentalItemChoiceAdapter(options, { model ->
bottomSheetBinding.itemTitleText.text = model.name
bottomSheetBinding.itemDescriptionText.text = model.description
bottomSheetDialog?.show()
}, { model, isSelected ->
if (isSelected) {
viewModel.addRentalObject(model)
} else {
viewModel.removeRentalObject(model)
}
})
binding.itemsRecyclerview.layoutManager = GridLayoutManager(this.context, 3)
binding.itemsRecyclerview.adapter = adapter
......
package de.asta.hochschule.trier.verleih.rental.viewmodel
import android.util.Log
import androidx.lifecycle.*
import com.google.gson.Gson
import de.asta.hochschule.trier.verleih.helper.DateHelper
import de.asta.hochschule.trier.verleih.rental.model.Rental
import de.asta.hochschule.trier.verleih.rental.model.*
import org.joda.time.DateTime
class NewRentalViewModel : ViewModel() {
......@@ -10,6 +12,25 @@ class NewRentalViewModel : ViewModel() {
private val mutableRental = MutableLiveData<Rental>()
val rentalLiveData: LiveData<Rental> get() = mutableRental
fun addRentalObject(rentalObject: RentalObject) {
val rental = getRental()
if (rental?.objects == null) {
rental?.objects = mutableMapOf()
}
rentalObject.name?.let { rental?.objects?.put(it, mutableMapOf()) }
mutableRental.value = rental
Log.d(TAG, Gson().toJson(mutableRental.value))
}
fun removeRentalObject(rentalObject: RentalObject) {
val rental = getRental()
rentalObject.name?.let { rental?.objects?.remove(it) }
mutableRental.value = rental
Log.d(TAG, Gson().toJson(mutableRental.value))
}
fun enterEventTitle(text: String) {
val rental = getRental()
rental?.eventname = text
......@@ -35,4 +56,8 @@ class NewRentalViewModel : ViewModel() {
Rental()
}
}
companion object {
private const val TAG = "NewRentalViewModel"
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/item_info_icon"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginStart="16dp"
android:src="@drawable/ic_info"
app:layout_constraintBottom_toBottomOf="@id/item_title_text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/item_title_text" />
<TextView
android:id="@+id/item_title_text"
style="@style/Headline6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:ellipsize="end"
android:maxLines="1"
android:text="Gegenstand"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/item_info_icon"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/item_description_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="Beschreibungstext"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/item_title_text" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
......@@ -13,13 +13,13 @@
<ImageView
android:id="@+id/item_image_view"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/placeholder"
android:scaleType="centerInside"
android:clickable="true"
android:focusable="true"
android:padding="8dp"
app:layout_constraintBottom_toTopOf="@id/item_footer_layout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
......@@ -28,7 +28,7 @@
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/item_footer_layout"
android:layout_width="0dp"
android:layout_height="42dp"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
......@@ -38,23 +38,26 @@
android:id="@+id/item_name_text"
style="@style/Caption"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:ellipsize="end"
android:gravity="center_vertical|start"
android:maxLines="1"
android:text="Gegenstand"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/item_information_button"
app:layout_constraintStart_toStartOf="parent" />
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="@+id/item_information_button"
android:layout_width="42dp"
android:layout_width="32dp"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:padding="12dp"
android:padding="4dp"
android:scaleType="fitCenter"
android:src="@drawable/ic_info"
app:layout_constraintBottom_toBottomOf="parent"
......
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