protoncore_android/plan/presentation/src/main/kotlin/me/proton/core/plan/presentation/viewmodel/SignupPlansViewModel.kt

63 lines
2.4 KiB
Kotlin

/*
* Copyright (c) 2021 Proton Technologies AG
* This file is part of Proton Technologies AG and ProtonCore.
*
* ProtonCore is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ProtonCore is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ProtonCore. If not, see <https://www.gnu.org/licenses/>.
*/
package me.proton.core.plan.presentation.viewmodel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import me.proton.core.payment.presentation.PaymentsOrchestrator
import me.proton.core.plan.domain.SupportedSignupPaidPlans
import me.proton.core.plan.domain.usecase.GetPlans
import me.proton.core.plan.presentation.entity.PlanDetailsListItem
import javax.inject.Inject
@HiltViewModel
internal class SignupPlansViewModel @Inject constructor(
private val getPlans: GetPlans,
@SupportedSignupPaidPlans val supportedPaidPlanNames: List<String>,
paymentsOrchestrator: PaymentsOrchestrator
) : BasePlansViewModel(paymentsOrchestrator) {
fun getAllPlansForSignup() = flow {
emit(PlanState.Processing)
val plans: MutableList<PlanDetailsListItem> = mutableListOf()
plans.apply {
addAll(getPlans(supportedPaidPlans = supportedPaidPlanNames.map { it }, userId = null)
.map {
it.toPaidPlanDetailsItem(
subscribedPlans = null,
upgrade = false
)
}
)
add(createFreePlan(currentlySubscribed = false, selectable = true))
}
emit(PlanState.Success.Plans(plans = plans))
}.catch { error ->
_availablePlansState.tryEmit(PlanState.Error.Message(error.message))
}.onEach { plans ->
_availablePlansState.tryEmit(plans)
}.launchIn(viewModelScope)
}