-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
177 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...main/java/au/com/shiftyjelly/pocketcasts/account/viewmodel/OnboardingActivityViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package au.com.shiftyjelly.pocketcasts.account.viewmodel | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import au.com.shiftyjelly.pocketcasts.account.onboarding.OnboardingActivityContract.OnboardingFinish | ||
import au.com.shiftyjelly.pocketcasts.models.to.SignInState | ||
import au.com.shiftyjelly.pocketcasts.models.to.SubscriptionStatus | ||
import au.com.shiftyjelly.pocketcasts.repositories.user.UserManager | ||
import au.com.shiftyjelly.pocketcasts.settings.onboarding.OnboardingExitInfo | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asSharedFlow | ||
import kotlinx.coroutines.flow.combine | ||
import kotlinx.coroutines.flow.stateIn | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.reactive.asFlow | ||
|
||
@HiltViewModel | ||
class OnboardingActivityViewModel @Inject constructor( | ||
private val userManager: UserManager, | ||
) : ViewModel() { | ||
|
||
private var showPlusPromotionForFreeUserFlow = MutableStateFlow(false) | ||
|
||
private val _finishState = MutableSharedFlow<OnboardingFinish>() | ||
val finishState = _finishState.asSharedFlow() | ||
|
||
init { | ||
viewModelScope.launch { | ||
combine( | ||
showPlusPromotionForFreeUserFlow, | ||
userManager.getSignInState().asFlow(), | ||
) { showPlusPromotionForFreeUser, signInState -> | ||
if (showPlusPromotionForFreeUser) { | ||
// subscriptionStatus is null just after sign in, so we need to wait for it to be set | ||
// before we can finish the onboarding flow to show plus promotion for a free user | ||
(signInState as? SignInState.SignedIn)?.subscriptionStatus?.let { status -> | ||
showPlusPromotionForFreeUserFlow.value = false | ||
if (status is SubscriptionStatus.Free) { | ||
_finishState.emit(OnboardingFinish.DoneShowPlusPromotion) | ||
} else { | ||
_finishState.emit(OnboardingFinish.Done) | ||
} | ||
} | ||
} | ||
}.stateIn(viewModelScope) | ||
} | ||
} | ||
|
||
fun onExitOnboarding(exitInfo: OnboardingExitInfo) { | ||
if (exitInfo.showPlusPromotionForFreeUser) { | ||
showPlusPromotionForFreeUserFlow.value = true | ||
} else { | ||
viewModelScope.launch { | ||
_finishState.emit(OnboardingFinish.Done) | ||
} | ||
} | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
.../java/au/com/shiftyjelly/pocketcasts/account/viewmodel/OnboardingActivityViewModelTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package au.com.shiftyjelly.pocketcasts.account.viewmodel | ||
|
||
import app.cash.turbine.test | ||
import au.com.shiftyjelly.pocketcasts.account.onboarding.OnboardingActivityContract.OnboardingFinish | ||
import au.com.shiftyjelly.pocketcasts.models.to.SignInState | ||
import au.com.shiftyjelly.pocketcasts.models.to.SubscriptionStatus | ||
import au.com.shiftyjelly.pocketcasts.repositories.user.UserManager | ||
import au.com.shiftyjelly.pocketcasts.settings.onboarding.OnboardingExitInfo | ||
import au.com.shiftyjelly.pocketcasts.sharedtest.MainCoroutineRule | ||
import io.reactivex.Flowable | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.Mock | ||
import org.mockito.junit.MockitoJUnitRunner | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.whenever | ||
|
||
@RunWith(MockitoJUnitRunner::class) | ||
class OnboardingActivityViewModelTest { | ||
@OptIn(ExperimentalCoroutinesApi::class) | ||
@get:Rule | ||
val coroutineRule = MainCoroutineRule() | ||
|
||
@Mock | ||
private lateinit var userManager: UserManager | ||
|
||
@Mock | ||
private lateinit var paidSubscriptionStatus: SubscriptionStatus.Paid | ||
|
||
@Mock | ||
private lateinit var freeSubscriptionStatus: SubscriptionStatus.Free | ||
|
||
private lateinit var viewModel: OnboardingActivityViewModel | ||
|
||
@Test | ||
fun `given showPlusPromotionForFreeUser is false, when exit onboarding, then finish with Done`() = runTest { | ||
initViewModel() | ||
|
||
viewModel.finishState.test { | ||
viewModel.onExitOnboarding(OnboardingExitInfo(showPlusPromotionForFreeUser = false)) | ||
assert(awaitItem() == OnboardingFinish.Done) | ||
} | ||
} | ||
|
||
@Test | ||
fun `given showPlusPromotionForFreeUser is true and free user, when exit onboarding, then finish with DoneShowPlusPromotion`() = runTest { | ||
initViewModel(freeSubscriptionStatus) | ||
|
||
viewModel.finishState.test { | ||
viewModel.onExitOnboarding(OnboardingExitInfo(showPlusPromotionForFreeUser = true)) | ||
assert(awaitItem() == OnboardingFinish.DoneShowPlusPromotion) | ||
} | ||
} | ||
|
||
@Test | ||
fun `given showPlusPromotionForFreeUser is true and paid user, when exit onboarding, then finish with Done`() = runTest { | ||
initViewModel(paidSubscriptionStatus) | ||
|
||
viewModel.finishState.test { | ||
viewModel.onExitOnboarding(OnboardingExitInfo(showPlusPromotionForFreeUser = true)) | ||
assert(awaitItem() == OnboardingFinish.Done) | ||
} | ||
} | ||
|
||
private fun initViewModel(subscriptionStatus: SubscriptionStatus = mock<SubscriptionStatus>()) { | ||
whenever(userManager.getSignInState()).thenReturn( | ||
Flowable.just( | ||
SignInState.SignedIn(email = "", subscriptionStatus = subscriptionStatus), | ||
), | ||
) | ||
viewModel = OnboardingActivityViewModel( | ||
userManager = userManager, | ||
) | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...gs/src/main/java/au/com/shiftyjelly/pocketcasts/settings/onboarding/OnboardingExitInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package au.com.shiftyjelly.pocketcasts.settings.onboarding | ||
|
||
data class OnboardingExitInfo( | ||
val showPlusPromotionForFreeUser: Boolean = false, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters