Skip to content

Commit

Permalink
Compose migration (#26)
Browse files Browse the repository at this point in the history
* Modify configuration files to support Kotlin & Jetpack Compose

As discussed this morning, we plan on moving to Kotlin and Jetpack compose. This commit marks the beginning of the modification to make it.

* Update minSdk to comply with firebase requirement

Changes:
* Change from minSdk:24 to minSdk:28 to comply with the firebase
  realtime database requirements

* Delete old java activity file and write basic test

Changes:
* Remove `GreetingActivity.java` and `MainActivity.java` and
  dependencies
* Write simple `ComposeActvitityTest.kt`

* fix: Support for coverage with Kotlin

* Added Mockito

* Fixed the build.gradle with proper versions

* 🔥 💯 :triump: base 😡

---------

Co-authored-by: BoyeGuillaume <guillaume.boye@epfl.ch>
  • Loading branch information
Maeeen and BoyeGuillaume authored Mar 8, 2023
1 parent 4d62037 commit f9251b6
Show file tree
Hide file tree
Showing 22 changed files with 371 additions and 207 deletions.
48 changes: 46 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'com.android.application'
id 'jacoco'
id 'org.jetbrains.kotlin.android'
}

android {
Expand All @@ -9,12 +10,15 @@ android {

defaultConfig {
applicationId "com.github.geohunt.app"
minSdk 24
minSdk 28
targetSdk 33
versionCode 1
versionName "1.0"

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

buildTypes {
Expand All @@ -30,18 +34,58 @@ android {
sourceCompatibility JavaVersion.VERSION_1_9
targetCompatibility JavaVersion.VERSION_1_9
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.2.0'
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}

testOptions {
// Some dirty hack
// see http://tools.android.com/tech-docs/unit-testing-support#TOC-Method-...-not-mocked.-
unitTests.returnDefaultValues = true
}
}

dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
implementation 'androidx.navigation:navigation-compose:2.5.3'

// Add compose dependencies
implementation 'androidx.activity:activity-compose:1.6.1'
implementation "androidx.compose.ui:ui:$compose_ui_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
implementation 'androidx.compose.material:material:1.3.1'
implementation 'com.google.android.gms:play-services-location:19.0.1'

// Firebase
implementation platform('com.google.firebase:firebase-bom:31.2.3')

// Tests
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation 'androidx.test:runner:1.5.2'
androidTestImplementation 'androidx.test:rules:1.5.0'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.5.1'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_ui_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version"
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version"
testImplementation "org.mockito.kotlin:mockito-kotlin:4.1.0"
testImplementation "org.mockito:mockito-inline:4.1.0"
}

tasks.withType(Test) {
Expand All @@ -64,7 +108,7 @@ task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'crea
'android/**/*.*',
]

def debugTree = fileTree(dir: "$project.buildDir/intermediates/javac/debug/classes", excludes: fileFilter)
def debugTree = fileTree(dir: "$project.buildDir/tmp/kotlin-classes/debug", excludes: fileFilter)
def mainSrc = "$project.projectDir/src/main/java"

sourceDirectories.setFrom(files([mainSrc]))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.github.geohunt.app

import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import org.junit.Rule
import org.junit.Test

class ComposeActivityTest {

@get:Rule
val composeTestRule = createComposeRule()

@Test
fun testMainComposeActivity() {
// Start the application
composeTestRule.setContent {
DefaultPreview()
}

composeTestRule.onNodeWithText("Hello Android!")
.assertIsDisplayed();
}
}

This file was deleted.

This file was deleted.

10 changes: 5 additions & 5 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
android:theme="@style/Theme.GeoHunt"
tools:targetApi="31">
<activity
android:name=".GreetingActivity"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
android:name=".ComposeActivity"
android:exported="true"
android:label="@string/title_activity_compose"
android:theme="@style/Theme.GeoHunt">

<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
40 changes: 40 additions & 0 deletions app/src/main/java/com/github/geohunt/app/ComposeActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.github.geohunt.app

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.github.geohunt.app.ui.theme.GeoHuntTheme

class ComposeActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
GeoHuntTheme {
// A surface container using the 'background' color from the theme
Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background) {
Greeting("Android")
}
}
}
}
}

@Composable
fun Greeting(name: String) {
Text(text = "Hello $name!")
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
GeoHuntTheme {
Greeting("Android")
}
}
22 changes: 0 additions & 22 deletions app/src/main/java/com/github/geohunt/app/GreetingActivity.java

This file was deleted.

27 changes: 0 additions & 27 deletions app/src/main/java/com/github/geohunt/app/MainActivity.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.github.geohunt.app.model.database

import android.app.Activity
import android.graphics.Bitmap
import com.github.geohunt.app.model.database.api.Challenge
import com.github.geohunt.app.model.database.api.Location
import java.util.concurrent.CompletableFuture


interface Database {
fun createChallenge(bitmap: Bitmap, location: Location) : CompletableFuture<Challenge>

fun getChallengeById(cid: String) : CompletableFuture<Challenge>

fun getNearbyChallenge(location: Location) : CompletableFuture<List<Challenge>>

// fun getProfileById(uid: String) : CompletableFuture<Profile>

}

/**
* Utility class to create a new database handle (this is used for testing purpose
* to replace the database instance with a mock one using mockito)
*/
object DatabaseFactory {

/**
* Create a new instance of a database
*/
fun createDatabaseHandle(activity: Activity) : Database {
TODO("Not implemented")
}
}

Loading

0 comments on commit f9251b6

Please sign in to comment.