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. 23
      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 { kotlin {
android() android()
jvm("desktop") jvm("desktop")
sourceSets { sourceSets {
named("commonMain") { named("commonMain") {
dependencies { dependencies {
api(compose.runtime) api(compose.runtime)
api(compose.foundation) api(compose.foundation)
api(compose.material) api(compose.material)
implementation("io.ktor:ktor-client-core:1.4.1")
} }
} }
named("androidMain") { named("androidMain") {
dependencies { dependencies {
api("androidx.appcompat:appcompat:1.1.0") api("androidx.appcompat:appcompat:1.1.0")
api("androidx.core:core-ktx:1.3.1") api("androidx.core:core-ktx:1.3.1")
implementation("io.ktor:ktor-client-cio:1.4.1")
} }
} }
named("desktopMain") { named("desktopMain") {
dependencies { dependencies {
api(compose.desktop.common) 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 import android.graphics.Bitmap
data class Picture( actual data class Picture(
var source: String = "", var source: String = "",
var name: String = "", var name: String = "",
var image: Bitmap, 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 package example.imageviewer.model
import example.imageviewer.core.Repository import example.imageviewer.core.Repository
import java.io.BufferedReader import example.imageviewer.utils.ktorHttpClient
import java.io.InputStreamReader import example.imageviewer.utils.runBlocking
import java.net.HttpURLConnection import io.ktor.client.request.*
import java.net.URL
class ImageRepository( class ImageRepository(
private val httpsURL: String private val httpsURL: String
) : Repository<MutableList<String>> { ) : Repository<MutableList<String>> {
override fun get(): MutableList<String> { override fun get(): MutableList<String> {
val list: MutableList<String> = ArrayList() return runBlocking {
try { val content = ktorHttpClient.get<String>(httpsURL)
val url = URL(httpsURL) content.lines().toMutableList()
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 list
} }
} }

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

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

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

@ -14,13 +14,24 @@
package example.imageviewer.utils 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 { fun isInternetAvailable(): Boolean {
return try { return runBlocking {
val ipAddress: InetAddress = InetAddress.getByName("google.com") try {
!ipAddress.equals("") ktorHttpClient.head<String>("http://google.com")
} catch (e: Exception) { true
false } 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 import java.awt.image.BufferedImage
data class Picture( actual data class Picture(
var source: String = "", var source: String = "",
var name: String = "", var name: String = "",
var image: BufferedImage, 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