在jetpack compose/Kotlin应用程序中创建多个applicationScope

ktecyv1j  于 2023-03-13  发布在  Kotlin
关注(0)|答案(1)|浏览(162)

在一个jetpack compose/Kotlin中有可能有多个应用范围吗?
我想管理的第一个启动界面和主界面(托盘)在2个不同的应用程序范围没有混合代码。
我在想这样的事情:

suspend fun main() = coroutineScope {
    val repo = MyRepository()
    initDefaultUserConfigIfNeeded(repo)
    startDesktop(repo)
}

suspend fun initDefaultUserConfigIfNeeded(repo: MyRepository) {
    if (repo.isFirstStart()) {
        val defaultPath = getUserConfig(repo)

        repo.addContentRepository(defaultPath)
        repo.markApplicationStartedOnce()
    }
}

suspend fun getUserConfig(repo: MyRepository) = coroutineScope {
    suspendCoroutine { cont ->
        val defaultManagedDir = repo.config.getDefaultManagedDirectory()

        startFirstStartInterface(defaultManagedDir) {
            cont.resume(it)
        }
    }
}

fun startFirstStartInterface(default: String, onSelectedPath: (info: String) -> Unit) {
    application {
        Window(onCloseRequest = { exitApplication() }) {
            Text("here my init interface to select path on first start")
            Button(onClick = { exitApplication(); onSelectedPath("/tmp") } )
        }
    }
}

fun startMainApplication(repo: MyRepository) {
    application {
        val isMainWindowVisible = remember { mutableStateOf(false) }

        Tray(menu = Item("Open", onClick = { isMainWindowVisible.value = true }))

        Window(
            onCloseRequest = { exitApplication() },
            visible = isMainWindowVisible.value,
        ) {
            Text("here my main interface")
        }
    }
}

我的问题是exitApplication方法终止了应用程序,并且不让代码继续。

wkyowqbh

wkyowqbh1#

感谢@Tenfour4(再次),下面是我最终实现它的方法:

suspend fun main() = coroutineScope {
    val repo = MyRepository()
    initDefaultUserConfigIfNeeded(repo)
    startDesktop(repo)
}

suspend fun initDefaultUserConfigIfNeeded(repo: MyRepository) {
    if (repo.isFirstStart()) {
        val defaultPath = getUserConfig(repo)

        repo.addContentRepository(defaultPath)
        repo.markApplicationStartedOnce()
    }
}

suspend fun getUserConfig(repo: MyRepository) = coroutineScope {
    var info = String? = null

    val defaultManagedDir = repo.config.getDefaultManagedDirectory()
    startFirstStartInterface(
        default = defaultManagedDir,
        onSelectedPath = { info = it; exitApplication() },
        onClose = { exitApplication() },
    )
    info
}

fun startFirstStartInterface(default: String, onSelectedPath: (info: String) -> Unit) {
    awaitApplication {
        Window(onCloseRequest = { onClose() }) {
            Text("here my init interface to select path on first start")
            Button(onClick = { onSelectedPath("/tmp") } )
        }
    }
}

fun startMainApplication(repo: MyRepository) {
    application {
        val isMainWindowVisible = remember { mutableStateOf(false) }

        Tray(menu = Item("Open", onClick = { isMainWindowVisible.value = true }))

        Window(
            onCloseRequest = { exitApplication() },
            visible = isMainWindowVisible.value,
        ) {
            Text("here my main interface")
        }
    }
}

相关问题