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.content.Context
20 import android.widget.AdapterView
21 import android.widget.ArrayAdapter
22 import android.widget.AutoCompleteTextView
23 import android.widget.TextView
24 import com.android.healthconnect.testapps.toolbox.R
25 import com.android.healthconnect.testapps.toolbox.utils.EnumFieldsWithValues
26 
27 @SuppressLint("ViewConstructor")
28 class EnumDropDown(
29     context: Context,
30     title: String?,
31     enumFieldsWithValues: EnumFieldsWithValues,
32 ) : InputFieldView(context) {
33 
34     private var mSelectedPosition = -1
35     private var mDropdownValues: List<String>
36     private var mEnumFieldsWithValues: EnumFieldsWithValues
37 
38     init {
39         inflate(context, R.layout.fragment_dropdown, this)
40         mDropdownValues = enumFieldsWithValues.getAllFieldNames()
41         mEnumFieldsWithValues = enumFieldsWithValues
42         val spinnerTitle = findViewById<TextView>(R.id.title)
43         spinnerTitle.text = title
44         setupSpinner()
45     }
46 
47     private fun setupSpinner() {
48         val autoCompleteTextView =
49             findViewById<AutoCompleteTextView>(R.id.enum_auto_complete_textview)
50         val dataAdapter: ArrayAdapter<Any> =
51             ArrayAdapter<Any>(context, R.layout.simple_spinner_item, mDropdownValues)
52         dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
53         autoCompleteTextView.setAdapter(dataAdapter)
54 
55         autoCompleteTextView.onItemClickListener =
56             AdapterView.OnItemClickListener { _, _, position, _ -> mSelectedPosition = position }
57     }
58 
59     override fun getFieldValue(): Any {
60         return mEnumFieldsWithValues.getFieldValue(mDropdownValues[mSelectedPosition])
61     }
62 
63     override fun isEmpty(): Boolean {
64         return mSelectedPosition == -1
65     }
66 }
67