Skip to content

Commit

Permalink
Refactor : Repository가 2개 이상 필요할 시 UseCase로 감싸기
Browse files Browse the repository at this point in the history
  • Loading branch information
DongChyeon committed Jun 28, 2024
1 parent 699dae9 commit 4579d78
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package store.newsbriefing.app.core.domain

import kotlinx.coroutines.flow.first
import store.newsbriefing.app.core.data.repository.MemberRepository
import store.newsbriefing.app.core.data.repository.MemberTokenRepository
import javax.inject.Inject

class DeleteMemberUseCase @Inject constructor(
private val memberRepository: MemberRepository,
private val memberTokenRepository: MemberTokenRepository
) {
suspend operator fun invoke() {
val memberId = memberTokenRepository.getMemberToken().first().memberId
memberRepository.deleteMember(memberId)
memberTokenRepository.clearMemberToken()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package store.newsbriefing.app.core.domain

import kotlinx.coroutines.flow.first
import store.newsbriefing.app.core.data.repository.MemberTokenRepository
import store.newsbriefing.app.core.data.repository.ScrapRepository
import javax.inject.Inject

class SetScrapUseCase @Inject constructor(
private val scrapRepository: ScrapRepository,
private val memberTokenRepository: MemberTokenRepository
) {
suspend operator fun invoke(articleId: Long) {
val userId = memberTokenRepository.getMemberToken().first().memberId
scrapRepository.setScrap(userId, articleId)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package store.newsbriefing.app.core.domain

import kotlinx.coroutines.flow.first
import store.newsbriefing.app.core.data.repository.MemberTokenRepository
import store.newsbriefing.app.core.data.repository.ScrapRepository
import javax.inject.Inject

class UnScrapUseCase @Inject constructor(
private val scrapRepository: ScrapRepository,
private val memberTokenRepository: MemberTokenRepository
) {
suspend operator fun invoke(articleId: Long) {
val userId = memberTokenRepository.getMemberToken().first().memberId
scrapRepository.unScrap(userId, articleId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private fun createGoogleIdOption(): GetGoogleIdOption {
.setFilterByAuthorizedAccounts(true)
.setServerClientId(BuildConfig.GOOGLE_WEB_CLIENT_ID)
.setAutoSelectEnabled(true)
.setNonce(generateNonce())
//.setNonce(generateNonce())
.build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class SignInViewModel @Inject constructor(
_eventFlow.emit(SignInEvent.NavigateToMain)
} catch (e: GoogleIdTokenParsingException) {
_eventFlow.emit(SignInEvent.ErrorOccurred("Failed to parse Google ID token"))
}catch (e : BriefingApiErrorException) {
} catch (e : BriefingApiErrorException) {
BriefingLogger.e("Failed to sign in with Google ID token: ${e.message}")
_eventFlow.emit(SignInEvent.ErrorOccurred(e.message ?: "Failed to sign in with Google ID token"))
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ class BookmarkViewModel @Inject constructor(

init {
viewModelScope.launch {
loadBriefingScrapArticleUseCase().collect {
_uiState.update { currentState ->
currentState.copy(
articles = BookmarkArticleUiState.Success(it)
)
try {
loadBriefingScrapArticleUseCase().collect {
_uiState.update { currentState ->
currentState.copy(
articles = BookmarkArticleUiState.Success(it)
)
}
}
} catch (e: Exception) {
_eventFlow.emit(BookmarkEvent.ErrorOccurred(e.toString()))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,34 @@ class HomeViewModel @Inject constructor(
return@launch
}

briefingRepository.getBriefingArticleSummaries(
briefingArticleCategory = category
).onStart {
_uiState.update {
it.copy(
articles = HashMap(it.articles.toMutableMap().apply {
this[category] = BriefingCategoryArticleUiState.Loading
})
)
try {
briefingRepository.getBriefingArticleSummaries(
briefingArticleCategory = category
).onStart {
_uiState.update {
it.copy(
articles = HashMap(it.articles.toMutableMap().apply {
this[category] = BriefingCategoryArticleUiState.Loading
})
)
}
}.collect { summaries ->
_uiState.update {
it.copy(
articles = HashMap(it.articles.toMutableMap().apply {
this[category] = BriefingCategoryArticleUiState.Success(
categoryArticles = summaries
)
})
)
}
}
}.collect { summaries ->
} catch (e: Exception) {
_eventFlow.emit(HomeEvent.ErrorOccurred(e.toString()))
_uiState.update {
it.copy(
articles = HashMap(it.articles.toMutableMap().apply {
this[category] = BriefingCategoryArticleUiState.Success(
categoryArticles = summaries
)
this[category] = BriefingCategoryArticleUiState.Error
})
)
}
Expand Down
1 change: 1 addition & 0 deletions feature/newsdetail/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ dependencies {
api(projects.core.model)
api(projects.core.common)
api(projects.core.designsystem)
api(projects.core.domain)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import store.newsbriefing.app.core.common.util.EventFlow
import store.newsbriefing.app.core.common.util.MutableEventFlow
import store.newsbriefing.app.core.common.util.asEventFlow
import store.newsbriefing.app.core.data.repository.BriefingRepository
import store.newsbriefing.app.core.data.repository.MemberTokenRepository
import store.newsbriefing.app.core.data.repository.ScrapRepository
import store.newsbriefing.app.core.domain.SetScrapUseCase
import store.newsbriefing.app.core.domain.UnScrapUseCase
import store.newsbriefing.app.core.model.BriefingArticle
import javax.inject.Inject

Expand All @@ -37,8 +36,8 @@ sealed interface BriefingArticleUiState {
class NewsDetailViewModel @Inject constructor(
savedStateHandle: SavedStateHandle,
private val briefingRepository: BriefingRepository,
private val scrapRepository: ScrapRepository,
private val memberTokenRepository: MemberTokenRepository
private val setScrapUseCase: SetScrapUseCase,
private val unScrapUseCase: UnScrapUseCase
) : ViewModel() {

private val newsDetailArgs = NewsDetailArgs(savedStateHandle)
Expand Down Expand Up @@ -70,32 +69,40 @@ class NewsDetailViewModel @Inject constructor(

fun setScrap(id: Long) {
viewModelScope.launch {
val memberId = memberTokenRepository.getMemberToken().first().memberId
scrapRepository.setScrap(memberId, id)
_uiState.update { currentState ->
currentState.copy(
article = when (val currentNews = currentState.article) {
is BriefingArticleUiState.Success ->
currentNews.copy(data = currentNews.data.copy(isScrap = true))
else -> currentNews
}
)
try {
setScrapUseCase(id)
_uiState.update { currentState ->
currentState.copy(
article = when (val currentNews = currentState.article) {
is BriefingArticleUiState.Success ->
currentNews.copy(data = currentNews.data.copy(isScrap = true))

else -> currentNews
}
)
}
} catch (e: Exception) {
_eventFlow.emit(NewsDetailEvent.ErrorOccurred(e.toString()))
}
}
}

fun unScrap(id: Long) {
viewModelScope.launch {
val memberId = memberTokenRepository.getMemberToken().first().memberId
scrapRepository.unScrap(memberId, id)
_uiState.update { currentState ->
currentState.copy(
article = when (val currentNews = currentState.article) {
is BriefingArticleUiState.Success ->
currentNews.copy(data = currentNews.data.copy(isScrap = false))
else -> currentNews
}
)
try {
unScrapUseCase(id)
_uiState.update { currentState ->
currentState.copy(
article = when (val currentNews = currentState.article) {
is BriefingArticleUiState.Success ->
currentNews.copy(data = currentNews.data.copy(isScrap = false))

else -> currentNews
}
)
}
} catch (e: Exception) {
_eventFlow.emit(NewsDetailEvent.ErrorOccurred(e.toString()))
}
}
}
Expand Down
1 change: 1 addition & 0 deletions feature/setting/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ dependencies {
api(projects.core.model)
api(projects.core.common)
api(projects.core.designsystem)
api(projects.core.domain)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package store.newsbriefing.app.feature.setting
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import store.newsbriefing.app.core.common.util.EventFlow
import store.newsbriefing.app.core.common.util.MutableEventFlow
import store.newsbriefing.app.core.common.util.asEventFlow
import store.newsbriefing.app.core.data.repository.MemberRepository
import store.newsbriefing.app.core.data.repository.MemberTokenRepository
import store.newsbriefing.app.core.domain.DeleteMemberUseCase
import javax.inject.Inject

sealed class SettingEvent {
Expand All @@ -20,21 +19,28 @@ sealed class SettingEvent {

@HiltViewModel
class SettingViewModel @Inject constructor(
private val memberRepository: MemberRepository,
private val deleteMemberUseCase: DeleteMemberUseCase,
private val memberTokenRepository: MemberTokenRepository
) : ViewModel() {
val eventFlow: EventFlow<SettingEvent>
get() = _eventFlow.asEventFlow()
private val _eventFlow = MutableEventFlow<SettingEvent>()

fun logout() = viewModelScope.launch {
memberTokenRepository.clearMemberToken()
_eventFlow.emit(SettingEvent.Logout)
try {
memberTokenRepository.clearMemberToken()
_eventFlow.emit(SettingEvent.Logout)
} catch (e: Exception) {
_eventFlow.emit(SettingEvent.ErrorOccurred(e.toString()))
}
}

fun deleteMember() = viewModelScope.launch {
val userId = memberTokenRepository.getMemberToken().first().memberId
memberRepository.deleteMember(userId)
_eventFlow.emit(SettingEvent.DeleteMember)
try {
deleteMemberUseCase()
_eventFlow.emit(SettingEvent.DeleteMember)
} catch (e: Exception) {
_eventFlow.emit(SettingEvent.ErrorOccurred(e.toString()))
}
}
}

0 comments on commit 4579d78

Please sign in to comment.