Repository navigation
๐ :: (#565) ๊ณต์ง์ฌํญ ๋ชฉ๋ก ์ ์ฒด ์กฐํ ์ฒ๋ฆฌ #569
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
The head ref may contain hidden characters: "bug/565-\uACF5\uC9C0\uC0AC\uD56D-\uBAA9\uB85D-\uC804\uCCB4-\uC870\uD68C-\uCC98\uB9AC"
Changes from 13 commits
1e6e67c
94370c8
9790c8c
9898cf2
b6e2bf4
2fcd402
40951ca
040789a
a09a01e
51b5fdc
1af264e
afdd2d7
4d1d5d9
5231789
4df466b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,13 @@ | ||
| package team.retum.data.repository.notice | ||
|
|
||
| import team.retum.network.model.response.notice.FetchNoticeDetailsResponse | ||
| import team.retum.network.model.response.notice.FetchNoticesCountResponse | ||
| import team.retum.network.model.response.notice.FetchNoticesResponse | ||
|
|
||
| interface NoticeRepository { | ||
| suspend fun fetchNotices(): FetchNoticesResponse | ||
| suspend fun fetchNotices(page: Long): FetchNoticesResponse | ||
|
|
||
| suspend fun fetchNoticesCount(): FetchNoticesCountResponse | ||
|
|
||
| suspend fun fetchNoticeDetails(noticeId: Long): FetchNoticeDetailsResponse | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package team.retum.usecase.entity.notice | ||
|
|
||
| import team.retum.network.model.response.notice.FetchNoticesCountResponse | ||
|
|
||
| data class NoticesCountEntity( | ||
| val totalPageCount: Long, | ||
| ) | ||
|
|
||
| internal fun FetchNoticesCountResponse.toEntity() = NoticesCountEntity( | ||
| totalPageCount = totalPageCount, | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package team.retum.usecase.usecase.notice | ||
|
|
||
| import team.retum.data.repository.notice.NoticeRepository | ||
| import team.retum.usecase.entity.notice.toEntity | ||
| import javax.inject.Inject | ||
|
|
||
| class FetchNoticesCountUseCase @Inject constructor( | ||
| private val noticeRepository: NoticeRepository, | ||
| ) { | ||
| suspend operator fun invoke() = runCatching { | ||
| noticeRepository.fetchNoticesCount().toEntity() | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,13 @@ | ||
| package team.retum.network.datasource.notice | ||
|
|
||
| import team.retum.network.model.response.notice.FetchNoticeDetailsResponse | ||
| import team.retum.network.model.response.notice.FetchNoticesCountResponse | ||
| import team.retum.network.model.response.notice.FetchNoticesResponse | ||
|
|
||
| interface NoticeDataSource { | ||
| suspend fun fetchNotices(): FetchNoticesResponse | ||
| suspend fun fetchNotices(page: Long): FetchNoticesResponse | ||
|
|
||
| suspend fun fetchNoticesCount(): FetchNoticesCountResponse | ||
|
|
||
| suspend fun fetchNoticeDetails(noticeId: Long): FetchNoticeDetailsResponse | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package team.retum.network.model.response.notice | ||
|
|
||
| import com.squareup.moshi.Json | ||
| import com.squareup.moshi.JsonClass | ||
|
|
||
| @JsonClass(generateAdapter = true) | ||
| data class FetchNoticesCountResponse( | ||
| @Json(name = "total_page_count") val totalPageCount: Long, | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,19 +8,44 @@ import kotlinx.coroutines.launch | |
| import team.retum.common.base.BaseViewModel | ||
| import team.retum.common.exception.BadRequestException | ||
| import team.retum.usecase.entity.notice.NoticesEntity | ||
| import team.retum.usecase.usecase.notice.FetchNoticesCountUseCase | ||
| import team.retum.usecase.usecase.notice.FetchNoticesUseCase | ||
| import javax.inject.Inject | ||
|
|
||
| private const val NOTICE_PAGE_SIZE = 12 | ||
|
|
||
| @HiltViewModel | ||
| internal class NoticesViewModel @Inject constructor( | ||
| private val fetchNoticesUseCase: FetchNoticesUseCase, | ||
| private val fetchNoticesCountUseCase: FetchNoticesCountUseCase, | ||
| ) : BaseViewModel<NoticesState, NoticesSideEffect>(NoticesState.getDefaultState()) { | ||
| private val fetchLock = Any() | ||
|
|
||
| internal fun fetchNotices() { | ||
| val page = synchronized(fetchLock) { | ||
| val currentState = state.value | ||
| if (currentState.isLoading || currentState.totalPage?.let { currentState.page >= it } == true) { | ||
| null | ||
| } else { | ||
| val nextPage = currentState.page + 1 | ||
| setState { state.value.copy(isLoading = true) } | ||
| nextPage | ||
| } | ||
| } ?: return | ||
|
|
||
| viewModelScope.launch(Dispatchers.IO) { | ||
| fetchNoticesUseCase() | ||
| fetchNoticesUseCase(page = page) | ||
| .onSuccess { | ||
| setState { state.value.copy(notices = it.notices) } | ||
| setState { | ||
| state.value.copy( | ||
| page = page, | ||
| notices = state.value.notices + it.notices, | ||
| totalPage = state.value.totalPage ?: if (it.notices.size < NOTICE_PAGE_SIZE) page else null, | ||
| isLoading = false, | ||
| ) | ||
| } | ||
| }.onFailure { | ||
| setState { state.value.copy(isLoading = false) } | ||
| when (it) { | ||
| is BadRequestException -> { | ||
| postSideEffect(NoticesSideEffect.BadRequest) | ||
|
|
@@ -29,16 +54,51 @@ internal class NoticesViewModel @Inject constructor( | |
| } | ||
| } | ||
| } | ||
|
|
||
| internal fun fetchTotalNoticeCount() { | ||
| viewModelScope.launch(Dispatchers.IO) { | ||
| fetchNoticesCountUseCase() | ||
| .onSuccess { | ||
| setState { | ||
| state.value.copy( | ||
| totalPage = it.totalPageCount, | ||
| isNoticeCountFetchFailed = false, | ||
| ) | ||
| } | ||
| fetchNotices() | ||
| }.onFailure { | ||
| setState { state.value.copy(isNoticeCountFetchFailed = true) } | ||
| fetchNotices() | ||
| when (it) { | ||
| is BadRequestException -> { | ||
| postSideEffect(NoticesSideEffect.BadRequest) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| internal fun whetherFetchNextPage(lastVisibleItemIndex: Int): Boolean = with(state.value) { | ||
| return !isLoading && | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๋ค์ ํ์ด์ง ์์ฒญ์ ํญ๋ชฉ ์ปดํฌ์ง์ ์์์ ์คํํ์ง ๋ง์ธ์.
์์ฒญ ํธ๋ฆฌ๊ฑฐ๋ ์ปดํฌ์ง์ ๋ฐ์ผ๋ก ์ฎ๊ฒจ์ผ ํฉ๋๋ค. ์คํจํ ํ์ด์ง๋ ์๋ก์ด ์ฌ์ฉ์ ๋์์ด๋ ๋ช ์์ ์ธ ์ฌ์๋ ์ ์ฑ ์์ด ์๋์ผ๋ก ๋ค์ ์์ฒญํ์ง ์์์ผ ํฉ๋๋ค.
|
||
| lastVisibleItemIndex == notices.lastIndex && | ||
| (totalPage == null || page < totalPage) | ||
| } | ||
| } | ||
|
|
||
| internal data class NoticesState( | ||
| val noticeId: Long, | ||
| val notices: List<NoticesEntity.NoticeEntity>, | ||
| val page: Long, | ||
| val totalPage: Long?, | ||
| val isLoading: Boolean, | ||
| val isNoticeCountFetchFailed: Boolean, | ||
| ) { | ||
| companion object { | ||
| fun getDefaultState() = NoticesState( | ||
| noticeId = 0, | ||
| notices = mutableStateListOf(), | ||
| page = 0, | ||
| totalPage = null, | ||
| isLoading = false, | ||
| isNoticeCountFetchFailed = false, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.