Skip to content

Commit f31edc0

Browse files
committed
collect data
1 parent 7b9567d commit f31edc0

File tree

2 files changed

+112
-2
lines changed

2 files changed

+112
-2
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,110 @@
11
package com.rafsan.dynamicui_fromjson
22

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+
315
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+
}
4110
}

app/src/main/java/com/rafsan/dynamicui_fromjson/GenerateFormActivity.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ class GenerateFormActivity : AppCompatActivity() {
4747
var formViewCollection: ArrayList<FormViewComponent> = arrayListOf()
4848

4949
var submitRootJsonObj: JsonObject? = null
50-
var submitPropertyArrayJson: JsonArray? = null
50+
51+
companion object {
52+
var submitPropertyArrayJson: JsonArray? = null
53+
}
54+
5155
var formComponent: FormComponent? = null
5256
val textColor = Color.parseColor("#000000")
5357

@@ -882,7 +886,7 @@ class GenerateFormActivity : AppCompatActivity() {
882886
}
883887
}
884888

885-
submitRootJsonObj.add("properties", submitPropertyArrayJson)
889+
submitRootJsonObj?.add("properties", submitPropertyArrayJson)
886890
Log.i("JsonArray", submitRootJsonObj.toString())
887891
}
888892
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy