Skip to content

Commit dc52b8f

Browse files
committed
Kotlin 1.4 + Jetpack Compose alpha
1 parent 3ad9454 commit dc52b8f

File tree

31 files changed

+343
-326
lines changed

31 files changed

+343
-326
lines changed

README.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
### Android Galway Bus app using Kotlin Multiplatform shared code
1+
### Galway Bus Kotlin Multiplatform project using Jetpack Compose and SwiftUI
22

33

44
[![Get it on Google Play](https://play.google.com/intl/en_us/badges/images/generic/en-play-badge.png)](https://play.google.com/store/apps/details?id=com.surrus.galwaybus)
55

66

7-
Note also that this is using [REST endpoint](https://github.com/appsandwich/galwaybus) provided by @appsandwich to retrieve
8-
Galway Bus route/timetable info (Thanks Vinny!)
9-
10-
11-
127
### Kotlin Multiplatform
138

149
Currently this project is being mostly used as platform to explore some of the relatively new Kotlin Multiplatform
@@ -23,10 +18,10 @@ written a couple of post about some of my experiences doing this so far
2318

2419
* [Kotlin](https://kotlinlang.org/)
2520
* [Kotlin Corooutines](https://kotlinlang.org/docs/reference/coroutines-overview.html)
26-
* [Kotlin Serialization](https://github.com/Kotlin/kotlinx.serialization)
21+
* [Kotlinx Serialization](https://github.com/Kotlin/kotlinx.serialization)
2722
* [Ktor client library](https://github.com/ktorio/ktor)
2823
* [Android Architecture Components](https://developer.android.com/topic/libraries/architecture/index.html)
29-
* [Android Support Libraries](https://developer.android.com/topic/libraries/support-library/index.html)
3024
* [Koin](https://github.com/InsertKoinIO/koin)
3125
* [Logger](https://github.com/orhanobut/logger)
32-
* [Mockito](http://site.mockito.org/)
26+
* [Jetpack Compose](https://developer.android.com/jetpack/compose)
27+
* [SwiftUI](https://developer.apple.com/documentation/swiftui)

SharedCode/build.gradle

Lines changed: 26 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,6 @@ android {
1818
versionName "1.0"
1919
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
2020
}
21-
22-
buildTypes {
23-
release {
24-
minifyEnabled false
25-
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
26-
}
27-
28-
//This is for MultiplatformSettings
29-
debug {
30-
// MPP libraries don't currently get this resolution automatically
31-
matchingFallbacks = ['release']
32-
}
33-
}
3421
}
3522

3623

@@ -46,17 +33,13 @@ kotlin {
4633
}
4734
}
4835

49-
fromPreset(presets.android, 'android')
50-
5136
macosX64("macOS")
37+
android()
38+
jvm()
5239
}
5340

5441
js {
55-
compileKotlinJs {
56-
kotlinOptions.metaInfo = true
57-
kotlinOptions.sourceMap = true
58-
kotlinOptions.moduleKind = "commonjs"
59-
kotlinOptions.main = "call"
42+
browser {
6043
}
6144
}
6245

@@ -70,115 +53,53 @@ kotlin {
7053

7154
sourceSets {
7255
commonMain.dependencies {
73-
// Kotlin
74-
implementation "org.jetbrains.kotlin:kotlin-stdlib-common:${Versions.kotlin}"
75-
7656
// Coroutines
77-
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:${Versions.kotlinCoroutines}"
57+
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.kotlinCoroutines}") {
58+
force = true
59+
}
7860

7961
// Ktor
80-
implementation "io.ktor:ktor-client-core:${Versions.ktor}"
81-
implementation "io.ktor:ktor-client-json:${Versions.ktor}"
82-
implementation "io.ktor:ktor-client-logging:${Versions.ktor}"
83-
implementation "io.ktor:ktor-client-serialization:${Versions.ktor}"
62+
implementation("io.ktor:ktor-client-core:${Versions.ktor}")
63+
implementation("io.ktor:ktor-client-json:${Versions.ktor}")
64+
implementation("io.ktor:ktor-client-logging:${Versions.ktor}")
65+
implementation("io.ktor:ktor-client-serialization:${Versions.ktor}")
8466

8567
// Serialize
86-
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:${Versions.kotlinxSerialization}"
68+
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:${Versions.kotlinxSerialization}")
8769

8870
// SQL Delight
89-
implementation "com.squareup.sqldelight:runtime:${Versions.sqlDelight}"
90-
implementation "com.squareup.sqldelight:coroutines-extensions:${Versions.sqlDelight}"
71+
implementation("com.squareup.sqldelight:runtime:${Versions.sqlDelight}")
72+
implementation("com.squareup.sqldelight:coroutines-extensions:${Versions.sqlDelight}")
73+
74+
// koin
75+
api("org.koin:koin-core:${Versions.koin}")
9176
}
9277

9378
androidMain.dependencies {
94-
// Kotlin
95-
implementation "org.jetbrains.kotlin:kotlin-stdlib:${Versions.kotlin}"
96-
97-
// Coroutines
98-
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:${Versions.kotlinCoroutines}"
99-
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.kotlinCoroutines}"
100-
101-
// Ktor
102-
implementation "io.ktor:ktor-client-android:${Versions.ktor}"
103-
implementation "io.ktor:ktor-client-core-jvm:${Versions.ktor}"
104-
implementation "io.ktor:ktor-client-json-jvm:${Versions.ktor}"
105-
implementation "io.ktor:ktor-client-logging-jvm:${Versions.ktor}"
106-
implementation "io.ktor:ktor-client-serialization-jvm:${Versions.ktor}"
107-
108-
// Serialize
109-
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:${Versions.kotlinxSerialization}"
110-
111-
// SQL Delight
112-
implementation "com.squareup.sqldelight:android-driver:${Versions.sqlDelight}"
113-
implementation "com.squareup.sqldelight:coroutines-extensions-jvm:${Versions.sqlDelight}"
79+
implementation("io.ktor:ktor-client-android:${Versions.ktor}")
80+
implementation("com.squareup.sqldelight:android-driver:${Versions.sqlDelight}")
11481
}
11582

11683
iOSMain.dependencies {
117-
// Coroutines
118-
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:${Versions.kotlinCoroutines}"
119-
120-
// Ktor
121-
implementation "io.ktor:ktor-client-ios:${Versions.ktor}"
122-
implementation "io.ktor:ktor-client-core-native:${Versions.ktor}"
123-
implementation "io.ktor:ktor-client-json-native:${Versions.ktor}"
124-
implementation "io.ktor:ktor-client-logging-native:${Versions.ktor}"
125-
implementation "io.ktor:ktor-client-serialization-native:${Versions.ktor}"
126-
127-
// Serialize
128-
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:${Versions.kotlinxSerialization}"
129-
130-
// SQL Delight
84+
implementation("io.ktor:ktor-client-ios:${Versions.ktor}")
13185
implementation("com.squareup.sqldelight:native-driver:${Versions.sqlDelight}")
13286
}
13387

13488
macOSMain.dependencies {
135-
// Coroutines
136-
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-macosx64:${Versions.kotlinCoroutines}"
137-
138-
139-
// Ktor
140-
implementation "io.ktor:ktor-client-curl:${Versions.ktor}"
141-
implementation "io.ktor:ktor-client-core-macosx64:${Versions.ktor}"
142-
implementation "io.ktor:ktor-client-json-macosx64:${Versions.ktor}"
143-
implementation "io.ktor:ktor-client-logging-macosx64:${Versions.ktor}"
144-
implementation "io.ktor:ktor-client-serialization-macosx64:${Versions.ktor}"
145-
146-
// Serialize
147-
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-macosx64:${Versions.kotlinxSerialization}"
148-
149-
// SQL Delight
150-
implementation "com.squareup.sqldelight:native-driver-macosx64:${Versions.sqlDelight}"
151-
implementation "com.squareup.sqldelight:runtime-macosx64:${Versions.sqlDelight}"
89+
implementation("io.ktor:ktor-client-ios:${Versions.ktor}")
90+
implementation("com.squareup.sqldelight:native-driver-macosx64:${Versions.sqlDelight}")
15291
}
15392

15493

15594
jvmMain.dependencies {
156-
// Kotlin
157-
implementation "org.jetbrains.kotlin:kotlin-stdlib:${Versions.kotlin}"
158-
159-
// Coroutines
160-
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.kotlinCoroutines}"
161-
162-
// Ktor
163-
implementation "io.ktor:ktor-client-core-jvm:${Versions.ktor}"
164-
implementation "io.ktor:ktor-client-json-jvm:${Versions.ktor}"
165-
implementation "io.ktor:ktor-client-serialization-jvm:${Versions.ktor}"
166-
implementation Ktor.slf4j
167-
168-
// Serialize
169-
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:${Versions.kotlinxSerialization}"
170-
171-
// SQL Delight
172-
implementation "com.squareup.sqldelight:sqlite-driver:${Versions.sqlDelight}"
95+
implementation("io.ktor:ktor-client-apache:${Versions.ktor}")
96+
//implementation(Ktor.slf4j)
97+
//implementation("org.xerial:sqlite-jdbc:${Versions.sqliteJdbcDriver}")
98+
implementation("com.squareup.sqldelight:sqlite-driver:${Versions.sqlDelight}")
17399
}
174100

175101
jsMain.dependencies {
176-
api Kotlin.stdlibJs
177-
api Kotlin.serializationRuntimeJs
178-
api Ktor.clientJs
179-
api Ktor.clientJsonJs
180-
api Ktor.clientLoggingJs
181-
api Ktor.clientSerializationJs
102+
implementation("io.ktor:ktor-client-js:${Versions.ktor}")
182103
}
183104

184105
}

SharedCode/src/commonMain/kotlin/com/surrus/galwaybus/common/GalwayBusRepository.kt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,14 @@ import kotlinx.coroutines.launch
1616

1717
expect fun createDb() : MyDatabase?
1818

19-
// TEMP until following is resolved https://github.com/ktorio/ktor/issues/1622
20-
expect fun ktorScope(block: suspend () -> Unit)
21-
22-
2319
open class GalwayBusRepository {
2420

2521
private val galwayBusApi = GalwayBusApi()
2622
private val galwayBusDb = createDb()
2723
private val galwayBusQueries = galwayBusDb?.galwayBusQueries
2824

2925
init {
30-
ktorScope {
26+
GlobalScope.launch(Dispatchers.Main) {
3127
fetchAndStoreBusStops()
3228
}
3329
}
@@ -77,7 +73,7 @@ open class GalwayBusRepository {
7773
}
7874

7975
fun fetchBusListForRoute(routeId: String, success: (List<Bus>) -> Unit) {
80-
ktorScope {
76+
GlobalScope.launch(Dispatchers.Main) {
8177
val busList = galwayBusApi.fetchBusListForRoute(routeId)
8278
success(busList)
8379
}
@@ -102,7 +98,7 @@ open class GalwayBusRepository {
10298
}
10399

104100
fun fetchBusRoutes(success: (List<BusRoute>) -> Unit) {
105-
ktorScope {
101+
GlobalScope.launch(Dispatchers.Main) {
106102
success(fetchBusRoutes())
107103
}
108104
}

SharedCode/src/commonMain/kotlin/com/surrus/galwaybus/common/remote/GalwayBusApi.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import io.ktor.client.request.get
1414
import io.ktor.client.request.parameter
1515
import io.ktor.client.request.url
1616
import kotlinx.serialization.Serializable
17+
import kotlinx.serialization.builtins.ListSerializer
1718
import kotlinx.serialization.builtins.MapSerializer
1819
import kotlinx.serialization.builtins.list
1920
import kotlinx.serialization.builtins.serializer
2021
import kotlinx.serialization.json.Json
21-
import kotlinx.serialization.json.JsonConfiguration
2222

2323

2424
@Serializable
@@ -31,7 +31,7 @@ data class GetBusListForRouteResponse(val bus: List<Bus>)
3131
// https://github.com/appsandwich/galwaybus
3232

3333
class GalwayBusApi(val baseUrl: String = "https://galwaybus.herokuapp.com") {
34-
private val nonStrictJson = Json(JsonConfiguration(isLenient = true, ignoreUnknownKeys = true))
34+
private val nonStrictJson = Json { isLenient = true; ignoreUnknownKeys = true }
3535

3636
private val client by lazy {
3737
HttpClient() {
@@ -40,12 +40,12 @@ class GalwayBusApi(val baseUrl: String = "https://galwaybus.herokuapp.com") {
4040
}
4141
install(Logging) {
4242
logger = Logger.DEFAULT
43-
level = LogLevel.ALL
43+
level = LogLevel.INFO
4444
}
4545
}
4646
}
4747

48-
private val busScheduleMapSerializer = MapSerializer(String.serializer(), MapSerializer(String.serializer(), String.serializer()).list)
48+
private val busScheduleMapSerializer = MapSerializer(String.serializer(), ListSerializer(MapSerializer(String.serializer(), String.serializer())))
4949

5050

5151
suspend fun fetchBusRoutes(): Map<String, BusRoute> {
@@ -58,7 +58,7 @@ class GalwayBusApi(val baseUrl: String = "https://galwaybus.herokuapp.com") {
5858

5959
suspend fun fetchSchedules(): Map<String, List<Map<String, String>>> {
6060
val jsonString = client.get<String>("$baseUrl/schedules.json")
61-
return nonStrictJson.parse(busScheduleMapSerializer, jsonString)
61+
return nonStrictJson.decodeFromString(busScheduleMapSerializer, jsonString)
6262
}
6363

6464
suspend fun fetchNearestStops(latitude: Double, longitude: Double): List<BusStop> {

SharedCode/src/commonMain/kotlin/com/surrus/galwaybus/common/remote/RTPIApi.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,22 @@ import io.ktor.client.request.parameter
1212
import io.ktor.client.request.url
1313
import kotlinx.serialization.Serializable
1414
import kotlinx.serialization.json.Json
15-
import kotlinx.serialization.json.JsonConfiguration
1615

1716

1817
class RTPIApi {
1918
private val baseUrl = "https://data.smartdublin.ie/cgi-bin/rtpi"
2019

21-
private val nonStrictJson = Json(JsonConfiguration(isLenient = true, ignoreUnknownKeys = true))
20+
private val nonStrictJson = Json { isLenient = true; ignoreUnknownKeys = true }
2221

2322
private val client by lazy {
2423
HttpClient() {
2524
install(JsonFeature) {
2625
serializer = KotlinxSerializer(nonStrictJson)
2726
}
28-
// install(Logging) {
29-
// logger = Logger.DEFAULT
30-
// level = LogLevel.ALL
31-
// }
27+
install(Logging) {
28+
logger = Logger.DEFAULT
29+
level = LogLevel.INFO
30+
}
3231
}
3332
}
3433

SharedCode/src/iosMain/kotlin/com/surrus/galwaybus/common/actual.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,3 @@ actual fun createDb(): MyDatabase? {
1111
return MyDatabase(driver)
1212
}
1313

14-
actual fun ktorScope(block: suspend () -> Unit) {
15-
GlobalScope.launch(Dispatchers.Main) { block() }
16-
}
17-

SharedCode/src/jsMain/kotlin/com/surrus/galwaybus/common/actual.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,3 @@ actual fun createDb(): MyDatabase? {
1010
return null
1111
}
1212

13-
actual fun ktorScope(block: suspend () -> Unit) {
14-
GlobalScope.launch(Dispatchers.Main) { block() }
15-
}

SharedCode/src/jvmMain/kotlin/com/surrus/galwaybus/common/actual.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@ actual fun createDb(): MyDatabase? {
88
return MyDatabase(driver)
99
}
1010

11-
actual fun ktorScope(block: suspend () -> Unit) = kotlinx.coroutines.runBlocking { block() }

SharedCode/src/macOSMain/kotlin/common/actual.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@ actual fun createDb(): MyDatabase? {
88
return MyDatabase(driver)
99
}
1010

11-
actual fun ktorScope(block: suspend () -> Unit) = kotlinx.coroutines.runBlocking { block() }

0 commit comments

Comments
 (0)