Skip to content

Commit 2fda266

Browse files
committed
custom dialog, collect data
1 parent f31edc0 commit 2fda266

File tree

6 files changed

+256
-26
lines changed

6 files changed

+256
-26
lines changed

.idea/misc.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ import androidx.appcompat.widget.LinearLayoutCompat
2222
import androidx.appcompat.widget.SwitchCompat
2323
import androidx.core.app.NavUtils
2424
import androidx.core.content.ContextCompat
25-
import androidx.core.content.res.ResourcesCompat
2625
import com.google.gson.Gson
2726
import com.google.gson.JsonArray
2827
import com.google.gson.JsonObject
28+
import com.rafsan.dynamicui_fromjson.data.CollectData.Companion.getDataFromCheckBoxGroup
29+
import com.rafsan.dynamicui_fromjson.data.CollectData.Companion.getDataFromDateTextView
2930
import com.rafsan.dynamicui_fromjson.databinding.ActivityGenerateFormBinding
3031
import com.rafsan.dynamicui_fromjson.model.FormComponent
3132
import com.rafsan.dynamicui_fromjson.model.FormComponentItem
@@ -39,6 +40,7 @@ import com.rafsan.dynamicui_fromjson.utils.Utils.Companion.getDateStringToShow
3940
import com.rafsan.dynamicui_fromjson.utils.Utils.Companion.method
4041
import com.rafsan.dynamicui_fromjson.utils.Utils.Companion.setMerginToviews
4142
import com.rafsan.dynamicui_fromjson.utils.Utils.Companion.setSwitchColor
43+
import com.rafsan.dynamicui_fromjson.utils.dialog.ShowDialog
4244
import java.util.*
4345

4446
class GenerateFormActivity : AppCompatActivity() {
@@ -862,22 +864,35 @@ class GenerateFormActivity : AppCompatActivity() {
862864
val viewComponentModel: FormComponentItem =
863865
formViewComponent.getViewComponentModel()
864866
when (viewComponentModel.type) {
865-
"text" -> if (!getDataFromEditText(view, viewComponentModel)) {
866-
submitPropertyArrayJson = JsonArray()
867-
}
868-
"textarea" -> if (!getDataFromEditText(view, viewComponentModel)) {
869-
submitPropertyArrayJson = JsonArray()
870-
}
871-
"select" -> if (!getDataFromSpinner(view, viewComponentModel)) {
872-
submitPropertyArrayJson = JsonArray()
873-
}
874-
"radio-group" -> if (!getDataFromRadioGroup(view, viewComponentModel)) {
875-
submitPropertyArrayJson = JsonArray()
876-
}
867+
"text" ->
868+
if (!getDataFromEditText(view, viewComponentModel)) {
869+
submitPropertyArrayJson = JsonArray()
870+
}
871+
"textarea" ->
872+
if (!getDataFromEditText(view, viewComponentModel)) {
873+
submitPropertyArrayJson = JsonArray()
874+
}
875+
"select" ->
876+
if (!getDataFromSpinner(view, viewComponentModel)) {
877+
submitPropertyArrayJson = JsonArray()
878+
}
879+
"radio-group" ->
880+
if (!getDataFromRadioGroup(view, viewComponentModel)) {
881+
submitPropertyArrayJson = JsonArray()
882+
}
877883
"date" -> getDataFromDateTextView(view, viewComponentModel)
878-
"checkbox-group" -> if (!getDataFromCheckBoxGroup(view, viewComponentModel)) {
879-
submitPropertyArrayJson = JsonArray()
880-
}
884+
"checkbox-group" ->
885+
if (!getDataFromCheckBoxGroup(view, viewComponentModel)) {
886+
submitPropertyArrayJson = JsonArray()
887+
} else {
888+
viewComponentModel.label?.let { labelStr ->
889+
ShowDialog.customDialog(
890+
this,
891+
"Required",
892+
labelStr, null
893+
)
894+
}
895+
}
881896
"number" -> {
882897
if (!getDataFromEditText(view, viewComponentModel)) {
883898
submitPropertyArrayJson = JsonArray()

app/src/main/java/com/rafsan/dynamicui_fromjson/CollectData.kt renamed to app/src/main/java/com/rafsan/dynamicui_fromjson/data/CollectData.kt

Lines changed: 71 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
package com.rafsan.dynamicui_fromjson
1+
package com.rafsan.dynamicui_fromjson.data
22

3-
import android.content.res.Resources
4-
import android.util.Log
53
import android.view.View
64
import android.widget.*
75
import androidx.appcompat.widget.SwitchCompat
@@ -10,7 +8,6 @@ import com.google.gson.JsonObject
108
import com.rafsan.dynamicui_fromjson.GenerateFormActivity.Companion.submitPropertyArrayJson
119
import com.rafsan.dynamicui_fromjson.model.FormComponentItem
1210
import com.rafsan.dynamicui_fromjson.model.Value
13-
import java.util.ArrayList
1411

1512
class CollectData {
1613
companion object {
@@ -20,7 +17,7 @@ class CollectData {
2017
* @param view
2118
* @param FormComponentItem
2219
*/
23-
private fun getDataFromDateTextView(
20+
fun getDataFromDateTextView(
2421
view: View,
2522
viewComponentModel: FormComponentItem
2623
) {
@@ -29,16 +26,16 @@ class CollectData {
2926
submitPropertiesValueObj.addProperty("label", viewComponentModel.label)
3027
submitPropertiesValueObj.addProperty("value", dateView.text.toString())
3128
submitPropertiesValueObj.addProperty("type", viewComponentModel.type)
32-
submitPropertiesValueObj.addProperty("subtype", viewComponentModel.subtype))
33-
submitPropertyArrayJson.add(submitPropertiesValueObj)
29+
submitPropertiesValueObj.addProperty("subtype", viewComponentModel.subtype)
30+
submitPropertyArrayJson?.add(submitPropertiesValueObj)
3431
}
3532

3633
/**
3734
* @param view
3835
* @param FormComponentItem
3936
* @return
4037
*/
41-
private fun getDataFromCheckBoxGroup(
38+
fun getDataFromCheckBoxGroup(
4239
view: View,
4340
viewComponentModel: FormComponentItem
4441
): Boolean {
@@ -52,14 +49,15 @@ class CollectData {
5249
* @param FormComponentItem
5350
* @return
5451
*/
55-
private fun getDataFromCheckBoxContainer(
52+
fun getDataFromCheckBoxContainer(
5653
view: View,
5754
viewComponentModel: FormComponentItem
5855
): Boolean {
5956
val checkBoxContainer = view as LinearLayout
6057
val submitJsonValues = JsonArray()
6158
val submitPropertiesValueObj = JsonObject()
6259
var valueModel: Value?
60+
var isChecked = false
6361
return if (viewComponentModel.required != null && viewComponentModel.required) {
6462
for (i in 0 until checkBoxContainer.childCount) {
6563
val submitJsonValue = JsonObject()
@@ -74,14 +72,15 @@ class CollectData {
7472
} else submitJsonValue.addProperty("value", checkBox.text.toString())
7573
}
7674
submitJsonValues.add(submitJsonValue)
75+
isChecked = true
7776
}
7877
}
7978
submitPropertiesValueObj.addProperty("label", viewComponentModel.label)
8079
submitPropertiesValueObj.add("value", submitJsonValues)
8180
submitPropertiesValueObj.addProperty("type", viewComponentModel.type)
8281
submitPropertiesValueObj.addProperty("subtype", viewComponentModel.subtype)
8382
submitPropertyArrayJson?.add(submitPropertiesValueObj)
84-
true
83+
return isChecked
8584
} else {
8685
for (i in 0 until checkBoxContainer.childCount) {
8786
val submitJsonValue = JsonObject()
@@ -106,5 +105,67 @@ class CollectData {
106105
true
107106
}
108107
}
108+
109+
/**
110+
* @param view
111+
* @param FormComponentItem
112+
* @return
113+
*/
114+
fun getDataFromSwitchContainer(
115+
view: View,
116+
viewComponentModel: FormComponentItem
117+
): Boolean {
118+
val checkBoxContainer = view as LinearLayout
119+
val submitJsonValues = JsonArray()
120+
val submitPropertiesValueObj = JsonObject()
121+
var valueModel: Value?
122+
var isChecked = false
123+
return if (viewComponentModel.required != null && viewComponentModel.required) {
124+
for (i in 0 until (viewComponentModel.values?.size ?: 0)) {
125+
val submitJsonValue = JsonObject()
126+
val aSwitch = checkBoxContainer.getChildAt(i) as SwitchCompat
127+
valueModel = viewComponentModel.values?.get(i)
128+
if (aSwitch.isChecked) {
129+
submitJsonValue.addProperty("label", aSwitch.text.toString())
130+
//submitJsonValue.addProperty("value", valueModel.getValue());
131+
if (valueModel != null) {
132+
if (valueModel.label.equals("Other")) {
133+
submitJsonValue.addProperty("value", valueModel.value)
134+
} else submitJsonValue.addProperty("value", aSwitch.text.toString())
135+
}
136+
submitJsonValues.add(submitJsonValue)
137+
isChecked = true
138+
}
139+
}
140+
submitPropertiesValueObj.addProperty("label", viewComponentModel.label)
141+
submitPropertiesValueObj.add("value", submitJsonValues)
142+
submitPropertiesValueObj.addProperty("type", viewComponentModel.type)
143+
submitPropertiesValueObj.addProperty("subtype", viewComponentModel.subtype)
144+
submitPropertyArrayJson?.add(submitPropertiesValueObj)
145+
return isChecked
146+
} else {
147+
for (i in 0 until (viewComponentModel.values?.size ?: 0)) {
148+
val submitJsonValue = JsonObject()
149+
val aSwitch = checkBoxContainer.getChildAt(i) as SwitchCompat
150+
valueModel = viewComponentModel.values?.get(i)
151+
if (aSwitch.isChecked) {
152+
submitJsonValue.addProperty("label", aSwitch.text.toString())
153+
//submitJsonValue.addProperty("value", valueModel.getValue());
154+
if (valueModel != null) {
155+
if (valueModel.label.equals("Other")) {
156+
submitJsonValue.addProperty("value", valueModel.value)
157+
} else submitJsonValue.addProperty("value", aSwitch.text.toString())
158+
}
159+
submitJsonValues.add(submitJsonValue)
160+
}
161+
}
162+
submitPropertiesValueObj.addProperty("label", viewComponentModel.label)
163+
submitPropertiesValueObj.add("value", submitJsonValues)
164+
submitPropertiesValueObj.addProperty("type", viewComponentModel.type)
165+
submitPropertiesValueObj.addProperty("subtype", viewComponentModel.subtype)
166+
submitPropertyArrayJson?.add(submitPropertiesValueObj)
167+
true
168+
}
169+
}
109170
}
110171
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.rafsan.dynamicui_fromjson.utils.dialog
2+
3+
interface DialogListener {
4+
fun OnSuccess(obj: Any?)
5+
fun OnError(obj: Any?)
6+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.rafsan.dynamicui_fromjson.utils.dialog
2+
3+
import android.app.Activity
4+
import android.app.Dialog
5+
import android.content.Context
6+
import android.view.Gravity
7+
import android.view.LayoutInflater
8+
import android.view.Window
9+
import android.view.WindowManager
10+
import com.rafsan.dynamicui_fromjson.databinding.DialogLayoutBinding
11+
12+
class ShowDialog {
13+
companion object {
14+
fun customDialog(
15+
context: Context,
16+
title: String,
17+
message: String,
18+
listener: DialogListener?
19+
) {
20+
val dialog = Dialog(context)
21+
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
22+
val inflater =
23+
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
24+
val binding: DialogLayoutBinding = DialogLayoutBinding.inflate(inflater)
25+
binding.modalTitle.text = title
26+
binding.modalMsg.text = message
27+
binding.modalConfirm.setOnClickListener {
28+
dialog.dismiss()
29+
listener?.OnSuccess("")
30+
}
31+
(context as Activity).window.setSoftInputMode(
32+
WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE or
33+
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
34+
)
35+
dialog.setContentView(binding.root)
36+
val window = dialog.window
37+
window!!.setLayout(
38+
WindowManager.LayoutParams.MATCH_PARENT,
39+
WindowManager.LayoutParams.WRAP_CONTENT
40+
)
41+
window.setGravity(Gravity.CENTER)
42+
dialog.show()
43+
}
44+
}
45+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
android:layout_width="match_parent"
5+
android:layout_height="wrap_content"
6+
android:layout_gravity="center|center_horizontal"
7+
android:orientation="vertical">
8+
9+
<androidx.cardview.widget.CardView
10+
android:layout_width="match_parent"
11+
android:layout_height="wrap_content"
12+
android:layout_marginLeft="40dp"
13+
android:layout_marginRight="40dp"
14+
android:background="@color/white"
15+
app:cardCornerRadius="12dp">
16+
17+
<LinearLayout
18+
android:layout_width="match_parent"
19+
android:layout_height="wrap_content"
20+
android:orientation="vertical"
21+
android:paddingLeft="23dp"
22+
android:paddingTop="49dp"
23+
android:paddingRight="23dp"
24+
android:paddingBottom="29dp">
25+
26+
<TextView
27+
android:id="@+id/modal_title"
28+
android:layout_width="match_parent"
29+
android:layout_height="wrap_content"
30+
android:textAlignment="center"
31+
android:textColor="#011F3B"
32+
android:textSize="28sp" />
33+
34+
<TextView
35+
android:id="@+id/modal_msg"
36+
android:layout_width="match_parent"
37+
android:layout_height="wrap_content"
38+
android:layout_marginTop="16dp"
39+
android:textAlignment="center"
40+
android:textColor="#455A64"
41+
android:textSize="16sp" />
42+
43+
<LinearLayout
44+
android:layout_width="match_parent"
45+
android:layout_height="wrap_content">
46+
47+
<com.google.android.material.card.MaterialCardView
48+
android:id="@+id/modal_deny"
49+
android:layout_width="match_parent"
50+
android:layout_height="wrap_content"
51+
android:layout_marginTop="36dp"
52+
android:layout_weight="1"
53+
android:visibility="gone"
54+
app:cardBackgroundColor="@color/white"
55+
app:cardCornerRadius="10dp"
56+
app:strokeColor="#455A64"
57+
app:strokeWidth="1dp">
58+
59+
<TextView
60+
android:id="@+id/modal_deny_text"
61+
android:layout_width="match_parent"
62+
android:layout_height="wrap_content"
63+
android:letterSpacing="0.06"
64+
android:paddingTop="16dp"
65+
android:paddingBottom="16dp"
66+
android:text="@android:string/no"
67+
android:textAlignment="center"
68+
android:textColor="#455A64"
69+
android:textSize="17sp" />
70+
</com.google.android.material.card.MaterialCardView>
71+
72+
<View
73+
android:id="@+id/modal_spacer"
74+
android:layout_width="20dp"
75+
android:layout_height="1dp"
76+
android:visibility="gone" />
77+
78+
<androidx.cardview.widget.CardView
79+
android:id="@+id/modal_confirm"
80+
android:layout_width="match_parent"
81+
android:layout_height="wrap_content"
82+
android:layout_marginTop="36dp"
83+
android:layout_weight="1"
84+
app:cardBackgroundColor="@color/teal_700"
85+
app:cardCornerRadius="10dp">
86+
87+
<TextView
88+
android:id="@+id/modal_confirm_text"
89+
android:layout_width="match_parent"
90+
android:layout_height="wrap_content"
91+
android:letterSpacing="0.06"
92+
android:paddingTop="16dp"
93+
android:paddingBottom="16dp"
94+
android:text="@android:string/ok"
95+
android:textAlignment="center"
96+
android:textColor="@color/white"
97+
android:textSize="17sp" />
98+
</androidx.cardview.widget.CardView>
99+
</LinearLayout>
100+
</LinearLayout>
101+
</androidx.cardview.widget.CardView>
102+
</LinearLayout>

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