Skip to content

Commit c8bb9ac

Browse files
committed
update mosaic, doesnt really work rn
1 parent 8c7ae4f commit c8bb9ac

File tree

9 files changed

+64
-67
lines changed

9 files changed

+64
-67
lines changed

.editorconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[*.{kt,kts}]
2+
ktlint_code_style = intellij_idea

.idea/misc.xml

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle.kts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,4 @@ buildscript {
1313
google()
1414
gradlePluginPortal()
1515
}
16-
dependencies {
17-
classpath("com.jakewharton.mosaic:mosaic-gradle-plugin:0.4.0")
18-
}
19-
}
20-
21-
//allprojects {
22-
// repositories {
23-
// google()
24-
// mavenCentral()
25-
// }
26-
//}
16+
}

cli.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
./gradlew cli:installDist
2+
./cli/build/install/cli/bin/cli

cli/build.gradle.kts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
plugins {
22
kotlin("jvm")
3-
id("com.jakewharton.mosaic")
43
id("application")
4+
alias(libs.plugins.compose.compiler)
55
}
66

77
group = "org.example"
88
version = "1.0-SNAPSHOT"
99

10-
1110
dependencies {
1211
implementation(project(":scryfall"))
1312
implementation(project(":carddb"))
1413
implementation(project(":collection-import"))
1514
implementation(libs.bundles.base)
15+
implementation(libs.mosaic)
16+
implementation(libs.koin.compose)
1617
implementation("org.jline:jline:3.22.0")
1718
}
1819

1920
application {
2021
mainClass.set("CliKt")
21-
}
22+
}

cli/src/main/kotlin/Cli.kt

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
import androidx.compose.runtime.Composable
2+
import androidx.compose.runtime.LaunchedEffect
23
import androidx.compose.runtime.collectAsState
34
import androidx.compose.runtime.getValue
45
import androidx.compose.runtime.mutableStateOf
56
import androidx.compose.runtime.remember
67
import androidx.compose.runtime.setValue
7-
import com.jakewharton.mosaic.Color
8-
import com.jakewharton.mosaic.Column
9-
import com.jakewharton.mosaic.Text
8+
import co.touchlab.kermit.Logger
9+
import com.jakewharton.mosaic.layout.KeyEvent
10+
import com.jakewharton.mosaic.layout.onKeyEvent
11+
import com.jakewharton.mosaic.modifier.Modifier
1012
import com.jakewharton.mosaic.runMosaic
13+
import com.jakewharton.mosaic.ui.Color
14+
import com.jakewharton.mosaic.ui.Column
15+
import com.jakewharton.mosaic.ui.Text
1116
import com.mfriend.collection.CollectionImporter
1217
import com.mfriend.collection.CollectionImporterImpl
1318
import com.mfriend.db.databaseModule
14-
import kotlinx.coroutines.Dispatchers
15-
import kotlinx.coroutines.isActive
16-
import kotlinx.coroutines.withContext
19+
import kotlinx.coroutines.awaitCancellation
1720
import models.CardDto
18-
import org.koin.core.KoinApplication
19-
import org.koin.core.context.GlobalContext.startKoin
21+
import org.koin.compose.KoinApplication
22+
import org.koin.compose.koinInject
2023
import org.koin.core.module.dsl.singleOf
21-
import org.koin.core.parameter.parametersOf
2224
import org.koin.dsl.bind
2325
import org.koin.dsl.module
2426
import kotlin.system.exitProcess
@@ -28,15 +30,17 @@ enum class Action(val text: String) {
2830
Parse("Translate CardCastle file"),
2931
ViewCollection("View Collection"),
3032
BuildCube("Build Cube"),
31-
Exit("Exit")
33+
Exit("Exit"),
3234
}
3335

3436
suspend fun main() = runMosaic {
35-
var activeAction by mutableStateOf<Action?>(null)
36-
val koin = initKoin().koin
37-
val viewModel: CliViewModel = koin.get { parametersOf(coroutineContext) }
37+
KoinApplication(application = {
38+
Logger.setLogWriters()
39+
modules(scryfallModule, databaseModule, cliModule)
40+
}) {
41+
var activeAction by mutableStateOf<Action?>(null)
42+
val viewModel: CliViewModel = koinInject()
3843

39-
setContent {
4044
when (activeAction) {
4145
Action.Search -> SearchCardAction(viewModel) { activeAction = null }
4246
Action.Parse -> Parse(viewModel) { activeAction = null }
@@ -45,10 +49,8 @@ suspend fun main() = runMosaic {
4549
Action.Exit -> exitProcess(0)
4650
null -> ActionSelection(viewModel) { activeAction = it }
4751
}
48-
}
49-
withContext(Dispatchers.IO) {
50-
while (isActive) {
51-
// Wait loop
52+
LaunchedEffect(Unit) {
53+
awaitCancellation()
5254
}
5355
}
5456
}
@@ -98,19 +100,22 @@ fun Collection(viewModel: CliViewModel, onComplete: Event) {
98100
@Composable
99101
fun ActionSelection(viewModel: CliViewModel, onSelect: (Action) -> Unit) {
100102
var selection by mutableStateOf(0)
101-
Column {
102-
for (action in Action.values()) {
103-
Text(action.text, background = Color.White.takeIf { selection == action.ordinal })
103+
Column(
104+
modifier = Modifier.onKeyEvent {
105+
println(it)
106+
selection = when (it) {
107+
KeyEvent("ArrowUp") -> (selection - 1).coerceAtLeast(0)
108+
KeyEvent("ArrowDown") -> (selection + 1).coerceAtLeast(0)
109+
KeyEvent("q") -> exitProcess(0)
110+
else -> return@onKeyEvent false
111+
}
112+
true
113+
},
114+
) {
115+
for (action in Action.entries) {
116+
Text(action.text, background = Color.White.takeIf { selection == action.ordinal } ?: Color.Unspecified)
104117
}
105118
}
106-
TrackKeysFlow(
107-
viewModel,
108-
'q' to { exitProcess(0) },
109-
upArrow = { selection = (selection - 1).coerceAtLeast(0) },
110-
downArrow = { selection = (selection + 1).coerceAtMost(Action.values().size - 1) },
111-
onEnter = { onSelect(Action.values()[selection]) },
112-
onSpace = { onSelect(Action.values()[selection]) }
113-
)
114119
}
115120

116121
@Composable
@@ -129,10 +134,6 @@ fun SearchCardAction(viewModel: CliViewModel, onComplete: () -> Unit) {
129134
}
130135
}
131136

132-
fun initKoin(): KoinApplication = startKoin {
133-
modules(scryfallModule, databaseModule, cliModule)
134-
}
135-
136137
private val cliModule = module {
137138
singleOf(::CliViewModel)
138139
singleOf(::CollectionImporterImpl) bind CollectionImporter::class

cli/src/main/kotlin/CliViewModel.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,30 @@ import kotlinx.coroutines.flow.SharingStarted
1414
import kotlinx.coroutines.flow.buffer
1515
import kotlinx.coroutines.flow.flow
1616
import kotlinx.coroutines.flow.shareIn
17-
import kotlinx.coroutines.job
1817
import kotlinx.coroutines.yield
1918
import models.CardDto
2019
import org.jline.terminal.Terminal
2120
import org.jline.terminal.TerminalBuilder
22-
import kotlin.coroutines.CoroutineContext
2321

2422
sealed interface KeyStroke
2523
enum class Thing : KeyStroke {
26-
Delete, Space, Enter, UpArrow, DownArrow
24+
Delete,
25+
Space,
26+
Enter,
27+
UpArrow,
28+
DownArrow,
2729
}
2830

2931
class Character(code: Int) : KeyStroke {
3032
val letter: Char = code.toChar()
3133
}
34+
3235
class CliViewModel(
3336
private val importer: CollectionImporter,
3437
private val database: DatabaseHelper,
3538
private val api: ScryfallApi,
36-
context: CoroutineContext
3739
) {
38-
private val scope = CoroutineScope(context + SupervisorJob(context.job))
40+
private val scope = CoroutineScope(SupervisorJob())
3941
val keyStrokes: SharedFlow<KeyStroke> = flow {
4042
val terminal: Terminal = TerminalBuilder.terminal()
4143
terminal.enterRawMode()
@@ -107,7 +109,5 @@ class CliViewModel(
107109

108110
fun getCards() = database.getCards()
109111

110-
suspend fun searchCards(query: String): List<CardDto>? {
111-
return api.searchCard(query).getOrElse { null }
112-
}
113-
}
112+
suspend fun searchCards(query: String): List<CardDto>? = api.searchCard(query).getOrElse { null }
113+
}

gradle/libs.versions.toml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
[versions]
2-
kotlin = "2.0.0"
2+
kotlin = "2.1.20"
33
kermit = "1.2.2"
44
arrow = "1.2.4"
5-
koin = "3.3.3"
5+
koin = "4.0.3"
66
ktor = "2.0.0"
77
sqlDelight = "2.0.0"
8-
#compose = "1.6.7"
8+
jetbrains-compose = "1.7.3"
99

1010
[libraries]
1111
kermit = { module = "co.touchlab:kermit", version.ref = "kermit" }
1212
coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version = "1.6.4" }
1313
arrow = { module = "io.arrow-kt:arrow-core", version.ref = "arrow" }
1414

1515
koin = { module = "io.insert-koin:koin-core", version.ref = "koin" }
16-
16+
koin-compose = { module = "io.insert-koin:koin-compose", version.ref = "koin" }
1717
ktor-client-core = { name = "ktor-client-core", group = "io.ktor", version.ref = "ktor" }
1818
#ktor-client-jvm = { name = "ktor-client-jvm", group = "io.ktor", version.ref = "ktor" }
1919
ktor-client-okhttp = { name = "ktor-client-okhttp", group = "io.ktor", version.ref = "ktor" }
@@ -23,12 +23,12 @@ ktor-kotlinx-serialization = { name = "ktor-serialization-kotlinx-json", group =
2323
ktor-content-negotiation = { name = "ktor-client-content-negotiation", group = "io.ktor", version.ref = "ktor" }
2424
ktor-logging = { name = "ktor-client-logging", group = "io.ktor", version.ref = "ktor" }
2525

26-
sqldelight-driver-jvm = {name = "sqlite-driver", group = "app.cash.sqldelight", version.ref = "sqlDelight"}
26+
sqldelight-driver-jvm = { name = "sqlite-driver", group = "app.cash.sqldelight", version.ref = "sqlDelight" }
2727
#sqldelight-driver-android = {name = "android-driver", group = "com.squareup.sqldelight", version.ref = "sqlDelight"}
28-
sqldelight-driver-native = {name = "native-driver", group = "app.cash.sqldelight", version.ref = "sqlDelight"}
29-
sqldelight-coroutines= {name = "coroutines-extensions", group = "app.cash.sqldelight", version.ref = "sqlDelight"}
28+
sqldelight-driver-native = { name = "native-driver", group = "app.cash.sqldelight", version.ref = "sqlDelight" }
29+
sqldelight-coroutines = { name = "coroutines-extensions", group = "app.cash.sqldelight", version.ref = "sqlDelight" }
3030

31-
mosaic = { module = "com.jakewharton.mosaic:mosaic-runtime", version = "0.14.0" }
31+
mosaic = { module = "com.jakewharton.mosaic:mosaic-runtime", version = "0.16.0" }
3232

3333
[plugins]
3434
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }

scryfall/src/commonMain/kotlin/client/newKtorClient.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ package client
33
import io.ktor.client.HttpClient
44
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
55
import io.ktor.client.plugins.logging.LogLevel
6-
import io.ktor.client.plugins.logging.Logger
76
import io.ktor.client.plugins.logging.Logging
8-
import io.ktor.client.plugins.logging.SIMPLE
97
import io.ktor.serialization.kotlinx.json.json
108
import kotlinx.serialization.json.Json
119

0 commit comments

Comments
 (0)