Commit a4b51b0c authored by SusanneFischer's avatar SusanneFischer
Browse files

overwrite existing rental on edit, update detail view after editing

parent 358023a4
Pipeline #3602 canceled with stages
......@@ -43,7 +43,15 @@ class EditRentalActivity : FragmentActivity() {
getOnNoteEditorActionListener()
)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == EDIT_RENTAL_REQUEST_CODE) {
rental =
Gson().fromJson(data?.getStringExtra(INTENT_EXTRA_EDIT_RENTAL), Rental::class.java)
setupRecyclerViews(true)
}
}
private fun getOnNoteEditorActionListener(): TextView.OnEditorActionListener {
......@@ -151,8 +159,10 @@ class EditRentalActivity : FragmentActivity() {
const val INTENT_EXTRA_RENTAL = "Rental"
const val INTENT_EXTRA_RENTAL_OBJECTS = "Rental_Objects"
const val INTENT_EXTRA_DELETE_RENTAL = "Delete_Rental"
private const val EDIT_RENTAL_REQUEST_CODE = 1
const val INTENT_EXTRA_EDIT_RENTAL = "Edit_Rental"
const val EDIT_RENTAL_REQUEST_CODE = 1
const val DELETE_RENTAL_REQUEST_CODE = 2
private const val TAG = "EditRentalActivity"
}
}
\ No newline at end of file
......@@ -65,8 +65,12 @@ class NewRentalActivity : FragmentActivity() {
binding.pagerNextButton.setOnClickListener {
if (viewModel.validateInput(binding.newRentalPager.currentItem)) {
if (binding.newRentalPager.currentItem == PAGE_OVERVIEW) {
viewModel.saveRentalToDatabase()
finish()
viewModel.saveRentalToDatabase {
val i = intent
i.putExtra(EditRentalActivity.INTENT_EXTRA_EDIT_RENTAL, Gson().toJson(it))
setResult(EditRentalActivity.EDIT_RENTAL_REQUEST_CODE, i)
finish()
}
} else {
binding.newRentalPager.currentItem = binding.newRentalPager.currentItem + 1
}
......
package de.asta.hochschule.trier.verleih.rental.viewmodel
import android.util.Log
import androidx.lifecycle.*
import com.google.firebase.database.FirebaseDatabase
import de.asta.hochschule.trier.verleih.app.util.*
......@@ -19,12 +20,32 @@ class NewRentalViewModel : ViewModel() {
private val mutableNote = MutableLiveData<String>()
private val mutableValidPages = MutableLiveData<ArrayList<Boolean>>()
val validPagesLiveData: LiveData<ArrayList<Boolean>> get() = mutableValidPages
private var editRental = false
fun saveRentalToDatabase() {
fun saveRentalToDatabase(completeUpdate: (Rental?) -> Unit) {
val rental = compileRental()
val firebaseRef =
FirebaseDatabase.getInstance().reference.child(Constants.RENTALS.childName)
firebaseRef.push().setValue(rental)
if (editRental) {
val childId = rental?.id
if (childId != null) {
val rentalUpdate = mutableMapOf<String, Rental>()
rental.id = null
rentalUpdate[childId] = rental
firebaseRef.updateChildren(rentalUpdate.toMap()) { error, _ ->
if (error != null) {
Log.e(TAG, error.message)
} else {
rental.id = childId
completeUpdate.invoke(rental)
}
}
}
} else {
firebaseRef.push().setValue(rental)
completeUpdate.invoke(rental)
}
}
private fun compileRental(): Rental? {
......@@ -159,6 +180,7 @@ class NewRentalViewModel : ViewModel() {
mutableRental.value = rental
mutableRentalObjects.value = rental.objects
mutableObjects.value = rentalObjects
editRental = true
}
fun validateInput(currentPage: Int): Boolean {
......@@ -198,4 +220,8 @@ class NewRentalViewModel : ViewModel() {
return validItems
}
companion object {
private const val TAG = "NewRentalViewModel"
}
}
\ 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