From e69adb4e50011d32c7f5fb418fc741718f5ff01d Mon Sep 17 00:00:00 2001 From: mdb1217 <70698151+mdb1217@users.noreply.github.com> Date: Sun, 23 Jan 2022 22:20:27 +0900 Subject: [PATCH 01/10] [ADD] #26 - Add NetworkModule --- .../main/java/org/mascota/di/NetworkModule.kt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 app/src/main/java/org/mascota/di/NetworkModule.kt diff --git a/app/src/main/java/org/mascota/di/NetworkModule.kt b/app/src/main/java/org/mascota/di/NetworkModule.kt new file mode 100644 index 0000000..2fb930d --- /dev/null +++ b/app/src/main/java/org/mascota/di/NetworkModule.kt @@ -0,0 +1,16 @@ +package org.mascota.di + +import okhttp3.OkHttpClient +import okhttp3.logging.HttpLoggingInterceptor + +object NetworkModule { + private fun httpLoggingInterceptor(): HttpLoggingInterceptor { + val loggingInterceptor = HttpLoggingInterceptor() + loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY + return loggingInterceptor + } + + private fun getOkHttpClient(): OkHttpClient = OkHttpClient.Builder() + .addInterceptor(httpLoggingInterceptor()) + .build() +} From 43b3aa273aafa0ca41a2f803205135df9aefea22 Mon Sep 17 00:00:00 2001 From: mdb1217 <70698151+mdb1217@users.noreply.github.com> Date: Fri, 18 Feb 2022 14:35:55 +0900 Subject: [PATCH 02/10] [CHORE] #26 - Modify Data Module Gradle --- data/build.gradle.kts | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/data/build.gradle.kts b/data/build.gradle.kts index 9419295..e444f28 100644 --- a/data/build.gradle.kts +++ b/data/build.gradle.kts @@ -17,37 +17,15 @@ dependencies { // Android Core implementation(KotlinDependencies.kotlin) implementation(AndroidXDependencies.coreKtx) - implementation(AndroidXDependencies.appCompat) - implementation(AndroidXDependencies.constraintLayout) implementation(AndroidXDependencies.coroutines) - // Material Design - implementation(MaterialDesignDependencies.materialDesign) - // Dagger-Hilt implementation(AndroidXDependencies.hilt) kapt(KaptDependencies.hiltCompiler) - // Jetpack Navigation Component - implementation(AndroidXDependencies.navigationFragment) - implementation(AndroidXDependencies.navigationUI) - // Jetpack Security implementation(AndroidXDependencies.security) - // Jetpack Fragment - implementation(AndroidXDependencies.fragment) - - // Jetpack Lifecycle - implementation(AndroidXDependencies.coroutines) - implementation(AndroidXDependencies.lifeCycleKtx) - implementation(AndroidXDependencies.lifecycleJava8) - - // ImageLoading Library - // Glide for general - implementation(ThirdPartyDependencies.glide) - kapt(KaptDependencies.glideCompiler) - // Http Client Library implementation(ThirdPartyDependencies.retrofit) implementation(platform(ThirdPartyDependencies.okHttpBom)) From ba5ca1816a57e5d72a7f7616cd54dffa1aef037e Mon Sep 17 00:00:00 2001 From: mdb1217 <70698151+mdb1217@users.noreply.github.com> Date: Fri, 18 Feb 2022 14:36:42 +0900 Subject: [PATCH 03/10] [ADD] #26 - Init NetworkModule --- .../main/java/org/mascota/di/NetworkModule.kt | 16 ----- .../java/org/mascota/data/di/NetworkModule.kt | 60 +++++++++++++++++++ 2 files changed, 60 insertions(+), 16 deletions(-) delete mode 100644 app/src/main/java/org/mascota/di/NetworkModule.kt create mode 100644 data/src/main/java/org/mascota/data/di/NetworkModule.kt diff --git a/app/src/main/java/org/mascota/di/NetworkModule.kt b/app/src/main/java/org/mascota/di/NetworkModule.kt deleted file mode 100644 index 2fb930d..0000000 --- a/app/src/main/java/org/mascota/di/NetworkModule.kt +++ /dev/null @@ -1,16 +0,0 @@ -package org.mascota.di - -import okhttp3.OkHttpClient -import okhttp3.logging.HttpLoggingInterceptor - -object NetworkModule { - private fun httpLoggingInterceptor(): HttpLoggingInterceptor { - val loggingInterceptor = HttpLoggingInterceptor() - loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY - return loggingInterceptor - } - - private fun getOkHttpClient(): OkHttpClient = OkHttpClient.Builder() - .addInterceptor(httpLoggingInterceptor()) - .build() -} diff --git a/data/src/main/java/org/mascota/data/di/NetworkModule.kt b/data/src/main/java/org/mascota/data/di/NetworkModule.kt new file mode 100644 index 0000000..3ddc1e0 --- /dev/null +++ b/data/src/main/java/org/mascota/data/di/NetworkModule.kt @@ -0,0 +1,60 @@ +package org.mascota.data.di + +import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory +import dagger.Module +import dagger.Provides +import dagger.hilt.InstallIn +import dagger.hilt.components.SingletonComponent +import java.util.concurrent.TimeUnit +import javax.inject.Singleton +import kotlinx.serialization.ExperimentalSerializationApi +import kotlinx.serialization.json.Json +import okhttp3.MediaType.Companion.toMediaType +import okhttp3.OkHttpClient +import okhttp3.logging.HttpLoggingInterceptor +import org.mascota.data.local.MascotaSharedPreferences +import org.mascota.data.network.AuthInterceptor +import retrofit2.Retrofit + +@InstallIn(SingletonComponent::class) +@Module +object NetworkModule { + @Provides + @Singleton + private fun provideHttpLoggingInterceptor(): HttpLoggingInterceptor = + HttpLoggingInterceptor().apply { + level = HttpLoggingInterceptor.Level.BODY + } + + @Provides + @Singleton + fun provideAuthInterceptor(preferences: MascotaSharedPreferences) = + AuthInterceptor(preferences) + + @Provides + @Singleton + private fun provideOkHttpClient( + httpLoggingInterceptor: HttpLoggingInterceptor, + authInterceptor: AuthInterceptor + ): OkHttpClient = + OkHttpClient.Builder() + .connectTimeout(1, TimeUnit.MINUTES) + .writeTimeout(30, TimeUnit.SECONDS) + .readTimeout(30, TimeUnit.SECONDS) + .addInterceptor(authInterceptor) + .addInterceptor(httpLoggingInterceptor) + .build() + + @ExperimentalSerializationApi + @Provides + @Singleton + fun provideRetrofitObject(okHttpClient: OkHttpClient): Retrofit { + return Retrofit.Builder() + .baseUrl(MASCOTA_URL) + .client(okHttpClient) + .addConverterFactory(Json.asConverterFactory("application/json".toMediaType())) + .build() + } + + private const val MASCOTA_URL = "https://mascota.kr/" +} From efb2e6b07ac4215f5fde7609fa406ca4068c7f2d Mon Sep 17 00:00:00 2001 From: mdb1217 <70698151+mdb1217@users.noreply.github.com> Date: Fri, 18 Feb 2022 14:36:56 +0900 Subject: [PATCH 04/10] [CHORE] #26 - Delete Unused File --- data/src/main/java/org/mascota/data/EmptyFile.kt | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 data/src/main/java/org/mascota/data/EmptyFile.kt diff --git a/data/src/main/java/org/mascota/data/EmptyFile.kt b/data/src/main/java/org/mascota/data/EmptyFile.kt deleted file mode 100644 index 00dcba1..0000000 --- a/data/src/main/java/org/mascota/data/EmptyFile.kt +++ /dev/null @@ -1,3 +0,0 @@ -package org.mascota.data - -class EmptyFile From 66b695d2ef28e7291bd187b57e2c220f1f12c363 Mon Sep 17 00:00:00 2001 From: mdb1217 <70698151+mdb1217@users.noreply.github.com> Date: Fri, 18 Feb 2022 14:37:18 +0900 Subject: [PATCH 05/10] [ADD] #26 - Add LocalPreferencesModule --- .../mascota/data/di/LocalPreferencesModule.kt | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 data/src/main/java/org/mascota/data/di/LocalPreferencesModule.kt diff --git a/data/src/main/java/org/mascota/data/di/LocalPreferencesModule.kt b/data/src/main/java/org/mascota/data/di/LocalPreferencesModule.kt new file mode 100644 index 0000000..90c874b --- /dev/null +++ b/data/src/main/java/org/mascota/data/di/LocalPreferencesModule.kt @@ -0,0 +1,19 @@ +package org.mascota.data.di + +import android.app.Application +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 +import org.mascota.data.local.MascotaSharedPreferences + +@Module +@InstallIn(SingletonComponent::class) +object LocalPreferencesModule { + @Provides + @Singleton + fun providesLocalPreferences(@ApplicationContext context: Application) = + MascotaSharedPreferences(context) +} From a179ce79af75c944a9ab28651bc5f12b5058e23b Mon Sep 17 00:00:00 2001 From: mdb1217 <70698151+mdb1217@users.noreply.github.com> Date: Fri, 18 Feb 2022 14:37:34 +0900 Subject: [PATCH 06/10] [ADD] #26 - Add AuthInterceptor --- .../mascota/data/network/AuthInterceptor.kt | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 data/src/main/java/org/mascota/data/network/AuthInterceptor.kt diff --git a/data/src/main/java/org/mascota/data/network/AuthInterceptor.kt b/data/src/main/java/org/mascota/data/network/AuthInterceptor.kt new file mode 100644 index 0000000..efe90e2 --- /dev/null +++ b/data/src/main/java/org/mascota/data/network/AuthInterceptor.kt @@ -0,0 +1,20 @@ +package org.mascota.data.network + +import okhttp3.Interceptor +import okhttp3.Response +import org.mascota.data.local.MascotaSharedPreferences +import javax.inject.Inject + +class AuthInterceptor @Inject constructor(private val preferences: MascotaSharedPreferences) : + Interceptor { + + override fun intercept(chain: Interceptor.Chain): Response { + val requestBuilder = chain.request().newBuilder() + + preferences.userToken.let { + requestBuilder.addHeader("x-access-token", it) + } + + return chain.proceed(requestBuilder.build()) + } +} From 35036095950f9ce24c6695d00444fc4bf82916f5 Mon Sep 17 00:00:00 2001 From: mdb1217 <70698151+mdb1217@users.noreply.github.com> Date: Fri, 18 Feb 2022 14:38:04 +0900 Subject: [PATCH 07/10] [ADD] #26 - Init MascotaSharedPreferences --- .../data/local/MascotaSharedPreferences.kt | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 data/src/main/java/org/mascota/data/local/MascotaSharedPreferences.kt diff --git a/data/src/main/java/org/mascota/data/local/MascotaSharedPreferences.kt b/data/src/main/java/org/mascota/data/local/MascotaSharedPreferences.kt new file mode 100644 index 0000000..75b091c --- /dev/null +++ b/data/src/main/java/org/mascota/data/local/MascotaSharedPreferences.kt @@ -0,0 +1,30 @@ +package org.mascota.data.local + +import android.content.Context +import androidx.core.content.edit +import androidx.databinding.ktx.BuildConfig +import androidx.security.crypto.EncryptedSharedPreferences +import androidx.security.crypto.MasterKeys +import javax.inject.Inject + +class MascotaSharedPreferences @Inject constructor( + context: Context +) { + private val preferences = if (!BuildConfig.DEBUG) EncryptedSharedPreferences.create( + FILE_NAME, + MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC), + context, + EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, + EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM + ) else context.getSharedPreferences(DEBUG_FILE_NAME, Context.MODE_PRIVATE) + + var userToken: String + set(value) = preferences.edit { putString(USER_TOKEN, value) } + get() = preferences.getString(USER_TOKEN, "") ?: "" + + companion object { + private const val USER_TOKEN = "USER_TOKEN" + private const val FILE_NAME = "MASCOTAAUTH" + const val DEBUG_FILE_NAME = "MASCOTAAUTHDEBUG" + } +} From 573f9048e2e6f60e55723142a3570e4e8ab379b5 Mon Sep 17 00:00:00 2001 From: mdb1217 <70698151+mdb1217@users.noreply.github.com> Date: Fri, 18 Feb 2022 14:40:52 +0900 Subject: [PATCH 08/10] [CHORE] #26 - Lint Check --- data/src/main/java/org/mascota/data/network/AuthInterceptor.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/src/main/java/org/mascota/data/network/AuthInterceptor.kt b/data/src/main/java/org/mascota/data/network/AuthInterceptor.kt index efe90e2..d64a405 100644 --- a/data/src/main/java/org/mascota/data/network/AuthInterceptor.kt +++ b/data/src/main/java/org/mascota/data/network/AuthInterceptor.kt @@ -1,9 +1,9 @@ package org.mascota.data.network +import javax.inject.Inject import okhttp3.Interceptor import okhttp3.Response import org.mascota.data.local.MascotaSharedPreferences -import javax.inject.Inject class AuthInterceptor @Inject constructor(private val preferences: MascotaSharedPreferences) : Interceptor { From 5e9c2ce03e217473f4df3c055dd11383a1c91a21 Mon Sep 17 00:00:00 2001 From: mdb1217 <70698151+mdb1217@users.noreply.github.com> Date: Fri, 18 Feb 2022 14:58:57 +0900 Subject: [PATCH 09/10] [CHORE] #26 - Set JvmTarget Version 11 In common.gradle --- gradle/common.gradle | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gradle/common.gradle b/gradle/common.gradle index 8e709b4..0e4aaa4 100644 --- a/gradle/common.gradle +++ b/gradle/common.gradle @@ -18,5 +18,8 @@ if (hasLibraryPlugin || hasApplicationPlugin) { sourceCompatibility = JavaVersion.VERSION_11 targetCompatibility = JavaVersion.VERSION_11 } + kotlinOptions { + jvmTarget = JavaVersion.VERSION_11 + } } } From 0c4076dd127df8a27c0a908898fe5ee297051715 Mon Sep 17 00:00:00 2001 From: mdb1217 <70698151+mdb1217@users.noreply.github.com> Date: Fri, 18 Feb 2022 15:00:14 +0900 Subject: [PATCH 10/10] [CHORE] #26 - Modify Access Modifier In NetworkModule --- data/src/main/java/org/mascota/data/di/NetworkModule.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data/src/main/java/org/mascota/data/di/NetworkModule.kt b/data/src/main/java/org/mascota/data/di/NetworkModule.kt index 3ddc1e0..188f4f2 100644 --- a/data/src/main/java/org/mascota/data/di/NetworkModule.kt +++ b/data/src/main/java/org/mascota/data/di/NetworkModule.kt @@ -21,7 +21,7 @@ import retrofit2.Retrofit object NetworkModule { @Provides @Singleton - private fun provideHttpLoggingInterceptor(): HttpLoggingInterceptor = + fun provideHttpLoggingInterceptor(): HttpLoggingInterceptor = HttpLoggingInterceptor().apply { level = HttpLoggingInterceptor.Level.BODY } @@ -33,7 +33,7 @@ object NetworkModule { @Provides @Singleton - private fun provideOkHttpClient( + fun provideOkHttpClient( httpLoggingInterceptor: HttpLoggingInterceptor, authInterceptor: AuthInterceptor ): OkHttpClient =