1
1
package com.rafsan.dynamicui_fromjson
2
2
3
+ import android.content.res.Resources
4
+ import android.util.Log
5
+ import android.view.View
6
+ import android.widget.*
7
+ import androidx.appcompat.widget.SwitchCompat
8
+ import com.google.gson.JsonArray
9
+ import com.google.gson.JsonObject
10
+ import com.rafsan.dynamicui_fromjson.GenerateFormActivity.Companion.submitPropertyArrayJson
11
+ import com.rafsan.dynamicui_fromjson.model.FormComponentItem
12
+ import com.rafsan.dynamicui_fromjson.model.Value
13
+ import java.util.ArrayList
14
+
3
15
class CollectData {
16
+ companion object {
17
+ /* *
18
+ * Get selected Date from date TextView.
19
+ *
20
+ * @param view
21
+ * @param FormComponentItem
22
+ */
23
+ private fun getDataFromDateTextView (
24
+ view : View ,
25
+ viewComponentModel : FormComponentItem
26
+ ) {
27
+ val dateView = view as TextView
28
+ val submitPropertiesValueObj = JsonObject ()
29
+ submitPropertiesValueObj.addProperty(" label" , viewComponentModel.label)
30
+ submitPropertiesValueObj.addProperty(" value" , dateView.text.toString())
31
+ submitPropertiesValueObj.addProperty(" type" , viewComponentModel.type)
32
+ submitPropertiesValueObj.addProperty(" subtype" , viewComponentModel.subtype))
33
+ submitPropertyArrayJson.add(submitPropertiesValueObj)
34
+ }
35
+
36
+ /* *
37
+ * @param view
38
+ * @param FormComponentItem
39
+ * @return
40
+ */
41
+ private fun getDataFromCheckBoxGroup (
42
+ view : View ,
43
+ viewComponentModel : FormComponentItem
44
+ ): Boolean {
45
+ return if (viewComponentModel.toggle != null && viewComponentModel.toggle) {
46
+ getDataFromSwitchContainer(view, viewComponentModel)
47
+ } else getDataFromCheckBoxContainer(view, viewComponentModel)
48
+ }
49
+
50
+ /* *
51
+ * @param view
52
+ * @param FormComponentItem
53
+ * @return
54
+ */
55
+ private fun getDataFromCheckBoxContainer (
56
+ view : View ,
57
+ viewComponentModel : FormComponentItem
58
+ ): Boolean {
59
+ val checkBoxContainer = view as LinearLayout
60
+ val submitJsonValues = JsonArray ()
61
+ val submitPropertiesValueObj = JsonObject ()
62
+ var valueModel: Value ?
63
+ return if (viewComponentModel.required != null && viewComponentModel.required) {
64
+ for (i in 0 until checkBoxContainer.childCount) {
65
+ val submitJsonValue = JsonObject ()
66
+ val checkBox = checkBoxContainer.getChildAt(i) as CheckBox
67
+ valueModel = viewComponentModel.values?.get(i)
68
+ if (checkBox.isChecked) {
69
+ submitJsonValue.addProperty(" label" , checkBox.text.toString())
70
+ // submitJsonValue.addProperty("value", valueModel.getValue());
71
+ if (valueModel != null ) {
72
+ if (valueModel.label.equals(" Other" )) {
73
+ submitJsonValue.addProperty(" value" , valueModel.value)
74
+ } else submitJsonValue.addProperty(" value" , checkBox.text.toString())
75
+ }
76
+ submitJsonValues.add(submitJsonValue)
77
+ }
78
+ }
79
+ submitPropertiesValueObj.addProperty(" label" , viewComponentModel.label)
80
+ submitPropertiesValueObj.add(" value" , submitJsonValues)
81
+ submitPropertiesValueObj.addProperty(" type" , viewComponentModel.type)
82
+ submitPropertiesValueObj.addProperty(" subtype" , viewComponentModel.subtype)
83
+ submitPropertyArrayJson?.add(submitPropertiesValueObj)
84
+ true
85
+ } else {
86
+ for (i in 0 until checkBoxContainer.childCount) {
87
+ val submitJsonValue = JsonObject ()
88
+ val checkBox = checkBoxContainer.getChildAt(i) as CheckBox
89
+ valueModel = viewComponentModel.values?.get(i)
90
+ if (checkBox.isChecked) {
91
+ submitJsonValue.addProperty(" label" , checkBox.text.toString())
92
+ // submitJsonValue.addProperty("value", valueModel.getValue());
93
+ if (valueModel != null ) {
94
+ if (valueModel.label.equals(" Other" )) {
95
+ submitJsonValue.addProperty(" value" , valueModel.value)
96
+ } else submitJsonValue.addProperty(" value" , checkBox.text.toString())
97
+ }
98
+ submitJsonValues.add(submitJsonValue)
99
+ }
100
+ }
101
+ submitPropertiesValueObj.addProperty(" label" , viewComponentModel.label)
102
+ submitPropertiesValueObj.add(" value" , submitJsonValues)
103
+ submitPropertiesValueObj.addProperty(" type" , viewComponentModel.type)
104
+ submitPropertiesValueObj.addProperty(" subtype" , viewComponentModel.subtype)
105
+ submitPropertyArrayJson?.add(submitPropertiesValueObj)
106
+ true
107
+ }
108
+ }
109
+ }
4
110
}
0 commit comments