Skip to content

Commit

Permalink
Chore: Hilt removed and singleton pattern added
Browse files Browse the repository at this point in the history
  • Loading branch information
jdsdhp committed Jan 29, 2024
1 parent 71e6546 commit d934720
Show file tree
Hide file tree
Showing 17 changed files with 47 additions and 269 deletions.
6 changes: 0 additions & 6 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
plugins {
alias(libs.plugins.com.android.application)
alias(libs.plugins.org.jetbrains.kotlin.android)
kotlin("kapt")
id("com.google.dagger.hilt.android")
}

android {
Expand Down Expand Up @@ -55,10 +53,6 @@ android {
dependencies {
implementation(project(":enzona-payment-embedded"))

// Hilt
implementation(libs.hilt.android)
kapt(libs.hilt.android.compiler)

implementation(libs.core.ktx)
implementation(platform(libs.compose.bom))
implementation(libs.activity.compose)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.github.jdsdhp.enzona.payment.embedded.sample

import android.app.Application
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class App : Application()
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.github.jdsdhp.enzona.payment.embedded.sample.ui.theme.AppTheme
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

private val viewModel: MainViewModel by viewModels()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@ import com.github.jdsdhp.enzona.payment.embedded.Enzona
import com.github.jdsdhp.enzona.payment.embedded.domain.model.Item
import com.github.jdsdhp.enzona.payment.embedded.domain.model.Payment
import com.github.jdsdhp.enzona.payment.embedded.util.ResultValue
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import java.util.regex.Pattern
import javax.inject.Inject

private const val TAG = "dev/tag"

@HiltViewModel
class MainViewModel @Inject constructor(private val enzona: Enzona) : ViewModel() {
internal class MainViewModel : ViewModel() {

private val enzona = Enzona.getInstance()

data class UiState(
val isLoading: Boolean = false,
Expand Down
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ plugins {
alias(libs.plugins.com.android.application) apply false
alias(libs.plugins.org.jetbrains.kotlin.android) apply false
alias(libs.plugins.com.android.library) apply false
id("com.google.dagger.hilt.android") version "2.48" apply false
}
true // Needed to make the Suppress annotation work for the plugins block
7 changes: 1 addition & 6 deletions enzona-payment-embedded/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
plugins {
alias(libs.plugins.com.android.library)
alias(libs.plugins.org.jetbrains.kotlin.android)
kotlin("kapt")
id("com.google.dagger.hilt.android")
}

android {
Expand Down Expand Up @@ -41,10 +39,7 @@ dependencies {
implementation(libs.gson)
implementation(libs.okhttp)
implementation(libs.okhttp.logging.interceptor)

// Hilt
implementation(libs.hilt.android)
kapt(libs.hilt.android.compiler)
implementation(libs.kotlinx.coroutines.core)

// Test
testImplementation(libs.junit)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.jdsdhp.enzona.payment.embedded

import com.github.jdsdhp.enzona.payment.embedded.di.DatasourceModule
import com.github.jdsdhp.enzona.payment.embedded.domain.model.CancelStatus
import com.github.jdsdhp.enzona.payment.embedded.domain.model.Item
import com.github.jdsdhp.enzona.payment.embedded.domain.model.Payment
Expand All @@ -11,6 +12,20 @@ import com.github.jdsdhp.enzona.payment.embedded.util.ResultValue
*/
interface Enzona {

companion object {

@Volatile
private var instance: Enzona? = null // Volatile modifier is necessary

fun getInstance() =
instance ?: synchronized(this) { // synchronized to avoid concurrency problem
instance ?: EnzonaImpl(
authRemoteDatasource = DatasourceModule.authRemoteDatasource,
paymentRemoteDatasource = DatasourceModule.paymentRemoteDatasource,
).also { instance = it }
}
}

/**
* Enumeration representing different API URLs.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import com.github.jdsdhp.enzona.payment.embedded.domain.model.Item
import com.github.jdsdhp.enzona.payment.embedded.domain.model.Payment
import com.github.jdsdhp.enzona.payment.embedded.domain.model.Token
import com.github.jdsdhp.enzona.payment.embedded.util.ResultValue
import javax.inject.Inject

internal class EnzonaImpl @Inject constructor(
internal class EnzonaImpl(
private val authRemoteDatasource: AuthRemoteDatasource,
private val paymentRemoteDatasource: PaymentRemoteDatasource,
) : Enzona {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.github.jdsdhp.enzona.payment.embedded.data.datasource.remote.datasou

import com.github.jdsdhp.enzona.payment.embedded.Enzona
import com.github.jdsdhp.enzona.payment.embedded.data.datasource.remote.datasource.util.post
import com.github.jdsdhp.enzona.payment.embedded.di.IoDispatcher
import com.github.jdsdhp.enzona.payment.embedded.domain.datasource.AuthRemoteDatasource
import com.github.jdsdhp.enzona.payment.embedded.domain.datasource.RemoteDatasource
import com.github.jdsdhp.enzona.payment.embedded.domain.model.Scope
Expand All @@ -13,13 +12,12 @@ import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import org.json.JSONException
import org.json.JSONObject
import javax.inject.Inject
import kotlin.io.encoding.Base64
import kotlin.io.encoding.ExperimentalEncodingApi

internal class AuthRemoteDatasourceImpl @Inject constructor(
internal class AuthRemoteDatasourceImpl(
private val okHttpClient: OkHttpClient,
@IoDispatcher private val dispatcher: CoroutineDispatcher,
private val dispatcher: CoroutineDispatcher,
private val remoteDatasource: RemoteDatasource,
) : AuthRemoteDatasource {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.github.jdsdhp.enzona.payment.embedded.data.datasource.remote.dto.resp
import com.github.jdsdhp.enzona.payment.embedded.data.datasource.remote.dto.response.cancel.CancelResponseDto
import com.github.jdsdhp.enzona.payment.embedded.data.datasource.remote.mapper.asData
import com.github.jdsdhp.enzona.payment.embedded.data.datasource.remote.mapper.asDomain
import com.github.jdsdhp.enzona.payment.embedded.di.IoDispatcher
import com.github.jdsdhp.enzona.payment.embedded.domain.datasource.PaymentRemoteDatasource
import com.github.jdsdhp.enzona.payment.embedded.domain.datasource.RemoteDatasource
import com.github.jdsdhp.enzona.payment.embedded.domain.model.CancelStatus
Expand All @@ -21,11 +20,10 @@ import com.google.gson.Gson
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import javax.inject.Inject

internal class PaymentRemoteDatasourceImpl @Inject constructor(
internal class PaymentRemoteDatasourceImpl(
private val okHttpClient: OkHttpClient,
@IoDispatcher private val dispatcher: CoroutineDispatcher,
private val dispatcher: CoroutineDispatcher,
private val remoteDatasource: RemoteDatasource,
) : PaymentRemoteDatasource {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package com.github.jdsdhp.enzona.payment.embedded.data.datasource.remote.datasou

import com.github.jdsdhp.enzona.payment.embedded.domain.datasource.RemoteDatasource
import com.github.jdsdhp.enzona.payment.embedded.util.ResultValue
import javax.inject.Inject

/**
* Internal class representing a remote data source abstraction.
*/
internal class RemoteDatasourceImpl @Inject constructor() : RemoteDatasource {
internal class RemoteDatasourceImpl : RemoteDatasource {

override suspend fun <T> call(request: suspend () -> T): ResultValue<T> =
safeApiCall(request = request)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,27 @@ package com.github.jdsdhp.enzona.payment.embedded.di
import com.github.jdsdhp.enzona.payment.embedded.data.datasource.remote.datasource.AuthRemoteDatasourceImpl
import com.github.jdsdhp.enzona.payment.embedded.data.datasource.remote.datasource.PaymentRemoteDatasourceImpl
import com.github.jdsdhp.enzona.payment.embedded.data.datasource.remote.datasource.RemoteDatasourceImpl
import com.github.jdsdhp.enzona.payment.embedded.domain.datasource.AuthRemoteDatasource
import com.github.jdsdhp.enzona.payment.embedded.domain.datasource.PaymentRemoteDatasource
import com.github.jdsdhp.enzona.payment.embedded.domain.datasource.RemoteDatasource
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton
import com.github.jdsdhp.enzona.payment.embedded.di.NetworkModule.okHttpClient
import kotlinx.coroutines.Dispatchers

@Module
@InstallIn(SingletonComponent::class)
internal interface DatasourceModule {
internal object DatasourceModule {

@Singleton
@Binds
fun bindsRemoteDatasource(impl: RemoteDatasourceImpl): RemoteDatasource
private val remoteDatasource by lazy { RemoteDatasourceImpl() }

@Singleton
@Binds
fun bindsAuthRemoteDatasource(impl: AuthRemoteDatasourceImpl): AuthRemoteDatasource
internal val authRemoteDatasource by lazy {
AuthRemoteDatasourceImpl(
okHttpClient = okHttpClient,
dispatcher = Dispatchers.IO,
remoteDatasource = remoteDatasource,
)
}

@Singleton
@Binds
fun bindsPaymentRemoteDatasource(impl: PaymentRemoteDatasourceImpl): PaymentRemoteDatasource
internal val paymentRemoteDatasource by lazy {
PaymentRemoteDatasourceImpl(
okHttpClient = okHttpClient,
dispatcher = Dispatchers.IO,
remoteDatasource = remoteDatasource,
)
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
package com.github.jdsdhp.enzona.payment.embedded.di

import com.github.jdsdhp.enzona.payment.embedded.data.datasource.remote.datasource.util.ignoreCertificates
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import java.util.concurrent.TimeUnit
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
internal object NetworkModule {

private const val TIME_OUT_CONNECT = 20L
private const val TIME_OUT_WRITE = 20L
private const val TIME_OUT_READ = 20L

@Singleton
@Provides
fun provideOkHttpClient(): OkHttpClient = OkHttpClient.Builder()
internal val okHttpClient = OkHttpClient.Builder()
.connectTimeout(TIME_OUT_CONNECT, TimeUnit.SECONDS)
.writeTimeout(TIME_OUT_WRITE, TimeUnit.SECONDS)
.readTimeout(TIME_OUT_READ, TimeUnit.SECONDS)
Expand Down
Loading

0 comments on commit d934720

Please sign in to comment.