-
Notifications
You must be signed in to change notification settings - Fork 4
/
MainActivity.kt
131 lines (107 loc) · 3.65 KB
/
MainActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.example.forms
import android.os.Bundle
import android.view.View.VISIBLE
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatButton
import androidx.appcompat.widget.AppCompatEditText
import androidx.core.widget.doOnTextChanged
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.ViewModelProvider
import com.example.forms.data.Gender
import com.nwagu.forms.utils.observe
import kotlinx.coroutines.launch
class MainActivity : AppCompatActivity() {
private lateinit var nameEdit: AppCompatEditText
private lateinit var postalCodeEdit: AppCompatEditText
private lateinit var genderSelect: RadioGroup
private lateinit var submitBtn: AppCompatButton
lateinit var viewModel: MainViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
viewModel = ViewModelProvider(this).get(MainViewModel::class.java)
initializeViews()
setupViews()
}
private fun initializeViews() {
nameEdit = findViewById(R.id.nameEdit)
postalCodeEdit = findViewById(R.id.postalCodeEdit)
genderSelect = findViewById(R.id.genderRadioGroup)
submitBtn = findViewById(R.id.submitBtn)
}
private fun setupViews() {
attachToFormFields()
observeFormFields()
nameEdit.setOnFocusChangeListener { v, hasFocus ->
if (!hasFocus)
viewModel.name.errorReportingEnabled = true
}
postalCodeEdit.setOnFocusChangeListener { v, hasFocus ->
if (!hasFocus)
viewModel.postalCode.errorReportingEnabled = true
}
submitBtn.setOnClickListener {
viewModel.submit()
}
// Activate submit button when form is verified complete
lifecycleScope.launch {
viewModel.form.isComplete.collect {
submitBtn.isActivated = it
}
}
}
private fun attachToFormFields() {
nameEdit.doOnTextChanged { text, _, _, _ ->
viewModel.name.value = text?.toString()
}
postalCodeEdit.doOnTextChanged { text, _, _, _ ->
viewModel.postalCode.value = text?.toString()
}
genderSelect.setOnCheckedChangeListener { group, checkedId ->
run {
viewModel.gender.value = (Gender.valueOf(group.findViewById<RadioButton>(checkedId).tag.toString()))
}
}
}
private fun observeFormFields() {
viewModel.name.observe(
lifecycleScope,
onFeedback = {
//
},
onError = {
nameEdit.error = it
},
onFocusRequest = {
// bring nameEdit to focus
}
)
viewModel.postalCode.observe(
lifecycleScope,
onFeedback = {
//
},
onError = {
postalCodeEdit.error = it
},
onFocusRequest = {
// bring postalCodeEdit to focus
}
)
viewModel.gender.observe(
lifecycleScope,
onError = { errorMessage ->
findViewById<TextView>(R.id.genderSelectFeedback).let { feedbackView ->
feedbackView.text = errorMessage
feedbackView.visibility = VISIBLE
}
},
onFocusRequest = {
// bring gender radio group back to focus
}
)
}
}