Skip to content
Snippets Groups Projects
Commit bae34633 authored by Sebastian's avatar Sebastian
Browse files

Move application{} block outside of BriarUI

parent 0637d8eb
No related branches found
No related tags found
1 merge request!35Make it possible to run multiple versions of briar-desktop concurrently
package org.briarproject.briar.desktop package org.briarproject.briar.desktop
import androidx.compose.ui.ExperimentalComposeUiApi import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.window.application
import com.github.ajalt.clikt.core.CliktCommand import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.options.counted import com.github.ajalt.clikt.parameters.options.counted
import com.github.ajalt.clikt.parameters.options.default import com.github.ajalt.clikt.parameters.options.default
...@@ -63,7 +64,9 @@ private class Main : CliktCommand( ...@@ -63,7 +64,9 @@ private class Main : CliktCommand(
BrambleCoreEagerSingletons.Helper.injectEagerSingletons(app) BrambleCoreEagerSingletons.Helper.injectEagerSingletons(app)
BriarCoreEagerSingletons.Helper.injectEagerSingletons(app) BriarCoreEagerSingletons.Helper.injectEagerSingletons(app)
app.getBriarUi().start() application {
app.getBriarUi().start(this)
}
} }
private fun getDataDir(): Path { private fun getDataDir(): Path {
......
package org.briarproject.briar.desktop.ui package org.briarproject.briar.desktop.ui
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import androidx.compose.ui.window.ApplicationScope
import androidx.compose.ui.window.Window import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import org.briarproject.bramble.api.account.AccountManager import org.briarproject.bramble.api.account.AccountManager
import org.briarproject.bramble.api.lifecycle.LifecycleManager import org.briarproject.bramble.api.lifecycle.LifecycleManager
import org.briarproject.bramble.api.lifecycle.LifecycleManager.LifecycleState.RUNNING import org.briarproject.bramble.api.lifecycle.LifecycleManager.LifecycleState.RUNNING
...@@ -34,7 +35,8 @@ enum class Screen { ...@@ -34,7 +35,8 @@ enum class Screen {
interface BriarUi { interface BriarUi {
fun start() @Composable
fun start(applicationScope: ApplicationScope)
fun stop() fun stop()
} }
...@@ -67,51 +69,51 @@ constructor( ...@@ -67,51 +69,51 @@ constructor(
} }
} }
override fun start() { @Composable
application { override fun start(applicationScope: ApplicationScope) {
val (isDark, setDark) = remember { mutableStateOf(true) } val (isDark, setDark) = remember { mutableStateOf(true) }
val title = i18n("main.title") val title = i18n("main.title")
var screenState by remember { var screenState by remember {
mutableStateOf( mutableStateOf(
if (accountManager.hasDatabaseKey()) { if (accountManager.hasDatabaseKey()) {
// this should only happen during testing when we launch the main UI directly // this should only happen during testing when we launch the main UI directly
// without a need to enter the password. // without a need to enter the password.
contactListViewModel.loadContacts() contactListViewModel.loadContacts()
Screen.MAIN Screen.MAIN
} else if (accountManager.accountExists()) { } else if (accountManager.accountExists()) {
Screen.LOGIN Screen.LOGIN
} else { } else {
Screen.REGISTRATION Screen.REGISTRATION
} }
) )
} }
Window( Window(
title = title, title = title,
onCloseRequest = { stop(); exitApplication() }, onCloseRequest = { stop(); applicationScope.exitApplication() },
) { ) {
window.minimumSize = Dimension(800, 600) window.minimumSize = Dimension(800, 600)
BriarTheme(isDarkTheme = isDark) { BriarTheme(isDarkTheme = isDark) {
when (screenState) { when (screenState) {
Screen.REGISTRATION -> Screen.REGISTRATION ->
Registration(registrationViewModel) { Registration(registrationViewModel) {
screenState = Screen.MAIN contactListViewModel.loadContacts()
} screenState = Screen.MAIN
Screen.LOGIN -> }
Login(loginViewModel) { Screen.LOGIN ->
contactListViewModel.loadContacts() Login(loginViewModel) {
screenState = Screen.MAIN contactListViewModel.loadContacts()
} screenState = Screen.MAIN
else -> }
MainScreen( else ->
contactListViewModel, MainScreen(
conversationViewModel, contactListViewModel,
addContactViewModel, conversationViewModel,
introductionViewModel, addContactViewModel,
sidebarViewModel, introductionViewModel,
isDark, sidebarViewModel,
setDark isDark,
) setDark
} )
} }
} }
} }
......
package org.briarproject.briar.desktop package org.briarproject.briar.desktop
import androidx.compose.ui.ExperimentalComposeUiApi import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.window.application
import org.briarproject.bramble.BrambleCoreEagerSingletons import org.briarproject.bramble.BrambleCoreEagerSingletons
import org.briarproject.briar.BriarCoreEagerSingletons import org.briarproject.briar.BriarCoreEagerSingletons
import org.briarproject.briar.desktop.utils.FileUtils import org.briarproject.briar.desktop.utils.FileUtils
...@@ -56,7 +57,9 @@ internal class RunWithTemporaryAccount(val customization: BriarDesktopTestApp.() ...@@ -56,7 +57,9 @@ internal class RunWithTemporaryAccount(val customization: BriarDesktopTestApp.()
// list yet, we need to wait a moment in order for that to finish (hopefully). // list yet, we need to wait a moment in order for that to finish (hopefully).
Thread.sleep(1000) Thread.sleep(1000)
app.getBriarUi().start() application {
app.getBriarUi().start(this)
}
} }
private fun getDataDir(): Path { private fun getDataDir(): Path {
......
package org.briarproject.briar.desktop
import androidx.compose.runtime.Composable
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.window.ApplicationScope
import androidx.compose.ui.window.application
import org.briarproject.bramble.BrambleCoreEagerSingletons
import org.briarproject.briar.BriarCoreEagerSingletons
import org.briarproject.briar.desktop.utils.FileUtils
import java.io.IOException
import java.nio.file.Files
import java.nio.file.Path
import java.util.logging.Level.INFO
import java.util.logging.LogManager
import java.util.logging.Logger
import kotlin.io.path.absolute
fun main(args: Array<String>) = TestWithTwoTemporaryAccounts().run()
internal class TestWithTwoTemporaryAccounts() {
companion object {
private val LOG = Logger.getLogger(TestWithTwoTemporaryAccounts::class.java.name)
}
@OptIn(ExperimentalComposeUiApi::class)
fun run() {
LogManager.getLogManager().getLogger("").level = INFO
application {
app(this, "alice")
app(this, "bob")
}
}
@Composable
private fun app(applicationScope: ApplicationScope, name: String) {
val dataDir = getDataDir()
LOG.info("Using data directory '$dataDir'")
val app =
DaggerBriarDesktopTestApp.builder().desktopTestModule(
DesktopTestModule(dataDir.toFile())
).build()
app.getShutdownManager().addShutdownHook {
LOG.info("deleting temporary account at $dataDir")
org.apache.commons.io.FileUtils.deleteDirectory(dataDir.toFile())
}
// We need to load the eager singletons directly after making the
// dependency graphs
BrambleCoreEagerSingletons.Helper.injectEagerSingletons(app)
BriarCoreEagerSingletons.Helper.injectEagerSingletons(app)
val lifecycleManager = app.getLifecycleManager()
val accountManager = app.getAccountManager()
val password = "verySecret123!"
accountManager.createAccount(name, password)
val dbKey = accountManager.databaseKey ?: throw AssertionError()
lifecycleManager.startServices(dbKey)
lifecycleManager.waitForStartup()
app.getDeterministicTestDataCreator().createTestData(5, 20, 50)
// Creating test data happens on a background thread. As we do not get notified about updates to the conact
// list yet, we need to wait a moment in order for that to finish (hopefully).
Thread.sleep(1000)
app.getBriarUi().start(applicationScope)
}
private fun getDataDir(): Path {
val dataDir = Files.createTempDirectory("briar")
if (!Files.exists(dataDir)) {
throw IOException("Could not create directory: ${dataDir.absolute()}")
} else if (!Files.isDirectory(dataDir)) {
throw IOException("Data dir is not a directory: ${dataDir.absolute()}")
}
FileUtils.setRWX(dataDir)
return dataDir
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment