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-27 19:20:38 +01:00
|
|
|
import dagger.android.support.DaggerDialogFragment
|
2019-12-19 23:45:27 +01:00
|
|
|
import info.nightscout.androidaps.R
|
2019-12-30 00:53:44 +01:00
|
|
|
import info.nightscout.androidaps.logging.AAPSLogger
|
|
|
|
import info.nightscout.androidaps.logging.LTag
|
2019-12-18 20:59:33 +01:00
|
|
|
import info.nightscout.androidaps.utils.DateUtil
|
2019-12-30 00:53:44 +01:00
|
|
|
import info.nightscout.androidaps.utils.sharedPreferences.SP
|
2020-04-08 14:03:57 +02:00
|
|
|
import info.nightscout.androidaps.utils.extensions.toVisibility
|
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 java.util.*
|
2019-12-30 00:53:44 +01:00
|
|
|
import javax.inject.Inject
|
2019-12-18 20:59:33 +01:00
|
|
|
|
2019-12-27 19:20:38 +01:00
|
|
|
abstract class DialogFragmentWithDate : DaggerDialogFragment() {
|
2019-12-30 00:53:44 +01:00
|
|
|
@Inject lateinit var aapsLogger: AAPSLogger
|
|
|
|
@Inject lateinit var sp: SP
|
2019-12-18 20:59:33 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-12-20 23:05:35 +01:00
|
|
|
fun onCreateViewGeneral() {
|
2019-12-18 20:59:33 +01:00
|
|
|
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,
|
2019-12-20 23:05:35 +01:00
|
|
|
cal.get(Calendar.YEAR),
|
|
|
|
cal.get(Calendar.MONTH),
|
|
|
|
cal.get(Calendar.DAY_OF_MONTH)
|
2019-12-18 20:59:33 +01:00
|
|
|
).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,
|
2019-12-20 23:05:35 +01:00
|
|
|
cal.get(Calendar.HOUR_OF_DAY),
|
|
|
|
cal.get(Calendar.MINUTE),
|
|
|
|
DateFormat.is24HourFormat(context)
|
2019-12-18 20:59:33 +01:00
|
|
|
).show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-30 00:53:44 +01:00
|
|
|
notes_layout?.visibility = sp.getBoolean(R.string.key_show_notes_entry_dialogs, false).toVisibility()
|
2019-12-19 23:45:27 +01:00
|
|
|
|
2019-12-18 20:59:33 +01:00
|
|
|
ok.setOnClickListener {
|
|
|
|
synchronized(okClicked) {
|
|
|
|
if (okClicked) {
|
2019-12-30 00:53:44 +01:00
|
|
|
aapsLogger.warn(LTag.UI, "guarding: ok already clicked")
|
2019-12-18 20:59:33 +01:00
|
|
|
} else {
|
|
|
|
okClicked = true
|
2019-12-21 23:17:20 +01:00
|
|
|
if (submit()) dismiss()
|
|
|
|
else okClicked = false
|
2019-12-18 20:59:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cancel.setOnClickListener { dismiss() }
|
|
|
|
}
|
|
|
|
|
2019-12-21 23:17:20 +01:00
|
|
|
abstract fun submit(): Boolean
|
2019-12-18 20:59:33 +01:00
|
|
|
}
|