Commit 5c4ffccd authored by KingVanti's avatar KingVanti
Browse files

Add logic for deleting pots

parent 87397130
......@@ -36,6 +36,9 @@ interface PotDao {
@Update
fun update(entity: PotEntity)
@Query("Delete from pot where id = :id")
fun deleteById(id: Int)
private fun tryParseDateTime(text: String) =
try {
LocalDateTime.parse(text, dateTimeFormatter)
......@@ -83,4 +86,12 @@ interface PotDao {
)
}
suspend fun tryDeletePotWithId(potId: PotId) =
doDBOp {
runForResult(
{ deleteById(potId.hash) },
{ Error.makeUnknownError() }
)
}
}
\ No newline at end of file
......@@ -11,6 +11,7 @@ import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.findNavController
import androidx.navigation.fragment.navArgs
import kotlinx.coroutines.launch
......@@ -50,6 +51,7 @@ class PotDisplayFragment : QRdenFragment() {
viewModel.handleErrorCommand.observe(this, ::handleViewModelError)
viewModel.updatePotInDBCommand.observe(this, ::updatePotInDB)
viewModel.showWateringInfoCommand.observe(this, ::showWateringInfo)
viewModel.deletePotCommand.observe(this, ::deletePotWithId)
}
private fun handleViewModelError(error: PotDisplayViewModel.Error) = when (error) {
......@@ -65,4 +67,17 @@ class PotDisplayFragment : QRdenFragment() {
private fun showWateringInfo(preferences: String) =
TextPopup.show(preferences, requireView(), layoutInflater)
private fun deletePotWithId(potId: PotId) =
lifecycleScope.launch {
potDao.tryDeletePotWithId(potId)
.iterBoth(
{ closeDisplay() },
{ toast(R.string.error_delete_pot) }
)
}
private fun closeDisplay() {
requireView().findNavController().navigate(R.id.action_return_home)
}
}
\ No newline at end of file
......@@ -25,6 +25,7 @@ class PotDisplayViewModel : ViewModel() {
val handleErrorCommand = Command<Error>()
val showWateringInfoCommand = Command<String>()
val deletePotCommand = Command<PotId>()
private val pot = MutableLiveData<Pot?>(null)
private val plant = pot.map { it?.plantId?.let { id -> PlantRepo.plants[id] } }
......@@ -60,4 +61,8 @@ class PotDisplayViewModel : ViewModel() {
plant.value?.wateringPreferenceDescription?.also { showWateringInfoCommand.fire(it) }
}
fun deletePot() {
pot.value?.id.doIfNotNull { deletePotCommand.fire(it) }
}
}
\ No newline at end of file
......@@ -70,6 +70,16 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/label_time_last_watered" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:onClick="@{_ -> viewModel.deletePot()}"
android:text="@string/label_button_delete_pot"
android:textSize="11sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
......
......@@ -41,4 +41,8 @@
android:id="@+id/action_open_pot_display"
app:destination="@id/fragment_pot_display" />
<action
android:id="@+id/action_return_home"
app:destination="@id/fragment_home" />
</navigation>
\ No newline at end of file
......@@ -20,4 +20,6 @@
<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>
<string name="label_button_delete_pot">Delete pot</string>
<string name="error_delete_pot">There was an error when deleting the pot!</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