Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
Setup project for Jetpack Compose
Browse files Browse the repository at this point in the history
Add CI

Lint with ktlint

All gradle files now use kotlin for configuring.

Change libraries for Jetpack Compose

Change main activity to Jetpack Compose

Fix build error
  • Loading branch information
mads256h committed Apr 4, 2024
1 parent 4098fc5 commit e0f1c36
Show file tree
Hide file tree
Showing 25 changed files with 385 additions and 164 deletions.
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[*{kt,kts}]
ktlint_function_naming_ignore_when_annotated_with=Composable
30 changes: 30 additions & 0 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Java CI with Gradle

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: '21'
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3
- name: Build with Gradle
run: ./gradlew build
- name: Lint
run: |
./gradlew lint
./gradlew ktlintCheck
84 changes: 84 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("org.jlleitschuh.gradle.ktlint") version "12.1.0"
}

android {
namespace = "dk.scheduling.schedulingfrontend"
compileSdk = 34

defaultConfig {
applicationId = "dk.scheduling.schedulingfrontend"
minSdk = 28
targetSdk = 34
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro",
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}

buildFeatures {
compose = true
}

composeOptions {
kotlinCompilerExtensionVersion = "1.5.11"
}

packaging {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
}

dependencies {

implementation("androidx.core:core-ktx:1.12.0")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.7.0")
implementation("androidx.activity:activity-compose:1.8.2")
implementation(platform("androidx.compose:compose-bom:2024.04.00"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3")
implementation("androidx.compose.material:material-icons-core")
implementation("androidx.compose.material:material-icons-extended")
implementation("org.apache.commons:commons-lang3:3.14.0")
implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation("com.squareup.retrofit2:converter-gson:2.9.0")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
androidTestImplementation(platform("androidx.compose:compose-bom:2024.04.00"))
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
debugImplementation("androidx.compose.ui:ui-tooling")
debugImplementation("androidx.compose.ui:ui-test-manifest")
}

ktlint {
dependencies {
ktlintRuleset("com.twitter.compose.rules:ktlint:0.0.26")
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
# proguardFiles setting in build.gradle.kts.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
@file:Suppress("ktlint:standard:no-wildcard-imports")

package dk.scheduling.schedulingfrontend

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.runner.RunWith

import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith

/**
* Instrumented test, which will execute on an Android device.
Expand All @@ -14,10 +15,10 @@ import org.junit.Assert.*
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@org.junit.jupiter.api.Test
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("dk.scheduling.schedulingfrontend", appContext.packageName)
}
}
}
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.SchedulingFrontend"
android:usesCleartextTraffic="true"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
android:exported="true"
android:theme="@style/Theme.SchedulingFrontend">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
67 changes: 42 additions & 25 deletions app/src/main/java/dk/scheduling/schedulingfrontend/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,39 +1,56 @@
package dk.scheduling.schedulingfrontend

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.util.Log.ERROR
import android.widget.Button
import dk.scheduling.schedulingfrontend.API.ApiClient
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import dk.scheduling.schedulingfrontend.api.ApiClient
import dk.scheduling.schedulingfrontend.ui.theme.SchedulingFrontendTheme
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
class MainActivity : AppCompatActivity() {

class MainActivity : ComponentActivity() {
@OptIn(ExperimentalMaterial3Api::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.button)
button.setOnClickListener {
val call = ApiClient.apiService.test()
setContent {
SchedulingFrontendTheme {
Button(onClick = {
val call = ApiClient.apiService.test()

call.enqueue(object : Callback<String>{
override fun onResponse(call: Call<String>, response: Response<String>) {
if (response.isSuccessful) {
//val post = response.body()
println("we got a response")
// Handle the retrieved post data
} else {
println("we did not get a successful response")
// Handle error
}
}
call.enqueue(
object : Callback<String> {
override fun onResponse(
call: Call<String>,
response: Response<String>,
) {
if (response.isSuccessful) {
// val post = response.body()
Log.i("testAPI", "we got a response")
// Handle the retrieved post data
} else {
Log.w("testAPI", "we did not get a successful response")
// Handle error
}
}

override fun onFailure(call: Call<String>, t: Throwable) {
// Handle failure
Log.e("error message", "could not get a response")
override fun onFailure(
call: Call<String>,
t: Throwable,
) {
// Handle failure
Log.e("testAPI", "could not get a response")
}
},
)
}) {
Text("Test API")
}
})
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dk.scheduling.schedulingfrontend.API
package dk.scheduling.schedulingfrontend.api

import com.google.gson.GsonBuilder
import okhttp3.OkHttpClient
Expand All @@ -19,11 +19,22 @@ object RetrofitClient {
private const val BASE_URL = "http://10.0.2.2:2222/"
private val gson = GsonBuilder().setLenient().create()
private val okHttpClient: OkHttpClient by lazy {
val trustAllCertificates = arrayOf<TrustManager>(object : X509TrustManager {
override fun checkClientTrusted(chain: Array<out X509Certificate>?, authType: String?) {}
override fun checkServerTrusted(chain: Array<out X509Certificate>?, authType: String?) {}
override fun getAcceptedIssuers(): Array<X509Certificate> = arrayOf()
})
val trustAllCertificates =
arrayOf<TrustManager>(
object : X509TrustManager {
override fun checkClientTrusted(
chain: Array<out X509Certificate>?,
authType: String?,
) {}

override fun checkServerTrusted(
chain: Array<out X509Certificate>?,
authType: String?,
) {}

override fun getAcceptedIssuers(): Array<X509Certificate> = arrayOf()
},
)

val sslContext = SSLContext.getInstance("SSL")
sslContext.init(null, trustAllCertificates, SecureRandom())
Expand All @@ -44,10 +55,8 @@ object RetrofitClient {
}
}



object ApiClient {
val apiService: ApiService by lazy {
RetrofitClient.retrofit.create(ApiService::class.java)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package dk.scheduling.schedulingfrontend.API
package dk.scheduling.schedulingfrontend.api
import retrofit2.Call
import retrofit2.http.GET

interface ApiService {
@GET("test")
fun test() : Call<String>
}
fun test(): Call<String>
}
Loading

0 comments on commit e0f1c36

Please sign in to comment.