Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AN/USER] refactor: Hilt 적용(#453) #454

Merged
merged 9 commits into from
Sep 20, 2023
Merged
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
15 changes: 12 additions & 3 deletions android/festago/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ plugins {
id("com.google.gms.google-services")
id("com.google.firebase.crashlytics")
id("org.jlleitschuh.gradle.ktlint")
id("com.google.dagger.hilt.android")
}

android {
Expand Down Expand Up @@ -63,13 +64,24 @@ kotlin {
jvmToolchain(17)
}

kapt {
correctErrorTypes = true
}

dependencies {
// domain
implementation(project(":domain"))

// android
implementation("androidx.core:core-ktx:1.10.1")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.9.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")

// hilt
implementation("com.google.dagger:hilt-android:2.44")
kapt("com.google.dagger:hilt-android-compiler:2.44")

// recyclerview
implementation("androidx.recyclerview:recyclerview:1.3.1-rc01")

Expand Down Expand Up @@ -139,9 +151,6 @@ dependencies {

// turbine
testImplementation("app.cash.turbine:turbine:1.0.0")

// domain
implementation(project(":domain"))
}

fun getSecretKey(propertyKey: String): String {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,58 +1,18 @@
package com.festago.festago

import android.app.Application
import com.festago.festago.analytics.FirebaseAnalyticsHelper
import com.festago.festago.di.AnalysisContainer
import com.festago.festago.di.AuthServiceContainer
import com.festago.festago.di.LocalDataSourceContainer
import com.festago.festago.di.NormalServiceContainer
import com.festago.festago.di.RepositoryContainer
import com.festago.festago.di.TokenContainer
import com.kakao.sdk.common.KakaoSdk
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class FestagoApplication : Application() {

override fun onCreate() {
super.onCreate()
initKakaoSdk()
initRepositoryContainer()
initFirebaseContainer()
}

private fun initKakaoSdk() {
KakaoSdk.init(this, BuildConfig.KAKAO_NATIVE_APP_KEY)
}

private fun initRepositoryContainer() {
normalServiceContainer = NormalServiceContainer(BuildConfig.BASE_URL)

tokenContainer = TokenContainer(
normalServiceContainer = normalServiceContainer,
localDataSourceContainer = LocalDataSourceContainer(applicationContext),
)

authServiceContainer = AuthServiceContainer(
baseUrl = BuildConfig.BASE_URL,
tokenContainer = tokenContainer,
)

repositoryContainer = RepositoryContainer(
authServiceContainer = authServiceContainer,
normalServiceContainer = normalServiceContainer,
tokenContainer = tokenContainer,
)
}

private fun initFirebaseContainer() {
FirebaseAnalyticsHelper.init(applicationContext)
analysisContainer = AnalysisContainer()
}

companion object DependencyContainer {
lateinit var normalServiceContainer: NormalServiceContainer
lateinit var authServiceContainer: AuthServiceContainer
lateinit var repositoryContainer: RepositoryContainer
lateinit var analysisContainer: AnalysisContainer
lateinit var tokenContainer: TokenContainer
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package com.festago.festago.analytics
import android.content.Context
import android.os.Bundle
import com.google.firebase.analytics.FirebaseAnalytics
import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject

object FirebaseAnalyticsHelper : AnalyticsHelper {
class FirebaseAnalyticsHelper @Inject constructor(
@ApplicationContext context: Context
) : AnalyticsHelper {

private const val LOG_NAME = "festago_log"
private lateinit var firebaseAnalytics: FirebaseAnalytics

fun init(context: Context) {
if (::firebaseAnalytics.isInitialized) return
firebaseAnalytics = FirebaseAnalytics.getInstance(context.applicationContext)
private val firebaseAnalytics: FirebaseAnalytics by lazy {
FirebaseAnalytics.getInstance(context.applicationContext)
}

override fun logEvent(event: AnalyticsEvent) {
Expand All @@ -22,4 +22,8 @@ object FirebaseAnalyticsHelper : AnalyticsHelper {
}
firebaseAnalytics.logEvent(LOG_NAME, params)
}

companion object {
private const val LOG_NAME = "festago_log"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import android.content.Context
import android.content.SharedPreferences
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKey
import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject

class TokenLocalDataSource(context: Context) : TokenDataSource {
class TokenLocalDataSource @Inject constructor(
@ApplicationContext context: Context
) : TokenDataSource {

private val sharedPreference: SharedPreferences by lazy {
val masterKeyAlias = MasterKey
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.festago.festago.data.di

import com.festago.festago.data.repository.ReservationTicketDefaultRepository
import com.festago.festago.data.repository.StudentVerificationDefaultRepository
import com.festago.festago.data.repository.UserDefaultRepository
import com.festago.festago.repository.ReservationTicketRepository
import com.festago.festago.repository.StudentVerificationRepository
import com.festago.festago.repository.UserRepository
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ViewModelComponent
import dagger.hilt.android.scopes.ViewModelScoped

@InstallIn(ViewModelComponent::class)
@Module
interface ViewModelScopeModule {

@Binds
@ViewModelScoped
fun bindsReservationTicketDefaultRepository(reservationTicketRepository: ReservationTicketDefaultRepository): ReservationTicketRepository

@Binds
@ViewModelScoped
fun bindsStudentVerificationDefaultRepository(studentVerificationRepository: StudentVerificationDefaultRepository): StudentVerificationRepository

@Binds
@ViewModelScoped
fun binsUserDefaultRepository(userRepository: UserDefaultRepository): UserRepository
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.festago.festago.data.di.singletonscope

import com.festago.festago.analytics.AnalyticsHelper
import com.festago.festago.analytics.FirebaseAnalyticsHelper
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@InstallIn(SingletonComponent::class)
@Module
interface AnalyticsModule {
@Binds
@Singleton
fun bindsFirebaseAnalyticsHelper(
analyticsHelper: FirebaseAnalyticsHelper
): AnalyticsHelper
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.festago.festago.data.di.singletonscope

import com.festago.festago.BuildConfig
import com.festago.festago.data.retrofit.AuthInterceptor
import com.festago.festago.data.retrofit.TokenManager
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import javax.inject.Qualifier
import javax.inject.Singleton

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class NormalRetrofitQualifier

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class AuthRetrofitQualifier

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class BaseUrlQualifier

@InstallIn(SingletonComponent::class)
@Module
object ApiModule {

@Provides
@Singleton
fun provideOkHttpClient(tokenManager: TokenManager): OkHttpClient = OkHttpClient
.Builder()
.addInterceptor(AuthInterceptor(tokenManager))
.build()

@Provides
@Singleton
@NormalRetrofitQualifier
fun providesNormalRetrofit(@BaseUrlQualifier baseUrl: String): Retrofit = Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(Json.asConverterFactory("application/json".toMediaType()))
.build()

@Provides
@Singleton
@AuthRetrofitQualifier
fun providesAuthRetrofit(
@BaseUrlQualifier baseUrl: String,
okHttpClient: OkHttpClient
): Retrofit = Retrofit.Builder()
.baseUrl(baseUrl)
.client(okHttpClient)
.addConverterFactory(Json.asConverterFactory("application/json".toMediaType()))
.build()

@Provides
@Singleton
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

반환 타입이 원시 타입이라 Qualifier를 미리 정의하는건 어떤가요??

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BaseUrlQualifier 추가 완료!

@BaseUrlQualifier
fun providesBaseUrl(): String = BuildConfig.BASE_URL
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.festago.festago.data.di.singletonscope

import com.festago.festago.data.datasource.TokenDataSource
import com.festago.festago.data.datasource.TokenLocalDataSource
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@InstallIn(SingletonComponent::class)
@Module
interface DataSourceModule {
@Binds
@Singleton
fun bindsLocalTokenDataSource(tokenDataSource: TokenLocalDataSource): TokenDataSource
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.festago.festago.data.di.singletonscope

import com.festago.festago.data.repository.AuthDefaultRepository
import com.festago.festago.data.repository.FestivalDefaultRepository
import com.festago.festago.data.repository.SchoolDefaultRepository
import com.festago.festago.data.repository.TicketDefaultRepository
import com.festago.festago.data.repository.TokenDefaultRepository
import com.festago.festago.repository.AuthRepository
import com.festago.festago.repository.FestivalRepository
import com.festago.festago.repository.SchoolRepository
import com.festago.festago.repository.TicketRepository
import com.festago.festago.repository.TokenRepository
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@InstallIn(SingletonComponent::class)
@Module
interface RepositoryModule {

@Binds
fun bindsTokenDefaultRepository(tokenRepository: TokenDefaultRepository): TokenRepository

@Binds
fun bindsAuthDefaultRepository(authRepository: AuthDefaultRepository): AuthRepository

@Binds
@Singleton
fun bindsFestivalDefaultRepository(festivalRepository: FestivalDefaultRepository): FestivalRepository

@Binds
@Singleton
fun bindsSchoolDefaultRepository(schoolRepository: SchoolDefaultRepository): SchoolRepository

@Binds
@Singleton
fun bindsTicketDefaultRepository(ticketRepository: TicketDefaultRepository): TicketRepository
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.festago.festago.data.di.singletonscope

import com.festago.festago.data.service.FestivalRetrofitService
import com.festago.festago.data.service.ReservationTicketRetrofitService
import com.festago.festago.data.service.StudentVerificationRetrofitService
import com.festago.festago.data.service.TicketRetrofitService
import com.festago.festago.data.service.TokenRetrofitService
import com.festago.festago.data.service.UserRetrofitService
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import retrofit2.Retrofit
import javax.inject.Singleton

@InstallIn(SingletonComponent::class)
@Module
object ServiceModule {

@Provides
@Singleton
fun providesFestivalRetrofitService(
@NormalRetrofitQualifier retrofit: Retrofit
): FestivalRetrofitService {
return retrofit.create(FestivalRetrofitService::class.java)
}

@Provides
@Singleton
fun providesTokenRetrofitService(
@NormalRetrofitQualifier retrofit: Retrofit
): TokenRetrofitService {
return retrofit.create(TokenRetrofitService::class.java)
}

@Provides
@Singleton
fun providesReservationTicketRetrofitService(
@NormalRetrofitQualifier retrofit: Retrofit
): ReservationTicketRetrofitService {
return retrofit.create(ReservationTicketRetrofitService::class.java)
}

@Provides
@Singleton
fun providesTicketRetrofitService(
@AuthRetrofitQualifier retrofit: Retrofit
): TicketRetrofitService {
return retrofit.create(TicketRetrofitService::class.java)
}

@Provides
@Singleton
fun providesUserRetrofitService(
@AuthRetrofitQualifier retrofit: Retrofit
): UserRetrofitService {
return retrofit.create(UserRetrofitService::class.java)
}

@Provides
@Singleton
fun providesStudentVerificationRetrofitService(
@AuthRetrofitQualifier retrofit: Retrofit
): StudentVerificationRetrofitService {
return retrofit.create(StudentVerificationRetrofitService::class.java)
}
}
Loading