-
Notifications
You must be signed in to change notification settings - Fork 1
[Refactor/#192] DailyEmotion 데이터 흐름 개선 #193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
e96a6e7
Refactor: DailyEmotion 데이터 흐름을 Flow 기반 관찰 방식으로 변경
wjdrjs00 ead9462
Fix: EmotionRepository 내 중복 호출 방지 및 HomeViewModel 초기화 로직 수정
wjdrjs00 3edbfd1
Refactor: DailyEmotion 내 isStale 로직 개선 및 LocalDate 주입 방식 변경
wjdrjs00 46ea194
Refactor: UserDataSource를 UserRemoteDataSource와 UserLocalDataSource로 분리
wjdrjs00 9236266
Feat: UserRepository 내 캐싱 로직 및 Mutex를 이용한 Race Condition 방지 로직 구현
wjdrjs00 1b40a14
Refactor: FetchUserProfileUseCase를 ObserveUserProfileUseCase(Flow)로 변경
wjdrjs00 35bf092
Feat: 로그아웃 및 회원탈퇴 성공 시 유저 정보 캐시를 초기화하도록 수정
wjdrjs00 58e9b3c
Test: UserRepositoryImpl에 대한 단위 테스트 추가 (캐싱, 동시성, 캐시 초기화 검증)
wjdrjs00 e5c743e
Refactor: Home, MyPage, OnBoarding ViewModel에서 유저 정보를 Flow로 구독하도록 변경
wjdrjs00 fae4788
Feat: userProfile 단일 조회를 위한 GetUserProfileUseCase 추가
wjdrjs00 9d4add3
Refactor: Emotion 데이터 레이어 구조 개선 및 캐싱 로직 수정
wjdrjs00 12f5dec
Chore: 오타수정
wjdrjs00 64eb15e
Merge pull request #195 from YAPP-Github/refactor/#194-userprofile-flow
wjdrjs00 90f5a30
Refactor: DailyEmotion 데이터 흐름을 Flow 기반 관찰 방식으로 변경
wjdrjs00 29f878d
Fix: EmotionRepository 내 중복 호출 방지 및 HomeViewModel 초기화 로직 수정
wjdrjs00 f559bac
Refactor: DailyEmotion 내 isStale 로직 개선 및 LocalDate 주입 방식 변경
wjdrjs00 1de807c
Refactor: Emotion 데이터 레이어 구조 개선 및 캐싱 로직 수정
wjdrjs00 2f88992
Chore: 오타수정
wjdrjs00 b12d67d
Refactor: 로그아웃/회원탈퇴 시 감정 캐시 초기화
wjdrjs00 b9e2e88
Merge branch 'refactor/#192-daily-emotion-flow' of https://github.com…
wjdrjs00 a417594
Refactor: UserLocalDataSource 내 saveUserProfile의 suspend 키워드 제거
wjdrjs00 ae20777
Chore: 누락한 suspend 제거
wjdrjs00 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
10 changes: 10 additions & 0 deletions
10
data/src/main/java/com/threegap/bitnagil/data/emotion/datasource/EmotionLocalDataSource.kt
This file contains hidden or 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,10 @@ | ||
| package com.threegap.bitnagil.data.emotion.datasource | ||
|
|
||
| import com.threegap.bitnagil.domain.emotion.model.DailyEmotion | ||
| import kotlinx.coroutines.flow.StateFlow | ||
|
|
||
| interface EmotionLocalDataSource { | ||
| val dailyEmotion: StateFlow<DailyEmotion?> | ||
| fun saveDailyEmotion(dailyEmotion: DailyEmotion) | ||
| fun clearCache() | ||
| } |
This file contains hidden or 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
22 changes: 22 additions & 0 deletions
22
...main/java/com/threegap/bitnagil/data/emotion/datasourceImpl/EmotionLocalDataSourceImpl.kt
This file contains hidden or 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,22 @@ | ||
| package com.threegap.bitnagil.data.emotion.datasourceImpl | ||
|
|
||
| import com.threegap.bitnagil.data.emotion.datasource.EmotionLocalDataSource | ||
| import com.threegap.bitnagil.domain.emotion.model.DailyEmotion | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import kotlinx.coroutines.flow.asStateFlow | ||
| import kotlinx.coroutines.flow.update | ||
| import javax.inject.Inject | ||
|
|
||
| class EmotionLocalDataSourceImpl @Inject constructor() : EmotionLocalDataSource { | ||
| private val _dailyEmotion = MutableStateFlow<DailyEmotion?>(null) | ||
| override val dailyEmotion: StateFlow<DailyEmotion?> = _dailyEmotion.asStateFlow() | ||
|
|
||
| override fun saveDailyEmotion(dailyEmotion: DailyEmotion) { | ||
| _dailyEmotion.update { dailyEmotion } | ||
| } | ||
|
|
||
| override fun clearCache() { | ||
| _dailyEmotion.update { null } | ||
| } | ||
| } |
6 changes: 3 additions & 3 deletions
6
...n/datasourceImpl/EmotionDataSourceImpl.kt → ...sourceImpl/EmotionRemoteDataSourceImpl.kt
This file contains hidden or 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 contains hidden or 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
74 changes: 58 additions & 16 deletions
74
.../src/main/java/com/threegap/bitnagil/data/emotion/repositoryImpl/EmotionRepositoryImpl.kt
This file contains hidden or 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 |
|---|---|---|
| @@ -1,42 +1,84 @@ | ||
| package com.threegap.bitnagil.data.emotion.repositoryImpl | ||
|
|
||
| import com.threegap.bitnagil.data.emotion.datasource.EmotionDataSource | ||
| import com.threegap.bitnagil.data.emotion.datasource.EmotionLocalDataSource | ||
| import com.threegap.bitnagil.data.emotion.datasource.EmotionRemoteDataSource | ||
| import com.threegap.bitnagil.data.emotion.model.response.toDomain | ||
| import com.threegap.bitnagil.domain.emotion.model.DailyEmotion | ||
| import com.threegap.bitnagil.domain.emotion.model.Emotion | ||
| import com.threegap.bitnagil.domain.emotion.model.EmotionChangeEvent | ||
| import com.threegap.bitnagil.domain.emotion.model.EmotionRecommendRoutine | ||
| import com.threegap.bitnagil.domain.emotion.repository.EmotionRepository | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.MutableSharedFlow | ||
| import kotlinx.coroutines.flow.asSharedFlow | ||
| import kotlinx.coroutines.flow.emitAll | ||
| import kotlinx.coroutines.flow.filterNotNull | ||
| import kotlinx.coroutines.flow.flow | ||
| import kotlinx.coroutines.flow.map | ||
| import kotlinx.coroutines.sync.Mutex | ||
| import kotlinx.coroutines.sync.withLock | ||
| import java.time.LocalDate | ||
| import javax.inject.Inject | ||
|
|
||
| class EmotionRepositoryImpl @Inject constructor( | ||
| private val emotionDataSource: EmotionDataSource, | ||
| private val emotionRemoteDataSource: EmotionRemoteDataSource, | ||
| private val emotionLocalDateSource: EmotionLocalDataSource, | ||
| ) : EmotionRepository { | ||
|
|
||
| private val fetchMutex = Mutex() | ||
|
|
||
| override suspend fun getEmotions(): Result<List<Emotion>> { | ||
| return emotionDataSource.getEmotions().map { response -> | ||
| return emotionRemoteDataSource.getEmotions().map { response -> | ||
| response.map { it.toDomain() } | ||
| } | ||
| } | ||
|
|
||
| override suspend fun registerEmotion(emotionMarbleType: String): Result<List<EmotionRecommendRoutine>> { | ||
| return emotionDataSource.registerEmotion(emotionMarbleType).map { | ||
| it.recommendedRoutines.map { | ||
| emotionRecommendedRoutineDto -> | ||
| return emotionRemoteDataSource.registerEmotion(emotionMarbleType).map { | ||
| it.recommendedRoutines.map { emotionRecommendedRoutineDto -> | ||
| emotionRecommendedRoutineDto.toEmotionRecommendRoutine() | ||
| } | ||
| }.also { | ||
| if (it.isSuccess) { | ||
| _emotionChangeEventFlow.emit(EmotionChangeEvent.ChangeEmotion(emotionMarbleType)) | ||
| } | ||
| if (it.isSuccess) fetchAndSaveDailyEmotion(today = LocalDate.now(), forceRefresh = true) | ||
| } | ||
| } | ||
|
|
||
| override suspend fun fetchDailyEmotion(currentDate: String): Result<DailyEmotion> = | ||
| emotionDataSource.fetchDailyEmotion(currentDate).map { it.toDomain() } | ||
| override fun observeDailyEmotion(): Flow<Result<DailyEmotion>> = flow { | ||
| fetchAndSaveDailyEmotion(LocalDate.now()) | ||
| .onFailure { | ||
| emit(Result.failure(it)) | ||
| return@flow | ||
| } | ||
|
|
||
| emitAll( | ||
| emotionLocalDateSource.dailyEmotion | ||
| .filterNotNull() | ||
| .map { Result.success(it) }, | ||
| ) | ||
| } | ||
|
|
||
| override fun clearCache() { | ||
| emotionLocalDateSource.clearCache() | ||
| } | ||
|
|
||
| private suspend fun fetchAndSaveDailyEmotion( | ||
| today: LocalDate, | ||
| forceRefresh: Boolean = false, | ||
| ): Result<DailyEmotion> { | ||
| if (!forceRefresh) { | ||
| val currentLocalData = emotionLocalDateSource.dailyEmotion.value | ||
| if (currentLocalData != null && !currentLocalData.isStale(today)) { | ||
| return Result.success(currentLocalData) | ||
| } | ||
| } | ||
|
|
||
| private val _emotionChangeEventFlow = MutableSharedFlow<EmotionChangeEvent>() | ||
| override suspend fun getEmotionChangeEventFlow(): Flow<EmotionChangeEvent> = _emotionChangeEventFlow.asSharedFlow() | ||
| return fetchMutex.withLock { | ||
| if (!forceRefresh) { | ||
| val doubleCheckData = emotionLocalDateSource.dailyEmotion.value | ||
| if (doubleCheckData != null && !doubleCheckData.isStale(today)) { | ||
| return@withLock Result.success(doubleCheckData) | ||
| } | ||
| } | ||
| emotionRemoteDataSource.fetchDailyEmotion(today.toString()) | ||
| .onSuccess { emotionLocalDateSource.saveDailyEmotion(it.toDomain(today)) } | ||
| .map { it.toDomain(today) } | ||
| } | ||
| } | ||
| } | ||
17 changes: 16 additions & 1 deletion
17
domain/src/main/java/com/threegap/bitnagil/domain/emotion/model/DailyEmotion.kt
This file contains hidden or 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 |
|---|---|---|
| @@ -1,8 +1,23 @@ | ||
| package com.threegap.bitnagil.domain.emotion.model | ||
|
|
||
| import java.time.LocalDate | ||
|
|
||
| data class DailyEmotion( | ||
| val type: EmotionMarbleType?, | ||
| val name: String?, | ||
| val imageUrl: String?, | ||
| val homeMessage: String?, | ||
| ) | ||
| val fetchedDate: LocalDate = LocalDate.MIN, | ||
| ) { | ||
| fun isStale(today: LocalDate): Boolean = fetchedDate != today | ||
|
|
||
| companion object { | ||
| val INIT = DailyEmotion( | ||
| type = null, | ||
| name = null, | ||
| imageUrl = null, | ||
| homeMessage = null, | ||
| fetchedDate = LocalDate.MIN, | ||
| ) | ||
| } | ||
| } |
5 changes: 0 additions & 5 deletions
5
domain/src/main/java/com/threegap/bitnagil/domain/emotion/model/EmotionChangeEvent.kt
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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
12 changes: 0 additions & 12 deletions
12
...ain/java/com/threegap/bitnagil/domain/emotion/usecase/GetEmotionChangeEventFlowUseCase.kt
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.