You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
4.6 KiB
Kotlin

package com.cyb3rko.techniklogger
import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.text.Html
import android.text.SpannableStringBuilder
import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.afollestad.materialdialogs.MaterialDialog
import com.google.android.material.datepicker.MaterialDatePicker
import com.google.firebase.database.DatabaseReference
import com.google.firebase.database.FirebaseDatabase
import es.dmoral.toasty.Toasty
import kotlinx.android.synthetic.main.activity_einsatz_pusher.*
import java.text.SimpleDateFormat
import java.util.*
class EinsatzPusher : AppCompatActivity() {
private lateinit var childKey: String
private lateinit var databaseReference: DatabaseReference
private lateinit var databaseReferenceNew: DatabaseReference
private var date = ""
private lateinit var location: String
private lateinit var name: String
private val simpleDateFormat = SimpleDateFormat("dd.MM.yyyy", Locale.GERMANY)
@SuppressLint("SetTextI18n")
@Suppress("DEPRECATION")
override fun onCreate(savedInstanceState: Bundle?) {
getBundleInformation()
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_einsatz_pusher)
databaseReference = FirebaseDatabase.getInstance().getReference("einsätze")
restoreInformation()
supportActionBar?.setDisplayHomeAsUpEnabled(true)
val builder = MaterialDatePicker.Builder.datePicker().setTitleText("Datum")
val picker = builder.build()
date_button.setOnClickListener {
picker.addOnPositiveButtonClickListener {
date = simpleDateFormat.format(Date(it))
date_view.text = Html.fromHtml("<b>Datum:</b><br/>${date}")
}
picker.show(supportFragmentManager, picker.tag)
}
if (childKey != "null") {
delete_button.visibility = View.VISIBLE
delete_button.setOnClickListener {
MaterialDialog(this)
.show {
message(0, "Möchtest du diesen Einsatz entfernen?")
positiveButton(0, "Ja") {
databaseReferenceNew.removeValue()
finish()
startActivity(Intent(applicationContext, MainActivity::class.java))
}
negativeButton(0, "Abbrechen")
}
}
}
finished_button.setOnClickListener {
val name = nameEditText.text.toString()
val location = locationEditText.text.toString()
if (name != "" && location != "" && date != "") {
databaseReferenceNew.child("name").setValue(name)
databaseReferenceNew.child("location").setValue(location)
databaseReferenceNew.child("date").setValue(date)
finish()
startActivity(Intent(applicationContext, MainActivity::class.java))
} else {
Toasty.error(applicationContext, "Fülle alle Felder aus").show()
}
}
}
private fun getBundleInformation() {
childKey = intent.extras?.getString("childKey").toString()
name = intent.extras?.getString("name").toString()
location = intent.extras?.getString("location").toString()
date = intent.extras?.getString("date").toString()
}
private fun restoreInformation() {
if (name != "null") {
nameEditText.text = SpannableStringBuilder(name)
}
if (location != "null") {
locationEditText.text = SpannableStringBuilder(location)
}
if (date != "null") {
date_view.text = "Datum:\n$date"
}
if (childKey == "null") {
databaseReferenceNew = databaseReference.push()
} else {
databaseReferenceNew = databaseReference.child(childKey)
}
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
when (item.itemId) {
android.R.id.home -> {
finish()
startActivity(Intent(applicationContext, MainActivity::class.java))
}
}
return super.onOptionsItemSelected(item)
}
override fun onBackPressed() {
finish()
startActivity(Intent(applicationContext, MainActivity::class.java))
}
}