From 8acd72bd9ef29b7479ba692ab56665338c50137d Mon Sep 17 00:00:00 2001 From: grtsinry43 Date: Wed, 23 Apr 2025 09:26:24 +0800 Subject: [PATCH] feat: Introduce Network Module and Basic Network Communication - Implement `NetworkModule` for managing HTTP client with JSON content negotiation. - Add `kotlinx-serialization-json` and `ktor-client` dependencies for network requests and serialization. - Implement basic network communication in `Greeting` class to retrieve and display time data from a remote API. - Add `NetworkResponse` and `TimeResponse` data classes for handling the response from the remote API. - Refactor the `greet` method in `Greeting` class to integrate network request. --- gradle/libs.versions.toml | 8 +++++ shared/build.gradle.kts | 10 ++++++ .../grtsinry43/activityanalyzer/Greeting.kt | 31 +++++++++++++++++-- .../activityanalyzer/NetworkModule.kt | 17 ++++++++++ 4 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 shared/src/commonMain/kotlin/com/grtsinry43/activityanalyzer/NetworkModule.kt diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index f2e0aab..f2930d3 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -15,6 +15,8 @@ compose-multiplatform = "1.7.3" junit = "4.13.2" kotlin = "2.1.10" kotlinx-coroutines = "1.10.1" +kotlinxSerializationJson = "1.6.2" +ktorClientCore = "2.3.8" [libraries] kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" } @@ -30,6 +32,12 @@ androidx-activity-compose = { module = "androidx.activity:activity-compose", ver androidx-lifecycle-viewmodel = { group = "org.jetbrains.androidx.lifecycle", name = "lifecycle-viewmodel", version.ref = "androidx-lifecycle" } androidx-lifecycle-runtime-compose = { group = "org.jetbrains.androidx.lifecycle", name = "lifecycle-runtime-compose", version.ref = "androidx-lifecycle" } kotlinx-coroutines-swing = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-swing", version.ref = "kotlinx-coroutines" } +kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinxSerializationJson" } +ktor-client-android = { module = "io.ktor:ktor-client-android", version.ref = "ktorClientCore" } +ktor-client-content-negotiation = { module = "io.ktor:ktor-client-content-negotiation", version.ref = "ktorClientCore" } +ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktorClientCore" } +ktor-client-ios = { module = "io.ktor:ktor-client-ios", version.ref = "ktorClientCore" } +ktor-serialization-kotlinx-json = { module = "io.ktor:ktor-serialization-kotlinx-json", version.ref = "ktorClientCore" } [plugins] androidApplication = { id = "com.android.application", version.ref = "agp" } diff --git a/shared/build.gradle.kts b/shared/build.gradle.kts index d546fb1..3cb2a61 100644 --- a/shared/build.gradle.kts +++ b/shared/build.gradle.kts @@ -30,6 +30,16 @@ kotlin { sourceSets { commonMain.dependencies { // put your Multiplatform dependencies here + implementation(libs.ktor.client.core) + implementation(libs.ktor.client.content.negotiation) + implementation(libs.ktor.serialization.kotlinx.json) + implementation(libs.kotlinx.serialization.json) + } + androidMain.dependencies { + implementation(libs.ktor.client.android) + } + iosMain.dependencies { + implementation(libs.ktor.client.ios) } } } diff --git a/shared/src/commonMain/kotlin/com/grtsinry43/activityanalyzer/Greeting.kt b/shared/src/commonMain/kotlin/com/grtsinry43/activityanalyzer/Greeting.kt index 3d4b7c4..cdd9351 100644 --- a/shared/src/commonMain/kotlin/com/grtsinry43/activityanalyzer/Greeting.kt +++ b/shared/src/commonMain/kotlin/com/grtsinry43/activityanalyzer/Greeting.kt @@ -1,9 +1,36 @@ package com.grtsinry43.activityanalyzer +import io.ktor.client.call.body +import io.ktor.client.request.get +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + class Greeting { private val platform = getPlatform() fun greet(): String { - return "Hello, ${platform.name}!" +// val response: NetworkResponse = +// NetworkModule.httpClient.get("https://wlbzx.grtsinry43.com/api/time").body() +// println("response is : $response") + return "Hello, ${platform.name}! " +// +"Current time is ${response.data.time}" } -} \ No newline at end of file +} + +@Serializable +data class NetworkResponse( + @SerialName("code") + val code: Int, + @SerialName("message") + val message: String, + @SerialName("data") + val data: TimeResponse +) + +@Serializable +data class TimeResponse( + @SerialName("time") + val time: String, + @SerialName("count") + val count: Int +) \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/com/grtsinry43/activityanalyzer/NetworkModule.kt b/shared/src/commonMain/kotlin/com/grtsinry43/activityanalyzer/NetworkModule.kt new file mode 100644 index 0000000..7847921 --- /dev/null +++ b/shared/src/commonMain/kotlin/com/grtsinry43/activityanalyzer/NetworkModule.kt @@ -0,0 +1,17 @@ +package com.grtsinry43.activityanalyzer + +import io.ktor.client.HttpClient +import io.ktor.client.plugins.contentnegotiation.ContentNegotiation +import io.ktor.serialization.kotlinx.json.json +import kotlinx.serialization.json.Json + +object NetworkModule { + val httpClient = HttpClient { + install(ContentNegotiation) { + json(Json { + prettyPrint = true + isLenient = true + }) + } + } +} \ No newline at end of file