Skip to content

Commit f56d682

Browse files
committed
feature: jetpack compose setup
1 parent 242b975 commit f56d682

8 files changed

Lines changed: 150 additions & 10 deletions

File tree

app/build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ android {
6161
buildFeatures {
6262
android.buildFeatures.viewBinding = true
6363
android.buildFeatures.dataBinding = true
64+
compose = true
6465
}
6566

6667
sourceSets {
@@ -91,6 +92,10 @@ android {
9192
kotlinOptions {
9293
jvmTarget = "1.8"
9394
}
95+
96+
composeOptions {
97+
kotlinCompilerExtensionVersion = Compose.kotlinCompilerExtensionVersion
98+
}
9499
}
95100

96101
kapt {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.androidvip.sysctlgui.ui.params.list
2+
3+
import androidx.compose.foundation.clickable
4+
import androidx.compose.foundation.layout.Column
5+
import androidx.compose.foundation.layout.Spacer
6+
import androidx.compose.foundation.layout.fillMaxWidth
7+
import androidx.compose.foundation.layout.height
8+
import androidx.compose.foundation.layout.padding
9+
import androidx.compose.material3.MaterialTheme
10+
import androidx.compose.material3.Text
11+
import androidx.compose.runtime.Composable
12+
import androidx.compose.ui.Modifier
13+
import androidx.compose.ui.text.style.TextOverflow
14+
import androidx.compose.ui.tooling.preview.Preview
15+
import androidx.compose.ui.unit.dp
16+
import com.androidvip.sysctlgui.data.models.KernelParam
17+
18+
@Composable
19+
fun ParamItem(onParamClick: (KernelParam) -> Unit, param: KernelParam) {
20+
Column(
21+
modifier = Modifier
22+
.fillMaxWidth()
23+
.clickable { onParamClick(param) }
24+
) {
25+
Text(
26+
modifier = Modifier.padding(start = 16.dp, end = 16.dp, top = 16.dp),
27+
text = param.shortName,
28+
maxLines = 1,
29+
overflow = TextOverflow.Ellipsis,
30+
style = MaterialTheme.typography.bodyLarge
31+
)
32+
Spacer(modifier = Modifier.height(2.dp))
33+
Text(
34+
modifier = Modifier.padding(start = 16.dp, end = 16.dp, bottom = 16.dp),
35+
text = param.value,
36+
style = MaterialTheme.typography.bodyMedium
37+
)
38+
}
39+
}
40+
41+
@Preview
42+
@Composable
43+
fun ParamItemPreview() {
44+
val param = KernelParam(name = "test", value = "success")
45+
ParamItem(onParamClick = {}, param = param)
46+
}

buildSrc/src/main/kotlin/AndroidX.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ object AndroidX {
33
const val constraintLayout = "androidx.constraintlayout:constraintlayout:2.1.4"
44
const val core = "androidx.core:core-ktx:1.10.1"
55
const val splashScreen = "androidx.core:core-splashscreen:1.0.1"
6-
const val lifecycleLiveData = "androidx.lifecycle:lifecycle-livedata-ktx:2.6.1"
6+
7+
private const val lifecycleVersion = "2.6.1"
8+
const val lifecycleLiveData = "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycleVersion"
9+
const val lifecycleViewModel = "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycleVersion"
10+
711
const val preference = "androidx.preference:preference-ktx:1.2.0"
812
const val swipeRefreshLayout = "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
913

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
object Compose {
2+
const val BoM = "androidx.compose:compose-bom:2023.01.00"
3+
const val kotlinCompilerExtensionVersion = "1.3.2"
4+
const val material3 = "androidx.compose.material3:material3"
5+
const val uiTooling = "androidx.compose.ui:ui-tooling"
6+
const val activity = "androidx.activity:activity-compose:1.6.1"
7+
}

buildSrc/src/main/kotlin/Dependencies.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ object Dependencies {
44
const val koinAndroid = "io.insert-koin:koin-android:$koinVersion"
55
const val koinCore = "io.insert-koin:koin-core:$koinVersion"
66

7+
const val coroutinesCore = "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3"
78
const val tapTargetView = "com.getkeepsafe.taptargetview:taptargetview:1.13.3"
89
const val libSuCore = "com.github.topjohnwu.libsu:core:2.5.1"
910
const val liveEvent = "com.github.hadilq:live-event:1.3.0"

common/design/build.gradle.kts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ android {
2828

2929
buildFeatures {
3030
viewBinding = true
31+
compose = true
3132
}
3233

3334
compileOptions {
@@ -38,13 +39,25 @@ android {
3839
kotlinOptions {
3940
jvmTarget = "1.8"
4041
}
42+
43+
composeOptions {
44+
kotlinCompilerExtensionVersion = Compose.kotlinCompilerExtensionVersion
45+
}
4146
}
4247

4348
dependencies {
49+
val composeBom = platform(Compose.BoM)
50+
api(composeBom)
51+
androidTestImplementation(composeBom)
52+
4453
api(AndroidX.appCompat)
4554
api(AndroidX.constraintLayout)
4655
api(AndroidX.core)
4756
api(AndroidX.swipeRefreshLayout)
57+
api(Compose.material3)
58+
api(Compose.activity)
59+
api(Compose.uiTooling)
60+
debugApi(Compose.uiTooling)
4861
implementation(AndroidX.splashScreen)
4962
implementation(Google.material)
5063
}

common/utils/build.gradle.kts

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,42 @@
11
plugins {
2-
id("java-library")
3-
id("kotlin")
2+
id("com.android.library")
3+
id("org.jetbrains.kotlin.android")
44
}
55

6-
java {
7-
sourceCompatibility = JavaVersion.VERSION_1_8
8-
targetCompatibility = JavaVersion.VERSION_1_8
9-
}
6+
android {
7+
namespace = "${AppConfig.appId}.utils"
8+
compileSdk = AppConfig.compileSdkVersion
9+
10+
defaultConfig {
11+
minSdk = AppConfig.minSdkVersion
12+
targetSdk = AppConfig.targetSdlVersion
13+
14+
testInstrumentationRunner = AppConfig.testInstrumentationRunner
15+
consumerProguardFiles(AppConfig.proguardConsumerRules)
16+
}
17+
18+
buildTypes {
19+
release {
20+
isMinifyEnabled = !AppConfig.devCycle
21+
isShrinkResources = !AppConfig.devCycle
22+
proguardFiles(
23+
getDefaultProguardFile("proguard-android-optimize.txt"),
24+
"proguard-rules.pro"
25+
)
26+
}
27+
}
28+
29+
compileOptions {
30+
sourceCompatibility = JavaVersion.VERSION_1_8
31+
targetCompatibility = JavaVersion.VERSION_1_8
32+
}
1033

11-
sourceSets {
12-
maybeCreate("main").java.srcDir("src/main/kotlin")
34+
kotlinOptions {
35+
jvmTarget = "1.8"
36+
}
1337
}
1438

1539
dependencies {
16-
implementation(Dependencies.koinCore)
40+
implementation(AndroidX.lifecycleViewModel)
41+
api(Dependencies.coroutinesCore)
1742
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.androidvip.sysctlgui.utils
2+
3+
import androidx.lifecycle.ViewModel
4+
import androidx.lifecycle.viewModelScope
5+
import kotlinx.coroutines.channels.Channel
6+
import kotlinx.coroutines.flow.MutableStateFlow
7+
import kotlinx.coroutines.flow.SharingStarted
8+
import kotlinx.coroutines.flow.asStateFlow
9+
import kotlinx.coroutines.flow.receiveAsFlow
10+
import kotlinx.coroutines.flow.shareIn
11+
import kotlinx.coroutines.launch
12+
13+
abstract class BaseViewModel<Event, State, Effect> : ViewModel() {
14+
abstract fun createInitialState(): State
15+
16+
private val initialState: State by lazy { createInitialState() }
17+
18+
val currentState: State get() = uiState.value
19+
20+
private val _uiState: MutableStateFlow<State> = MutableStateFlow(initialState)
21+
private val uiState = _uiState.asStateFlow()
22+
23+
private val _effect: Channel<Effect> = Channel()
24+
private val effect = _effect
25+
.receiveAsFlow()
26+
.shareIn(viewModelScope, SharingStarted.WhileSubscribed())
27+
28+
abstract fun processEvent(event: Event)
29+
30+
protected fun setState(block: State.() -> State) {
31+
_uiState.value = currentState.block()
32+
}
33+
34+
protected fun setEffect(block: () -> Effect) {
35+
viewModelScope.launch {
36+
_effect.send(block())
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)