feat(creator): 커뮤니티 탭 상태 관리를 추가한다

This commit is contained in:
2026-06-21 20:44:27 +09:00
parent 744132fd7e
commit d4448820d6
4 changed files with 604 additions and 0 deletions

View File

@@ -178,6 +178,7 @@ import kr.co.vividnext.sodalive.user.login.LoginViewModel
import kr.co.vividnext.sodalive.user.signup.SignUpViewModel import kr.co.vividnext.sodalive.user.signup.SignUpViewModel
import kr.co.vividnext.sodalive.v2.creator.channel.CreatorChannelHomeViewModel import kr.co.vividnext.sodalive.v2.creator.channel.CreatorChannelHomeViewModel
import kr.co.vividnext.sodalive.v2.creator.channel.audio.CreatorChannelAudioViewModel import kr.co.vividnext.sodalive.v2.creator.channel.audio.CreatorChannelAudioViewModel
import kr.co.vividnext.sodalive.v2.creator.channel.community.CreatorChannelCommunityViewModel
import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelApi import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelApi
import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelRepository import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelRepository
import kr.co.vividnext.sodalive.v2.creator.channel.live.CreatorChannelLiveViewModel import kr.co.vividnext.sodalive.v2.creator.channel.live.CreatorChannelLiveViewModel
@@ -414,6 +415,7 @@ class AppDI(private val context: Context, isDebugMode: Boolean) {
viewModel { CreatorChannelLiveViewModel(get()) } viewModel { CreatorChannelLiveViewModel(get()) }
viewModel { CreatorChannelAudioViewModel(get()) } viewModel { CreatorChannelAudioViewModel(get()) }
viewModel { CreatorChannelSeriesViewModel(get()) } viewModel { CreatorChannelSeriesViewModel(get()) }
viewModel { CreatorChannelCommunityViewModel(get()) }
viewModel { PushNotificationListViewModel(get()) } viewModel { PushNotificationListViewModel(get()) }
viewModel { CharacterTabViewModel(get()) } viewModel { CharacterTabViewModel(get()) }
viewModel { CharacterDetailViewModel(get()) } viewModel { CharacterDetailViewModel(get()) }

View File

@@ -0,0 +1,177 @@
package kr.co.vividnext.sodalive.v2.creator.channel.community
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.orhanobut.logger.Logger
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers
import kr.co.vividnext.sodalive.base.BaseViewModel
import kr.co.vividnext.sodalive.common.ApiResponse
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
import kr.co.vividnext.sodalive.v2.creator.channel.community.data.CreatorChannelCommunityPostResponse
import kr.co.vividnext.sodalive.v2.creator.channel.community.data.CreatorChannelCommunityTabResponse
import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelRepository
class CreatorChannelCommunityViewModel(
private val repository: CreatorChannelRepository
) : BaseViewModel() {
private val _communityStateLiveData = MutableLiveData<CreatorChannelCommunityUiState>()
val communityStateLiveData: LiveData<CreatorChannelCommunityUiState>
get() = _communityStateLiveData
private var creatorId: Long = 0L
private var isOwner: Boolean = false
private var viewMode: CreatorChannelCommunityViewMode = CreatorChannelCommunityViewMode.List
private var requestGeneration: Int = 0
fun loadCommunity(creatorId: Long, isOwner: Boolean) {
if (creatorId <= 0) return
val shouldSkipReload = this.creatorId == creatorId && this.isOwner == isOwner && _communityStateLiveData.value != null
if (shouldSkipReload) return
this.creatorId = creatorId
this.isOwner = isOwner
loadFirstPage()
}
fun toggleViewMode() {
val content = _communityStateLiveData.value as? CreatorChannelCommunityUiState.Content ?: return
viewMode = when (content.viewMode) {
CreatorChannelCommunityViewMode.List -> CreatorChannelCommunityViewMode.Grid
CreatorChannelCommunityViewMode.Grid -> CreatorChannelCommunityViewMode.List
}
_communityStateLiveData.value = content.copy(viewMode = viewMode)
}
fun retryCommunity() {
if (creatorId <= 0) return
loadFirstPage()
}
fun loadMore() {
val content = _communityStateLiveData.value as? CreatorChannelCommunityUiState.Content ?: return
if (!content.hasNext || content.isLoadingMore || creatorId <= 0) return
val generation = requestGeneration
_communityStateLiveData.value = content.copy(isLoadingMore = true, paginationErrorMessage = null)
requestCommunity(page = content.page + 1, generation = generation) { response ->
val data = response.data
val current = _communityStateLiveData.value as? CreatorChannelCommunityUiState.Content ?: content
if (response.success && data != null) {
_communityStateLiveData.value = current.copy(
communityPosts = current.communityPosts + data.communityPosts,
page = data.page,
size = data.size,
hasNext = data.hasNext,
isLoadingMore = false
)
} else {
_communityStateLiveData.value = current.copy(
isLoadingMore = false,
paginationErrorMessage = response.message
)
}
}
}
fun consumePaginationErrorMessage() {
val content = _communityStateLiveData.value as? CreatorChannelCommunityUiState.Content ?: return
if (content.paginationErrorMessage == null) return
_communityStateLiveData.value = content.copy(paginationErrorMessage = null)
}
private fun loadFirstPage() {
val generation = ++requestGeneration
_communityStateLiveData.value = CreatorChannelCommunityUiState.Loading
requestCommunity(page = FIRST_PAGE, generation = generation) { response ->
val data = response.data
if (response.success && data != null) {
val communityPosts = data.communityPosts
_communityStateLiveData.value = if (communityPosts.isEmpty() || data.communityPostCount == 0) {
CreatorChannelCommunityUiState.Empty
} else {
data.toContentState(communityPosts = communityPosts)
}
} else {
_communityStateLiveData.value = CreatorChannelCommunityUiState.Error(response.message)
}
}
}
private fun requestCommunity(
page: Int,
generation: Int,
onSuccess: (ApiResponse<CreatorChannelCommunityTabResponse>) -> Unit
) {
compositeDisposable.add(
repository.getCommunity(
creatorId = creatorId,
page = page,
size = DEFAULT_PAGE_SIZE,
token = authToken()
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{
if (generation == requestGeneration) {
onSuccess(it)
}
},
{
if (generation != requestGeneration) return@subscribe
it.message?.let { message -> Logger.e(message) }
val current = _communityStateLiveData.value as? CreatorChannelCommunityUiState.Content
_communityStateLiveData.value = if (current != null && page > FIRST_PAGE) {
current.copy(isLoadingMore = false, paginationErrorMessage = it.message)
} else {
CreatorChannelCommunityUiState.Error(it.message)
}
}
)
)
}
private fun CreatorChannelCommunityTabResponse.toContentState(
communityPosts: List<CreatorChannelCommunityPostResponse>
) = CreatorChannelCommunityUiState.Content(
communityPostCount = communityPostCount,
communityPosts = communityPosts,
viewMode = viewMode,
page = page,
size = size,
hasNext = hasNext
)
private fun authToken(): String = "Bearer ${SharedPreferenceManager.token}"
companion object {
val DEFAULT_PAGE_SIZE = 20
private const val FIRST_PAGE = 0
}
}
enum class CreatorChannelCommunityViewMode {
List,
Grid
}
sealed interface CreatorChannelCommunityUiState {
data object Loading : CreatorChannelCommunityUiState
data object Empty : CreatorChannelCommunityUiState
data class Error(val message: String?) : CreatorChannelCommunityUiState
data class Content(
val communityPostCount: Int,
val communityPosts: List<CreatorChannelCommunityPostResponse>,
val viewMode: CreatorChannelCommunityViewMode,
val page: Int,
val size: Int,
val hasNext: Boolean,
val isLoadingMore: Boolean = false,
val paginationErrorMessage: String? = null
) : CreatorChannelCommunityUiState
}

View File

@@ -0,0 +1,219 @@
package kr.co.vividnext.sodalive.v2.creator.channel.community
import android.app.Application
import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import androidx.test.core.app.ApplicationProvider
import io.reactivex.rxjava3.android.plugins.RxAndroidPlugins
import io.reactivex.rxjava3.core.Scheduler
import io.reactivex.rxjava3.core.Single
import io.reactivex.rxjava3.plugins.RxJavaPlugins
import io.reactivex.rxjava3.schedulers.Schedulers
import io.reactivex.rxjava3.subjects.SingleSubject
import kr.co.vividnext.sodalive.common.ApiResponse
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
import kr.co.vividnext.sodalive.v2.creator.channel.community.data.CreatorChannelCommunityPostResponse
import kr.co.vividnext.sodalive.v2.creator.channel.community.data.CreatorChannelCommunityTabResponse
import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelRepository
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], application = Application::class)
class CreatorChannelCommunityPaginationTest {
private val context: Context = ApplicationProvider.getApplicationContext()
private lateinit var repository: CreatorChannelRepository
private lateinit var viewModel: CreatorChannelCommunityViewModel
@Before
fun setUp() {
setImmediateRxSchedulers()
SharedPreferenceManager.resetForTest()
SharedPreferenceManager.init(context)
SharedPreferenceManager.token = "test-token"
repository = org.mockito.kotlin.mock()
viewModel = CreatorChannelCommunityViewModel(repository)
}
@After
fun tearDown() {
RxJavaPlugins.reset()
RxAndroidPlugins.reset()
SharedPreferenceManager.resetForTest()
}
@Test
fun `hasNext가 true이면 다음 페이지는 마지막 성공 응답의 page plus 1로 요청하고 append한다`() {
stubGetCommunity(
page = 0,
response = Single.just(
ApiResponse(true, communityResponse(page = 0, ids = listOf(1L), hasNext = true), null)
)
)
stubGetCommunity(
page = 1,
response = Single.just(
ApiResponse(true, communityResponse(page = 1, ids = listOf(2L), hasNext = false), null)
)
)
viewModel.loadCommunity(100L, isOwner = false)
viewModel.loadMore()
val state = viewModel.communityStateLiveData.requireValue() as CreatorChannelCommunityUiState.Content
assertEquals(1, state.page)
assertEquals(listOf(1L, 2L), state.communityPosts.map { it.postId })
assertFalse(state.hasNext)
verifyGetCommunity(page = 1)
}
@Test
fun `다음 페이지 로딩 중 중복 load-more 요청은 막고 size 20을 유지한다`() {
val pending = SingleSubject.create<ApiResponse<CreatorChannelCommunityTabResponse>>()
stubGetCommunity(
page = 0,
response = Single.just(
ApiResponse(true, communityResponse(page = 0, ids = listOf(1L), hasNext = true), null)
)
)
stubGetCommunity(page = 1, response = pending)
viewModel.loadCommunity(100L, isOwner = false)
viewModel.loadMore()
viewModel.loadMore()
val loadingState = viewModel.communityStateLiveData.requireValue() as CreatorChannelCommunityUiState.Content
assertTrue(loadingState.isLoadingMore)
verifyGetCommunity(page = 1, times = 1)
pending.onSuccess(
ApiResponse(true, communityResponse(page = 1, ids = listOf(2L), hasNext = false), null)
)
val loadedState = viewModel.communityStateLiveData.requireValue() as CreatorChannelCommunityUiState.Content
assertEquals(CreatorChannelCommunityViewModel.DEFAULT_PAGE_SIZE, loadedState.size)
assertEquals(listOf(1L, 2L), loadedState.communityPosts.map { it.postId })
}
@Test
fun `다음 페이지 실패는 기존 목록을 유지하고 pagination error를 표시한다`() {
stubGetCommunity(
page = 0,
response = Single.just(
ApiResponse(true, communityResponse(page = 0, ids = listOf(1L), hasNext = true), null)
)
)
stubGetCommunity(
page = 1,
response = Single.just(ApiResponse(false, null, "failed"))
)
viewModel.loadCommunity(100L, isOwner = false)
viewModel.loadMore()
val state = viewModel.communityStateLiveData.requireValue() as CreatorChannelCommunityUiState.Content
assertEquals(listOf(1L), state.communityPosts.map { it.postId })
assertFalse(state.isLoadingMore)
assertEquals("failed", state.paginationErrorMessage)
}
@Test
fun `pagination error message는 표시 후 clear되어 다시 표시되지 않는다`() {
stubGetCommunity(
page = 0,
response = Single.just(
ApiResponse(true, communityResponse(page = 0, ids = listOf(1L), hasNext = true), null)
)
)
stubGetCommunity(
page = 1,
response = Single.just(ApiResponse(false, null, "failed"))
)
viewModel.loadCommunity(100L, isOwner = false)
viewModel.loadMore()
viewModel.consumePaginationErrorMessage()
val state = viewModel.communityStateLiveData.requireValue() as CreatorChannelCommunityUiState.Content
assertEquals(null, state.paginationErrorMessage)
assertEquals(listOf(1L), state.communityPosts.map { it.postId })
}
private fun stubGetCommunity(
page: Int,
response: Single<ApiResponse<CreatorChannelCommunityTabResponse>>
) {
whenever(
repository.getCommunity(
100L,
page,
CreatorChannelCommunityViewModel.DEFAULT_PAGE_SIZE,
"Bearer test-token"
)
).thenReturn(response)
}
private fun verifyGetCommunity(page: Int, times: Int? = null) {
val verification = times?.let { verify(repository, times(it)) } ?: verify(repository)
verification.getCommunity(
100L,
page,
CreatorChannelCommunityViewModel.DEFAULT_PAGE_SIZE,
"Bearer test-token"
)
}
private fun setImmediateRxSchedulers() {
val trampoline = { _: Scheduler -> Schedulers.trampoline() }
RxJavaPlugins.setIoSchedulerHandler(trampoline)
RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() }
RxAndroidPlugins.setMainThreadSchedulerHandler { Schedulers.trampoline() }
}
private fun communityResponse(
page: Int,
ids: List<Long>,
hasNext: Boolean
) = CreatorChannelCommunityTabResponse(
communityPostCount = ids.size,
communityPosts = ids.map { communityPost(it) },
page = page,
size = CreatorChannelCommunityViewModel.DEFAULT_PAGE_SIZE,
hasNext = hasNext
)
private fun communityPost(id: Long) = CreatorChannelCommunityPostResponse(
postId = id,
creatorId = 100L,
creatorNickname = "creator",
creatorProfileUrl = "profile.png",
createdAtUtc = "2026-06-21T00:00:00Z",
content = "community $id",
imageUrl = null,
audioUrl = null,
price = 0,
existOrdered = true,
isCommentAvailable = true,
likeCount = 1,
commentCount = 2,
isPinned = false
)
private fun <T> LiveData<T>.requireValue(): T? {
var value: T? = null
val observer = Observer<T> { value = it }
observeForever(observer)
removeObserver(observer)
return value
}
}

View File

@@ -0,0 +1,206 @@
package kr.co.vividnext.sodalive.v2.creator.channel.community
import android.app.Application
import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import androidx.test.core.app.ApplicationProvider
import io.reactivex.rxjava3.android.plugins.RxAndroidPlugins
import io.reactivex.rxjava3.core.Scheduler
import io.reactivex.rxjava3.core.Single
import io.reactivex.rxjava3.plugins.RxJavaPlugins
import io.reactivex.rxjava3.schedulers.Schedulers
import kr.co.vividnext.sodalive.common.ApiResponse
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
import kr.co.vividnext.sodalive.v2.creator.channel.community.data.CreatorChannelCommunityPostResponse
import kr.co.vividnext.sodalive.v2.creator.channel.community.data.CreatorChannelCommunityTabResponse
import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelRepository
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.any
import org.mockito.kotlin.never
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], application = Application::class)
class CreatorChannelCommunityViewModelTest {
private val context: Context = ApplicationProvider.getApplicationContext()
private lateinit var repository: CreatorChannelRepository
private lateinit var viewModel: CreatorChannelCommunityViewModel
@Before
fun setUp() {
setImmediateRxSchedulers()
SharedPreferenceManager.resetForTest()
SharedPreferenceManager.init(context)
SharedPreferenceManager.token = "test-token"
repository = org.mockito.kotlin.mock()
viewModel = CreatorChannelCommunityViewModel(repository)
}
@After
fun tearDown() {
RxJavaPlugins.reset()
RxAndroidPlugins.reset()
SharedPreferenceManager.resetForTest()
}
@Test
fun `최초 로드는 page 0 size 20으로 커뮤니티 API를 호출하고 기본 리스트형 Content를 emit한다`() {
stubGetCommunity(response = Single.just(ApiResponse(true, communityResponse(ids = listOf(1L)), null)))
viewModel.loadCommunity(100L, isOwner = false)
val state = viewModel.communityStateLiveData.requireValue() as CreatorChannelCommunityUiState.Content
assertEquals(CreatorChannelCommunityViewMode.List, state.viewMode)
assertEquals(0, state.page)
assertEquals(CreatorChannelCommunityViewModel.DEFAULT_PAGE_SIZE, state.size)
assertEquals(listOf(1L), state.communityPosts.map { it.postId })
verifyGetCommunity()
}
@Test
fun `보기 방식 toggle은 리스트형 그리드형 리스트형 순서이며 API를 재호출하지 않는다`() {
stubGetCommunity(response = Single.just(ApiResponse(true, communityResponse(ids = listOf(1L)), null)))
viewModel.loadCommunity(100L, isOwner = false)
viewModel.toggleViewMode()
val gridState = viewModel.communityStateLiveData.requireValue() as CreatorChannelCommunityUiState.Content
viewModel.toggleViewMode()
val listState = viewModel.communityStateLiveData.requireValue() as CreatorChannelCommunityUiState.Content
assertEquals(CreatorChannelCommunityViewMode.Grid, gridState.viewMode)
assertEquals(CreatorChannelCommunityViewMode.List, listState.viewMode)
assertEquals(listOf(1L), listState.communityPosts.map { it.postId })
verifyGetCommunity(times = 1)
}
@Test
fun `communityPostCount가 0이면 표시 가능한 item이 있어도 Empty를 emit한다`() {
stubGetCommunity(
response = Single.just(
ApiResponse(true, communityResponse(communityPostCount = 0, ids = listOf(1L)), null)
)
)
viewModel.loadCommunity(100L, isOwner = false)
assertTrue(viewModel.communityStateLiveData.requireValue() is CreatorChannelCommunityUiState.Empty)
}
@Test
fun `표시 가능한 communityPosts가 없으면 Empty를 emit한다`() {
stubGetCommunity(response = Single.just(ApiResponse(true, communityResponse(ids = emptyList()), null)))
viewModel.loadCommunity(100L, isOwner = false)
assertTrue(viewModel.communityStateLiveData.requireValue() is CreatorChannelCommunityUiState.Empty)
}
@Test
fun `retryCommunity는 첫 페이지를 다시 조회한다`() {
whenever(
repository.getCommunity(
100L,
0,
CreatorChannelCommunityViewModel.DEFAULT_PAGE_SIZE,
"Bearer test-token"
)
).thenReturn(
Single.just(ApiResponse(false, null, "failed")),
Single.just(ApiResponse(true, communityResponse(ids = listOf(2L)), null))
)
viewModel.loadCommunity(100L, isOwner = false)
viewModel.retryCommunity()
val state = viewModel.communityStateLiveData.requireValue() as CreatorChannelCommunityUiState.Content
assertEquals(listOf(2L), state.communityPosts.map { it.postId })
verifyGetCommunity(times = 2)
}
@Test
fun `creatorId가 0 이하이면 커뮤니티 API를 호출하지 않는다`() {
viewModel.loadCommunity(0L, isOwner = false)
verify(repository, never()).getCommunity(any(), any(), any(), any())
}
private fun stubGetCommunity(
page: Int = 0,
response: Single<ApiResponse<CreatorChannelCommunityTabResponse>>
) {
whenever(
repository.getCommunity(
100L,
page,
CreatorChannelCommunityViewModel.DEFAULT_PAGE_SIZE,
"Bearer test-token"
)
).thenReturn(response)
}
private fun verifyGetCommunity(page: Int = 0, times: Int? = null) {
val verification = times?.let { verify(repository, times(it)) } ?: verify(repository)
verification.getCommunity(
100L,
page,
CreatorChannelCommunityViewModel.DEFAULT_PAGE_SIZE,
"Bearer test-token"
)
}
private fun setImmediateRxSchedulers() {
val trampoline = { _: Scheduler -> Schedulers.trampoline() }
RxJavaPlugins.setIoSchedulerHandler(trampoline)
RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() }
RxAndroidPlugins.setMainThreadSchedulerHandler { Schedulers.trampoline() }
}
private fun communityResponse(
page: Int = 0,
communityPostCount: Int? = null,
ids: List<Long>,
hasNext: Boolean = false
) = CreatorChannelCommunityTabResponse(
communityPostCount = communityPostCount ?: ids.size,
communityPosts = ids.map { communityPost(it) },
page = page,
size = CreatorChannelCommunityViewModel.DEFAULT_PAGE_SIZE,
hasNext = hasNext
)
private fun communityPost(id: Long) = CreatorChannelCommunityPostResponse(
postId = id,
creatorId = 100L,
creatorNickname = "creator",
creatorProfileUrl = "profile.png",
createdAtUtc = "2026-06-21T00:00:00Z",
content = "community $id",
imageUrl = null,
audioUrl = null,
price = 0,
existOrdered = true,
isCommentAvailable = true,
likeCount = 1,
commentCount = 2,
isPinned = false
)
private fun <T> LiveData<T>.requireValue(): T? {
var value: T? = null
val observer = Observer<T> { value = it }
observeForever(observer)
removeObserver(observer)
return value
}
}