1 /**
<lambda>null2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * ```
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * ```
10  *
11  * Unless required by applicable law or agreed to in writing, software distributed under the License
12  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13  * or implied. See the License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 package com.android.healthconnect.testapps.toolbox.fieldviews
17 
18 import android.annotation.SuppressLint
19 import android.app.DatePickerDialog
20 import android.app.TimePickerDialog
21 import android.content.Context
22 import android.text.InputType
23 import android.widget.DatePicker
24 import android.widget.EditText
25 import android.widget.TextView
26 import android.widget.TimePicker
27 import com.android.healthconnect.testapps.toolbox.R
28 import java.time.Instant
29 import java.time.LocalDate
30 import java.time.LocalDateTime
31 import java.time.LocalTime
32 import java.time.ZoneId
33 import java.util.Calendar
34 
35 @SuppressLint("ViewConstructor")
36 class DateTimePicker(context: Context, fieldName: String, setPreviousDay: Boolean = false) :
37     InputFieldView(context) {
38 
39     private val mCalendar: Calendar = Calendar.getInstance()
40     private var mSelectedYear: Int = mCalendar.get(Calendar.YEAR)
41     private var mSelectedMonth: Int = mCalendar.get(Calendar.MONTH) + 1
42     private var mSelectedDay: Int = mCalendar.get(Calendar.DATE) + (if (setPreviousDay) -1 else 0)
43     private var mSelectedHour = 0
44     private var mSelectedMinute = 0
45 
46     init {
47         inflate(context, R.layout.date_time_picker, this)
48         findViewById<TextView>(R.id.title).text = fieldName
49         setupDate()
50         setupTime()
51     }
52 
53     private fun setupDate() {
54         findViewById<EditText>(R.id.select_date).let { date ->
55             date.setText(getDateString())
56             date.inputType = InputType.TYPE_NULL
57             date.setOnClickListener { showDatePicker(date) }
58         }
59     }
60 
61     private fun setupTime() {
62         findViewById<EditText>(R.id.select_time).let { time ->
63             time.setText(getTimeString())
64             time.inputType = InputType.TYPE_NULL
65             time.setOnClickListener { showTimePicker(time) }
66         }
67     }
68 
69     private fun getDateString(): String {
70         return "$mSelectedDay/$mSelectedMonth/$mSelectedYear"
71     }
72 
73     private fun getTimeString(): String {
74         return (((if (mSelectedHour < 10) "0" else "") + mSelectedHour) +
75             (if (mSelectedMinute < 10) ":0" else ":") +
76             mSelectedMinute)
77     }
78 
79     private fun showDatePicker(text: EditText) {
80         val picker =
81             DatePickerDialog(
82                 context,
83                 { _: DatePicker?, year: Int, month: Int, dayOfMonth: Int ->
84                     mSelectedYear = year
85                     mSelectedMonth = month + 1
86                     mSelectedDay = dayOfMonth
87                     text.setText(getDateString())
88                 },
89                 mSelectedYear,
90                 mSelectedMonth - 1,
91                 mSelectedDay)
92         picker.show()
93     }
94 
95     private fun showTimePicker(text: EditText) {
96         val picker =
97             TimePickerDialog(
98                 context,
99                 { _: TimePicker?, hourOfDay: Int, minute: Int ->
100                     mSelectedHour = hourOfDay
101                     mSelectedMinute = minute
102                     text.setText(getTimeString())
103                 },
104                 mSelectedHour,
105                 mSelectedMinute,
106                 true)
107         picker.show()
108     }
109 
110     override fun getFieldValue(): Instant {
111         val systemZoneId: String = ZoneId.systemDefault().id
112         val localDate: LocalDate = LocalDate.of(mSelectedYear, mSelectedMonth, mSelectedDay)
113         val localTime: LocalTime = LocalTime.of(mSelectedHour, mSelectedMinute)
114         return LocalDateTime.of(localDate, localTime).atZone(ZoneId.of(systemZoneId)).toInstant()
115     }
116 
117     override fun isEmpty(): Boolean {
118         return false
119     }
120 }
121