xetra11
11/28/2020, 8:36 PMTimo Drick
11/28/2020, 10:17 PMIgor Demin
11/29/2020, 10:59 AMcreate a class which holds all states and share this class with every view component that needs accessI would implement something like this (split implementation into View/Model):
import androidx.compose.desktop.AppManager
import androidx.compose.desktop.Window
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.onActive
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import <http://androidx.compose.ui.window.Menu|androidx.compose.ui.window.Menu>
import androidx.compose.ui.window.MenuBar
import androidx.compose.ui.window.MenuItem
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File
import javax.swing.UIManager
fun main() {
System.setProperty("apple.laf.useScreenMenuBar", "true")
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
val model = TextViewer()
initMenu(model)
Window {
TextViewerView(model)
}
}
private fun initMenu(textViewer: TextViewer) {
AppManager.setMenu(
MenuBar(
Menu(
name = "File",
MenuItem(
name = "Open",
onClick = {
textViewer.loadFile(File("D:/1.txt"))
}
),
)
)
)
}
@Composable
fun TextViewerView(textViewer: TextViewer) {
val scope = rememberCoroutineScope()
onActive {
textViewer.scope = scope
onDispose {
textViewer.scope = null
}
}
Text(textViewer.text)
}
class TextViewer {
var text by mutableStateOf("")
var scope: CoroutineScope? = null
fun loadFile(file: File) {
scope?.launch(<http://Dispatchers.IO|Dispatchers.IO>) {
text = file.readText()
}
}
}
Timo Drick
11/29/2020, 12:53 PM@Composable
fun TextViewerView(textViewer: TextViewer) {
Text(textViewer.text)
}
class TextViewer {
var text by mutableStateOf("")
fun loadFile(file: File) {
GlobalScope.launch(<http://Dispatchers.IO|Dispatchers.IO>) {
text = file.readText()
}
}
}
Igor Demin
11/29/2020, 1:36 PMNot necessary to use the coroutine scope of the view.It is good thing if we cancel coroutineScope (and also file loading) if we leave Composable view. We can use own coroutineScope inside TextViewer, but we should cancel it if we leave Composable (or close the window)
Timo Drick
11/29/2020, 7:32 PMfun main() {
System.setProperty("apple.laf.useScreenMenuBar", "true")
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
val model = TextViewer()
initMenu(model)
Window {
TextViewerView(model)
onDispose {
model.cancel()
}
}
}private fun initMenu(textViewer: TextViewer) {
AppManager.setMenu(
MenuBar(
Menu(
name = "File",
MenuItem(
name = "Open",
onClick = {
textViewer.loadFile(File("D:/1.txt"))
}
),
)
)
)
}
@Composable
fun TextViewerView(textViewer: TextViewer) {
Text(textViewer.text)
}
class TextViewer : CoroutineScope {
private val job = Job()
override val coroutineContext = Dispatchers.Main + job
var text by mutableStateOf("")
fun loadFile(file: File) {
launch(<http://Dispatchers.IO|Dispatchers.IO>) {
text = file.readText()
}
}
fun cancel() {
job.cancel()
}
}
Igor Demin
11/29/2020, 8:08 PMfun main() = Window {
val scope = rememberCoroutineScope()
val model = remember { TextViewer(scope) }
onActive {
initMenu(model)
}
TextViewerView(model)
}
@Composable
fun TextViewerView(textViewer: TextViewer) {
Text(textViewer.text)
}
class TextViewer(private val scope: CoroutineScope) {
var text by mutableStateOf("")
fun loadFile(file: File) {
scope.launch(<http://Dispatchers.IO|Dispatchers.IO>) {
text = file.readText()
}
}
}
Timo Drick
11/30/2020, 12:39 AMIgor Demin
11/30/2020, 7:55 AMYour model should be independent from the viewIt is. But it should live only if Composable view lives. So we can manually create/dispose it in Composable or just use already suitable scope and inject it into the model during its construction. It is controversial architectural question, there is approaches when the model is created indirectly, outside of the view, and the view is just a visual mirror of the model.
Timo Drick
11/30/2020, 3:38 PM