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

introduce compose to the project #85

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 44 additions & 4 deletions Ladefuchs/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,29 @@ plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-parcelize'
id 'kotlinx-serialization'
}

android {
compileSdkVersion 33
compileSdk 34
defaultConfig {
applicationId "app.ladefuchs.android"
minSdkVersion 28
targetSdkVersion 33
targetSdkVersion 34
versionCode 210
versionName '2.1.0'


testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildFeatures {
compose true
}

composeOptions {
kotlinCompilerExtensionVersion = "1.5.8"
}

buildTypes {
release {
minifyEnabled false
Expand Down Expand Up @@ -48,18 +56,50 @@ android {
}

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation(platform("org.jetbrains.kotlin:kotlin-bom:$kotlin_version"))

// compose
implementation(platform('androidx.compose:compose-bom:2023.10.01'))
implementation("androidx.compose.foundation:foundation")
implementation("androidx.compose.foundation:foundation-layout")
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.ui:ui-text-google-fonts")
debugImplementation("androidx.compose.ui:ui-tooling")


// theming
implementation("androidx.compose.material3:material3")

// dependency injection
implementation("io.insert-koin:koin-core:3.1.6")
implementation("io.insert-koin:koin-android:3.1.6")
implementation("io.insert-koin:koin-androidx-navigation:3.1.6")
implementation("io.insert-koin:koin-androidx-compose:3.1.6")

// http client
implementation("io.ktor:ktor-client-core:2.3.7")
implementation("io.ktor:ktor-client-cio:2.3.7")
implementation("io.ktor:ktor-client-auth:2.3.7")
implementation("io.ktor:ktor-client-serialization:2.3.7")
implementation("io.ktor:ktor-client-content-negotiation:2.3.7")
implementation("io.ktor:ktor-serialization-kotlinx-json:2.3.7")
implementation("io.ktor:ktor-client-logging-jvm:2.3.7")
implementation("io.ktor:ktor-client-mock-jvm:2.3.7")


implementation 'androidx.core:core-ktx:1.10.1'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.navigation:navigation-compose'
implementation 'androidx.navigation:navigation-fragment-ktx:2.6.0'
implementation 'androidx.navigation:navigation-ui-ktx:2.6.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.navigation:navigation-fragment-ktx:2.6.0'
implementation 'androidx.navigation:navigation-ui-ktx:2.6.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1'
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.7.0"
implementation 'androidx.preference:preference-ktx:1.2.0'
implementation 'androidx.recyclerview:recyclerview:1.3.0'
implementation "androidx.cardview:cardview:1.0.0"
Expand Down
6 changes: 2 additions & 4 deletions Ladefuchs/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
android:name=".LadefuchsApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand All @@ -23,8 +24,5 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
</application>
</manifest>
</manifest>
19 changes: 0 additions & 19 deletions Ladefuchs/app/src/main/java/app/ladefuchs/android/MainActivity.kt

This file was deleted.

24 changes: 19 additions & 5 deletions Ladefuchs/app/src/main/java/app/ladefuchs/android/helper/API.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,14 @@ fun retrieveCards(
if (forceDownload && !isOffline(context)) {
printLog("$pocOperatorId force download triggered")
val url = "$apiBaseURL/$apiVersionPath/cards/de/$pocOperatorId"
val acJson = downloadJson("$url/${ChargeType.AC}", context)
val dcJson = downloadJson("$url/${ChargeType.DC}", context)
val acJson = downloadJson(
"$url/${ChargeType.AC}",
context
)
val dcJson = downloadJson(
"$url/${ChargeType.DC}",
context
)
val klaxon = Klaxon()
if (acJson.isNotEmpty()) {
chargeCards.ac = klaxon.parseArray(acJson) ?: emptyList()
Expand Down Expand Up @@ -127,7 +133,10 @@ fun retrieveCards(
return chargeCards
}

private fun downloadJson(JSONUrl: String, context: Context): String {
private fun downloadJson(
JSONUrl: String,
context: Context
): String {
if (isOffline(context)) {
printLog("Device is offline", "network")
return ""
Expand Down Expand Up @@ -168,9 +177,14 @@ private fun downloadJson(JSONUrl: String, context: Context): String {
return jsonResponse;
}

fun retrieveOperatorList(context: Context): List<Operator> {
fun retrieveOperatorList(
context: Context
): List<Operator> {
val operatorFileName = "operators.json"
val operatorJson = downloadJson("$apiBaseURL/$apiVersionPath/operators/enabled", context)
val operatorJson = downloadJson(
"$apiBaseURL/$apiVersionPath/operators/enabled",
context
)
val json =
if (operatorJson.isEmpty()) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,6 @@ fun manipulateColor(color: Int, factor: Float): Int {
)
}

/**
* A function to safely open other pages
*/
fun NavController.safeNavigate(actionId: Int) {
currentDestination?.getAction(actionId)?.run { navigate(actionId) }
}

/**
* This function retrieves the prices for a specific operator
*/
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package app.ladefuchs.android

import android.app.Application
import app.ladefuchs.android.di.appModule
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.component.KoinComponent
import org.koin.core.context.GlobalContext.startKoin
import org.koin.core.logger.Level

class LadefuchsApplication : Application(), KoinComponent {
override fun onCreate() {
super.onCreate()

// setup dependency injection
startKoin {
// Koin Android logger
androidLogger(
if (BuildConfig.DEBUG) {
Level.ERROR
} else {
Level.NONE
}
)

// inject Android context
androidContext(applicationContext)

// use modules
modules(
listOf(appModule)
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package app.ladefuchs.android

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.exclude
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBars
import androidx.compose.material3.Scaffold
import androidx.compose.material3.ScaffoldDefaults
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import app.ladefuchs.android.chargecards.ui.ChargeCardScreen
import app.ladefuchs.android.ui.settings.SettingsScreen

/**
* Application entry-point
*/
class MainActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setContent {
LadefuchsApp()
}
}
}

@Composable
fun LadefuchsApp() {
val snackbarHostState = remember { SnackbarHostState() }
val navController = rememberNavController()

Scaffold(
modifier = Modifier.fillMaxSize(),
snackbarHost = { SnackbarHost(snackbarHostState) },
contentWindowInsets = ScaffoldDefaults.contentWindowInsets.exclude(WindowInsets.statusBars), // Is hanled by content
content = { innerPadding ->
NavHost(
modifier = Modifier
.fillMaxSize()
.padding(top = innerPadding.calculateTopPadding()),
navController = navController,
startDestination = "chargeCards",
) {
composable(route = "chargeCards") {
ChargeCardScreen(navController = navController)
}
composable(route = "settings") {
SettingsScreen(navController = navController)
}
composable(route = "cardDetails") {

}
}
}
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package app.ladefuchs.android.chargecards.api

import app.ladefuchs.android.chargecards.api.entity.Card
import app.ladefuchs.android.chargecards.api.entity.Operator
import app.ladefuchs.android.chargecards.data.model.PlugType
import app.ladefuchs.android.dataClasses.Banner

interface ChargeCardsApi {
suspend fun fetchOperators(): List<Operator>

suspend fun fetchCards(pocOperatorId: String, plugType: PlugType): List<Card>

suspend fun retrieveBanners(): Banner?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package app.ladefuchs.android.chargecards.api

import app.ladefuchs.android.chargecards.api.entity.Card
import app.ladefuchs.android.chargecards.api.entity.Operator
import app.ladefuchs.android.chargecards.data.model.PlugType
import app.ladefuchs.android.dataClasses.Banner
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.request.get

class ChargeCardsApiImpl(
private val httpClient: HttpClient,
): ChargeCardsApi {
override suspend fun fetchOperators(): List<Operator> {
return httpClient.get(urlString = "/v2/operators/enabled").body()
}

override suspend fun fetchCards(pocOperatorId: String, plugType: PlugType): List<Card> {
return httpClient.get(urlString = "/v2/cards/de/$pocOperatorId/${plugType.name.uppercase()}").body()
}

override suspend fun retrieveBanners(): Banner? {
return httpClient.get(urlString = "/v2/banners").body()
}
}
Loading