Skip to content

Commit

Permalink
Merge pull request #19 from DACOS-SOLUX-Hackathon-Team5/feature/#17-c…
Browse files Browse the repository at this point in the history
…ode-refactor

[Feature/#17] : 좋아요 페이지 리팩토링
  • Loading branch information
gaeulzzang authored Nov 7, 2024
2 parents 59975e1 + a917dd3 commit 5529252
Show file tree
Hide file tree
Showing 30 changed files with 671 additions and 436 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.hackathon.alddeul_babsang.app
package com.hackathon.alddeul_babsang.app.di

import com.hackathon.alddeul_babsang.data.datasource.ExampleDataSource
import com.hackathon.alddeul_babsang.data.datasource.UserPreferencesDataSource
import com.hackathon.alddeul_babsang.data.datasourceimpl.ExampleDataSourceImpl
import com.hackathon.alddeul_babsang.data.datasourceimpl.UserPreferencesDataSourceImpl
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
Expand All @@ -15,4 +17,8 @@ abstract class DataSourceModule {
@Binds
@Singleton
abstract fun bindExampleDataSource(exampleDataSourceImpl: ExampleDataSourceImpl): ExampleDataSource

@Binds
@Singleton
abstract fun bindUserPreferencesDataSource(userPreferencesDataSourceImpl: UserPreferencesDataSourceImpl): UserPreferencesDataSource
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.hackathon.alddeul_babsang.app
package com.hackathon.alddeul_babsang.app.di

fun String?.isJsonObject(): Boolean = this?.startsWith("{") == true && this.endsWith("}")
fun String?.isJsonArray(): Boolean = this?.startsWith("[") == true && this.endsWith("]")
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.hackathon.alddeul_babsang.app.di

import javax.inject.Qualifier

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class AlddeulRetrofit

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class AccessToken

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class UserPreferences
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.hackathon.alddeul_babsang.app
package com.hackathon.alddeul_babsang.app.di

import com.hackathon.alddeul_babsang.data.repositoryimpl.ExampleRepositoryImpl
import com.hackathon.alddeul_babsang.data.repositoryimpl.UserPreferencesRepositoryImpl
import com.hackathon.alddeul_babsang.domain.repository.ExampleRepository
import com.hackathon.alddeul_babsang.domain.repository.UserPreferencesRepository
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
Expand All @@ -15,4 +17,8 @@ abstract class RepositoryModule {
@Binds
@Singleton
abstract fun bindExampleRepository(exampleRepositoryImpl: ExampleRepositoryImpl): ExampleRepository

@Binds
@Singleton
abstract fun bindUserPreferencesRepository(userPreferencesRepositoryImpl: UserPreferencesRepositoryImpl): UserPreferencesRepository
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.hackathon.alddeul_babsang.app
package com.hackathon.alddeul_babsang.app.di

import com.hackathon.alddeul_babsang.BuildConfig.BASE_URL
import com.hackathon.alddeul_babsang.app.interceptor.TokenInterceptor
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
Expand All @@ -23,13 +25,20 @@ object RetrofitModule {
@Provides
@Singleton
fun provideOkHttpClient(
loggingInterceptor: HttpLoggingInterceptor
loggingInterceptor: HttpLoggingInterceptor,
@AccessToken tokenInterceptor: Interceptor,
): OkHttpClient = OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.addInterceptor(tokenInterceptor)
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build()

@Provides
@Singleton
@AccessToken
fun provideAuthInterceptor(interceptor: TokenInterceptor): Interceptor = interceptor

@Provides
@Singleton
fun provideLoggingInterceptor(): HttpLoggingInterceptor {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.hackathon.alddeul_babsang.app
package com.hackathon.alddeul_babsang.app.di

import com.sopt.data.service.ExampleApiService
import dagger.Module
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.hackathon.alddeul_babsang.app.di

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.preferencesDataStore
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 UserPreferencesModule {
private const val USER_PREFERENCES = "user_preferences"
private val Context.userDataStore by preferencesDataStore(name = USER_PREFERENCES)

@Provides
@Singleton
@UserPreferences
fun provideDataStore(
@ApplicationContext context: Context
): DataStore<Preferences> = context.userDataStore
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.hackathon.alddeul_babsang.app.interceptor

import com.hackathon.alddeul_babsang.data.datasource.UserPreferencesDataSource
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import okhttp3.Interceptor
import okhttp3.Response
import timber.log.Timber
import javax.inject.Inject

class TokenInterceptor @Inject constructor(
private val userPreferencesDataSource: UserPreferencesDataSource
) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response = runBlocking {
val request = chain.request()
val url = request.url.toString()

// 특정 URL 패턴에 대해서는 토큰을 추가하지 않음
if (url.contains("/api/v1/auth/login") ||
url.contains("api/v1/signup") ||
url.contains("api/v1/member")) {
// 로그인 요청 등 토큰이 필요 없는 요청의 경우
return@runBlocking chain.proceed(request)
}

// 토큰이 필요한 요청의 경우
val accessToken = userPreferencesDataSource.getUserAccessToken().first()
val newRequest = request.newBuilder()
.addHeader("Authorization", "Bearer $accessToken")
.build()

Timber.tag("interceptor").d("accessToken $accessToken")
chain.proceed(newRequest)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.hackathon.alddeul_babsang.data.datasource

import kotlinx.coroutines.flow.Flow

interface UserPreferencesDataSource {
suspend fun saveUserAccessToken(accessToken: String)
fun getUserAccessToken(): Flow<String?>

suspend fun saveCheckLogin(checkLogin: Boolean)
fun getCheckLogin(): Flow<Boolean>

suspend fun saveUserRefreshToken(refreshToken: String)
fun getUserRefreshToken(): Flow<String?>

suspend fun clear()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.hackathon.alddeul_babsang.data.datasourceimpl

import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import com.hackathon.alddeul_babsang.app.di.UserPreferences
import com.hackathon.alddeul_babsang.data.datasource.UserPreferencesDataSource
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject

class UserPreferencesDataSourceImpl @Inject constructor(
@UserPreferences private val dataStore: DataStore<Preferences>
) : UserPreferencesDataSource {
private val USER_ACCESS_TOKEN = stringPreferencesKey("user_access_token")
private val USER_REFRESH_TOKEN = stringPreferencesKey("user_refresh_token")
private val CHECK_LOGIN = booleanPreferencesKey("check_login")

override suspend fun saveUserAccessToken(accessToken: String) {
dataStore.edit { preferences ->
preferences[USER_ACCESS_TOKEN] = accessToken
}
}

override fun getUserAccessToken(): Flow<String?> = dataStore.data.map { preferences ->
preferences[USER_ACCESS_TOKEN]
}

override suspend fun saveCheckLogin(checkLogin: Boolean) {
dataStore.edit { preferences ->
preferences[CHECK_LOGIN] = checkLogin
}
}

override fun getCheckLogin(): Flow<Boolean> = dataStore.data.map { preferences ->
preferences[CHECK_LOGIN] ?: false
}

override suspend fun saveUserRefreshToken(refreshToken: String) {
dataStore.edit { preferences ->
preferences[USER_REFRESH_TOKEN] = refreshToken
}
}

override fun getUserRefreshToken(): Flow<String?> = dataStore.data.map { preferences ->
preferences[USER_REFRESH_TOKEN]
}

override suspend fun clear() {
dataStore.edit { preferences ->
preferences.remove(USER_ACCESS_TOKEN)
preferences.remove(USER_REFRESH_TOKEN)
preferences.remove(CHECK_LOGIN)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.hackathon.alddeul_babsang.data.repositoryimpl

import com.hackathon.alddeul_babsang.data.datasource.UserPreferencesDataSource
import com.hackathon.alddeul_babsang.domain.repository.UserPreferencesRepository
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

class UserPreferencesRepositoryImpl @Inject constructor(
private val dataSource: UserPreferencesDataSource
) : UserPreferencesRepository {

override suspend fun saveUserAccessToken(accessToken: String) {
dataSource.saveUserAccessToken(accessToken)
}

override fun getUserAccessToken(): Flow<String?> = dataSource.getUserAccessToken()

override suspend fun saveCheckLogin(checkLogin: Boolean) {
dataSource.saveCheckLogin(checkLogin)
}

override fun getCheckLogin(): Flow<Boolean> = dataSource.getCheckLogin()

override suspend fun saveUserRefreshToken(refreshToken: String) {
dataSource.saveUserRefreshToken(refreshToken)
}

override fun getUserRefreshToken(): Flow<String?> = dataSource.getUserRefreshToken()

override suspend fun clear() = dataSource.clear()

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ data class BabsangDetailEntity(
val phone: String,
val rating: Double,
val menu: String,
val review: List<ReviewEntity>
val review: List<ReviewEntity>,
var isFavorite: Boolean = false
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.hackathon.alddeul_babsang.domain.entity

data class BabsangListEntity(
data class LikesEntity(
val id: Long,
val avatar: String? = null,
val name: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.hackathon.alddeul_babsang.domain.repository

import kotlinx.coroutines.flow.Flow

interface UserPreferencesRepository {
suspend fun saveUserAccessToken(accessToken: String)
fun getUserAccessToken(): Flow<String?>

suspend fun saveCheckLogin(checkLogin: Boolean)
fun getCheckLogin(): Flow<Boolean>

suspend fun saveUserRefreshToken(refreshToken: String)
fun getUserRefreshToken(): Flow<String?>

suspend fun clear()
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import com.hackathon.alddeul_babsang.R
import com.hackathon.alddeul_babsang.core_ui.component.AlddeulButton
Expand All @@ -46,6 +47,7 @@ fun LoginRoute(
) {
val systemUiController = rememberSystemUiController()
val keyboardController = LocalSoftwareKeyboardController.current
val loginViewModel: LoginViewModel = hiltViewModel()

SideEffect {
systemUiController.setStatusBarColor(
Expand All @@ -56,6 +58,10 @@ fun LoginRoute(
LoginScreen(
onLoginClick = {
keyboardController?.hide()
loginViewModel.apply {
saveCheckLogin(true)
saveUserAccessToken("111")
}
navigator.navigateMain()
},
onSignUpClick = { navigator.navigateSignUp1() }
Expand Down
Loading

0 comments on commit 5529252

Please sign in to comment.