2019-12-20 18:55:54 +01:00
|
|
|
package info.nightscout.androidaps.dialogs
|
2019-12-18 20:59:33 +01:00
|
|
|
|
|
|
|
import android.app.DatePickerDialog
|
|
|
|
import android.app.TimePickerDialog
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.text.format.DateFormat
|
2019-12-19 23:45:27 +01:00
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import android.view.Window
|
|
|
|
import android.view.WindowManager
|
2019-12-18 20:59:33 +01:00
|
|
|
import androidx.fragment.app.DialogFragment
|
2019-12-19 23:45:27 +01:00
|
|
|
import info.nightscout.androidaps.R
|
2019-12-18 20:59:33 +01:00
|
|
|
import info.nightscout.androidaps.utils.DateUtil
|
2019-12-19 23:45:27 +01:00
|
|
|
import info.nightscout.androidaps.utils.SP
|
2019-12-18 20:59:33 +01:00
|
|
|
import kotlinx.android.synthetic.main.datetime.*
|
2019-12-19 23:45:27 +01:00
|
|
|
import kotlinx.android.synthetic.main.notes.*
|
2019-12-18 20:59:33 +01:00
|
|
|
import kotlinx.android.synthetic.main.okcancel.*
|
|
|
|
import org.slf4j.LoggerFactory
|
|
|
|
import java.util.*
|
|
|
|
|
|
|
|
abstract class DialogFragmentWithDate : DialogFragment() {
|
|
|
|
private val log = LoggerFactory.getLogger(DialogFragmentWithDate::class.java)
|
|
|
|
|
|
|
|
var eventTime = DateUtil.now()
|
|
|
|
var eventTimeChanged = false
|
|
|
|
|
|
|
|
//one shot guards
|
|
|
|
private var okClicked: Boolean = false
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
private var seconds: Int = (Math.random() * 59.0).toInt()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onStart() {
|
|
|
|
super.onStart()
|
|
|
|
dialog?.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onSaveInstanceState(savedInstanceState: Bundle) {
|
|
|
|
super.onSaveInstanceState(savedInstanceState)
|
|
|
|
savedInstanceState.putLong("eventTime", eventTime)
|
|
|
|
savedInstanceState.putBoolean("eventTimeChanged", eventTimeChanged)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun onCreateView() {
|
|
|
|
dialog?.window?.requestFeature(Window.FEATURE_NO_TITLE)
|
|
|
|
dialog?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
|
|
|
|
isCancelable = true
|
|
|
|
dialog?.setCanceledOnTouchOutside(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
eventTime = savedInstanceState?.getLong("eventTime") ?: DateUtil.now()
|
|
|
|
eventTimeChanged = savedInstanceState?.getBoolean("eventTimeChanged") ?: false
|
2019-12-20 11:43:21 +01:00
|
|
|
overview_eventdate?.text = DateUtil.dateString(eventTime)
|
|
|
|
overview_eventtime?.text = DateUtil.timeString(eventTime)
|
2019-12-18 20:59:33 +01:00
|
|
|
|
|
|
|
// create an OnDateSetListener
|
|
|
|
val dateSetListener = DatePickerDialog.OnDateSetListener { _, year, monthOfYear, dayOfMonth ->
|
|
|
|
val cal = Calendar.getInstance()
|
|
|
|
cal.timeInMillis = eventTime
|
|
|
|
cal.set(Calendar.YEAR, year)
|
|
|
|
cal.set(Calendar.MONTH, monthOfYear)
|
|
|
|
cal.set(Calendar.DAY_OF_MONTH, dayOfMonth)
|
|
|
|
eventTime = cal.timeInMillis
|
|
|
|
eventTimeChanged = true
|
2019-12-20 11:43:21 +01:00
|
|
|
overview_eventdate?.text = DateUtil.dateString(eventTime)
|
2019-12-18 20:59:33 +01:00
|
|
|
}
|
|
|
|
|
2019-12-20 11:43:21 +01:00
|
|
|
overview_eventdate?.setOnClickListener {
|
2019-12-18 20:59:33 +01:00
|
|
|
context?.let {
|
|
|
|
val cal = Calendar.getInstance()
|
|
|
|
cal.timeInMillis = eventTime
|
|
|
|
DatePickerDialog(it, dateSetListener,
|
|
|
|
cal.get(Calendar.YEAR),
|
|
|
|
cal.get(Calendar.MONTH),
|
|
|
|
cal.get(Calendar.DAY_OF_MONTH)
|
|
|
|
).show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create an OnTimeSetListener
|
|
|
|
val timeSetListener = TimePickerDialog.OnTimeSetListener { _, hour, minute ->
|
|
|
|
val cal = Calendar.getInstance()
|
|
|
|
cal.timeInMillis = eventTime
|
|
|
|
cal.set(Calendar.HOUR_OF_DAY, hour)
|
|
|
|
cal.set(Calendar.MINUTE, minute)
|
|
|
|
cal.set(Calendar.SECOND, seconds++) // randomize seconds to prevent creating record of the same time, if user choose time manually
|
|
|
|
eventTime = cal.timeInMillis
|
|
|
|
eventTimeChanged = true
|
2019-12-20 11:43:21 +01:00
|
|
|
overview_eventtime?.text = DateUtil.timeString(eventTime)
|
2019-12-18 20:59:33 +01:00
|
|
|
}
|
|
|
|
|
2019-12-20 11:43:21 +01:00
|
|
|
overview_eventtime?.setOnClickListener {
|
2019-12-18 20:59:33 +01:00
|
|
|
context?.let {
|
|
|
|
val cal = Calendar.getInstance()
|
|
|
|
cal.timeInMillis = eventTime
|
|
|
|
TimePickerDialog(it, timeSetListener,
|
|
|
|
cal.get(Calendar.HOUR_OF_DAY),
|
|
|
|
cal.get(Calendar.MINUTE),
|
|
|
|
DateFormat.is24HourFormat(context)
|
|
|
|
).show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-19 23:45:27 +01:00
|
|
|
notes_layout?.visibility = if (SP.getBoolean(R.string.key_show_notes_entry_dialogs, false)) View.VISIBLE else View.GONE
|
|
|
|
|
2019-12-18 20:59:33 +01:00
|
|
|
ok.setOnClickListener {
|
|
|
|
synchronized(okClicked) {
|
|
|
|
if (okClicked) {
|
|
|
|
log.debug("guarding: ok already clicked")
|
|
|
|
} else {
|
|
|
|
okClicked = true
|
|
|
|
submit()
|
|
|
|
}
|
|
|
|
dismiss()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cancel.setOnClickListener { dismiss() }
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract fun submit()
|
|
|
|
}
|