Skip to content

Commit

Permalink
[Fix] Update name
Browse files Browse the repository at this point in the history
  • Loading branch information
sonder-joker committed Dec 30, 2021
1 parent fe297a8 commit 26a0647
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 74 deletions.
2 changes: 1 addition & 1 deletion app/src/main/kotlin/ui/HostPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fun HostPage() {
fun NavHost(
hostViewModel: Host = viewModel { HostViewModel() }
) {
val state by hostViewModel.hostState
val state by hostViewModel.state

HorizontalSplitPane {
first(minSize = MinFirstSize) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/kotlin/ui/NavHostBotMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import net.mamoe.mirai.Bot
fun NavHostFirstBotMenu(
hostViewModel: Host = viewModel { HostViewModel() },
) {
val state by hostViewModel.hostState
val state by hostViewModel.state

NavHostFirstBotMenuContent(
isExpand = state.menuExpand,
Expand Down
9 changes: 4 additions & 5 deletions app/src/main/kotlin/ui/log/ConsoleLog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import com.youngerhousea.mirai.compose.console.impl.MiraiCompose
import com.youngerhousea.mirai.compose.console.viewModel
import com.youngerhousea.mirai.compose.resource.R
import com.youngerhousea.mirai.compose.viewmodel.ConsoleLog
import com.youngerhousea.mirai.compose.viewmodel.ConsoleLogAction
import com.youngerhousea.mirai.compose.viewmodel.ConsoleLogViewModel
import java.util.*

Expand All @@ -38,12 +37,12 @@ fun ConsoleLog(consoleLog: ConsoleLog = viewModel { ConsoleLogViewModel() }) {
val log by MiraiCompose.logStorage

Scaffold(
modifier = Modifier.onPreviewCtrlFDown { consoleLog.dispatch(ConsoleLogAction.SetSearchBar) },
modifier = Modifier.onPreviewCtrlFDown { consoleLog.dispatch(ConsoleLog.Action.SetSearchBar) },
topBar = {
if (state.searchBarVisible)
TextField(
value = state.searchContent,
onValueChange = { consoleLog.dispatch(ConsoleLogAction.UpdateSearchContent(it)) },
onValueChange = { consoleLog.dispatch(ConsoleLog.Action.UpdateSearchContent(it)) },
leadingIcon = { Icon(R.Icon.Search, null) },
modifier = Modifier
.fillMaxWidth()
Expand Down Expand Up @@ -74,12 +73,12 @@ fun ConsoleLog(consoleLog: ConsoleLog = viewModel { ConsoleLogViewModel() }) {
command = state.currentCommand,
onCommandChange = { commandContent ->
consoleLog.dispatch(
ConsoleLogAction.UpdateCurrentCommand(
ConsoleLog.Action.UpdateCurrentCommand(
commandContent
)
)
},
onClick = { consoleLog.dispatch(ConsoleLogAction.EnterCommand) },
onClick = { consoleLog.dispatch(ConsoleLog.Action.EnterCommand) },
modifier = Modifier
.weight(1f)
.padding(horizontal = 40.dp),
Expand Down
14 changes: 7 additions & 7 deletions app/src/main/kotlin/ui/plugins/Plugins.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.window.AwtWindow
import com.youngerhousea.mirai.compose.console.viewModel
import com.youngerhousea.mirai.compose.viewmodel.PluginsRoute
import com.youngerhousea.mirai.compose.viewmodel.Plugins
import com.youngerhousea.mirai.compose.viewmodel.PluginsViewModel
import net.mamoe.mirai.console.plugin.PluginManager
import java.awt.FileDialog
import java.awt.Frame

@Composable
fun Plugins(pluginsViewModel: PluginsViewModel = viewModel { PluginsViewModel() }) {
val state by pluginsViewModel.state
fun Plugins(plugins: Plugins = viewModel { PluginsViewModel() }) {
val state by plugins.state

if(state.isFileChooserVisible) {
FileDialog {
Expand All @@ -21,15 +21,15 @@ fun Plugins(pluginsViewModel: PluginsViewModel = viewModel { PluginsViewModel()
}

when (val route = state.navigate) {
PluginsRoute.List ->
Plugins.Route.List ->
PluginList(
plugins = PluginManager.plugins,
onPluginClick = { pluginsViewModel.dispatch(PluginsRoute.Single(it)) },
onPluginClick = { plugins.dispatch(Plugins.Route.Single(it)) },
)
is PluginsRoute.Single ->
is Plugins.Route.Single ->
SinglePlugin(
plugin = route.plugin,
onExit = { pluginsViewModel.dispatch(PluginsRoute.List) }
onExit = { plugins.dispatch(Plugins.Route.List) }
)

}
Expand Down
47 changes: 23 additions & 24 deletions app/src/main/kotlin/viewmodel/ConsoleLogViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,49 +28,49 @@ import kotlin.reflect.full.isSubclassOf
*/
interface ConsoleLog {

val state: State<ConsoleLogState>
val state: State<InnerState>

fun dispatch(action: ConsoleLogAction)
}
fun dispatch(action: Action)

sealed interface Action {
class UpdateSearchContent(val content: String) : Action
class UpdateCurrentCommand(val content: String) : Action
object EnterCommand : Action
object SetSearchBar : Action
}

data class ConsoleLogState(
val searchContent: String = "",
val currentCommand: String = "",
val searchBarVisible: Boolean = false
)

sealed interface ConsoleLogAction {
class UpdateSearchContent(val content: String) : ConsoleLogAction
class UpdateCurrentCommand(val content: String) : ConsoleLogAction
object EnterCommand : ConsoleLogAction
object SetSearchBar : ConsoleLogAction
data class InnerState(
val searchContent: String = "",
val currentCommand: String = "",
val searchBarVisible: Boolean = false
)
}



class ConsoleLogViewModel @OptIn(ConsoleInternalApi::class) constructor(
private val logger: MiraiLogger = MiraiConsole.mainLogger,
) : ConsoleLog, ViewModelScope() {
override val state: MutableState<ConsoleLog.InnerState> = mutableStateOf(ConsoleLog.InnerState())

override fun dispatch(action: ConsoleLogAction) {
override fun dispatch(action: ConsoleLog.Action) {
viewModelScope.launch {
state.value = reduce(state.value, action)
}

}

private suspend fun reduce(value: ConsoleLogState, action: ConsoleLogAction): ConsoleLogState {
private suspend fun reduce(value: ConsoleLog.InnerState, action: ConsoleLog.Action): ConsoleLog.InnerState {
return when (action) {
is ConsoleLogAction.UpdateSearchContent -> value.copy(searchContent = action.content)
is ConsoleLogAction.UpdateCurrentCommand -> value.copy(currentCommand = action.content)
is ConsoleLogAction.EnterCommand -> {
is ConsoleLog.Action.UpdateSearchContent -> value.copy(searchContent = action.content)
is ConsoleLog.Action.UpdateCurrentCommand -> value.copy(currentCommand = action.content)
is ConsoleLog.Action.EnterCommand -> {
SolveCommandResult(value.currentCommand, logger)
return value.copy(currentCommand = "")
}
is ConsoleLogAction.SetSearchBar -> value.copy(searchBarVisible = !value.searchBarVisible)
is ConsoleLog.Action.SetSearchBar -> value.copy(searchBarVisible = !value.searchBarVisible)
}
}

override val state: MutableState<ConsoleLogState> = mutableStateOf(ConsoleLogState())

}

//@OptIn(ExperimentalCommandDescriptors::class)
Expand All @@ -85,7 +85,6 @@ class ConsoleLogViewModel @OptIn(ConsoleInternalApi::class) constructor(
// }
//}


@OptIn(ExperimentalCommandDescriptors::class)
private suspend fun SolveCommandResult(
currentCommand: String,
Expand Down
14 changes: 7 additions & 7 deletions app/src/main/kotlin/viewmodel/HostViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import net.mamoe.mirai.event.events.BotOnlineEvent


interface Host {
val hostState: State<HostState>
val state: State<InnerState>

fun dispatch(event: Action)

Expand All @@ -25,7 +25,7 @@ interface Host {
}

@Immutable
data class HostState(
data class InnerState(
val currentBot: Bot? = null,
val botList: List<Bot> = listOf(),
val menuExpand: Boolean = false,
Expand All @@ -43,13 +43,13 @@ interface Host {


class HostViewModel : ViewModelScope(), Host {
override val hostState = mutableStateOf(Host.HostState())
override val state = mutableStateOf(Host.InnerState())

override fun dispatch(event: Host.Action) {
hostState.value = reduce(hostState.value, event)
state.value = reduce(state.value, event)
}

private fun reduce(state: Host.HostState, action: Host.Action): Host.HostState {
private fun reduce(state: Host.InnerState, action: Host.Action): Host.InnerState {
return when (action) {
Host.Action.CloseLoginDialog -> state.copy(dialogExpand = false)
Host.Action.OpenLoginDialog -> state.copy(dialogExpand = true)
Expand All @@ -67,8 +67,8 @@ class HostViewModel : ViewModelScope(), Host {
init {
viewModelScope.launch {
GlobalEventChannel.subscribeAlways<BotOnlineEvent> { event ->
hostState.value =
hostState.value.copy(botList = hostState.value.botList + event.bot, currentBot = event.bot)
state.value =
state.value.copy(botList = state.value.botList + event.bot, currentBot = event.bot)
}
}
}
Expand Down
60 changes: 31 additions & 29 deletions app/src/main/kotlin/viewmodel/PluginsViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,44 @@ import com.youngerhousea.mirai.compose.console.ViewModelScope
import kotlinx.coroutines.launch
import net.mamoe.mirai.console.plugin.Plugin

class PluginsViewModel : ViewModelScope() {
private val _state = mutableStateOf(PluginsState())
interface Plugins {

val state: State<PluginsState> get() = _state
val state: State<InnerState>

fun dispatch(action: PluginAction) {
fun dispatch(action: Action)

sealed interface Route {
object List : Route, Action
class Single(val plugin: Plugin) : Route, Action
}

sealed interface Action {
class LoadingPlugin(val pluginName: String) : Action
object OpenFileChooser : Action
}


data class InnerState(
val navigate: Route = Route.List,
val isFileChooserVisible: Boolean = false
)
}

class PluginsViewModel : Plugins, ViewModelScope() {
override val state = mutableStateOf(Plugins.InnerState())

override fun dispatch(action: Plugins.Action) {
viewModelScope.launch {
_state.value = reduce(action, _state.value)
state.value = reduce(action, state.value)
}
}

private fun reduce(action: PluginAction, state: PluginsState): PluginsState {
private fun reduce(action: Plugins.Action, state: Plugins.InnerState): Plugins.InnerState {
return when (action) {
is PluginsRoute.List -> state.copy(navigate = action)
is PluginsRoute.Single -> state.copy(navigate = action)
is PluginAction.LoadingPlugin -> {
state
}
is PluginAction.OpenFileChooser -> {
state.copy(isFileChooserVisible = true)
}
is Plugins.Route.List -> state.copy(navigate = action)
is Plugins.Route.Single -> state.copy(navigate = action)
is Plugins.Action.LoadingPlugin -> state
is Plugins.Action.OpenFileChooser -> state.copy(isFileChooserVisible = true)
}
}
}

sealed interface PluginsRoute {
object List : PluginsRoute, PluginAction
class Single(val plugin: Plugin) : PluginsRoute, PluginAction
}

sealed interface PluginAction {
class LoadingPlugin(val pluginName: String) : PluginAction
object OpenFileChooser : PluginAction
}

data class PluginsState(
val navigate: PluginsRoute = PluginsRoute.List,
val isFileChooserVisible: Boolean = false
)

0 comments on commit 26a0647

Please sign in to comment.