This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 玩了下 DataStore - 成功动态更新 koin 的依赖
- Loading branch information
Showing
6 changed files
with
188 additions
and
25 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,74 @@ | ||
package com.wecom | ||
|
||
import android.app.Application | ||
import com.wecom.botfather.di.appModules | ||
import android.content.Context | ||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.booleanPreferencesKey | ||
import androidx.datastore.preferences.preferencesDataStore | ||
import com.wecom.botfather.BuildConfig | ||
import com.wecom.botfather.di.appModule | ||
import com.wecom.botfather.di.serviceModule | ||
import com.wecom.botfather.mock.MockService | ||
import com.wecom.botfather.sdk.service.WeComService | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.MainScope | ||
import kotlinx.coroutines.flow.collectLatest | ||
import kotlinx.coroutines.launch | ||
import org.koin.android.ext.koin.androidContext | ||
import org.koin.android.ext.koin.androidLogger | ||
import org.koin.core.Koin | ||
import org.koin.core.context.KoinContext | ||
import org.koin.core.context.loadKoinModules | ||
import org.koin.core.context.startKoin | ||
import org.koin.core.context.unloadKoinModules | ||
import org.koin.core.module.Module | ||
import org.koin.dsl.module | ||
import timber.log.Timber | ||
|
||
typealias T = Timber | ||
|
||
class BotFatherApp : Application() { | ||
class BotFatherApp : Application(), CoroutineScope by MainScope() { | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
T.plant(Timber.DebugTree()) | ||
T.tag(this::class.java.simpleName) | ||
|
||
startKoin { | ||
allowOverride(true) | ||
androidContext(this@BotFatherApp) | ||
androidLogger() | ||
modules(appModules) | ||
modules(appModule) | ||
} | ||
|
||
launch { | ||
// 监听变更 | ||
dataStore.data.collectLatest { | ||
loadServiceModule(it[ABORT_REQUEST_KEY] ?: BuildConfig.DEBUG) | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
private val debugModule = module { single<WeComService> { MockService() } } | ||
|
||
/** | ||
* Koin 允许加载同类的 module,覆盖之前的 | ||
*/ | ||
fun loadServiceModule(debug: Boolean) { | ||
if (debug) { | ||
loadKoinModules(debugModule) | ||
} else { | ||
unloadKoinModules(debugModule) | ||
loadKoinModules(serviceModule) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// At the top level of your kotlin file: | ||
val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings") | ||
|
||
// abort api request | ||
val ABORT_REQUEST_KEY = booleanPreferencesKey("abort_request") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
app/src/main/java/com/wecom/botfather/ui/settings/Preferences.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.wecom.botfather.ui.settings | ||
|
||
import androidx.compose.foundation.clickable | ||
import androidx.compose.foundation.layout.* | ||
import androidx.compose.material.Switch | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.unit.sp | ||
|
||
@Composable | ||
fun PreferenceWidget() { | ||
TODO() | ||
} | ||
|
||
/** | ||
* 配合 Prefrence 使用,分组展示 | ||
*/ | ||
@Composable | ||
fun PreferenceGroup( | ||
title: @Composable () -> Unit, | ||
content: @Composable () -> Unit | ||
) { | ||
Column { | ||
title() | ||
content() | ||
} | ||
} | ||
|
||
@Composable | ||
fun SwitchPreference( | ||
title: @Composable () -> Unit, | ||
checked: Boolean, | ||
onCheckedChange: ((Boolean) -> Unit)?, | ||
modifier: Modifier = Modifier, | ||
description: @Composable (() -> Unit)? = null, | ||
) { | ||
Row(modifier = modifier.fillMaxWidth().padding(16.dp)) { | ||
Column(modifier = Modifier.weight(1f)) { | ||
title() | ||
if (description != null) { | ||
Spacer(Modifier.size(4.dp)) | ||
description() | ||
} | ||
} | ||
Spacer(Modifier.size(4.dp)) | ||
Switch( | ||
checked = checked, | ||
onCheckedChange = onCheckedChange, | ||
modifier = Modifier.alignByBaseline() | ||
) | ||
} | ||
} | ||
|
||
@Preview(showBackground = true) | ||
@Composable | ||
fun SwitchPreferencePreview() { | ||
SwitchPreference( | ||
title = { Text("hahah") }, | ||
description = { Text(text = "xxxx", fontSize = 12.sp, color = Color.Gray) }, | ||
checked = true, | ||
onCheckedChange = {}, | ||
modifier = Modifier.clickable { | ||
|
||
} | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters