Skip to content

Commit 2b4dee7

Browse files
Merge pull request #1 from UmairKhalid786/feature/basic_parser_addition
Basic parser added for common widgets
2 parents 525ed0b + 93495d4 commit 2b4dee7

File tree

8 files changed

+232
-18
lines changed

8 files changed

+232
-18
lines changed

iosApp/iosApp.xcodeproj/project.pbxproj

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10-
058557BB273AAA24004C7B11 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 058557BA273AAA24004C7B11 /* Assets.xcassets */; };
11-
058557D9273AAEEB004C7B11 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 058557D8273AAEEB004C7B11 /* Preview Assets.xcassets */; };
1210
2152FB042600AC8F00CF470E /* iOSApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2152FB032600AC8F00CF470E /* iOSApp.swift */; };
1311
7555FF83242A565900829871 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7555FF82242A565900829871 /* ContentView.swift */; };
1412
CFDB58B53BB94DE262B13C24 /* Pods_iosApp.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6B1049432C0C2B312090ABF6 /* Pods_iosApp.framework */; };
@@ -20,7 +18,7 @@
2018
2152FB032600AC8F00CF470E /* iOSApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = iOSApp.swift; sourceTree = "<group>"; };
2119
4FF3202A603A284706412EDC /* Pods-iosApp.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-iosApp.debug.xcconfig"; path = "Target Support Files/Pods-iosApp/Pods-iosApp.debug.xcconfig"; sourceTree = "<group>"; };
2220
6B1049432C0C2B312090ABF6 /* Pods_iosApp.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_iosApp.framework; sourceTree = BUILT_PRODUCTS_DIR; };
23-
7555FF7B242A565900829871 /* iosApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = iosApp.app; sourceTree = BUILT_PRODUCTS_DIR; };
21+
7555FF7B242A565900829871 /* My application.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "My application.app"; sourceTree = BUILT_PRODUCTS_DIR; };
2422
7555FF82242A565900829871 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
2523
7555FF8C242A565B00829871 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
2624
AB3632DC29227652001CCB65 /* Config.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = Config.xcconfig; sourceTree = "<group>"; };
@@ -69,7 +67,7 @@
6967
7555FF7C242A565900829871 /* Products */ = {
7068
isa = PBXGroup;
7169
children = (
72-
7555FF7B242A565900829871 /* iosApp.app */,
70+
7555FF7B242A565900829871 /* My application.app */,
7371
);
7472
name = Products;
7573
sourceTree = "<group>";
@@ -121,7 +119,7 @@
121119
);
122120
name = iosApp;
123121
productName = iosApp;
124-
productReference = 7555FF7B242A565900829871 /* iosApp.app */;
122+
productReference = 7555FF7B242A565900829871 /* My application.app */;
125123
productType = "com.apple.product-type.application";
126124
};
127125
/* End PBXNativeTarget section */
@@ -331,7 +329,7 @@
331329
CODE_SIGN_IDENTITY = "Apple Development";
332330
CODE_SIGN_STYLE = Automatic;
333331
DEVELOPMENT_ASSET_PATHS = "\"iosApp/Preview Content\"";
334-
DEVELOPMENT_TEAM = "${TEAM_ID}";
332+
DEVELOPMENT_TEAM = "";
335333
ENABLE_PREVIEWS = YES;
336334
INFOPLIST_FILE = iosApp/Info.plist;
337335
IPHONEOS_DEPLOYMENT_TARGET = 14.1;
Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,50 @@
1-
import androidx.compose.material.Button
21
import androidx.compose.material.MaterialTheme
3-
import androidx.compose.material.Text
42
import androidx.compose.runtime.Composable
5-
import androidx.compose.runtime.getValue
6-
import androidx.compose.runtime.mutableStateOf
73
import androidx.compose.runtime.remember
8-
import androidx.compose.runtime.setValue
4+
import parsingSDK.Parser
5+
import parsingSDK.widgets.*
96

107
@Composable
118
internal fun App() {
129
MaterialTheme {
13-
var text by remember { mutableStateOf("Hello, World!") }
14-
15-
Button(onClick = {
16-
text = "Hello, ${getPlatformName()}"
17-
}) {
18-
Text(text)
19-
}
10+
val widget = remember { horizontalList() }
11+
Parser(widget)
2012
}
2113
}
2214

15+
fun horizontalList(): Widget {
16+
val button = Widget(constraints = hashMapOf<String, String>().apply {
17+
put(TEXT, "Hello")
18+
put(ID, "1")
19+
put(TYPE, "button")
20+
put(PADDING_BOTTOM, "5")
21+
put(PADDING_TOP, "5")
22+
put(PADDING_LEFT, "5")
23+
put(PADDING_RIGHT, "5")
24+
})
25+
26+
val text = Widget(constraints = hashMapOf<String, String>().apply {
27+
put(TEXT, "HiHi")
28+
put(ID, "2")
29+
put(TYPE, "text")
30+
put(PADDING_BOTTOM, "5")
31+
put(PADDING_TOP, "5")
32+
put(PADDING_LEFT, "5")
33+
put(PADDING_RIGHT, "5")
34+
})
35+
36+
val verticalList =
37+
MultiChildWidget(listOf(button, text), constraints = hashMapOf<String, String>().apply {
38+
put(ID, "3")
39+
put(TYPE, "horizontal_list")
40+
put(PADDING_BOTTOM, "5")
41+
put(PADDING_TOP, "5")
42+
put(PADDING_LEFT, "5")
43+
put(PADDING_RIGHT, "5")
44+
})
45+
46+
return verticalList
47+
}
48+
49+
2350
expect fun getPlatformName(): String
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package parsingSDK
2+
3+
import androidx.compose.foundation.layout.Column
4+
import androidx.compose.foundation.layout.Row
5+
import androidx.compose.foundation.layout.padding
6+
import androidx.compose.foundation.lazy.LazyColumn
7+
import androidx.compose.foundation.lazy.LazyRow
8+
import androidx.compose.material.Button
9+
import androidx.compose.material.Text
10+
import androidx.compose.runtime.Composable
11+
import androidx.compose.runtime.remember
12+
import androidx.compose.ui.Modifier
13+
import androidx.compose.ui.platform.testTag
14+
import parsingSDK.models.WidgetType
15+
import parsingSDK.widgets.*
16+
17+
@Composable
18+
fun Parser(widget: Widget, modifier: Modifier = Modifier) {
19+
when (widget.constraints.type()) {
20+
WidgetType.BUTTON -> {
21+
ParserButton(widget, modifier)
22+
}
23+
WidgetType.TEXT -> {
24+
ParserText(widget, modifier)
25+
}
26+
WidgetType.HORIZONTAL_LIST -> {
27+
HorizontalList(widget as? MultiChildWidget ?: return, modifier)
28+
}
29+
WidgetType.VERTICAL_LIST -> {
30+
VerticalList(widget as? MultiChildWidget ?: return, modifier)
31+
}
32+
WidgetType.HORIZONTAL_SCROLL -> {
33+
HorizontalScroll(widget as? MultiChildWidget ?: return, modifier)
34+
}
35+
WidgetType.VERTICAL_SCROLL -> {
36+
VerticalScroll(widget as? MultiChildWidget ?: return, modifier)
37+
}
38+
else -> {
39+
Text("No widget found")
40+
}
41+
}
42+
}
43+
44+
@Composable
45+
fun ParserButton(widget: Widget, modifier: Modifier = Modifier) {
46+
val constraints = remember { widget.constraints }
47+
Button(onClick = {}, modifier = modifier.setGeneralProperties(widget.constraints)) {
48+
Text(constraints.text())
49+
}
50+
}
51+
52+
@Composable
53+
fun ParserText(widget: Widget, modifier: Modifier = Modifier) {
54+
val constraints = remember { widget.constraints }
55+
Text(constraints.text(), modifier.setGeneralProperties(widget.constraints))
56+
}
57+
58+
@Composable
59+
fun HorizontalList(widget: MultiChildWidget, modifier: Modifier = Modifier) {
60+
val widgets = remember { widget.childWidget }
61+
LazyRow(modifier = modifier.setGeneralProperties(widget.constraints)) {
62+
items(widgets.size) {
63+
Parser(widgets[it])
64+
}
65+
}
66+
}
67+
68+
@Composable
69+
fun VerticalList(widget: MultiChildWidget, modifier: Modifier = Modifier) {
70+
val widgets = remember { widget.childWidget }
71+
LazyColumn(modifier = modifier.setGeneralProperties(widget.constraints)) {
72+
items(widgets.size) {
73+
Parser(widgets[it])
74+
}
75+
}
76+
}
77+
78+
@Composable
79+
fun VerticalScroll(widget: MultiChildWidget, modifier: Modifier = Modifier) {
80+
val widgets = remember { widget.childWidget }
81+
Column(modifier = modifier.setGeneralProperties(widget.constraints)) {
82+
repeat(widget.childWidget.size) {
83+
Parser(widgets[it])
84+
}
85+
}
86+
}
87+
88+
@Composable
89+
fun HorizontalScroll(widget: MultiChildWidget, modifier: Modifier = Modifier) {
90+
val widgets = remember { widget.childWidget }
91+
Row(modifier = modifier.setGeneralProperties(widget.constraints)) {
92+
repeat(widget.childWidget.size) {
93+
Parser(widgets[it])
94+
}
95+
}
96+
}
97+
98+
fun Modifier.setGeneralProperties(constraints: Constraints): Modifier {
99+
constraints.id()?.let { testTag(it) }
100+
return padding(constraints.padding())
101+
.padding(constraints.margin())
102+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package parsingSDK.models
2+
3+
sealed class WidgetType {
4+
object EMPTY: WidgetType()
5+
object BUTTON: WidgetType()
6+
object IMAGE_BUTTON: WidgetType()
7+
object TEXT: WidgetType()
8+
object HORIZONTAL_LIST: WidgetType()
9+
object VERTICAL_LIST: WidgetType()
10+
object IMAGE: WidgetType()
11+
object HORIZONTAL_SCROLL: WidgetType()
12+
object VERTICAL_SCROLL: WidgetType()
13+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package parsingSDK.widgets
2+
3+
import androidx.compose.foundation.layout.PaddingValues
4+
import androidx.compose.ui.unit.dp
5+
import parsingSDK.models.WidgetType
6+
7+
8+
typealias Constraints = HashMap<String, String>
9+
10+
const val ID = "property_id"
11+
const val TYPE = "property_type"
12+
13+
const val PADDING_LEFT = "property_padding_left_text"
14+
const val PADDING_RIGHT = "property_padding_right_text"
15+
const val PADDING_TOP = "property_padding_top_text"
16+
const val PADDING_BOTTOM = "property_padding_bottom_text"
17+
18+
const val MARGIN_LEFT = "property_margin_left_text"
19+
const val MARGIN_RIGHT = "property_margin_right_text"
20+
const val MARGIN_TOP = "property_margin_top_text"
21+
const val MARGIN_BOTTOM = "property_margin_bottom_text"
22+
23+
const val TEXT = "property_text"
24+
const val GRAVITY = "property_gravity"
25+
26+
fun Constraints.padding(): PaddingValues {
27+
val start = get(PADDING_LEFT)?.toIntOrNull() ?: 0
28+
val top = get(PADDING_TOP)?.toIntOrNull() ?: 0
29+
val right = get(PADDING_RIGHT)?.toIntOrNull() ?: 0
30+
val bottom = get(PADDING_BOTTOM)?.toIntOrNull() ?: 0
31+
return PaddingValues(start = start.dp, top = top.dp, end = right.dp, bottom = bottom.dp)
32+
}
33+
34+
fun Constraints.margin(): PaddingValues {
35+
val start = get(MARGIN_LEFT)?.toIntOrNull() ?: 0
36+
val top = get(MARGIN_TOP)?.toIntOrNull() ?: 0
37+
val right = get(MARGIN_RIGHT)?.toIntOrNull() ?: 0
38+
val bottom = get(MARGIN_BOTTOM)?.toIntOrNull() ?: 0
39+
return PaddingValues(start = start.dp, top = top.dp, end = right.dp, bottom = bottom.dp)
40+
}
41+
42+
fun Constraints.id(): String? = get(ID)
43+
fun Constraints.type(): WidgetType {
44+
return when (get(TYPE)) {
45+
"button" -> WidgetType.BUTTON
46+
"image_button" -> WidgetType.IMAGE_BUTTON
47+
"image" -> WidgetType.IMAGE
48+
"text" -> WidgetType.TEXT
49+
"horizontal_list" -> WidgetType.HORIZONTAL_LIST
50+
"vertical_list" -> WidgetType.VERTICAL_LIST
51+
"vertical_scroll" -> WidgetType.VERTICAL_SCROLL
52+
"horizontal_scroll" -> WidgetType.HORIZONTAL_SCROLL
53+
else -> WidgetType.EMPTY
54+
}
55+
}
56+
57+
fun Constraints.text(): String = get(TEXT) ?: ""
58+
59+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package parsingSDK.widgets
2+
3+
class MultiChildWidget(
4+
val childWidget: List<Widget>,
5+
override val constraints: Constraints
6+
) : Widget(constraints)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package parsingSDK.widgets
2+
3+
data class SingleChildWidget(
4+
val childWidgets: List<Widget>,
5+
override val constraints: Constraints
6+
) : Widget(constraints)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package parsingSDK.widgets
2+
3+
open class Widget(open val constraints: Constraints)

0 commit comments

Comments
 (0)