Skip to content

Commit

Permalink
feat: thread to coroutine (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
zipizigi authored Jan 8, 2024
1 parent 45efdbe commit a9397b6
Show file tree
Hide file tree
Showing 39 changed files with 847 additions and 824 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ jobs:
uses: ScaCap/action-ktlint@master
with:
github_token: ${{ secrets.github_token }}
ktlint_version: 1.0.1
ktlint_version: 1.1.0
file_glob: '!**/src/test/**'
reporter: github-pr-check # github-pr-check,github-pr-review,github-check
5 changes: 3 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ android {
isShrinkResources = true
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
"proguard-rules.pro",
)
}
getByName("debug") {
isMinifyEnabled = false
isShrinkResources = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
"proguard-rules.pro",
)
applicationIdSuffix = ".debug"
versionNameSuffix = "-debug"
Expand All @@ -78,6 +78,7 @@ android {
}

dependencies {
implementation(libs.kotlin.coroutine)

implementation(libs.androidx.appcompat)
implementation(libs.material)
Expand Down
15 changes: 0 additions & 15 deletions app/src/main/kotlin/me/zipi/navitotesla/AppExecutors.kt

This file was deleted.

129 changes: 60 additions & 69 deletions app/src/main/kotlin/me/zipi/navitotesla/AppRepository.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package me.zipi.navitotesla

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.room.withTransaction
import me.zipi.navitotesla.api.TeslaApi
import me.zipi.navitotesla.api.TeslaAuthApi
import me.zipi.navitotesla.db.AppDatabase
Expand All @@ -15,65 +14,56 @@ import retrofit2.converter.gson.GsonConverterFactory
import java.util.Date
import java.util.concurrent.TimeUnit

class AppRepository private constructor(context: Context, private val database: AppDatabase) {
val teslaApi: TeslaApi
class AppRepository private constructor(private val database: AppDatabase) {
val teslaApi: TeslaApi = Retrofit.Builder()
.baseUrl("https://owner-api.teslamotors.com")
.addConverterFactory(GsonConverterFactory.create())
.client(
OkHttpClient.Builder()
.connectTimeout(120, TimeUnit.SECONDS)
.readTimeout(120, TimeUnit.SECONDS)
.addInterceptor(Interceptor { chain: Interceptor.Chain ->
val token = PreferencesUtil.loadTokenSync()
val accessToken = token?.accessToken ?: ""
val request = chain.request().newBuilder()
.addHeader("User-Agent", "Navi_To_Tesla")
.addHeader("Accept", "*/*")
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "Bearer $accessToken")
.build()
chain.proceed(request)
})
.addInterceptor(HttpRetryInterceptor(20))
.build()
)
.build().create(TeslaApi::class.java)

val teslaAuthApi: TeslaAuthApi

init {
teslaApi = Retrofit.Builder()
.baseUrl("https://owner-api.teslamotors.com")
.addConverterFactory(GsonConverterFactory.create())
.client(
OkHttpClient.Builder()
.connectTimeout(120, TimeUnit.SECONDS)
.readTimeout(120, TimeUnit.SECONDS)
.addInterceptor(Interceptor { chain: Interceptor.Chain ->
val token = PreferencesUtil.loadToken(context)
val accessToken = token?.accessToken ?: ""
val request = chain.request().newBuilder()
.addHeader("User-Agent", "Navi_To_Tesla")
.addHeader("Accept", "*/*")
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "Bearer $accessToken")
.build()
chain.proceed(request)
})
.addInterceptor(HttpRetryInterceptor(20))
.build()
)
.build().create(TeslaApi::class.java)
teslaAuthApi = Retrofit.Builder()
.baseUrl("https://auth.tesla.com")
.addConverterFactory(GsonConverterFactory.create())
.client(
OkHttpClient.Builder()
.connectTimeout(120, TimeUnit.SECONDS)
.readTimeout(120, TimeUnit.SECONDS)
.addInterceptor(Interceptor { chain: Interceptor.Chain ->
val request = chain.request().newBuilder()
.addHeader("User-Agent", "Navi_To_Tesla")
.addHeader("Accept", "*/*")
.addHeader("Content-Type", "application/json")
.build()
chain.proceed(request)
})
.addInterceptor(HttpRetryInterceptor(20))
.build()
)
.build().create(TeslaAuthApi::class.java)
}
@Suppress("unused")
fun getPoi(poiName: String): LiveData<PoiAddressEntity> {
val teslaAuthApi: TeslaAuthApi = Retrofit.Builder()
.baseUrl("https://auth.tesla.com")
.addConverterFactory(GsonConverterFactory.create())
.client(
OkHttpClient.Builder()
.connectTimeout(120, TimeUnit.SECONDS)
.readTimeout(120, TimeUnit.SECONDS)
.addInterceptor(Interceptor { chain: Interceptor.Chain ->
val request = chain.request().newBuilder()
.addHeader("User-Agent", "Navi_To_Tesla")
.addHeader("Accept", "*/*")
.addHeader("Content-Type", "application/json")
.build()
chain.proceed(request)
})
.addInterceptor(HttpRetryInterceptor(20))
.build()
)
.build().create(TeslaAuthApi::class.java)

suspend fun getPoiSync(poiName: String): PoiAddressEntity? {
return database.poiAddressDao().findPoi(poiName)
}

fun getPoiSync(poiName: String): PoiAddressEntity? {
return database.poiAddressDao().findPoiSync(poiName)
}

fun savePoi(poiName: String, address: String, registered: Boolean) {
database.runInTransaction {
suspend fun savePoi(poiName: String, address: String, registered: Boolean) {
database.withTransaction {
database.poiAddressDao().insertPoi(
PoiAddressEntity(
poi = poiName,
Expand All @@ -85,34 +75,35 @@ class AppRepository private constructor(context: Context, private val database:
}
}

fun clearExpiredPoi() {
suspend fun clearExpiredPoi() {
// remove expire poi. (20% over)
val expireDate: Long =
(Date().time - PoiAddressEntity.expireDay * 1000 * 60 * 60 * 24 * 1.2).toLong()
for (entity in database.poiAddressDao().findExpired(expireDate)) {
database.runInTransaction {
database.withTransaction {
database.poiAddressDao().delete(entity)
}
}
}

fun clearAllPoi() {
database.runInTransaction {
suspend fun clearAllPoi() {
database.withTransaction {
database.poiAddressDao().deleteAllNotRegistered()
}
}

companion object {
private var instance: AppRepository? = null
fun getInstance(context: Context, database: AppDatabase): AppRepository {
if (instance == null) {
synchronized(AppRepository::class.java) {
if (instance == null) {
instance = AppRepository(context, database)
}
private lateinit var instance: AppRepository
fun initialize(database: AppDatabase) {
if (!this::instance.isInitialized) {
synchronized(AppDatabase::class) {
instance = AppRepository(database)
}
}
return instance!!
}

fun getInstance(): AppRepository {
return instance
}
}
}
9 changes: 6 additions & 3 deletions app/src/main/kotlin/me/zipi/navitotesla/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@ import com.google.firebase.crashlytics.FirebaseCrashlytics
import me.zipi.navitotesla.background.TokenWorker
import me.zipi.navitotesla.db.AppDatabase
import me.zipi.navitotesla.util.AnalysisUtil
import me.zipi.navitotesla.util.PreferencesUtil
import me.zipi.navitotesla.util.RemoteConfigUtil

class Application : Application() {
override fun onCreate() {
super.onCreate()
AppRepository.getInstance(this, database)
PreferencesUtil.initialize(this.applicationContext)
AppDatabase.initialize(this.applicationContext)
AppRepository.initialize(database)
RemoteConfigUtil.initialize()
AnalysisUtil.initialize(this)
AnalysisUtil.initialize(this.applicationContext)
if (!BuildConfig.DEBUG) {
TokenWorker.startBackgroundWork(this)
FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(false)
}
}

private val database: AppDatabase
get() = AppDatabase.getInstance(this)
get() = AppDatabase.getInstance()
}
6 changes: 3 additions & 3 deletions app/src/main/kotlin/me/zipi/navitotesla/api/GithubApi.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package me.zipi.navitotesla.api

import me.zipi.navitotesla.model.Github.Release
import retrofit2.Call
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Path

interface GithubApi {
@GET("/repos/{owner}/{repo}/releases?per_page=10")
fun getReleases(
suspend fun getReleases(
@Path("owner") owner: String,
@Path("repo") repo: String
): Call<List<Release>>
): Response<List<Release>>
}
4 changes: 2 additions & 2 deletions app/src/main/kotlin/me/zipi/navitotesla/api/KakaoMapApi.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package me.zipi.navitotesla.api

import me.zipi.navitotesla.model.KakaoMap
import retrofit2.Call
import retrofit2.Response
import retrofit2.http.Headers
import retrofit2.http.POST
import retrofit2.http.Query

interface KakaoMapApi {
@POST("/v2/local/search/keyword.json")
@Headers("Accept: application/json")
fun search(@Query("query") query: String): Call<KakaoMap.Response<KakaoMap.Place>>
suspend fun search(@Query("query") query: String): Response<KakaoMap.Response<KakaoMap.Place>>
}
4 changes: 2 additions & 2 deletions app/src/main/kotlin/me/zipi/navitotesla/api/NaverMapApi.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package me.zipi.navitotesla.api

import me.zipi.navitotesla.model.NaverMap
import retrofit2.Call
import retrofit2.Response
import retrofit2.http.Headers
import retrofit2.http.POST
import retrofit2.http.Query

interface NaverMapApi {
@POST("/search2/searchMore.naver")
@Headers("Accept: application/json")
fun search(@Query("query") query: String): Call<NaverMap.Response>
suspend fun search(@Query("query") query: String): Response<NaverMap.Response>
}
4 changes: 2 additions & 2 deletions app/src/main/kotlin/me/zipi/navitotesla/api/TMapApi.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package me.zipi.navitotesla.api

import me.zipi.navitotesla.model.TMap.SearchPoiResponse
import retrofit2.Call
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Headers
import retrofit2.http.Query

interface TMapApi {
@GET("/tmap/pois")
@Headers("Accept: application/json")
fun search(@Query("searchKeyword") query: String): Call<SearchPoiResponse>
suspend fun search(@Query("searchKeyword") query: String): Response<SearchPoiResponse>
}
8 changes: 4 additions & 4 deletions app/src/main/kotlin/me/zipi/navitotesla/api/TeslaApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ package me.zipi.navitotesla.api
import me.zipi.navitotesla.model.ShareRequest
import me.zipi.navitotesla.model.TeslaApiResponse
import me.zipi.navitotesla.model.Vehicle
import retrofit2.Call
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.Path

interface TeslaApi {
@GET("/api/1/vehicles")
fun vehicles(): Call<TeslaApiResponse.ListType<Vehicle>>
suspend fun vehicles(): Response<TeslaApiResponse.ListType<Vehicle>>

@POST("/api/1/vehicles/{id}/command/share")
fun share(
suspend fun share(
@Path("id") id: Long,
@Body request: ShareRequest
): Call<TeslaApiResponse.ObjectType<TeslaApiResponse.Result>>
): Response<TeslaApiResponse.ObjectType<TeslaApiResponse.Result>>
}
4 changes: 2 additions & 2 deletions app/src/main/kotlin/me/zipi/navitotesla/api/TeslaAuthApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package me.zipi.navitotesla.api

import me.zipi.navitotesla.model.TeslaRefreshTokenRequest
import me.zipi.navitotesla.model.Token
import retrofit2.Call
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.POST

interface TeslaAuthApi {
@POST("/oauth2/v3/token")
fun refreshAccessToken(@Body request: TeslaRefreshTokenRequest): Call<Token>
suspend fun refreshAccessToken(@Body request: TeslaRefreshTokenRequest): Response<Token>
}
Loading

0 comments on commit a9397b6

Please sign in to comment.