Skip to content

Commit

Permalink
[MBL-800] Update survey viewholder viewmodel to RX2 (#2083)
Browse files Browse the repository at this point in the history
* update survey viewholder viewmodel to RX2

* remove viewmodel extension for a viewholder viewmodel
  • Loading branch information
mtgriego authored Jul 31, 2024
1 parent 16435ed commit f54f905
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ import android.view.View
import com.kickstarter.R
import com.kickstarter.databinding.ActivitySurveyViewBinding
import com.kickstarter.libs.rx.transformers.Transformers
import com.kickstarter.libs.utils.extensions.addToDisposable
import com.kickstarter.models.Project
import com.kickstarter.models.SurveyResponse
import com.kickstarter.ui.IntentKey
import com.kickstarter.ui.activities.SurveyResponseActivity
import com.kickstarter.ui.extensions.loadCircleImage
import com.kickstarter.viewmodels.SurveyHolderViewModel
import io.reactivex.disposables.CompositeDisposable

class SurveyViewHolder(private val binding: ActivitySurveyViewBinding) :
KSViewHolder(binding.root) {
private val ksString = requireNotNull(environment().ksString())
private val viewModel: SurveyHolderViewModel.ViewModel = SurveyHolderViewModel.ViewModel(environment())
private val viewModel: SurveyHolderViewModel.ViewModel = SurveyHolderViewModel.ViewModel()
private val disposables = CompositeDisposable()

private fun setSurveyDescription(projectForSurveyDescription: Project) {
binding.surveyText.text = Html.fromHtml(
Expand Down Expand Up @@ -50,20 +53,25 @@ class SurveyViewHolder(private val binding: ActivitySurveyViewBinding) :

init {
viewModel.outputs.creatorAvatarImageUrl()
.compose(bindToLifecycle())
.compose(Transformers.observeForUI())
.compose(Transformers.observeForUIV2())
.subscribe { setCreatorAvatarImage(it) }
.addToDisposable(disposables)
viewModel.outputs.creatorNameTextViewText()
.compose(bindToLifecycle())
.compose(Transformers.observeForUI())
.compose(Transformers.observeForUIV2())
.subscribe { binding.surveyTitle.text = it }
.addToDisposable(disposables)
viewModel.outputs.projectForSurveyDescription()
.compose(bindToLifecycle())
.compose(Transformers.observeForUI())
.compose(Transformers.observeForUIV2())
.subscribe { setSurveyDescription(it) }
.addToDisposable(disposables)
viewModel.outputs.startSurveyResponseActivity()
.compose(bindToLifecycle())
.compose(Transformers.observeForUI())
.compose(Transformers.observeForUIV2())
.subscribe { startSurveyResponseActivity(it) }
.addToDisposable(disposables)
}

override fun destroy() {
disposables.clear()
super.destroy()
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package com.kickstarter.viewmodels

import androidx.annotation.NonNull
import com.kickstarter.libs.ActivityViewModel
import com.kickstarter.libs.Environment
import com.kickstarter.libs.rx.transformers.Transformers
import com.kickstarter.libs.utils.extensions.isNotNull
import com.kickstarter.models.Project
import com.kickstarter.models.SurveyResponse
import com.kickstarter.ui.viewholders.SurveyViewHolder
import rx.Observable
import rx.subjects.PublishSubject
import io.reactivex.Observable
import io.reactivex.subjects.PublishSubject

interface SurveyHolderViewModel {
interface Inputs {
Expand All @@ -34,12 +30,9 @@ interface SurveyHolderViewModel {
fun startSurveyResponseActivity(): Observable<SurveyResponse>
}

class ViewModel(@NonNull environment: Environment) :
ActivityViewModel<SurveyViewHolder>(environment),
Inputs,
Outputs {
class ViewModel : Inputs, Outputs {
private val surveyResponse = PublishSubject.create<SurveyResponse>()
private val surveyClicked = PublishSubject.create<Void?>()
private val surveyClicked = PublishSubject.create<Unit>()
private val creatorAvatarImageUrl: Observable<String>
private val creatorNameTextViewText: Observable<String>
private val projectForSurveyDescription: Observable<Project>
Expand All @@ -52,36 +45,36 @@ interface SurveyHolderViewModel {
creatorAvatarImageUrl = surveyResponse
.map { it.project() }
.filter { it.isNotNull() }
.map { requireNotNull(it) }
.map { it.creator().avatar().small() }

creatorNameTextViewText = surveyResponse
.map { it.project() }
.filter { it.isNotNull() }
.map { requireNotNull(it) }
.map { it.creator().name() }

projectForSurveyDescription = surveyResponse
.map { it.project() }

startSurveyResponseActivity = surveyResponse
.compose(Transformers.takeWhen(surveyClicked))
.compose(Transformers.takeWhenV2(surveyClicked))
}

override fun configureWith(surveyResponse: SurveyResponse) {
this.surveyResponse.onNext(surveyResponse)
}

override fun surveyClicked() {
surveyClicked.onNext(null)
surveyClicked.onNext(Unit)
}

override fun creatorAvatarImageUrl(): Observable<String> = creatorAvatarImageUrl

override fun creatorNameTextViewText(): Observable<String> = creatorNameTextViewText

override fun projectForSurveyDescription(): Observable<Project> = projectForSurveyDescription
override fun projectForSurveyDescription(): Observable<Project> =
projectForSurveyDescription

override fun startSurveyResponseActivity(): Observable<SurveyResponse> = startSurveyResponseActivity
override fun startSurveyResponseActivity(): Observable<SurveyResponse> =
startSurveyResponseActivity
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
package com.kickstarter.viewmodels

import com.kickstarter.KSRobolectricTestCase
import com.kickstarter.libs.Environment
import com.kickstarter.libs.utils.extensions.addToDisposable
import com.kickstarter.mock.factories.SurveyResponseFactory.surveyResponse
import com.kickstarter.models.Project
import com.kickstarter.models.SurveyResponse
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.subscribers.TestSubscriber
import org.junit.After
import org.junit.Test
import rx.observers.TestSubscriber

class SurveyHolderViewModelTest : KSRobolectricTestCase() {

private lateinit var vm: SurveyHolderViewModel.ViewModel

private val creatorAvatarImageUrl = TestSubscriber<String>()
private val creatorNameTextViewText = TestSubscriber<String>()
private val projectForSurveyDescription = TestSubscriber<Project?>()
private val projectForSurveyDescription = TestSubscriber<Project>()
private val startSurveyResponseActivity = TestSubscriber<SurveyResponse>()

private fun setUpEnvironment(environment: Environment) {
vm = SurveyHolderViewModel.ViewModel(environment)
vm.outputs.creatorAvatarImageUrl().subscribe(creatorAvatarImageUrl)
vm.outputs.creatorNameTextViewText().subscribe(creatorNameTextViewText)
vm.outputs.projectForSurveyDescription().subscribe(projectForSurveyDescription)
vm.outputs.startSurveyResponseActivity().subscribe(startSurveyResponseActivity)
private val disposable = CompositeDisposable()

private fun setUpEnvironment() {
vm = SurveyHolderViewModel.ViewModel()
vm.outputs.creatorAvatarImageUrl().subscribe { creatorAvatarImageUrl.onNext(it) }
.addToDisposable(disposable)
vm.outputs.creatorNameTextViewText().subscribe { creatorNameTextViewText.onNext(it) }
.addToDisposable(disposable)
vm.outputs.projectForSurveyDescription()
.subscribe { projectForSurveyDescription.onNext(it) }.addToDisposable(disposable)
vm.outputs.startSurveyResponseActivity()
.subscribe { startSurveyResponseActivity.onNext(it) }.addToDisposable(disposable)
}

@Test
fun testCreatorAvatarImageUrl() {
val surveyResponse = surveyResponse()
setUpEnvironment(environment())
setUpEnvironment()

vm.inputs.configureWith(surveyResponse)

Expand All @@ -38,7 +45,7 @@ class SurveyHolderViewModelTest : KSRobolectricTestCase() {
@Test
fun testCreatorNameEmits() {
val surveyResponse = surveyResponse()
setUpEnvironment(environment())
setUpEnvironment()

vm.inputs.configureWith(surveyResponse)

Expand All @@ -48,7 +55,7 @@ class SurveyHolderViewModelTest : KSRobolectricTestCase() {
@Test
fun testSurveyDescription() {
val surveyResponse = surveyResponse()
setUpEnvironment(environment())
setUpEnvironment()

vm.inputs.configureWith(surveyResponse)

Expand All @@ -59,11 +66,16 @@ class SurveyHolderViewModelTest : KSRobolectricTestCase() {
fun testStartSurveyResponseActivity() {
val surveyResponse = surveyResponse()

setUpEnvironment(environment())
setUpEnvironment()

vm.inputs.configureWith(surveyResponse)
vm.inputs.surveyClicked()

startSurveyResponseActivity.assertValue(surveyResponse)
}

@After
fun clear() {
disposable.clear()
}
}

0 comments on commit f54f905

Please sign in to comment.