Skip to content

Commit

Permalink
Update project
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbel committed Nov 9, 2024
1 parent a074d77 commit 6c322df
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.google.ksp)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.kotlin.serialization)
alias(libs.plugins.kotlin.compose)
}

Expand Down
54 changes: 53 additions & 1 deletion app/src/main/kotlin/org/michaelbel/template/AppModule.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
package org.michaelbel.template

import androidx.datastore.preferences.core.PreferenceDataStoreFactory
import com.chuckerteam.chucker.api.ChuckerCollector
import com.chuckerteam.chucker.api.ChuckerInterceptor
import com.chuckerteam.chucker.api.RetentionManager
import io.ktor.client.HttpClient
import io.ktor.client.engine.okhttp.OkHttp
import io.ktor.client.plugins.HttpTimeout
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.plugins.defaultRequest
import io.ktor.http.ContentType
import io.ktor.http.contentType
import io.ktor.serialization.kotlinx.json.json
import kotlinx.serialization.json.Json
import okhttp3.logging.HttpLoggingInterceptor
import okio.Path.Companion.toPath
import org.koin.android.ext.koin.androidContext
import org.koin.androidx.viewmodel.dsl.viewModelOf
import org.koin.dsl.module
import org.michaelbel.template.datastore.AppPreferences
import org.michaelbel.template.interactor.AppInteractor
import org.michaelbel.template.ktor.AppService
import org.michaelbel.template.repository.AppRepository
import org.michaelbel.template.room.AppDao
import org.michaelbel.template.room.AppDatabase
Expand All @@ -23,7 +37,45 @@ val appModule = module {
val appDatabase = AppDatabase.getInstance(androidContext())
appDatabase.appDao()
}
single<AppRepository> { AppRepository(get(), get()) }
single<AppService> {
val chuckerInterceptor = ChuckerInterceptor.Builder(androidContext())
.collector(
collector = ChuckerCollector(
context = androidContext(),
showNotification = true,
retentionPeriod = RetentionManager.Period.ONE_HOUR
)
)
.maxContentLength(
length = 250_000L
)
.alwaysReadResponseBody(true)
.build()
val httpLoggingInterceptor = HttpLoggingInterceptor()
val ktorHttpClient = HttpClient(OkHttp) {
defaultRequest {
contentType(ContentType.Application.Json)
url("https://api.example.com/")
}
install(ContentNegotiation) {
json(Json { ignoreUnknownKeys = true })
}
install(HttpTimeout) {
requestTimeoutMillis = AppService.REQUEST_TIMEOUT_MILLIS
connectTimeoutMillis = AppService.CONNECT_TIMEOUT_MILLIS
socketTimeoutMillis = AppService.SOCKET_TIMEOUT_SECONDS
}
engine {
clientCacheSize = AppService.HTTP_CACHE_SIZE_BYTES
config {
addInterceptor(chuckerInterceptor)
addInterceptor(httpLoggingInterceptor)
}
}
}
AppService(ktorHttpClient)
}
single<AppRepository> { AppRepository(get(), get(), get()) }
single<AppInteractor> { AppInteractor(get()) }
viewModelOf(::MainViewModel)
}
10 changes: 10 additions & 0 deletions app/src/main/kotlin/org/michaelbel/template/ktor/AppResponse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.michaelbel.template.ktor

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class AppResponse(
@SerialName("id") val id: Int,
@SerialName("name") val name: String
)
26 changes: 26 additions & 0 deletions app/src/main/kotlin/org/michaelbel/template/ktor/AppService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.michaelbel.template.ktor

import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.request.get
import io.ktor.client.request.parameter

class AppService(
private val httpClient: HttpClient
) {

suspend fun getAppResponse(
id: Int
): AppResponse {
return httpClient.get("route/$id") {
parameter("key", "1234")
}.body()
}

companion object {
const val REQUEST_TIMEOUT_MILLIS = 10_000L
const val SOCKET_TIMEOUT_SECONDS = 10_000L
const val HTTP_CACHE_SIZE_BYTES = 1024 * 1024 * 50
const val CONNECT_TIMEOUT_MILLIS = 10_000L
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.michaelbel.template.repository

import org.michaelbel.template.datastore.AppPreferences
import org.michaelbel.template.ktor.AppService
import org.michaelbel.template.room.AppDao

class AppRepository(
private val appPreferences: AppPreferences,
private val appDao: AppDao
private val appDao: AppDao,
private val appService: AppService
)
7 changes: 7 additions & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ android {
}

dependencies {
api(libs.kotlinx.serialization.json)
api(libs.google.material)
api(libs.androidx.activity.compose)
api(libs.androidx.appcompat)
Expand All @@ -53,4 +54,10 @@ dependencies {
api(libs.androidx.navigation.compose)
api(libs.androidx.paging.compose)
api(libs.koin.androidx.compose)
api(libs.ktor.client.okhttp)
api(libs.ktor.serialization.kotlinx.json)
api(libs.ktor.client.content.negotiation)
api(libs.okhttp.logging.interceptor)
debugApi(libs.chucker.library)
releaseApi(libs.chucker.library.no.op)
}

0 comments on commit 6c322df

Please sign in to comment.