Browse Source

ImageViewer: Use Ktor and expect/actual to make common source set pure Kotlin (#51)

* Add IDE resolution warning for examples sharing JVM & Android code.

* Use Ktor to remove JVM-specific networking code from common source set
Fix resolution for Picture class via expect/actual mechanism

Co-authored-by: Sebastian Aigner <sebastian.aigner@jetbrains.com>
pull/52/head
Sebastian Aigner 4 years ago committed by GitHub
parent
commit
33b041c31a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      examples/imageviewer/common/build.gradle.kts
  2. 2
      examples/imageviewer/common/src/androidMain/kotlin/example/imageviewer/model/Picture.kt
  3. 9
      examples/imageviewer/common/src/androidMain/kotlin/example/imageviewer/utils/Coroutines.kt
  4. 30
      examples/imageviewer/common/src/commonMain/kotlin/example/imageviewer/model/ImageRepository.kt
  5. 9
      examples/imageviewer/common/src/commonMain/kotlin/example/imageviewer/model/Miniatures.kt
  6. 7
      examples/imageviewer/common/src/commonMain/kotlin/example/imageviewer/utils/Coroutines.kt
  7. 25
      examples/imageviewer/common/src/commonMain/kotlin/example/imageviewer/utils/Network.kt
  8. 2
      examples/imageviewer/common/src/desktopMain/kotlin/example/imageviewer/model/Picture.kt
  9. 9
      examples/imageviewer/common/src/desktopMain/kotlin/example/imageviewer/utils/Coroutines.kt

4
examples/imageviewer/common/build.gradle.kts

@ -9,24 +9,26 @@ plugins {
kotlin {
android()
jvm("desktop")
sourceSets {
named("commonMain") {
dependencies {
api(compose.runtime)
api(compose.foundation)
api(compose.material)
implementation("io.ktor:ktor-client-core:1.4.1")
}
}
named("androidMain") {
dependencies {
api("androidx.appcompat:appcompat:1.1.0")
api("androidx.core:core-ktx:1.3.1")
implementation("io.ktor:ktor-client-cio:1.4.1")
}
}
named("desktopMain") {
dependencies {
api(compose.desktop.common)
implementation("io.ktor:ktor-client-cio:1.4.1")
}
}
}

2
examples/imageviewer/common/src/androidMain/kotlin/example/imageviewer/model/Picture.kt

@ -2,7 +2,7 @@ package example.imageviewer.model
import android.graphics.Bitmap
data class Picture(
actual data class Picture(
var source: String = "",
var name: String = "",
var image: Bitmap,

9
examples/imageviewer/common/src/androidMain/kotlin/example/imageviewer/utils/Coroutines.kt

@ -0,0 +1,9 @@
package example.imageviewer.utils
import kotlinx.coroutines.CoroutineScope
import kotlin.coroutines.CoroutineContext
actual fun <T> runBlocking(
context: CoroutineContext,
block: suspend CoroutineScope.() -> T
): T = kotlinx.coroutines.runBlocking(context, block)

30
examples/imageviewer/common/src/commonMain/kotlin/example/imageviewer/model/ImageRepository.kt

@ -16,36 +16,18 @@
package example.imageviewer.model
import example.imageviewer.core.Repository
import java.io.BufferedReader
import java.io.InputStreamReader
import java.net.HttpURLConnection
import java.net.URL
import example.imageviewer.utils.ktorHttpClient
import example.imageviewer.utils.runBlocking
import io.ktor.client.request.*
class ImageRepository(
private val httpsURL: String
) : Repository<MutableList<String>> {
override fun get(): MutableList<String> {
val list: MutableList<String> = ArrayList()
try {
val url = URL(httpsURL)
val connection: HttpURLConnection = url.openConnection() as HttpURLConnection
connection.connectTimeout = 5000
connection.connect()
val read = BufferedReader(InputStreamReader(connection.inputStream))
var line: String? = read.readLine()
while (line != null) {
list.add(line)
line = read.readLine()
}
read.close()
return list
} catch (e: Exception) {
e.printStackTrace()
return runBlocking {
val content = ktorHttpClient.get<String>(httpsURL)
content.lines().toMutableList()
}
return list
}
}

9
examples/imageviewer/common/src/commonMain/kotlin/example/imageviewer/model/Miniatures.kt

@ -14,20 +14,21 @@
package example.imageviewer.model
expect class Picture
class Miniatures(
private var list: MutableList<Picture> = ArrayList()
private var list: List<Picture> = emptyList()
) {
fun get(index: Int): Picture {
return list[index]
}
fun getMiniatures(): List<Picture> {
return ArrayList(list)
return list.toList()
}
fun setMiniatures(list: List<Picture>) {
this.list = ArrayList(list)
this.list = list.toList()
}
fun size(): Int {
@ -35,6 +36,6 @@ class Miniatures(
}
fun clear() {
list = ArrayList()
list = emptyList()
}
}

7
examples/imageviewer/common/src/commonMain/kotlin/example/imageviewer/utils/Coroutines.kt

@ -0,0 +1,7 @@
package example.imageviewer.utils
import kotlinx.coroutines.CoroutineScope
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
expect fun <T> runBlocking(context: CoroutineContext = EmptyCoroutineContext, block: suspend CoroutineScope.() -> T): T

25
examples/imageviewer/common/src/commonMain/kotlin/example/imageviewer/utils/Network.kt

@ -14,13 +14,24 @@
package example.imageviewer.utils
import java.net.InetAddress
import io.ktor.client.*
import io.ktor.client.request.*
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
//import java.net.InetAddress
fun isInternetAvailable(): Boolean {
return try {
val ipAddress: InetAddress = InetAddress.getByName("google.com")
!ipAddress.equals("")
} catch (e: Exception) {
false
return runBlocking {
try {
ktorHttpClient.head<String>("http://google.com")
true
} catch (e: Exception) {
println(e.message)
false
}
}
}
}
val ktorHttpClient = HttpClient {}

2
examples/imageviewer/common/src/desktopMain/kotlin/example/imageviewer/model/Picture.kt

@ -2,7 +2,7 @@ package example.imageviewer.model
import java.awt.image.BufferedImage
data class Picture(
actual data class Picture(
var source: String = "",
var name: String = "",
var image: BufferedImage,

9
examples/imageviewer/common/src/desktopMain/kotlin/example/imageviewer/utils/Coroutines.kt

@ -0,0 +1,9 @@
package example.imageviewer.utils
import kotlinx.coroutines.CoroutineScope
import kotlin.coroutines.CoroutineContext
actual fun <T> runBlocking(
context: CoroutineContext,
block: suspend CoroutineScope.() -> T
): T = kotlinx.coroutines.runBlocking(context, block)
Loading…
Cancel
Save