Skip to content

Commit

Permalink
[MERGE] #37 -> develop
Browse files Browse the repository at this point in the history
[FEAT/#37] 서버통신 기초 세팅
  • Loading branch information
Marchbreeze authored Jul 9, 2024
2 parents af3c22e + 08a340a commit 71af1c4
Show file tree
Hide file tree
Showing 71 changed files with 690 additions and 177 deletions.
90 changes: 90 additions & 0 deletions app/src/main/java/co/orange/ddanzi/di/AuthInterceptor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package co.orange.ddanzi.di

import android.content.Context
import android.content.Intent
import android.os.Handler
import android.os.Looper
import co.orange.core.extension.toast
import co.orange.data.local.UserSharedPref
import co.orange.domain.entity.request.TokenRequestModel
import co.orange.domain.repository.AuthRepository
import co.orange.presentation.auth.login.LoginActivity
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.runBlocking
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
import timber.log.Timber
import javax.inject.Inject

class AuthInterceptor
@Inject
constructor(
private val authRepository: AuthRepository,
private val sharedPref: UserSharedPref,
@ApplicationContext private val context: Context,
) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val originalRequest = chain.request()

Timber.d("GET ACCESS TOKEN : ${sharedPref.accessToken}")

val authRequest =
if (sharedPref.accessToken.isNotBlank()) {
originalRequest.newBuilder().newAuthBuilder().build()
} else {
originalRequest
}

val response = chain.proceed(authRequest)

when (response.code) {
CODE_TOKEN_EXPIRED -> {
try {
runBlocking {
authRepository.postReissueTokens(
sharedPref.refreshToken,
TokenRequestModel(sharedPref.userId),
)
}.onSuccess { data ->
sharedPref.apply {
accessToken = data.accessToken
refreshToken = data.refreshToken
userId = data.userId
}

response.close()

val newRequest =
authRequest.newBuilder().removeHeader(AUTHORIZATION).newAuthBuilder()
.build()

return chain.proceed(newRequest)
}
} catch (t: Throwable) {
Timber.d(t.message)
}

sharedPref.clearInfo()

Handler(Looper.getMainLooper()).post {
context.toast(TOKEN_EXPIRED_ERROR)
Intent(context, LoginActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
context.startActivity(this)
}
}
}
}
return response
}

private fun Request.Builder.newAuthBuilder() = this.addHeader(AUTHORIZATION, "$BEARER ${sharedPref.accessToken}")

companion object {
private const val CODE_TOKEN_EXPIRED = 401
private const val TOKEN_EXPIRED_ERROR = "토큰이 만료되었어요\n다시 로그인 해주세요"
private const val BEARER = "Bearer"
private const val AUTHORIZATION = "Authorization"
}
}
17 changes: 17 additions & 0 deletions app/src/main/java/co/orange/ddanzi/di/DataSourceModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package co.orange.ddanzi.di

import co.orange.data.dataSource.AuthDataSource
import co.orange.data.dataSourceImpl.AuthDataSourceImpl
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 provideAuthDataSource(authDataSourceImpl: AuthDataSourceImpl): AuthDataSource = authDataSourceImpl
}
17 changes: 17 additions & 0 deletions app/src/main/java/co/orange/ddanzi/di/RepositoryModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package co.orange.ddanzi.di

import co.orange.data.repositoryImpl.AuthRepositoryImpl
import co.orange.domain.repository.AuthRepository
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 {
@Provides
@Singleton
fun provideAuthRepository(authRepositoryImpl: AuthRepositoryImpl): AuthRepository = authRepositoryImpl
}
112 changes: 112 additions & 0 deletions app/src/main/java/co/orange/ddanzi/di/RetrofitModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package co.orange.ddanzi.di

import co.orange.core.extension.isJsonArray
import co.orange.core.extension.isJsonObject
import co.orange.ddanzi.BuildConfig.BASE_URL
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.Interceptor
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import org.json.JSONArray
import org.json.JSONObject
import retrofit2.Converter
import retrofit2.Retrofit
import timber.log.Timber
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object RetrofitModule {
private const val APPLICATION_JSON = "application/json"

@Provides
@Singleton
fun provideJson(): Json =
Json {
ignoreUnknownKeys = true
prettyPrint = true
}

@Provides
@Singleton
fun provideJsonConverter(json: Json): Converter.Factory =
json.asConverterFactory(
APPLICATION_JSON.toMediaType(),
)

@Provides
@Singleton
fun provideHttpLoggingInterceptor(): Interceptor =
HttpLoggingInterceptor { message ->
when {
message.isJsonObject() ->
Timber.tag("okhttp").d(JSONObject(message).toString(4))

message.isJsonArray() ->
Timber.tag("okhttp").d(JSONArray(message).toString(4))

else -> {
Timber.tag("okhttp").d("CONNECTION INFO -> $message")
}
}
}.apply {
level = HttpLoggingInterceptor.Level.BODY
}

@Provides
@Singleton
@RetrofitQualifier.JWT
fun provideAuthInterceptor(authInterceptor: AuthInterceptor): Interceptor = authInterceptor

@Provides
@Singleton
@RetrofitQualifier.JWT
fun provideJWTOkHttpClient(
loggingInterceptor: Interceptor,
@RetrofitQualifier.JWT authInterceptor: Interceptor,
): OkHttpClient =
OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.addInterceptor(authInterceptor)
.build()

@Provides
@Singleton
@RetrofitQualifier.NOTOKEN
fun provideReissueOkHttpClient(loggingInterceptor: Interceptor): OkHttpClient =
OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.build()

@Provides
@Singleton
@RetrofitQualifier.JWT
fun provideJWTRetrofit(
@RetrofitQualifier.JWT client: OkHttpClient,
factory: Converter.Factory,
): Retrofit =
Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(factory)
.build()

@Provides
@Singleton
@RetrofitQualifier.NOTOKEN
fun provideReissueRetrofit(
@RetrofitQualifier.NOTOKEN client: OkHttpClient,
factory: Converter.Factory,
): Retrofit =
Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(factory)
.build()
}
13 changes: 13 additions & 0 deletions app/src/main/java/co/orange/ddanzi/di/RetrofitQualifier.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package co.orange.ddanzi.di

import javax.inject.Qualifier

object RetrofitQualifier {
@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class JWT

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class NOTOKEN
}
19 changes: 19 additions & 0 deletions app/src/main/java/co/orange/ddanzi/di/ServiceModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package co.orange.ddanzi.di

import co.orange.data.service.AuthService
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import retrofit2.Retrofit
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object ServiceModule {
@Provides
@Singleton
fun provideAuthService(
@RetrofitQualifier.NOTOKEN retrofit: Retrofit,
): AuthService = retrofit.create(AuthService::class.java)
}
26 changes: 26 additions & 0 deletions app/src/main/java/co/orange/ddanzi/di/SharedPrefModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package co.orange.ddanzi.di

import android.content.Context
import android.content.SharedPreferences
import co.orange.data.local.UserSharedPref
import co.orange.data.local.UserSharedPrefImpl
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 SharedPrefModule {
@Provides
@Singleton
fun provideSharedPreferences(
@ApplicationContext context: Context,
): SharedPreferences = context.getSharedPreferences(context.packageName, Context.MODE_PRIVATE)

@Provides
@Singleton
fun provideSharedPref(sharedPrefImpl: UserSharedPrefImpl): UserSharedPref = sharedPrefImpl
}
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ object Versions {
const val circleIndicatorVersion = "2.1.6"
const val shimmerVersion = "0.5.0"
const val junitVersion = "4.13.2"
const val espressoVersion = "3.3.0"
const val espressoVersion = "3.5.1"
const val androidTestVersion = "1.1.2"

val javaVersion = JavaVersion.VERSION_17
const val jvmVersion = "17"
}
}
4 changes: 2 additions & 2 deletions core/src/main/java/co/orange/core/base/BaseActivity.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kr.genti.core.base
package co.orange.core.base

import android.graphics.Rect
import android.os.Bundle
Expand All @@ -8,7 +8,7 @@ import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import kr.genti.core.extension.hideKeyboard
import co.orange.core.extension.hideKeyboard

abstract class BaseActivity<T : ViewDataBinding>(
@LayoutRes private val layoutResId: Int,
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/co/orange/core/base/BaseBottomSheet.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kr.genti.core.base
package co.orange.core.base

import android.os.Bundle
import android.view.LayoutInflater
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/co/orange/core/base/BaseDialog.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kr.genti.core.base
package co.orange.core.base

import android.os.Bundle
import android.view.LayoutInflater
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/co/orange/core/base/BaseFragment.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kr.genti.core.base
package co.orange.core.base

import android.os.Bundle
import android.view.LayoutInflater
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/co/orange/core/extension/ActivityExt.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kr.genti.core.extension
package co.orange.core.extension

import android.app.Activity
import android.content.Context
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/co/orange/core/extension/ContextExt.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kr.genti.core.extension
package co.orange.core.extension

import android.app.Activity
import android.content.Context
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/co/orange/core/extension/FragmentExt.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kr.genti.core.extension
package co.orange.core.extension

import android.view.View
import android.widget.Toast
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/co/orange/core/extension/IntExt.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kr.genti.core.extension
package co.orange.core.extension

import android.content.Context
import android.util.TypedValue
Expand All @@ -19,4 +19,4 @@ fun Int.setOverThousand(): String =
this.toString()
} else {
"999+"
}
}
2 changes: 1 addition & 1 deletion core/src/main/java/co/orange/core/extension/IntentExt.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kr.genti.core.extension
package co.orange.core.extension

import android.content.Intent
import android.os.Build
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/co/orange/core/extension/StringExt.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kr.genti.core.extension
package co.orange.core.extension

import java.text.BreakIterator

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/co/orange/core/extension/UriExt.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kr.genti.core.extension
package co.orange.core.extension

import android.content.ContentResolver
import android.net.Uri
Expand Down
Loading

0 comments on commit 71af1c4

Please sign in to comment.