Skip to content

Commit

Permalink
Merge pull request #27 from NewMascota/feature/26-network-setting
Browse files Browse the repository at this point in the history
feature/26-network-setting
  • Loading branch information
mdb1217 authored Feb 18, 2022
2 parents 8ad5ed4 + 0c4076d commit cae63d7
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 25 deletions.
22 changes: 0 additions & 22 deletions data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
3 changes: 0 additions & 3 deletions data/src/main/java/org/mascota/data/EmptyFile.kt

This file was deleted.

19 changes: 19 additions & 0 deletions data/src/main/java/org/mascota/data/di/LocalPreferencesModule.kt
Original file line number Diff line number Diff line change
@@ -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)
}
60 changes: 60 additions & 0 deletions data/src/main/java/org/mascota/data/di/NetworkModule.kt
Original file line number Diff line number Diff line change
@@ -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
fun provideHttpLoggingInterceptor(): HttpLoggingInterceptor =
HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
}

@Provides
@Singleton
fun provideAuthInterceptor(preferences: MascotaSharedPreferences) =
AuthInterceptor(preferences)

@Provides
@Singleton
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/"
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
20 changes: 20 additions & 0 deletions data/src/main/java/org/mascota/data/network/AuthInterceptor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.mascota.data.network

import javax.inject.Inject
import okhttp3.Interceptor
import okhttp3.Response
import org.mascota.data.local.MascotaSharedPreferences

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())
}
}
3 changes: 3 additions & 0 deletions gradle/common.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@ if (hasLibraryPlugin || hasApplicationPlugin) {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_11
}
}
}

0 comments on commit cae63d7

Please sign in to comment.