Skip to content

Commit

Permalink
Abstracts Ktor client example to work for more future targets. Adds a…
Browse files Browse the repository at this point in the history
…n android module with an application example
  • Loading branch information
franciscoengenheiro committed Mar 19, 2024
1 parent fa0b89c commit fd5c86b
Show file tree
Hide file tree
Showing 20 changed files with 329 additions and 2,997 deletions.
52 changes: 52 additions & 0 deletions android-app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
plugins {
/*alias(libs.plugins.androidApplication)
alias(libs.plugins.kotlinAndroid)*/
// workaround:
id(libs.plugins.androidApplication.get().pluginId)
id(libs.plugins.kotlinAndroid.get().pluginId)
}

android {
namespace = "kresil.experiments.android"
compileSdk = 34
defaultConfig {
applicationId = "kresil.experiments.android"
minSdk = 25
targetSdk = 34
versionCode = 1
versionName = "1.0"
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = libs.versions.compose.compiler.get()
}
packaging {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}

dependencies {
implementation(project(":kmp"))
implementation(libs.compose.ui)
implementation(libs.compose.ui.tooling.preview)
implementation(libs.compose.material3)
implementation(libs.androidx.activity.compose)
implementation(libs.kotlinx.coroutines.android)
debugImplementation(libs.compose.ui.tooling)
}
15 changes: 15 additions & 0 deletions android-app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:theme="@style/AppTheme">
<activity
android:name="android.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
55 changes: 55 additions & 0 deletions android-app/src/main/java/android/ApplicationTheme.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package android

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Shapes
import androidx.compose.material3.Typography
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

@Composable
fun ApplicationTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
content: @Composable () -> Unit,
) {
val colors = if (darkTheme) {
darkColorScheme(
primary = Color(0xFFBB86FC),
secondary = Color(0xFF03DAC5),
tertiary = Color(0xFF3700B3)
)
} else {
lightColorScheme(
primary = Color(0xFF6200EE),
secondary = Color(0xFF03DAC5),
tertiary = Color(0xFF3700B3)
)
}
val typography = Typography(
bodyMedium = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Normal,
fontSize = 16.sp
)
)
val shapes = Shapes(
small = RoundedCornerShape(4.dp),
medium = RoundedCornerShape(4.dp),
large = RoundedCornerShape(0.dp)
)

MaterialTheme(
colorScheme = colors,
typography = typography,
shapes = shapes,
content = content
)
}
59 changes: 59 additions & 0 deletions android-app/src/main/java/android/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package android

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import kotlinx.coroutines.launch

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ApplicationTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
val scope = rememberCoroutineScope()
var text by remember { mutableStateOf("Loading") }
LaunchedEffect(true) {
scope.launch {
text = try {
"Hello, Android!"
} catch (e: Exception) {
e.localizedMessage ?: "error"
}
}
}
GreetingView(text)
}
}
}
}
}

@Composable
fun GreetingView(text: String) {
Text(text = text)
}

@Preview
@Composable
fun DefaultPreview() {
ApplicationTheme {
GreetingView("Hello, Android!")
}
}
3 changes: 3 additions & 0 deletions android-app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<style name="AppTheme" parent="android:Theme.Material.NoActionBar"/>
</resources>
8 changes: 3 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ plugins {
// trick: for the same plugin versions in all sub-modules
alias(libs.plugins.androidLibrary) apply false
alias(libs.plugins.kotlinMultiplatform) apply false
kotlin("jvm") version "1.9.23"
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
}

/*
repositories {
mavenCentral()
}
kotlin {
jvmToolchain(8)
}
}*/
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Gradle
gradle.properties.org.gradle.jvmargs=-xmx4608m
org.gradle.jvmargs=-Xmx2048M -Dfile.encoding=UTF-8 -Dkotlin.daemon.jvm.options\="-Xmx2048M"
#kotlin.mpp.applyDefaultHierarchyTemplate=false
org.gradle.caching=true
org.gradle.configuration-cache=true
Expand Down
25 changes: 23 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
[versions]
agp = "8.0.2"
kotlin = "1.9.0"
agp = "8.2.0"
kotlin = "1.9.20"
coroutines = "1.7.3"
ktor = "2.3.9"
nexus-publish = "2.0.0-rc-1"
android-minSdk = "24"
android-compileSdk = "33"
compose = "1.6.3"
compose-compiler = "1.5.4"
compose-material3 = "1.2.1"
androidx-activityCompose = "1.8.2"

[libraries]
kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" }
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
nexus-publish = { module = "io.github.gradle-nexus.publish-plugin:io.github.gradle-nexus.publish-plugin.gradle.plugin", version.ref = "nexus-publish" }
androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activityCompose" }
compose-ui = { module = "androidx.compose.ui:ui", version.ref = "compose" }
compose-ui-tooling = { module = "androidx.compose.ui:ui-tooling", version.ref = "compose" }
compose-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview", version.ref = "compose" }
compose-foundation = { module = "androidx.compose.foundation:foundation", version.ref = "compose" }
compose-material3 = { module = "androidx.compose.material3:material3", version.ref = "compose-material3" }
ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" }
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" }
kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "coroutines" }

[plugins]
androidLibrary = { id = "com.android.library", version.ref = "agp" }
kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
androidApplication = { id = "com.android.application", version.ref = "agp" }
kotlinAndroid = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
# kotlinCocoapods = { id = "org.jetbrains.kotlin.native.cocoapods", version.ref = "kotlin" }

# taken from: https://github.com/ktorio/ktor-documentation/blob/2.3.9/codeSnippets/snippets/tutorial-client-kmm/gradle/libs.versions.toml
26 changes: 0 additions & 26 deletions js-app/build.gradle.kts

This file was deleted.

6 changes: 3 additions & 3 deletions kmp/src/jsMain/kotlin/Adapter.js.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ object Adapter {
@Ignore
fun ignored(): String = "Should not be callable from JS"

// val cant be used KmpApi as its non-external since it
// was defined in commonMain
// cannot declare commonMain type instances, because they are not exported
// to JS (aka marked as non-external)

// needs to be Array type because List is not exported to JS
// supported types at: https://kotlinlang.org/docs/js-to-kotlin-interop.html#kotlin-types-in-javascript
fun getBooks(): Array<Book> = KmpApi.getBooks().toTypedArray()
fun getPlatformType(): String = KmpApi.getPlatformType()

@JsName("addBooks")
@JsName("addBooks") // renames a method when exported to JS
fun executePredefinedLogic() = KmpApi.executePredefinedLogic()
fun clearBooks() = KmpApi.clearBooks()
}
Loading

0 comments on commit fd5c86b

Please sign in to comment.