Skip to content

Commit

Permalink
[Console] Remove ComposeLocal
Browse files Browse the repository at this point in the history
  • Loading branch information
sonder-joker committed Dec 12, 2021
1 parent 9be9743 commit defb4a4
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 23 deletions.
16 changes: 6 additions & 10 deletions app/src/main/kotlin/EntryPoint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ package com.youngerhousea.mirai.compose
import androidx.compose.material.Colors
import androidx.compose.material.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.runtime.DisposableEffect
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.window.ApplicationScope
import androidx.compose.ui.window.application
import com.youngerhousea.mirai.compose.console.LocalViewModelStore
import com.youngerhousea.mirai.compose.console.MiraiComposeImplementation
import com.youngerhousea.mirai.compose.console.impl.MiraiCompose
import com.youngerhousea.mirai.compose.ui.HostPage
import net.mamoe.mirai.console.ConsoleFrontEndImplementation
Expand All @@ -23,11 +20,12 @@ fun main() = miraiComposeApplication {

@OptIn(ConsoleFrontEndImplementation::class)
fun miraiComposeApplication(content: @Composable ApplicationScope.() -> Unit) {
MiraiCompose.start()
themeApplication {
CompositionLocalProvider(LocalMiraiCompose provides MiraiCompose) {
CompositionLocalProvider(LocalViewModelStore provides MiraiCompose.viewModelStore) {
content()
content()
DisposableEffect(Unit) {
MiraiCompose.start()
onDispose {
MiraiCompose.cancel()
}
}
}
Expand All @@ -43,8 +41,6 @@ fun themeApplication(
}
}

val LocalMiraiCompose =
staticCompositionLocalOf<MiraiComposeImplementation> { error("No MiraiComposeImplementation provided") }

val color = Colors(
primary = Color(0xFF00b0ff),
Expand Down
1 change: 0 additions & 1 deletion app/src/main/kotlin/console/MiraiComposeImplementation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import net.mamoe.mirai.console.plugin.jvm.JvmPlugin

interface MiraiComposeImplementation :
MiraiConsoleImplementation,
ViewModelStoreOwner,
LifecycleOwner {
override val configStorageForBuiltIns: ReadablePluginConfigStorage

Expand Down
7 changes: 2 additions & 5 deletions app/src/main/kotlin/console/ViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.youngerhousea.mirai.compose.console

import androidx.compose.runtime.Composable
import androidx.compose.runtime.staticCompositionLocalOf
import com.youngerhousea.mirai.compose.console.impl.ViewModelStoreImpl
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
Expand All @@ -26,10 +27,6 @@ interface ViewModelStore {
fun clean()
}

interface ViewModelStoreOwner {
val viewModelStore: ViewModelStore
}

inline fun <reified T : ViewModel> ViewModelStore.get(): T? {
return get(T::class) as T?
}
Expand All @@ -42,7 +39,7 @@ inline fun <reified T : ViewModel> ViewModelStore.getOrCreate(key: Any, crossinl
return get(key) as? T ?: factory().apply { put(key, this) }
}

val LocalViewModelStore = staticCompositionLocalOf<ViewModelStore> { error("ViewModel Store not provide") }
val LocalViewModelStore = staticCompositionLocalOf<ViewModelStore> { ViewModelStoreImpl() }

@Composable
inline fun <reified T : ViewModel> viewModel(): T? {
Expand Down
6 changes: 1 addition & 5 deletions app/src/main/kotlin/console/impl/MiraiComposeImpl.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.youngerhousea.mirai.compose.console.impl

import com.youngerhousea.mirai.compose.console.MiraiComposeImplementation
import com.youngerhousea.mirai.compose.console.ViewModelStore
import com.youngerhousea.mirai.compose.console.ViewModelStoreOwner
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.MutableStateFlow
import mirai_compose.app.BuildConfig
Expand All @@ -28,13 +26,12 @@ import kotlin.io.path.div


/**
* Create a [MiraiComposeImplementation], this implementation for [MiraiConsoleImplementation], extend [ViewModelStoreOwner], [LifecycleOwner] and [CoroutineScope]
* Create a [MiraiComposeImplementation], this implementation for [MiraiConsoleImplementation], extend [LifecycleOwner] and [CoroutineScope]
*
*/
val MiraiCompose: MiraiComposeImplementation get() = MiraiComposeImpl

internal object MiraiComposeImpl : MiraiComposeImplementation {
override val viewModelStore: ViewModelStore = ViewModelStoreImpl()

private val logger by lazy { createLogger("compose") }

Expand Down Expand Up @@ -121,7 +118,6 @@ internal object MiraiComposeImpl : MiraiComposeImplementation {

override fun cancel() {
lifecycle.onDestroy()
viewModelStore.clean()
cancel("Normal Exit")
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/kotlin/ui/log/ConsoleLog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import androidx.compose.ui.input.key.*
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.unit.dp
import com.youngerhousea.mirai.compose.LocalMiraiCompose
import com.youngerhousea.mirai.compose.console.impl.Log
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
Expand All @@ -32,7 +32,7 @@ import java.util.*
@Composable
fun ConsoleLog(consoleLog: ConsoleLog = viewModel { ConsoleLogViewModel() }) {
val state by consoleLog.state
val log by LocalMiraiCompose.current.logStorage.collectAsState()
val log by MiraiCompose.logStorage.collectAsState()

Scaffold(
modifier = Modifier.onPreviewCtrlFDown { consoleLog.dispatch(ConsoleLogAction.SetSearchBar) },
Expand Down

0 comments on commit defb4a4

Please sign in to comment.