Skip to content

Commit

Permalink
refactor: make state flow non-mutable in interface
Browse files Browse the repository at this point in the history
Don't expose mutable flow types.
  • Loading branch information
cinit committed Sep 18, 2023
1 parent 11f923a commit 9ef2f48
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import io.github.qauxv.util.SyncUtils
import io.github.qauxv.dsl.item.DslTMsgListItemInflatable
import io.github.qauxv.dsl.item.TMsgListItem
import io.github.qauxv.dsl.item.UiAgentItem
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

class RecyclerListViewController(
val context: Context,
Expand Down Expand Up @@ -90,7 +90,7 @@ class RecyclerListViewController(
for (i in itemList.indices) {
val item = itemList[i]
if (item is UiAgentItem) {
val valueStateFlow: MutableStateFlow<String?>? = item.agentProvider.uiItemAgent.valueState
val valueStateFlow: StateFlow<String?>? = item.agentProvider.uiItemAgent.valueState
if (valueStateFlow != null) {
lifecycleScope.launchWhenStarted {
valueStateFlow.collect {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/io/github/qauxv/base/IUiItemAgent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ package io.github.qauxv.base
import android.app.Activity
import android.content.Context
import android.view.View
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

/**
* It's a single "cell" of the UI on the settings page.
Expand Down Expand Up @@ -63,7 +63,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
interface IUiItemAgent {
val titleProvider: (IUiItemAgent) -> String
val summaryProvider: ((IUiItemAgent, Context) -> CharSequence?)?
val valueState: MutableStateFlow<String?>?
val valueState: StateFlow<String?>?
val validator: ((IUiItemAgent) -> Boolean)?
val switchProvider: ISwitchCellAgent?
val onClickListener: ((IUiItemAgent, Activity, View) -> Unit)?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import android.view.View
import io.github.qauxv.base.ISwitchCellAgent
import io.github.qauxv.base.IUiItemAgent
import io.github.qauxv.hook.BaseFunctionHook
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

fun BaseFunctionHook.uiSwitchPreference(init: UiSwitchPreferenceItemFactory.() -> Unit): IUiItemAgent {
val uiSwitchPreferenceFactory = UiSwitchPreferenceItemFactory(this)
Expand All @@ -26,7 +26,7 @@ class UiSwitchPreferenceItemFactory(receiver: BaseFunctionHook) : IUiItemAgent {

override val titleProvider: (IUiItemAgent) -> String = { title }
override val summaryProvider: ((IUiItemAgent, Context) -> String?) = { _, _ -> summary }
override val valueState: MutableStateFlow<String?>? = null
override val valueState: StateFlow<String?>? = null
override val validator: ((IUiItemAgent) -> Boolean) = { receiver.isAvailable }
override val switchProvider: ISwitchCellAgent by lazy {
object : ISwitchCellAgent {
Expand All @@ -49,7 +49,7 @@ class UiClickableItemFactory(receiver: BaseFunctionHook) : IUiItemAgent {

override val titleProvider: (IUiItemAgent) -> String = { title }
override val summaryProvider: ((IUiItemAgent, Context) -> String?) = { _, _ -> summary }
override val valueState: MutableStateFlow<String?>? = null
override val valueState: StateFlow<String?>? = null
override val validator: ((IUiItemAgent) -> Boolean) = { receiver.isAvailable }
override val switchProvider: ISwitchCellAgent? = null
override var onClickListener: ((IUiItemAgent, Activity, View) -> Unit)? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import io.github.qauxv.util.SyncUtils.async
import io.github.qauxv.util.SyncUtils.runOnUiThread
import io.github.qauxv.util.UiThread
import io.github.qauxv.util.isInHostProcess
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import me.singleneuron.util.forSuBanXia

class SettingsMainFragment : BaseRootLayoutFragment() {
Expand Down Expand Up @@ -158,7 +158,7 @@ class SettingsMainFragment : BaseRootLayoutFragment() {
for (i in itemList.indices) {
val item = itemList[i]
if (item is UiAgentItem) {
val valueStateFlow: MutableStateFlow<String?>? = item.agentProvider.uiItemAgent.valueState
val valueStateFlow: StateFlow<String?>? = item.agentProvider.uiItemAgent.valueState
if (valueStateFlow != null) {
lifecycleScope.launchWhenResumed {
valueStateFlow.collect {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import android.content.Context
import io.github.qauxv.base.ISwitchCellAgent
import io.github.qauxv.base.IUiItemAgent
import io.github.qauxv.base.IUiItemAgentProvider
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

/**
* Just a button to be shown [IUiItemAgent]
Expand All @@ -37,7 +37,7 @@ abstract class BasePlainUiAgentItem(
) : IUiItemAgent, IUiItemAgentProvider {
override val titleProvider: (IUiItemAgent) -> String = { title }
override val summaryProvider: ((IUiItemAgent, Context) -> CharSequence?) = { _, _ -> description }
override val valueState: MutableStateFlow<String?>? = null
override val valueState: StateFlow<String?>? = null
override val validator: ((IUiItemAgent) -> Boolean)? = null
override val switchProvider: ISwitchCellAgent? = null
override val extraSearchKeywordProvider: ((IUiItemAgent, Context) -> Array<String>?)? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import io.github.qauxv.base.ISwitchCellAgent
import io.github.qauxv.base.IUiItemAgent
import io.github.qauxv.util.SyncUtils
import io.github.qauxv.util.dexkit.DexKitTarget
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

/**
* A function that has a custom configuration UI. They usually do NOT have a switch.
Expand Down Expand Up @@ -59,7 +59,7 @@ abstract class CommonConfigFunctionHook(
* The current human-readable state of the function(optional).
* Keep it as short as possible(e.g. "5 enabled", "disabled", no more than 10 characters).
*/
abstract val valueState: MutableStateFlow<String?>?
abstract val valueState: StateFlow<String?>?

/**
* Called when the function UI item is clicked.
Expand All @@ -83,7 +83,7 @@ abstract class CommonConfigFunctionHook(
pangu_spacing(description.toString())
else description
}
override val valueState: MutableStateFlow<String?>?
override val valueState: StateFlow<String?>?
get() = this@CommonConfigFunctionHook.valueState
override val validator: ((IUiItemAgent) -> Boolean) = { _ -> true }
override val switchProvider: ISwitchCellAgent? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import io.github.qauxv.base.ISwitchCellAgent
import io.github.qauxv.base.IUiItemAgent
import io.github.qauxv.util.SyncUtils
import io.github.qauxv.util.dexkit.DexKitTarget
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

/**
* A function that only has a enable/disable switch function.
Expand Down Expand Up @@ -74,7 +74,7 @@ abstract class CommonSwitchFunctionHook(
pangu_spacing(description.toString())
else description
}
override val valueState: MutableStateFlow<String?>? = null
override val valueState: StateFlow<String?>? = null
override val validator: ((IUiItemAgent) -> Boolean) = { _ -> true }
override val switchProvider: ISwitchCellAgent? by lazy {
object : ISwitchCellAgent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import android.view.View
import io.github.qauxv.base.ISwitchCellAgent
import io.github.qauxv.base.IUiItemAgent
import io.github.qauxv.util.dexkit.DexKitTarget
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

abstract class BaseSwitchFunctionDecorator(
hookKey: String? = null,
Expand All @@ -53,7 +53,7 @@ abstract class BaseSwitchFunctionDecorator(
private fun uiItemAgent() = object : IUiItemAgent {
override val titleProvider: (IUiItemAgent) -> String = { _ -> name }
override val summaryProvider: (IUiItemAgent, Context) -> CharSequence? = { _, _ -> description }
override val valueState: MutableStateFlow<String?>? = null
override val valueState: StateFlow<String?>? = null
override val validator: ((IUiItemAgent) -> Boolean) = { _ -> true }
override val switchProvider: ISwitchCellAgent? by lazy {
object : ISwitchCellAgent {
Expand Down

0 comments on commit 9ef2f48

Please sign in to comment.