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.
215 lines
9.0 KiB
Kotlin
215 lines
9.0 KiB
Kotlin
package com.cyb3rko.techniklogger.fragments
|
|
|
|
import android.annotation.SuppressLint
|
|
import android.content.Context
|
|
import android.os.Bundle
|
|
import android.text.Html
|
|
import android.text.SpannableStringBuilder
|
|
import android.view.LayoutInflater
|
|
import android.view.View
|
|
import android.view.ViewGroup
|
|
import androidx.fragment.app.Fragment
|
|
import androidx.navigation.fragment.findNavController
|
|
import androidx.navigation.fragment.navArgs
|
|
import com.cyb3rko.techniklogger.*
|
|
import com.cyb3rko.techniklogger.CURRENT_YEAR
|
|
import com.cyb3rko.techniklogger.data.objects.Mission
|
|
import com.cyb3rko.techniklogger.data.ParseController
|
|
import com.cyb3rko.techniklogger.databinding.FragmentMissionPusherBinding
|
|
import com.google.android.material.datepicker.MaterialDatePicker
|
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
|
import com.google.android.material.timepicker.MaterialTimePicker
|
|
import com.google.android.material.timepicker.TimeFormat
|
|
import com.parse.ParseObject
|
|
import java.sql.Time
|
|
import java.text.SimpleDateFormat
|
|
import java.util.*
|
|
|
|
class MissionPusherFragment : Fragment() {
|
|
private var _binding: FragmentMissionPusherBinding? = null
|
|
private lateinit var myContext: Context
|
|
private val args: MissionPusherFragmentArgs by navArgs()
|
|
|
|
private lateinit var childKey: String
|
|
private var date = ""
|
|
private lateinit var mission: Mission
|
|
private var time = ""
|
|
private lateinit var time1: Time
|
|
private lateinit var time2: Time
|
|
private var duration = 0f
|
|
private lateinit var location: String
|
|
private lateinit var name: String
|
|
@SuppressLint("SimpleDateFormat")
|
|
private val simpleDateFormat = SimpleDateFormat("dd.MM.yyyy")
|
|
|
|
// This property is only valid between onCreateView and onDestroyView.
|
|
private val binding get() = _binding!!
|
|
|
|
override fun onCreateView(
|
|
inflater: LayoutInflater,
|
|
container: ViewGroup?,
|
|
savedInstanceState: Bundle?
|
|
): View {
|
|
_binding = FragmentMissionPusherBinding.inflate(inflater, container, false)
|
|
val root = binding.root
|
|
myContext = requireContext()
|
|
|
|
getArgumentInformation()
|
|
restoreInformation()
|
|
|
|
val builder = MaterialDatePicker.Builder.datePicker()
|
|
.setTitleText("Datum")
|
|
val builder2 = MaterialTimePicker.Builder()
|
|
.setTimeFormat(TimeFormat.CLOCK_24H).setTitleText("Von")
|
|
val builder3 = MaterialTimePicker.Builder()
|
|
.setTimeFormat(TimeFormat.CLOCK_24H).setTitleText("Bis")
|
|
|
|
binding.dateButton.setOnClickListener {
|
|
if (date != "") {
|
|
val dateInMillis = simpleDateFormat.parse(date)!!.time
|
|
builder.setSelection(dateInMillis + 7200000)
|
|
}
|
|
if (time != "") {
|
|
val times = time.split(" - ")
|
|
builder2.setHour(times[0].split(":")[0].toInt())
|
|
.setMinute(times[0].split(":")[1].toInt())
|
|
builder3.setHour(times[1].split(":")[0].toInt())
|
|
.setMinute(times[1].split(":")[1].toInt())
|
|
}
|
|
var tempTime = ""
|
|
val picker = builder.build()
|
|
val picker2 = builder2.build()
|
|
val picker3 = builder3.build()
|
|
var hour: String
|
|
var minute: String
|
|
var hoursInMillis: Long
|
|
var minutesInMillis: Long
|
|
picker.addOnPositiveButtonClickListener {
|
|
date = simpleDateFormat.format(Date(it))
|
|
binding.dateView.text = Html.fromHtml("<b>Datum:</b><br/>${date}")
|
|
activity?.let { it1 -> picker2.show(it1.supportFragmentManager, picker2.tag) }
|
|
}
|
|
picker2.addOnPositiveButtonClickListener {
|
|
hour = picker2.hour.toString()
|
|
hour = if (hour.length != 1) hour else "0$hour"
|
|
minute = picker2.minute.toString()
|
|
minute = if (minute.length != 1) minute else "0$minute"
|
|
hoursInMillis = picker2.hour.toLong() * 3600000
|
|
minutesInMillis = picker2.minute.toLong() * 60000
|
|
time1 = Time(hoursInMillis + minutesInMillis)
|
|
tempTime += "${hour}:${minute} - "
|
|
activity?.let { it1 -> picker3.show(it1.supportFragmentManager, picker3.tag) }
|
|
}
|
|
picker3.addOnPositiveButtonClickListener {
|
|
hour = picker3.hour.toString()
|
|
hour = if (hour.length != 1) hour else "0$hour"
|
|
minute = picker3.minute.toString()
|
|
minute = if (minute.length != 1) minute else "0$minute"
|
|
hoursInMillis = hour.toLong() * 3600000
|
|
minutesInMillis = minute.toLong() * 60000
|
|
time2 = Time(hoursInMillis + minutesInMillis)
|
|
tempTime += "${hour}:${minute}"
|
|
time = tempTime
|
|
// duration = DecimalFormat("#.#").format((time2.time - time1.time) / 3600 / 1000.toFloat())
|
|
// duration = duration.replace(",", ".")
|
|
duration = (time2.time - time1.time) / 3600 / 1000.toFloat()
|
|
binding.durationView.text = Html.fromHtml("<b>Dauer:</b> $time Uhr, $duration Stunden")
|
|
}
|
|
activity?.let { it1 -> picker.show(it1.supportFragmentManager, picker.tag) }
|
|
}
|
|
|
|
if (childKey != "") {
|
|
binding.deleteButton.show()
|
|
binding.deleteButton.setOnClickListener {
|
|
MaterialAlertDialogBuilder(myContext)
|
|
.setTitle("Einsatz entfernen")
|
|
.setMessage("Möchtest du diesen Einsatz entfernen?")
|
|
.setPositiveButton("Ja") { _, _ ->
|
|
ParseController.fetchParticipations(childKey, false) { entries, e ->
|
|
if (e == null) {
|
|
ParseObject.deleteAllInBackground(entries) {
|
|
mission.deleteInBackground {
|
|
if (it == null) {
|
|
findNavController().navigate(R.id.navigation_listing)
|
|
} else {
|
|
showErrorToast("Löschen des Einsatzes fehlgeschlagen")
|
|
logE("Löschen des Einsatzes fehlgeschlagen")
|
|
it.printStackTrace()
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
showErrorToast("Löschen der Techniker fehlgeschlagen")
|
|
logE("Löschen der Techniker fehlgeschlagen")
|
|
e.printStackTrace()
|
|
}
|
|
}
|
|
}
|
|
.setNegativeButton("Abbrechen", null)
|
|
.show()
|
|
}
|
|
}
|
|
|
|
binding.finishedButton.setOnClickListener {
|
|
val name = binding.nameEditText.text.toString().trim()
|
|
val location = binding.locationEditText.text.toString().trim()
|
|
|
|
if (name != "" && location != "" && time != "") {
|
|
mission.setName(name)
|
|
mission.setLocation(location)
|
|
val dateTimes = date.split(".")
|
|
var dateTime = "${dateTimes[2]}.${dateTimes[1]}.${dateTimes[0]}"
|
|
if (time != "") dateTime += ",$time"
|
|
mission.setDate(dateTime)
|
|
mission.setDuration(duration)
|
|
mission.setYear(Safe.getKey(myContext, CURRENT_YEAR))
|
|
mission.saveInBackground {
|
|
if (it == null) {
|
|
findNavController().navigate(R.id.navigation_listing)
|
|
} else {
|
|
showErrorToast("Speichern des Einsatzes fehlgeschlagen")
|
|
logE("Speichern des Einsatzes fehlgeschlagen")
|
|
it.printStackTrace()
|
|
}
|
|
}
|
|
} else {
|
|
showWarningToast("Fülle zuerst alle Felder aus")
|
|
}
|
|
}
|
|
|
|
return root
|
|
}
|
|
|
|
private fun getArgumentInformation() {
|
|
childKey = args.objectId
|
|
name = args.name
|
|
location = args.location
|
|
date = args.date
|
|
time = args.time
|
|
duration = args.duration
|
|
}
|
|
|
|
private fun restoreInformation() {
|
|
if (name != "") {
|
|
binding.nameEditText.text = SpannableStringBuilder(name)
|
|
}
|
|
if (location != "") {
|
|
binding.locationEditText.text = SpannableStringBuilder(location)
|
|
}
|
|
if (date != "") {
|
|
binding.dateView.text = Html.fromHtml("<b>Datum:</b><br/>${date}")
|
|
}
|
|
if (time != "") {
|
|
binding.durationView.text = Html.fromHtml("<b>Dauer:</b> $time Uhr, $duration Stunden")
|
|
} else {
|
|
binding.durationView.text = Html.fromHtml("<b>Dauer:</b> $duration Stunden")
|
|
}
|
|
|
|
if (childKey == "") {
|
|
mission = Mission()
|
|
} else {
|
|
mission = Mission.emptyObject(childKey)
|
|
}
|
|
}
|
|
}
|