feat(home): 서브 배너 상태와 익명 인증 처리를 연결한다
This commit is contained in:
+3
-1
@@ -46,7 +46,9 @@ class HomeRecommendationViewModel(
|
||||
_recommendationStateLiveData.value = HomeRecommendationUiState.Loading
|
||||
|
||||
compositeDisposable.add(
|
||||
repository.getRecommendations(token = authToken())
|
||||
repository.getRecommendations(
|
||||
token = SharedPreferenceManager.token.takeIf { it.isNotBlank() }?.let { "Bearer $it" }
|
||||
)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(
|
||||
|
||||
+4
-1
@@ -12,6 +12,8 @@ import kr.co.vividnext.sodalive.v2.main.home.data.HomePopularCommunityPostItem
|
||||
import kr.co.vividnext.sodalive.v2.main.home.data.HomeRecommendationResponse
|
||||
import kr.co.vividnext.sodalive.v2.widget.characterchatthumbnail.CharacterChatThumbnailItem
|
||||
import kr.co.vividnext.sodalive.v2.widget.feed.FeedItem
|
||||
import kr.co.vividnext.sodalive.v2.widget.subbanner.SubBannerPlacement
|
||||
import kr.co.vividnext.sodalive.v2.widget.subbanner.toSubBannerItems
|
||||
|
||||
@StringRes
|
||||
fun String.toCreatorActivityTypeLabelRes(): Int? = CreatorActivityType.from(this)?.labelResId
|
||||
@@ -24,7 +26,8 @@ fun HomeRecommendationResponse.toContent(): HomeRecommendationUiState.Content =
|
||||
aiCharacters = HomeRecommendationAiCharacterSection(aiCharacters.map { it.toUiModel() }),
|
||||
genreCreators = HomeRecommendationGenreCreatorSection(genreCreators.map { it.toUiModel() }),
|
||||
cheerCreators = HomeRecommendationCheerCreatorSection(cheerCreators.map { it.toUiModel() }),
|
||||
popularCommunityPosts = HomeRecommendationPopularCommunityPostSection(popularCommunityPosts.map { it.toUiModel() })
|
||||
popularCommunityPosts = HomeRecommendationPopularCommunityPostSection(popularCommunityPosts.map { it.toUiModel() }),
|
||||
subBanners = subBanners.toSubBannerItems(SubBannerPlacement.HOME_RECOMMENDATION)
|
||||
)
|
||||
|
||||
fun HomeLiveItem.toUiModel(): HomeRecommendationLiveUiModel = HomeRecommendationLiveUiModel(
|
||||
|
||||
+6
-2
@@ -1,5 +1,7 @@
|
||||
package kr.co.vividnext.sodalive.v2.main.home.model
|
||||
|
||||
import kr.co.vividnext.sodalive.v2.widget.subbanner.SubBannerItem
|
||||
|
||||
sealed interface HomeRecommendationUiState {
|
||||
data object Loading : HomeRecommendationUiState
|
||||
|
||||
@@ -11,7 +13,8 @@ sealed interface HomeRecommendationUiState {
|
||||
val aiCharacters: HomeRecommendationAiCharacterSection,
|
||||
val genreCreators: HomeRecommendationGenreCreatorSection,
|
||||
val cheerCreators: HomeRecommendationCheerCreatorSection,
|
||||
val popularCommunityPosts: HomeRecommendationPopularCommunityPostSection
|
||||
val popularCommunityPosts: HomeRecommendationPopularCommunityPostSection,
|
||||
val subBanners: List<SubBannerItem> = emptyList()
|
||||
) : HomeRecommendationUiState {
|
||||
val isEmpty: Boolean = listOf(
|
||||
lives.items,
|
||||
@@ -20,7 +23,8 @@ sealed interface HomeRecommendationUiState {
|
||||
recentDebutCreators.items,
|
||||
aiCharacters.items,
|
||||
cheerCreators.items,
|
||||
popularCommunityPosts.items
|
||||
popularCommunityPosts.items,
|
||||
subBanners
|
||||
).all { it.isEmpty() }
|
||||
}
|
||||
|
||||
|
||||
+95
-8
@@ -1,10 +1,13 @@
|
||||
package kr.co.vividnext.sodalive.v2.main.home
|
||||
|
||||
import android.app.Application
|
||||
import android.content.Context
|
||||
import android.os.Looper
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import com.google.gson.Gson
|
||||
import io.mockk.every
|
||||
import io.mockk.mockkObject
|
||||
import io.mockk.unmockkObject
|
||||
import io.reactivex.rxjava3.android.plugins.RxAndroidPlugins
|
||||
import io.reactivex.rxjava3.core.Scheduler
|
||||
import io.reactivex.rxjava3.core.Single
|
||||
@@ -27,26 +30,31 @@ import kr.co.vividnext.sodalive.v2.main.home.data.HomeRecommendationResponse
|
||||
import kr.co.vividnext.sodalive.v2.main.home.model.HomeRecommendationUiState
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.Shadows.shadowOf
|
||||
import org.robolectric.annotation.Config
|
||||
import okhttp3.ResponseBody.Companion.toResponseBody
|
||||
import retrofit2.HttpException
|
||||
import retrofit2.Response
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], application = Application::class)
|
||||
class HomeRecommendationViewModelTest {
|
||||
|
||||
private val context: Context = ApplicationProvider.getApplicationContext()
|
||||
private lateinit var api: FakeHomeRecommendationApi
|
||||
private lateinit var viewModel: HomeRecommendationViewModel
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
setImmediateRxSchedulers()
|
||||
SharedPreferenceManager.resetForTest()
|
||||
SharedPreferenceManager.init(context)
|
||||
SharedPreferenceManager.token = "test-token"
|
||||
mockkObject(SharedPreferenceManager)
|
||||
every { SharedPreferenceManager.token } returns "test-token"
|
||||
api = FakeHomeRecommendationApi()
|
||||
viewModel = HomeRecommendationViewModel(HomeRecommendationRepository(api))
|
||||
}
|
||||
@@ -55,7 +63,82 @@ class HomeRecommendationViewModelTest {
|
||||
fun tearDown() {
|
||||
RxJavaPlugins.reset()
|
||||
RxAndroidPlugins.reset()
|
||||
SharedPreferenceManager.resetForTest()
|
||||
unmockkObject(SharedPreferenceManager)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `비로그인 추천은 빈 Bearer 대신 인증 헤더를 생략한다`() {
|
||||
every { SharedPreferenceManager.token } returns ""
|
||||
api.enqueueRecommendations(ApiResponse(true, response(), null))
|
||||
|
||||
viewModel.loadRecommendations()
|
||||
|
||||
assertNull(api.recommendationHeaders.single())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `서브 배너만 있는 홈 추천도 Content 상태가 된다`() {
|
||||
val data = Gson().fromJson(
|
||||
"""{
|
||||
"lives":[],"banners":[],"recentlyActiveCreators":[],"recentDebutCreators":[],"aiCharacters":[],
|
||||
"genreCreators":[],"cheerCreators":[],"popularCommunityPosts":[],
|
||||
"subBanners":[{"id":7,"placement":"HOME_RECOMMENDATION","imageUrl":"https://example.invalid/banner",
|
||||
"androidDeepLink":"sodalive://home/7","webUrl":null,"iosDeepLink":null}]}
|
||||
""",
|
||||
HomeRecommendationResponse::class.java
|
||||
)
|
||||
api.enqueueRecommendations(ApiResponse(true, data, null))
|
||||
|
||||
viewModel.loadRecommendations()
|
||||
|
||||
assertTrue(viewModel.recommendationStateLiveData.requireValue() is HomeRecommendationUiState.Content)
|
||||
val state = viewModel.recommendationStateLiveData.requireValue() as HomeRecommendationUiState.Content
|
||||
assertEquals(listOf(7L), state.subBanners.map { it.id })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `로그인 추천과 follow는 기존 인증 값을 유지한다`() {
|
||||
api.enqueueRecommendations(ApiResponse(true, response(), null))
|
||||
api.enqueueFollow(ApiResponse(true, Any(), null))
|
||||
|
||||
viewModel.loadRecommendations()
|
||||
viewModel.followCreators("cheerCreators", listOf(1L))
|
||||
|
||||
assertEquals(listOf("Bearer test-token"), api.recommendationHeaders)
|
||||
assertEquals(listOf("Bearer test-token"), api.followHeaders)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `빈 성공은 Empty이고 실패 envelope는 기존 Error와 일반 오류 피드백이다`() {
|
||||
api.enqueueRecommendations(ApiResponse(true, response().copy(lives = emptyList(), cheerCreators = emptyList()), null))
|
||||
viewModel.loadRecommendations()
|
||||
assertEquals(HomeRecommendationUiState.Empty, viewModel.recommendationStateLiveData.requireValue())
|
||||
assertNull(viewModel.toastLiveData.requireValue())
|
||||
|
||||
val failed = HomeRecommendationViewModel(HomeRecommendationRepository(api))
|
||||
api.enqueueRecommendations(ApiResponse(false, null, "denied"))
|
||||
failed.loadRecommendations()
|
||||
shadowOf(Looper.getMainLooper()).idle()
|
||||
assertTrue(failed.recommendationStateLiveData.requireValue() is HomeRecommendationUiState.Error)
|
||||
assertNotNull(failed.toastLiveData.requireValue())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `최초 HTTP 오류는 Empty가 아닌 Error이고 refresh 오류는 기존 추천을 보존한다`() {
|
||||
api.enqueueRecommendations(Single.error(HttpException(Response.error<Any>(500, "error".toResponseBody()))))
|
||||
viewModel.loadRecommendations()
|
||||
shadowOf(Looper.getMainLooper()).idle()
|
||||
assertTrue(viewModel.recommendationStateLiveData.requireValue() is HomeRecommendationUiState.Error)
|
||||
assertNotNull(viewModel.toastLiveData.requireValue())
|
||||
|
||||
api.enqueueRecommendations(ApiResponse(true, response(), null))
|
||||
viewModel.loadRecommendations()
|
||||
val previous = viewModel.recommendationStateLiveData.requireValue()
|
||||
api.enqueueRecommendations(Single.error(HttpException(Response.error<Any>(401, "error".toResponseBody()))))
|
||||
viewModel.loadRecommendations()
|
||||
shadowOf(Looper.getMainLooper()).idle()
|
||||
assertEquals(previous, viewModel.recommendationStateLiveData.requireValue())
|
||||
assertEquals(false, viewModel.isLoading.requireValue())
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -173,6 +256,8 @@ class HomeRecommendationViewModelTest {
|
||||
}
|
||||
|
||||
private class FakeHomeRecommendationApi : HomeRecommendationApi {
|
||||
val recommendationHeaders = mutableListOf<String?>()
|
||||
val followHeaders = mutableListOf<String>()
|
||||
private val recommendationResponses = ArrayDeque<Single<ApiResponse<HomeRecommendationResponse>>>()
|
||||
private val followResponses = ArrayDeque<Single<ApiResponse<Any>>>()
|
||||
|
||||
@@ -192,7 +277,8 @@ class HomeRecommendationViewModelTest {
|
||||
followResponses.addLast(response)
|
||||
}
|
||||
|
||||
override fun getRecommendations(authHeader: String): Single<ApiResponse<HomeRecommendationResponse>> {
|
||||
override fun getRecommendations(authHeader: String?): Single<ApiResponse<HomeRecommendationResponse>> {
|
||||
recommendationHeaders += authHeader
|
||||
return recommendationResponses.removeFirst()
|
||||
}
|
||||
|
||||
@@ -200,6 +286,7 @@ class HomeRecommendationViewModelTest {
|
||||
request: FollowRecommendedCreatorsRequest,
|
||||
authHeader: String
|
||||
): Single<ApiResponse<Any>> {
|
||||
followHeaders += authHeader
|
||||
return followResponses.removeFirst()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user