-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from NewMascota/feature/26-network-setting
feature/26-network-setting
- Loading branch information
Showing
7 changed files
with
132 additions
and
25 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 was deleted.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
data/src/main/java/org/mascota/data/di/LocalPreferencesModule.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,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) | ||
} |
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,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/" | ||
} |
30 changes: 30 additions & 0 deletions
30
data/src/main/java/org/mascota/data/local/MascotaSharedPreferences.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 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
20
data/src/main/java/org/mascota/data/network/AuthInterceptor.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,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()) | ||
} | ||
} |
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