From 4098fc5c906cb843ff7f8ffd3d25a2c0df7c932a Mon Sep 17 00:00:00 2001 From: S3JER <72788875+S3JER@users.noreply.github.com> Date: Thu, 4 Apr 2024 11:01:02 +0200 Subject: [PATCH] Connection to API (#8) --- app/build.gradle | 72 +++++++------------ .../ExampleInstrumentedTest.kt | 3 +- app/src/main/AndroidManifest.xml | 2 + .../schedulingfrontend/API/ApiClient.kt | 53 ++++++++++++++ .../schedulingfrontend/API/ApiService.kt | 8 +++ .../schedulingfrontend/MainActivity.kt | 30 +++++++- app/src/main/res/layout/activity_main.xml | 12 ++++ .../schedulingfrontend/ExampleUnitTest.kt | 4 +- gradle.properties | 1 - 9 files changed, 133 insertions(+), 52 deletions(-) create mode 100644 app/src/main/java/dk/scheduling/schedulingfrontend/API/ApiClient.kt create mode 100644 app/src/main/java/dk/scheduling/schedulingfrontend/API/ApiService.kt diff --git a/app/build.gradle b/app/build.gradle index 2474b93..541a1ac 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,6 +1,7 @@ plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' + id("org.jlleitschuh.gradle.ktlint") version "12.1.0" } android { @@ -42,52 +43,31 @@ android { dependencies { - implementation 'androidx.core:core-ktx:1.7.0' - implementation 'androidx.appcompat:appcompat:1.6.1' - implementation 'com.google.android.material:material:1.11.0' - implementation 'androidx.constraintlayout:constraintlayout:2.1.4' - testImplementation 'junit:junit:4.13.2' - androidTestImplementation 'androidx.test.ext:junit:1.1.5' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' + 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.03.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("org.apache.commons:commons-lang3:3.14.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.03.00")) + androidTestImplementation("androidx.compose.ui:ui-test-junit4") + debugImplementation("androidx.compose.ui:ui-tooling") + debugImplementation("androidx.compose.ui:ui-test-manifest") - def composeBom = platform('androidx.compose:compose-bom:2024.03.00') - implementation composeBom - androidTestImplementation composeBom - - // Choose one of the following: - // Material Design 3 - implementation 'androidx.compose.material3:material3' - // or Material Design 2 - implementation 'androidx.compose.material:material' - // or skip Material Design and build directly on top of foundational components - implementation 'androidx.compose.foundation:foundation' - // or only import the main APIs for the underlying toolkit systems, - // such as input and measurement/layout - implementation 'androidx.compose.ui:ui' - - // Android Studio Preview support - implementation 'androidx.compose.ui:ui-tooling-preview' - debugImplementation 'androidx.compose.ui:ui-tooling' - - // UI Tests - androidTestImplementation 'androidx.compose.ui:ui-test-junit4' - debugImplementation 'androidx.compose.ui:ui-test-manifest' - - // Optional - Included automatically by material, only add when you need - // the icons but not the material library (e.g. when using Material3 or a - // custom design system based on Foundation) - implementation 'androidx.compose.material:material-icons-core' - // Optional - Add full set of material icons - implementation 'androidx.compose.material:material-icons-extended' - // Optional - Add window size utils - implementation 'androidx.compose.material3:material3-window-size-class' + // For the API call + implementation 'com.squareup.retrofit2:retrofit:2.9.0' + implementation 'com.squareup.retrofit2:converter-gson:2.9.0' + +} - // Optional - Integration with activities - implementation 'androidx.activity:activity-compose:1.8.2' - // Optional - Integration with ViewModels - implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1' - // Optional - Integration with LiveData - implementation 'androidx.compose.runtime:runtime-livedata' - // Optional - Integration with RxJava - implementation 'androidx.compose.runtime:runtime-rxjava2' +ktlint { + dependencies { + ktlintRuleset("com.twitter.compose.rules:ktlint:0.0.26") + } } \ No newline at end of file diff --git a/app/src/androidTest/java/dk/scheduling/schedulingfrontend/ExampleInstrumentedTest.kt b/app/src/androidTest/java/dk/scheduling/schedulingfrontend/ExampleInstrumentedTest.kt index 30895e1..453cb20 100644 --- a/app/src/androidTest/java/dk/scheduling/schedulingfrontend/ExampleInstrumentedTest.kt +++ b/app/src/androidTest/java/dk/scheduling/schedulingfrontend/ExampleInstrumentedTest.kt @@ -3,7 +3,6 @@ package dk.scheduling.schedulingfrontend import androidx.test.platform.app.InstrumentationRegistry import androidx.test.ext.junit.runners.AndroidJUnit4 -import org.junit.Test import org.junit.runner.RunWith import org.junit.Assert.* @@ -15,7 +14,7 @@ import org.junit.Assert.* */ @RunWith(AndroidJUnit4::class) class ExampleInstrumentedTest { - @Test + @org.junit.jupiter.api.Test fun useAppContext() { // Context of the app under test. val appContext = InstrumentationRegistry.getInstrumentation().targetContext diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 8e8e7d8..30dbb70 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,6 +1,7 @@ + (object : X509TrustManager { + override fun checkClientTrusted(chain: Array?, authType: String?) {} + override fun checkServerTrusted(chain: Array?, authType: String?) {} + override fun getAcceptedIssuers(): Array = arrayOf() + }) + + val sslContext = SSLContext.getInstance("SSL") + sslContext.init(null, trustAllCertificates, SecureRandom()) + + val sslSocketFactory = sslContext.socketFactory + + OkHttpClient.Builder() + .sslSocketFactory(sslSocketFactory, trustAllCertificates[0] as X509TrustManager) + .hostnameVerifier { _, _ -> true } // Allow all hostnames + .build() + } + val retrofit: Retrofit by lazy { + Retrofit.Builder() + .baseUrl(BASE_URL) + .client(okHttpClient) + .addConverterFactory(GsonConverterFactory.create(gson)) + .build() + } +} + + + +object ApiClient { + val apiService: ApiService by lazy { + RetrofitClient.retrofit.create(ApiService::class.java) + } +} \ No newline at end of file diff --git a/app/src/main/java/dk/scheduling/schedulingfrontend/API/ApiService.kt b/app/src/main/java/dk/scheduling/schedulingfrontend/API/ApiService.kt new file mode 100644 index 0000000..d5c474b --- /dev/null +++ b/app/src/main/java/dk/scheduling/schedulingfrontend/API/ApiService.kt @@ -0,0 +1,8 @@ +package dk.scheduling.schedulingfrontend.API +import retrofit2.Call +import retrofit2.http.GET + +interface ApiService { + @GET("test") + fun test() : Call +} \ No newline at end of file diff --git a/app/src/main/java/dk/scheduling/schedulingfrontend/MainActivity.kt b/app/src/main/java/dk/scheduling/schedulingfrontend/MainActivity.kt index ca85c81..f1bdbf2 100644 --- a/app/src/main/java/dk/scheduling/schedulingfrontend/MainActivity.kt +++ b/app/src/main/java/dk/scheduling/schedulingfrontend/MainActivity.kt @@ -2,10 +2,38 @@ 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 retrofit2.Call +import retrofit2.Callback +import retrofit2.Response class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) + val button = findViewById