-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
#12 [feat] Add Di Module
- Loading branch information
Showing
26 changed files
with
459 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,33 @@ | ||
package com.jjbaksa.jjbaksa | ||
|
||
import android.app.Application | ||
import android.content.Context | ||
import android.content.pm.ApplicationInfo | ||
import android.content.pm.PackageManager | ||
import dagger.hilt.android.HiltAndroidApp | ||
|
||
@HiltAndroidApp | ||
class JjbaksaApp : Application() | ||
class JjbaksaApp : Application() { | ||
val appContext: Context = this | ||
val isDebug | ||
get() = isDebug(appContext) | ||
|
||
/** | ||
* 디버그모드인지 확인하는 함수 | ||
*/ | ||
private fun isDebug(context: Context): Boolean { | ||
var debuggable = false | ||
val pm: PackageManager = context.packageManager | ||
try { | ||
val appinfo = pm.getApplicationInfo(context.packageName, 0) | ||
debuggable = 0 != appinfo.flags and ApplicationInfo.FLAG_DEBUGGABLE | ||
} catch (e: PackageManager.NameNotFoundException) { | ||
} | ||
|
||
return debuggable | ||
} | ||
companion object { | ||
lateinit var instance: JjbaksaApp | ||
private set | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
app/src/main/java/com/jjbaksa/jjbaksa/di/DataBaseModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.jjbaksa.jjbaksa.di | ||
|
||
import android.content.Context | ||
import androidx.room.Room | ||
import com.jjbaksa.data.database.AppDatabase | ||
import com.jjbaksa.data.database.UserDao | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object DataBaseModule { | ||
@Provides | ||
@Singleton | ||
fun provideRoomDataBase(@ApplicationContext context: Context): AppDatabase { | ||
return Room | ||
.databaseBuilder(context, AppDatabase::class.java, "MyExoPlayer.db") | ||
.allowMainThreadQueries() | ||
.build() | ||
} | ||
@Provides | ||
@Singleton | ||
fun provideVideoDao(appDatabase: AppDatabase): UserDao { | ||
return appDatabase.userDao() | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
app/src/main/java/com/jjbaksa/jjbaksa/di/DataSourceModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.jjbaksa.jjbaksa.di | ||
|
||
import com.jjbaksa.data.api.AuthApi | ||
import com.jjbaksa.data.api.NoAuthApi | ||
import com.jjbaksa.data.database.UserDao | ||
import com.jjbaksa.data.datasource.local.UserLocalDataSource | ||
import com.jjbaksa.data.datasource.remote.UserRemoteDataSource | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object DataSourceModule { | ||
@Provides | ||
@Singleton | ||
fun provideUserRemoteDataSource(authApi: AuthApi, noAuthApi: NoAuthApi): UserRemoteDataSource { | ||
return UserRemoteDataSource(authApi, noAuthApi) | ||
} | ||
@Provides | ||
@Singleton | ||
fun provideLocalDataSource(userDao: UserDao): UserLocalDataSource { | ||
return UserLocalDataSource(userDao) | ||
} | ||
} |
186 changes: 186 additions & 0 deletions
186
app/src/main/java/com/jjbaksa/jjbaksa/di/NetworkModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
package com.jjbaksa.jjbaksa.di | ||
|
||
import com.jjbaksa.data.BASE_URL | ||
import com.jjbaksa.data.api.AuthApi | ||
import com.jjbaksa.data.api.NoAuthApi | ||
import com.jjbaksa.jjbaksa.JjbaksaApp | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import okhttp3.Interceptor | ||
import okhttp3.OkHttpClient | ||
import okhttp3.Request | ||
import okhttp3.logging.HttpLoggingInterceptor | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.gson.GsonConverterFactory | ||
import java.util.concurrent.TimeUnit | ||
import javax.inject.Qualifier | ||
import javax.inject.Singleton | ||
|
||
@Qualifier | ||
@Retention(AnnotationRetention.BINARY) | ||
annotation class AUTH | ||
|
||
@Qualifier | ||
@Retention(AnnotationRetention.BINARY) | ||
annotation class NOAUTH | ||
|
||
@Qualifier | ||
@Retention(AnnotationRetention.BINARY) | ||
annotation class REFRESH | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object NetworkModule { | ||
private val httpLoggingInterceptor = HttpLoggingInterceptor().apply { | ||
level = if (JjbaksaApp.instance.isDebug) { | ||
HttpLoggingInterceptor.Level.BODY | ||
} else { | ||
HttpLoggingInterceptor.Level.HEADERS | ||
} | ||
} | ||
|
||
@AUTH | ||
@Provides | ||
@Singleton | ||
fun provideAuthInterceptor(): Interceptor { | ||
return Interceptor { chain: Interceptor.Chain -> | ||
// val accessToken //todo 추후 ACCESS TOKEN 추가 | ||
val newRequest: Request = chain.request().newBuilder() | ||
// .addHeader("Authorization", "Bearer $accessToken") | ||
.build() | ||
chain.proceed(newRequest) | ||
} | ||
} | ||
// TODO 추후 Refresh 추가 | ||
/* | ||
@AUTH | ||
@Provides | ||
@Singleton | ||
fun provideTokenAuthenticator(@REFRESH refreshRetrofit: Retrofit): TokenAuthenticator { | ||
return TokenAuthenticator( | ||
KoalaApp.instance.applicationContext, | ||
refreshRetrofit | ||
) | ||
} | ||
*/ | ||
// TODO 추후 Refresh 추가 | ||
/* | ||
@REFRESH | ||
@Provides | ||
@Singleton | ||
fun provideRefreshInterceptor(): Interceptor { | ||
return Interceptor { chain: Interceptor.Chain -> | ||
val refreshToken = Hawk.get(REFRESH_TOKEN, "") | ||
val newRequest: Request = chain.request().newBuilder() | ||
.addHeader("RefreshToken", "Bearer $refreshToken") | ||
.build() | ||
chain.proceed(newRequest) | ||
} | ||
} | ||
*/ | ||
|
||
@NOAUTH | ||
@Provides | ||
@Singleton | ||
fun provideNoAuthOkHttpClient(): OkHttpClient { | ||
return OkHttpClient.Builder().apply { | ||
connectTimeout(10, TimeUnit.SECONDS) | ||
readTimeout(30, TimeUnit.SECONDS) | ||
writeTimeout(15, TimeUnit.SECONDS) | ||
addInterceptor(httpLoggingInterceptor) | ||
}.build() | ||
} | ||
// TODO 추후 Refresh 추가 | ||
/* | ||
@AUTH | ||
@Provides | ||
@Singleton | ||
fun provideAuthOkHttpClient( | ||
@AUTH authInterceptor: Interceptor, | ||
@AUTH tokenAuthenticator: TokenAuthenticator | ||
): OkHttpClient { | ||
return OkHttpClient.Builder().apply { | ||
connectTimeout(10, TimeUnit.SECONDS) | ||
readTimeout(30, TimeUnit.SECONDS) | ||
writeTimeout(15, TimeUnit.SECONDS) | ||
addInterceptor(httpLoggingInterceptor) | ||
addInterceptor(authInterceptor) | ||
authenticator(tokenAuthenticator) | ||
}.build() | ||
} | ||
*/ | ||
|
||
@REFRESH | ||
@Provides | ||
@Singleton | ||
fun provideRefreshOkHttpClient( | ||
@REFRESH refreshAuthInterceptor: Interceptor | ||
): OkHttpClient { | ||
return OkHttpClient.Builder().apply { | ||
connectTimeout(10, TimeUnit.SECONDS) | ||
readTimeout(30, TimeUnit.SECONDS) | ||
writeTimeout(15, TimeUnit.SECONDS) | ||
addInterceptor(httpLoggingInterceptor) | ||
addInterceptor(refreshAuthInterceptor) | ||
}.build() | ||
} | ||
|
||
@NOAUTH | ||
@Provides | ||
@Singleton | ||
fun provideNoAuthRetrofit(@NOAUTH okHttpClient: OkHttpClient): Retrofit { | ||
return Retrofit.Builder() | ||
.client(okHttpClient) | ||
.baseUrl(BASE_URL) | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.build() | ||
} | ||
|
||
@AUTH | ||
@Provides | ||
@Singleton | ||
fun provideAuthRetrofit(@AUTH okHttpClient: OkHttpClient): Retrofit { | ||
return Retrofit.Builder() | ||
.client(okHttpClient) | ||
.baseUrl(BASE_URL) | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.build() | ||
} | ||
|
||
@REFRESH | ||
@Provides | ||
@Singleton | ||
fun provideRefreshRetrofit(@REFRESH okHttpClient: OkHttpClient): Retrofit { | ||
return Retrofit.Builder() | ||
.client(okHttpClient) | ||
.baseUrl(BASE_URL) | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.build() | ||
} | ||
|
||
@NOAUTH | ||
@Provides | ||
@Singleton | ||
fun provideNoAuthApi(@NOAUTH retrofit: Retrofit): NoAuthApi { | ||
return retrofit.create(NoAuthApi::class.java) | ||
} | ||
|
||
@AUTH | ||
@Provides | ||
@Singleton | ||
fun provideAuthApi(@AUTH retrofit: Retrofit): AuthApi { | ||
return retrofit.create(AuthApi::class.java) | ||
} | ||
|
||
@REFRESH | ||
@Provides | ||
@Singleton | ||
fun provideRefreshApi(@REFRESH retrofit: Retrofit): AuthApi { | ||
return retrofit.create(AuthApi::class.java) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
app/src/main/java/com/jjbaksa/jjbaksa/di/RepositoryModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.jjbaksa.jjbaksa.di | ||
|
||
import com.jjbaksa.data.datasource.local.UserLocalDataSource | ||
import com.jjbaksa.data.datasource.remote.UserRemoteDataSource | ||
import com.jjbaksa.domain.repository.UserRepository | ||
import com.jjbaksa.data.repository.UserRepositoryImpl | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object RepositoryModule { | ||
@Singleton | ||
@Provides | ||
fun provideUserRepository( | ||
userRemoteDataSource: UserRemoteDataSource, | ||
userLocalDataSource: UserLocalDataSource | ||
): UserRepository { | ||
return UserRepositoryImpl(userRemoteDataSource, userLocalDataSource) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.jjbaksa.jjbaksa.di | ||
|
||
object UseCaseModule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.jjbaksa.data | ||
|
||
const val BASE_URL = "https://api.stage.jjbaksa.com:443" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.jjbaksa.data.api | ||
|
||
interface AuthApi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.jjbaksa.data.api | ||
|
||
import com.jjbaksa.data.resp.user.SignUpReq | ||
import com.jjbaksa.data.resp.user.SignUpResp | ||
import retrofit2.Response | ||
import retrofit2.http.Body | ||
import retrofit2.http.POST | ||
|
||
interface NoAuthApi { | ||
@POST("user") | ||
suspend fun signUp( | ||
@Body singUpReq: SignUpReq | ||
): Response<SignUpResp> | ||
} |
Oops, something went wrong.