Skip to content

Commit d418380

Browse files
authored
Merge pull request #33 from LinX64/refactor/refactoring-components-and-cleanup
Code refactor
2 parents d3ffaac + 44c5c6d commit d418380

File tree

8 files changed

+115
-19
lines changed

8 files changed

+115
-19
lines changed

app/src/main/java/com/client/reusable/ui/screens/HomeScreen.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import com.client.reusablecomponents.containers.ScrollableScreen
1313
import com.client.reusablecomponents.previews.AppScreenComponent
1414
import com.client.reusablecomponents.previews.MultiThemePreviews
1515
import com.client.reusablecomponents.spacers.FillHeightSpacer
16-
import com.client.reusablecomponents.spacers.FillHorizontalHeight
16+
import com.client.reusablecomponents.spacers.HorizontalSpacer
1717

1818
@Composable
1919
fun HomeScreen() {
@@ -26,15 +26,15 @@ fun HomeScreen() {
2626

2727
FillHeightSpacer()
2828

29-
FillHorizontalHeight()
29+
HorizontalSpacer()
3030

3131
PrimaryButton(text = R.string.app_name, onClick = {})
3232

33-
FillHorizontalHeight()
33+
HorizontalSpacer()
3434

3535
PrimaryButton(text = R.string.app_name, onClick = {})
3636

37-
FillHorizontalHeight()
37+
HorizontalSpacer()
3838

3939
SecondaryButton(text = R.string.app_name, onClick = {})
4040
}

gradle.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ org.gradle.caching=true
88
org.gradle.configuration-cache.problems=warn
99
org.gradle.configureondemand=false
1010
org.gradle.parallel=true
11+
org.gradle.configuration-cache=true
1112

1213
GROUP=io.github.linx64
1314
VERSION_NAME=1.0
@@ -29,4 +30,4 @@ POM_SCM_DEV_CONNECTION=scm:git:ssh://[email protected]/LinX64/Reusable.git
2930

3031
POM_DEVELOPER_ID=LinX64
3132
POM_DEVELOPER_NAME=Mohsen
32-
POM_DEVELOPER_URL=https://github.com/LinX64/
33+
POM_DEVELOPER_URL=https://github.com/LinX64/

reusablecomponents/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ dependencies {
5353
implementation(libs.androidx.core.ktx)
5454
implementation(libs.androidx.lifecycle.runtime.ktx)
5555
implementation(libs.androidx.activity.compose)
56+
5657
implementation(platform(libs.androidx.compose.bom))
5758
implementation(libs.androidx.ui)
5859
implementation(libs.androidx.ui.graphics)
5960
implementation(libs.androidx.ui.tooling.preview)
6061
implementation(libs.androidx.compose.material3)
6162
implementation(libs.androidx.compose.material.iconsExtended)
6263

63-
debugImplementation(libs.androidx.ui.test.manifest)
6464
debugImplementation(libs.androidx.ui.tooling)
6565
}
6666

reusablecomponents/src/main/kotlin/com/client/reusablecomponents/containers/CenteredColumn.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fun CenteredColumn(
3131

3232
@Preview(showBackground = true)
3333
@Composable
34-
private fun CenterColumnPreview() {
34+
private fun CenteredColumnPreview() {
3535
AppScreenComponent {
3636
CenteredColumn {
3737
Text(text = "Text in center")
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.client.reusablecomponents.dialogs
2+
3+
import androidx.compose.foundation.layout.Column
4+
import androidx.compose.foundation.layout.fillMaxSize
5+
import androidx.compose.foundation.layout.size
6+
import androidx.compose.material.icons.Icons
7+
import androidx.compose.material.icons.filled.Error
8+
import androidx.compose.material3.AlertDialog
9+
import androidx.compose.material3.Icon
10+
import androidx.compose.material3.MaterialTheme
11+
import androidx.compose.material3.Text
12+
import androidx.compose.material3.TextButton
13+
import androidx.compose.runtime.Composable
14+
import androidx.compose.runtime.MutableState
15+
import androidx.compose.runtime.mutableStateOf
16+
import androidx.compose.runtime.remember
17+
import androidx.compose.ui.Modifier
18+
import androidx.compose.ui.graphics.Color
19+
import androidx.compose.ui.graphics.vector.ImageVector
20+
import androidx.compose.ui.res.stringResource
21+
import androidx.compose.ui.tooling.preview.Preview
22+
import androidx.compose.ui.unit.dp
23+
import io.github.linx64.reusablecomponents.R
24+
25+
@Composable
26+
fun BaseErrorDialog(
27+
modifier: Modifier = Modifier,
28+
title: String,
29+
message: String,
30+
icon: ImageVector = Icons.Default.Error,
31+
iconTint: Color = MaterialTheme.colorScheme.error,
32+
shouldShowDismiss: Boolean = false
33+
) {
34+
val shouldShowDialog = remember { mutableStateOf(true) }
35+
if (shouldShowDialog.value) {
36+
AlertDialog(
37+
modifier = modifier,
38+
icon = { Icon(icon, iconTint) },
39+
title = { Text(text = title) },
40+
text = { Text(text = message) },
41+
onDismissRequest = { shouldShowDialog.value = false },
42+
confirmButton = { ConfirmButton(shouldShowDialog) },
43+
dismissButton = { DismissButton(shouldShowDismiss, shouldShowDialog) }
44+
)
45+
}
46+
}
47+
48+
@Composable
49+
private fun Icon(
50+
icon: ImageVector,
51+
iconTint: Color
52+
) {
53+
Icon(
54+
modifier = Modifier.size(48.dp),
55+
imageVector = icon,
56+
contentDescription = null,
57+
tint = iconTint
58+
)
59+
}
60+
61+
@Composable
62+
private fun ConfirmButton(shouldShowDialog: MutableState<Boolean>) {
63+
TextButton(
64+
onClick = {
65+
shouldShowDialog.value = false
66+
}
67+
) {
68+
Text(text = stringResource(R.string.ok))
69+
}
70+
}
71+
72+
@Composable
73+
private fun DismissButton(
74+
shouldShowDismiss: Boolean,
75+
shouldShowDialog: MutableState<Boolean>
76+
) {
77+
if (shouldShowDismiss) {
78+
TextButton(
79+
onClick = {
80+
shouldShowDialog.value = false
81+
}
82+
) {
83+
Text(text = "Dismiss")
84+
}
85+
}
86+
}
87+
88+
@Preview(showBackground = true)
89+
@Composable
90+
private fun BaseErrorDialogPreview() {
91+
Column(
92+
modifier = Modifier.fillMaxSize()
93+
) {
94+
BaseErrorDialog(
95+
title = "Error",
96+
message = "Please enter valid email and password!"
97+
)
98+
}
99+
}

reusablecomponents/src/main/kotlin/com/client/reusablecomponents/previews/AppScreenComponent.kt

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import com.client.reusablecomponents.containers.CenteredColumn
1010
import io.github.linx64.reusablecomponents.R
1111

1212
/**
13-
* A component that wraps the content in a [Surface] and centers the content.
13+
* A component that wraps the content in a [Surface] and then centers it
1414
* The content is placed in a [Column].
1515
* IMPORTANT: Remember to add your own Theme while using this component.
1616
*/
@@ -20,16 +20,12 @@ fun AppScreenComponent(
2020
content: @Composable () -> Unit,
2121
) {
2222
Surface(
23-
modifier = Modifier.fillMaxSize(),
23+
modifier = Modifier
24+
.fillMaxSize()
25+
.then(modifier),
2426
) {
25-
Column(
26-
modifier = Modifier
27-
.fillMaxSize()
28-
.then(modifier),
29-
) {
30-
CenteredColumn {
31-
content()
32-
}
27+
CenteredColumn {
28+
content()
3329
}
3430
}
3531
}

reusablecomponents/src/main/kotlin/com/client/reusablecomponents/spacers/Spacers.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import androidx.compose.ui.Modifier
99
import androidx.compose.ui.unit.dp
1010

1111
@Composable
12-
fun FillHorizontalHeight() = Spacer(modifier = Modifier.height(16.dp))
12+
fun HorizontalSpacer() = Spacer(modifier = Modifier.height(16.dp))
1313

1414
/**
1515
* Spacers for Column
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
3-
<string name="ok">Ok</string>
3+
<string name="ok">OK</string>
44
<string name="dismiss">Dismiss</string>
55
<string name="cancel">Cancel</string>
66
</resources>

0 commit comments

Comments
 (0)