Skip to content

Commit

Permalink
Support input label, bump version to 0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanhaustein committed Jan 15, 2023
1 parent 52e27a7 commit 67cf9b1
Show file tree
Hide file tree
Showing 29 changed files with 73 additions and 89 deletions.
2 changes: 1 addition & 1 deletion androidApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ android {
minSdk = 21
targetSdk = 33
versionCode = 2
versionName = "0.2.4"
versionName = "0.3.0"
}
buildTypes {
getByName("release") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ class DemoViewModel : ViewModel() {
val title = mutableStateOf("")
val konsole = mutableStateOf<ComposeKonsole>(ComposeKonsole())

fun show(title: String, demo: suspend (read: suspend () -> String, write: (String) -> Unit) -> Unit) {
fun show(title: String, demo: suspend (read: suspend (String?) -> String, write: (String) -> Unit) -> Unit) {
this.title.value = title
konsole.value = ComposeKonsole()
viewModelScope.launch {
demo( {konsole.value.read()}, {konsole.value.write(it)} )
demo( {konsole.value.read(it)}, {konsole.value.write(it)} )
this@DemoViewModel.title.value = ""
}
}
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
}
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.20")
classpath("com.android.tools.build:gradle:7.3.1")
classpath("com.android.tools.build:gradle:7.4.0")
}
}

Expand Down
4 changes: 2 additions & 2 deletions compose/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {


group = "org.kobjects.konsole"
version = "0.2.4"
version = "0.3.0"


dependencies {
Expand Down Expand Up @@ -59,7 +59,7 @@ publishing {
register<MavenPublication>("release") {
groupId = "org.kobjects.konsole"
artifactId = "compose"
version = "0.2.4"
version = "0.3.0"

afterEvaluate {
from(components["release"])
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class ComposeKonsole : Konsole, ViewModel() {
entries.add(Entry(s, input = false))
}

override suspend fun read() = suspendCancellableCoroutine<String> { continuation ->
val request = Request { continuation.resume(it) }
override suspend fun read(label: String?) = suspendCancellableCoroutine<String> { continuation ->
val request = Request(label) { continuation.resume(it) }
requests.add(request)
continuation.invokeOnCancellation {
requests.remove(request)
Expand All @@ -31,5 +31,7 @@ class ComposeKonsole : Konsole, ViewModel() {

data class Entry(val value: String, val input: Boolean)

data class Request(val consumer: (String) -> Unit)
data class Request(
val label: String?,
val consumer: (String) -> Unit)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import androidx.compose.foundation.shape.CornerSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Done
import androidx.compose.material.icons.filled.DoubleArrow
import androidx.compose.material.icons.filled.KeyboardDoubleArrowRight
import androidx.compose.material.icons.filled.Send
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
Expand Down Expand Up @@ -74,6 +79,7 @@ fun RenderKonsole(
Modifier.background(Color(0x77ffffff)),
verticalAlignment = Alignment.CenterVertically,
) {
val topRequest = konsole.requests.lastOrNull()
TextField(
modifier = Modifier.weight(1f),
colors = TextFieldDefaults.textFieldColors(
Expand All @@ -88,15 +94,17 @@ fun RenderKonsole(
onSend = { submit() }),
onValueChange = {
textState.value = it
}
},
label = { if (topRequest != null && topRequest.label != null) Text(topRequest.label) }
)
Button(
IconButton(
modifier = Modifier.padding(4.dp),
enabled = konsole.requests.isNotEmpty(),
onClick = {
submit()
}) {
Text("Enter")
// Text("Enter")
Icon(Icons.Default.Done, "Enter")
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "org.kobjects.konsole"
version = "0.2.4"
version = "0.3.0"

kotlin {
android {
Expand Down
2 changes: 1 addition & 1 deletion core/core.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = 'core'
spec.version = '0.2.3'
spec.version = '0.3.0'
spec.homepage = 'Link to the Shared Module homepage'
spec.source = { :http=> ''}
spec.authors = ''
Expand Down
2 changes: 1 addition & 1 deletion core/src/commonMain/kotlin/org/kobjects/konsole/Konsole.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ interface Konsole {

fun write(s: String)

suspend fun read(): String
suspend fun read(label: String? = null): String
}
2 changes: 1 addition & 1 deletion demo/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "org.kobjects.konsole.demo"
version = "0.2.4"
version = "0.3.0"

kotlin {
android {
Expand Down
2 changes: 1 addition & 1 deletion demo/demo.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = 'demo'
spec.version = '0.2.2'
spec.version = '0.3.0'
spec.homepage = 'Link to the Demo Module homepage'
spec.source = { :http=> ''}
spec.authors = ''
Expand Down
4 changes: 2 additions & 2 deletions demo/src/commonMain/kotlin/org/kobjects/konsole/demo/Demo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import org.kobjects.konsole.demo.rockpaperscissors.rockPaperScissors
class Demo(
val number: Int,
val name: String,
val code: suspend (read: suspend () -> String, write: (String) -> Unit) -> Unit
val code: suspend (read: suspend (String?) -> String, write: (String) -> Unit) -> Unit
) {
companion object {
val ALL = listOf(
Expand All @@ -28,7 +28,7 @@ class Demo(
run (konsole::read, konsole::write)
}

suspend fun run(read: suspend () -> String, write: (String) -> Unit) {
suspend fun run(read: suspend (String?) -> String, write: (String) -> Unit) {
println("run $this")
code(read, write)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import kotlin.coroutines.suspendCoroutine
/** Helper for iOS */
class KonsoleImpl : Konsole {
var writeFunction: (String) -> Unit = {}
var readFunction: ((String) -> Unit) -> Unit = {}
var readFunction: (String?, (String) -> Unit) -> Unit = { label, fn -> }

override fun write(s: String) {
writeFunction(s)
}

override suspend fun read() = suspendCoroutine<String> { cont ->
readFunction { cont.resume(it) }
override suspend fun read(label: String?) = suspendCoroutine<String> { cont ->
readFunction(label) { cont.resume(it) }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ package org.kobjects.konsole.demo.banner
* Converted from Java to Kotlin by Stefan Haustein
*/

suspend fun banner(read: suspend () -> String, write: (String) -> Unit ) {
suspend fun banner(read: suspend (String?) -> String, write: (String) -> Unit ) {
while (true) {
write("Text?")
val statement = read().uppercase()
val statement = read("Text?").uppercase()

if (statement.isBlank()) {
break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.kobjects.konsole.demo.checkers

import kotlin.math.abs

suspend fun checkers(read: suspend () -> String, write: (String) -> Unit) {
suspend fun checkers(read: suspend (String?) -> String, write: (String) -> Unit) {
write("Checkers")
write("Creative Computing Morristown, New Jersey")
write("""
Expand All @@ -18,8 +18,7 @@ suspend fun checkers(read: suspend () -> String, write: (String) -> Unit) {
while (true) {
Checkers().run(read, write)

write("Another game?")
val answer = read().lowercase()
val answer = read("Another game?").lowercase()
if (answer != "y" && answer != "yes") {
break
}
Expand All @@ -30,7 +29,7 @@ class Checkers {
val board = Array(8, { IntArray(8, {0}) })
val g = -1

suspend fun run(read: suspend () -> String, write: (String) -> Unit) {
suspend fun run(read: suspend (String?) -> String, write: (String) -> Unit) {

var p = 0
for (x in 0..7) {
Expand Down Expand Up @@ -69,8 +68,7 @@ class Checkers {
break;
}
while (true) {
write("Your move?")
val problem = playerMove(read().trim().lowercase())
val problem = playerMove(read("Your move?").trim().lowercase())
if (problem.isEmpty()) {
break
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import kotlin.random.Random
*/

class Hangman(
val read: suspend () -> String,
val read: suspend (String?) -> String,
val write: (String) -> Unit
) {

Expand Down Expand Up @@ -75,8 +75,7 @@ class Hangman(

while(misses < 10) {
write("Misses: $lettersUsed\nDiscovered: $discovered")
write("Your guess?")
val guess = read().trim().uppercase()
val guess = read("Your guess?").trim().uppercase()

if (guess.length == 0) {
write("Do you want to give up?")
Expand Down Expand Up @@ -206,7 +205,7 @@ class Hangman(
write(sb.toString())
}

suspend fun yesNo() = when (read().lowercase()) {
suspend fun yesNo() = when (read(null).lowercase()) {
"yes", "y" -> true
else -> false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package org.kobjects.konsole.demo.ktxml
import org.kobjects.ktxml.mini.MiniXmlPullParser
import org.kobjects.ktxml.api.EventType

suspend fun ktXmlDemo(read: suspend () -> String, write: (String) -> Unit) {
suspend fun ktXmlDemo(read: suspend (String?) -> String, write: (String) -> Unit) {
write("Enter XML an XML snippet see the corresponding KtXml parsing events.")
while(true) {
val input = read()
val input = read("XML?")
if (input.isBlank()) {
break
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fun random() = Random.Default.nextDouble()
fun random10() = (10 * random()).toInt()

class Poker(
val read: suspend () -> String,
val read: suspend (String?) -> String,
val write: (String) -> Unit) {

val allCards = mutableListOf<Card>()
Expand All @@ -23,7 +23,7 @@ class Poker(
suspend fun queryCardNumbers(): List<Int> {
write("Now we draw -- which cards do you want to replace?")
while (true) {
val s = read()
val s = read("Card numbers?")
val cards = mutableListOf<Int>()
var valid = true
for (c in s) {
Expand All @@ -50,7 +50,7 @@ class Poker(
suspend fun yesNo(question: String): Boolean {
while (true) {
write(question)
val text = read().lowercase()
val text = read("y/n").lowercase()
when (text) {
"yes", "y" -> return true
"no", "n" -> return false
Expand Down Expand Up @@ -122,12 +122,12 @@ class Poker(

suspend fun askForAmount(minimum: Int): Int {
while (true) {
if (minimum == 0) {
write("Fold, check or rise to <amount>?")
} else {
write("Fold, see or rise to <amount>?")
}
val input = read().trim().lowercase()
val question = if (minimum == 0)
"Fold, check or rise to <amount>?"
else
"Fold, see or rise to <amount>?"

val input = read(question).trim().lowercase()
if (input == "f" || input == "fold") {
return -1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ fun compare(userChoice: Choice, computerChoice: Choice) =
}
}

suspend fun readUserChoice(read: suspend () -> String, write: (String) -> Unit): Choice {
write("Rock, paper or scissors?")
suspend fun readUserChoice(read: suspend (String?) -> String, write: (String) -> Unit): Choice {
while (true) {
val input = read()
val input = read("Rock, paper or scissors?")
val errorMessage = validateInput(input)
if (errorMessage.isEmpty()) {
return processInput(input)!!
Expand All @@ -50,7 +49,7 @@ suspend fun readUserChoice(read: suspend () -> String, write: (String) -> Unit):
}
}

suspend fun rockPaperScissors(read: suspend () -> String, write: (String) -> Unit) {
suspend fun rockPaperScissors(read: suspend (String?) -> String, write: (String) -> Unit) {
while (true) {
val userChoice = readUserChoice(read, write)
val computerChoice = Choice.values()[Random.nextInt(0, 3)]
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue Jan 04 23:16:14 CET 2022
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
4 changes: 2 additions & 2 deletions iosApp/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- demo (0.2.2)
- demo (0.3.0)

DEPENDENCIES:
- demo (from `../demo`)
Expand All @@ -9,7 +9,7 @@ EXTERNAL SOURCES:
:path: "../demo"

SPEC CHECKSUMS:
demo: ba2f768e183abec8a4f1e72efa63b3739ff4a798
demo: f2943f5438f089b8dad2555b77748ee5ba849df2

PODFILE CHECKSUM: 8b9a17ea90421cac540152758feaa63690f5ea11

Expand Down
Loading

0 comments on commit 67cf9b1

Please sign in to comment.