Commit 87397130 authored by KingVanti's avatar KingVanti
Browse files

Add logic for displaying watering info

parent 283c52ca
......@@ -10,7 +10,8 @@ data class Pot(
data class Plant(
val id: Int,
val name: String
val name: String,
val wateringPreferenceDescription: String
) {
override fun toString() = name
}
\ No newline at end of file
......@@ -4,9 +4,9 @@ object PlantRepo {
val plants =
listOf(
Plant(0, "Chard"),
Plant(1, "Rosemary"),
Plant(2, "Spinach")
Plant(0, "Chard", "Keep soil evenly damp"),
Plant(1, "Rosemary", "Keep soil a little damp, but ensure good drainage"),
Plant(2, "Spinach", "Keep soil consistently damp, but not moist, at the surface")
)
}
\ No newline at end of file
......@@ -49,6 +49,7 @@ class PotDisplayFragment : QRdenFragment() {
private fun observeViewModel() {
viewModel.handleErrorCommand.observe(this, ::handleViewModelError)
viewModel.updatePotInDBCommand.observe(this, ::updatePotInDB)
viewModel.showWateringInfoCommand.observe(this, ::showWateringInfo)
}
private fun handleViewModelError(error: PotDisplayViewModel.Error) = when (error) {
......@@ -61,4 +62,7 @@ class PotDisplayFragment : QRdenFragment() {
.iterError { toast(R.string.error_update_pot_unknown) }
}
private fun showWateringInfo(preferences: String) =
TextPopup.show(preferences, requireView(), layoutInflater)
}
\ No newline at end of file
......@@ -24,6 +24,7 @@ class PotDisplayViewModel : ViewModel() {
val handleErrorCommand = Command<Error>()
val showWateringInfoCommand = Command<String>()
private val pot = MutableLiveData<Pot?>(null)
private val plant = pot.map { it?.plantId?.let { id -> PlantRepo.plants[id] } }
......@@ -55,4 +56,8 @@ class PotDisplayViewModel : ViewModel() {
pot.mapValueBack { it?.copy(lastWateredDate = LocalDateTime.now()) }
}
fun openWateringInfo() {
plant.value?.wateringPreferenceDescription?.also { showWateringInfoCommand.fire(it) }
}
}
\ No newline at end of file
package ac.at.fhstp.it211505.qrden.displayPot
import ac.at.fhstp.it211505.qrden.R
import android.annotation.SuppressLint
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.PopupWindow
import android.widget.TextView
object TextPopup {
private fun buildPopupView(text: String, inflater: LayoutInflater): View {
@SuppressLint("InflateParams")
val popup = inflater.inflate(R.layout.popup_text, null)
popup.findViewById<TextView>(R.id.popup_text).text = text
return popup
}
@SuppressLint("ClickableViewAccessibility")
private fun buildPopupWindow(popupView: View): PopupWindow {
val popupWindow = PopupWindow(
popupView,
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT,
true
)
popupView.setOnTouchListener { _, _ ->
popupWindow.dismiss()
true
}
return popupWindow
}
fun show(text: String, parent: View, inflater: LayoutInflater) =
buildPopupWindow(buildPopupView(text, inflater))
.showAtLocation(parent, Gravity.CENTER, 0, 0)
}
\ No newline at end of file
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM13,17h-2v-6h2v6zM13,9h-2L11,7h2v2z"/>
</vector>
......@@ -48,9 +48,22 @@
app:layout_constraintTop_toBottomOf="@id/label_pot_content"
tools:text="[Time since last watering]" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:background="@null"
android:contentDescription="@string/label_button_watering_info"
android:onClick="@{_ -> viewModel.openWateringInfo()}"
android:src="@drawable/ic_baseline_info_24"
app:layout_constraintBottom_toBottomOf="@id/label_time_last_watered"
app:layout_constraintStart_toEndOf="@id/label_time_last_watered"
app:layout_constraintTop_toTopOf="@id/label_time_last_watered" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="@{_ -> viewModel.waterPot()}"
android:text="@string/label_button_water_pot"
app:layout_constraintEnd_toEndOf="parent"
......
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardBackgroundColor="@color/design_default_color_primary"
app:cardCornerRadius="8dp"
app:contentPadding="10dp">
<TextView
android:id="@+id/popup_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="@color/design_default_color_on_primary"
tools:text="[Text]" />
</androidx.cardview.widget.CardView>
\ No newline at end of file
......@@ -19,4 +19,5 @@
<string name="label_button_water_pot">Water pot</string>
<string name="label_last_watered_now">This pot was watered just now</string>
<string name="error_update_pot_unknown">An unknown error occurred while updating the pot!</string>
<string name="label_button_watering_info">Watering info</string>
</resources>
\ 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