Commit 1cc2ee4d authored by SusanneFischer's avatar SusanneFischer
Browse files

delete rental

parent 1c7f701e
package de.asta.hochschule.trier.verleih.app.activity
import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.fragment.findNavController
import androidx.navigation.ui.NavigationUI
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.android.material.snackbar.Snackbar
import com.google.gson.Gson
import de.asta.hochschule.trier.verleih.R
import de.asta.hochschule.trier.verleih.app.viewmodel.MainViewModel
import de.asta.hochschule.trier.verleih.databinding.ActivityMainBinding
import de.asta.hochschule.trier.verleih.rental.model.Rental
import de.asta.hochschule.trier.verleih.rental.view.EditRentalActivity
class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
private lateinit var binding: ActivityMainBinding
val viewModel: MainViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
......@@ -23,7 +32,33 @@ class MainActivity : AppCompatActivity() {
if (navController != null) {
NavigationUI.setupWithNavController(bottomNavigationView, navController)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == EditRentalActivity.DELETE_RENTAL_REQUEST_CODE) {
if (resultCode == EditRentalActivity.DELETE_RENTAL_REQUEST_CODE && data != null) {
val rental = Gson().fromJson(
data.getStringExtra(EditRentalActivity.INTENT_EXTRA_DELETE_RENTAL),
Rental::class.java
)
viewModel.deleteRental(rental)
val snackBar = Snackbar.make(
binding.root,
"${rental.eventname} ${getString(R.string.deleted)}",
Snackbar.LENGTH_LONG
)
snackBar.setAction(R.string.undo) {
viewModel.reAddRental(rental)
}
snackBar.show()
}
}
}
companion object {
private const val TAG = "MainActivty"
}
}
\ No newline at end of file
package de.asta.hochschule.trier.verleih.app.viewmodel
import androidx.lifecycle.ViewModel
import com.google.firebase.database.FirebaseDatabase
import de.asta.hochschule.trier.verleih.rental.model.Rental
class MainViewModel : ViewModel() {
fun deleteRental(rental: Rental) {
rental.id?.let {
FirebaseDatabase.getInstance().reference.child("rentals").child(it).removeValue()
}
}
fun reAddRental(rental: Rental) {
rental.id = null
val firebaseRef = FirebaseDatabase.getInstance().reference.child("rentals")
firebaseRef.push().setValue(rental)
}
}
\ No newline at end of file
......@@ -45,7 +45,7 @@ class RentalMainListAdapter(
holder.itemView.setOnClickListener {
val intent = Intent(context, EditRentalActivity::class.java)
intent.putExtra(EditRentalActivity.INTENT_EXTRA_RENTAL, Gson().toJson(model))
context.startActivity(intent)
context.startActivityForResult(intent, EditRentalActivity.DELETE_RENTAL_REQUEST_CODE)
}
}
......
package de.asta.hochschule.trier.verleih.rental.model
class Rental() {
var id: String? = null
var timestamp: String? = null
var pickupdate: String? = null
var returndate: String? = null
......
......@@ -6,20 +6,19 @@ import android.view.*
import androidx.activity.viewModels
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.snackbar.*
import com.google.gson.Gson
import de.asta.hochschule.trier.verleih.R
import de.asta.hochschule.trier.verleih.databinding.ActivityEditRentalBinding
import de.asta.hochschule.trier.verleih.helper.DateHelper
import de.asta.hochschule.trier.verleih.rental.adapter.*
import de.asta.hochschule.trier.verleih.rental.model.Rental
import de.asta.hochschule.trier.verleih.rental.model.*
import de.asta.hochschule.trier.verleih.rental.viewmodel.EditRentalViewModel
class EditRentalActivity : FragmentActivity() {
private lateinit var binding: ActivityEditRentalBinding
private var rental: Rental? = null
private val viewModel: EditRentalViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
......@@ -27,7 +26,7 @@ class EditRentalActivity : FragmentActivity() {
binding = ActivityEditRentalBinding.inflate(layoutInflater)
setContentView(binding.root)
rental = Gson().fromJson(intent.getStringExtra(INTENT_EXTRA_RENTAL), Rental::class.java)
val rental = Gson().fromJson(intent.getStringExtra(INTENT_EXTRA_RENTAL), Rental::class.java)
binding.appbar.setNavigationOnClickListener {
onBackPressed()
......@@ -61,15 +60,11 @@ class EditRentalActivity : FragmentActivity() {
binding.appbar.setOnMenuItemClickListener { item ->
when (item.itemId) {
R.id.appbar_edit -> {
val intent = Intent(this, NewRentalActivity::class.java)
intent.putExtra(INTENT_EXTRA_RENTAL, Gson().toJson(rental))
intent.putExtra(INTENT_EXTRA_RENTAL_OBJECTS, Gson().toJson(it))
this.startActivityForResult(intent, EDIT_RENTAL_REQUEST_CODE)
editRental(rental, it)
return@setOnMenuItemClickListener true
}
R.id.appbar_delete -> {
// TODO
finish()
deleteRental(rental)
return@setOnMenuItemClickListener true
}
else -> return@setOnMenuItemClickListener false
......@@ -87,13 +82,28 @@ class EditRentalActivity : FragmentActivity() {
binding.rentalContainer.editItemsButton.visibility = View.INVISIBLE
binding.rentalContainer.editInformationButton.visibility = View.INVISIBLE
}
private fun editRental(rental: Rental, rentalObjects: ArrayList<RentalObject>) {
val intent = Intent(this, NewRentalActivity::class.java)
intent.putExtra(INTENT_EXTRA_RENTAL, Gson().toJson(rental))
intent.putExtra(INTENT_EXTRA_RENTAL_OBJECTS, Gson().toJson(rentalObjects))
this.startActivityForResult(intent, EDIT_RENTAL_REQUEST_CODE)
}
private fun deleteRental(rental: Rental) {
val i = intent
i.putExtra(INTENT_EXTRA_DELETE_RENTAL, Gson().toJson(rental))
setResult(DELETE_RENTAL_REQUEST_CODE, i)
finish()
}
companion object {
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 TAG = "EditRentalActivity"
private const val EDIT_RENTAL_REQUEST_CODE = 1
const val DELETE_RENTAL_REQUEST_CODE = 2
}
}
\ No newline at end of file
......@@ -5,16 +5,14 @@ import android.os.Bundle
import android.view.*
import android.widget.*
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.*
import com.firebase.ui.database.FirebaseRecyclerOptions
import com.firebase.ui.database.*
import com.google.firebase.database.FirebaseDatabase
import de.asta.hochschule.trier.verleih.R
import de.asta.hochschule.trier.verleih.databinding.FragmentRentalMainBinding
import de.asta.hochschule.trier.verleih.helper.DateHelper
import de.asta.hochschule.trier.verleih.rental.adapter.RentalMainListAdapter
import de.asta.hochschule.trier.verleih.rental.model.Rental
import de.asta.hochschule.trier.verleih.rental.viewmodel.RentalMainViewModel
import org.joda.time.DateTime
class RentalMainFragment : Fragment(R.layout.fragment_rental_main) {
......@@ -29,10 +27,6 @@ class RentalMainFragment : Fragment(R.layout.fragment_rental_main) {
private var recentRentalsIsExpanded = true
private var pastRentalsIsExpanded = true
private val viewModel: RentalMainViewModel by lazy {
ViewModelProvider(this).get(RentalMainViewModel::class.java)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
......@@ -50,12 +44,20 @@ class RentalMainFragment : Fragment(R.layout.fragment_rental_main) {
startActivity(intent)
}
val parser = SnapshotParser { snapshot ->
val rental = snapshot.getValue(Rental::class.java)
if (rental != null) {
rental.id = snapshot.key
return@SnapshotParser rental
}
return@SnapshotParser Rental()
}
val queryRecent =
FirebaseDatabase.getInstance().reference.child("rentals").orderByChild("returndate")
.startAt(DateTime.now().toString(DateHelper.TIMESTAMP_FORMAT))
val optionsRecent =
FirebaseRecyclerOptions.Builder<Rental>().setQuery(queryRecent, Rental::class.java)
.build()
FirebaseRecyclerOptions.Builder<Rental>().setQuery(queryRecent, parser).build()
recentRentalsAdapter =
RentalMainListAdapter(requireActivity(), optionsRecent) { showEmptyState ->
recentRentalsIsEmpty = showEmptyState
......@@ -73,7 +75,7 @@ class RentalMainFragment : Fragment(R.layout.fragment_rental_main) {
FirebaseDatabase.getInstance().reference.child("rentals").orderByChild("returndate")
.endAt(DateTime.now().toString(DateHelper.TIMESTAMP_FORMAT))
val optionsPast =
FirebaseRecyclerOptions.Builder<Rental>().setQuery(queryPast, Rental::class.java)
FirebaseRecyclerOptions.Builder<Rental>().setQuery(queryPast, parser)
.build()
pastRentalsAdapter =
RentalMainListAdapter(requireActivity(), optionsPast) { showEmptyState ->
......
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