Skip to content

Commit 20a7154

Browse files
committed
Async Highlight
1 parent 2e31be8 commit 20a7154

File tree

5 files changed

+41
-8
lines changed

5 files changed

+41
-8
lines changed

Diff for: application/build.gradle.kts

+5
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,16 @@ kotlin {
3636
implementation(compose.components.resources)
3737
implementation(compose.components.uiToolingPreview)
3838

39+
implementation(libs.kotlinx.coroutines.core)
40+
3941
implementation(libs.voyager.screenModel)
4042
implementation(libs.voyager.navigator)
4143
}
4244

4345
desktopMain.dependencies {
46+
47+
implementation(libs.kotlinx.coroutines.swing)
48+
4449
implementation(compose.desktop.currentOs)
4550
}
4651
}

Diff for: application/src/androidMain/kotlin/com/neoutils/json/ui/CodeEditor.android.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ actual fun CodeEditor(
7070
),
7171
onValueChange = {
7272
textFieldValue.value = it
73-
onCodeChange(it.text)
73+
74+
if(code != it.text) {
75+
onCodeChange(it.text)
76+
}
7477
},
7578
textStyle = mergedTextStyle.copy(
7679
lineHeightStyle = LineHeightStyle(

Diff for: application/src/commonMain/kotlin/com/neoutils/json/ui/AppViewModel.kt

+23-6
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ import androidx.compose.ui.graphics.Color
44
import androidx.compose.ui.text.AnnotatedString
55
import androidx.compose.ui.text.SpanStyle
66
import cafe.adriel.voyager.core.model.ScreenModel
7+
import cafe.adriel.voyager.core.model.screenModelScope
78
import com.neoutils.json.util.JsonHighlight
8-
import com.neoutils.json.util.Token
9+
import kotlinx.coroutines.Dispatchers
10+
import kotlinx.coroutines.Job
11+
import kotlinx.coroutines.delay
912
import kotlinx.coroutines.flow.MutableStateFlow
13+
import kotlinx.coroutines.flow.asStateFlow
14+
import kotlinx.coroutines.launch
1015

1116
class AppViewModel : ScreenModel {
1217

@@ -18,14 +23,26 @@ class AppViewModel : ScreenModel {
1823
nullColor = Color(0xffe41500)
1924
)
2025

21-
val code = MutableStateFlow("")
26+
private var highlightJob: Job? = null
2227

23-
val highlight = MutableStateFlow(listOf<AnnotatedString.Range<SpanStyle>>())
28+
private val _code = MutableStateFlow("")
29+
val code = _code.asStateFlow()
2430

25-
fun onCodeChange(it: String) {
31+
private val _highlight = MutableStateFlow(listOf<AnnotatedString.Range<SpanStyle>>())
32+
val highlight = _highlight.asStateFlow()
2633

27-
code.value = it
34+
fun onCodeChange(code: String) {
2835

29-
highlight.value = jsonHighlight(it).spanStyles
36+
_code.value = code
37+
38+
updateHighlight(code)
39+
}
40+
41+
private fun updateHighlight(text: String) {
42+
43+
highlightJob?.cancel()
44+
highlightJob = screenModelScope.launch(Dispatchers.IO) {
45+
_highlight.value = jsonHighlight(text).spanStyles
46+
}
3047
}
3148
}

Diff for: application/src/desktopMain/kotlin/com/neoutils/json/ui/CodeEditor.desktop.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ actual fun CodeEditor(
7878
scrollState = scrollState,
7979
onValueChange = {
8080
textFieldValue.value = it
81-
onCodeChange(it.text)
81+
82+
if(code != it.text) {
83+
onCodeChange(it.text)
84+
}
8285
},
8386
textStyle = mergedTextStyle.copy(
8487
lineHeightStyle = LineHeightStyle(

Diff for: gradle/libs.versions.toml

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ compose-plugin = "1.6.1"
55
kotlin = "1.9.22"
66
androidx-activity = "1.8.2"
77
voyager = "1.0.0"
8+
coroutines = "1.8.0"
89

910
[libraries]
1011

@@ -15,6 +16,10 @@ androidx-compose-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling
1516
androidx-activity = { module = "androidx.activity:activity", version.ref = "androidx-activity" }
1617
androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activity" }
1718

19+
# kotlinx
20+
kotlinx-coroutines-swing = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-swing", version.ref = "coroutines" }
21+
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" }
22+
1823
# voyager
1924
voyager-navigator = { module = "cafe.adriel.voyager:voyager-navigator", version.ref = "voyager" }
2025
voyager-screenModel = { module = "cafe.adriel.voyager:voyager-screenmodel", version.ref = "voyager" }

0 commit comments

Comments
 (0)