fix: simplify hasPremium with first(), leave couple before deleting account
This commit is contained in:
parent
dfd9974683
commit
2521e91db4
|
|
@ -10,15 +10,13 @@ import com.revenuecat.purchases.EntitlementInfo
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.SupervisorJob
|
import kotlinx.coroutines.SupervisorJob
|
||||||
import kotlinx.coroutines.cancel
|
|
||||||
import kotlinx.coroutines.channels.awaitClose
|
import kotlinx.coroutines.channels.awaitClose
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.callbackFlow
|
import kotlinx.coroutines.flow.callbackFlow
|
||||||
import kotlinx.coroutines.flow.combine
|
|
||||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||||
|
import kotlinx.coroutines.flow.first
|
||||||
import kotlinx.coroutines.flow.flatMapLatest
|
import kotlinx.coroutines.flow.flatMapLatest
|
||||||
import kotlinx.coroutines.flow.flowOf
|
import kotlinx.coroutines.flow.flowOf
|
||||||
import kotlinx.coroutines.flow.map
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
|
@ -64,17 +62,7 @@ class FirestoreEntitlementChecker @Inject constructor(
|
||||||
}
|
}
|
||||||
.distinctUntilChanged()
|
.distinctUntilChanged()
|
||||||
|
|
||||||
override suspend fun hasPremium(): Boolean = isPremium()
|
override suspend fun hasPremium(): Boolean = isPremium().first()
|
||||||
.map { it }
|
|
||||||
.let { flow ->
|
|
||||||
// Safe one-shot collector; this is called from non-flow callers.
|
|
||||||
var value = false
|
|
||||||
val job = CoroutineScope(Dispatchers.IO).launch {
|
|
||||||
flow.collect { value = it }
|
|
||||||
}
|
|
||||||
job.cancel()
|
|
||||||
value
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onCustomerInfoUpdated(customerInfo: CustomerInfo) {
|
override fun onCustomerInfoUpdated(customerInfo: CustomerInfo) {
|
||||||
revenueCatFallback = customerInfo.entitlements.active
|
revenueCatFallback = customerInfo.entitlements.active
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import app.closer.core.navigation.AppRoute
|
import app.closer.core.navigation.AppRoute
|
||||||
import app.closer.domain.repository.AuthRepository
|
import app.closer.domain.repository.AuthRepository
|
||||||
|
import app.closer.domain.repository.CoupleRepository
|
||||||
import app.closer.domain.repository.UserRepository
|
import app.closer.domain.repository.UserRepository
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
@ -68,6 +69,7 @@ data class DeleteAccountUiState(
|
||||||
@HiltViewModel
|
@HiltViewModel
|
||||||
class DeleteAccountViewModel @Inject constructor(
|
class DeleteAccountViewModel @Inject constructor(
|
||||||
private val authRepository: AuthRepository,
|
private val authRepository: AuthRepository,
|
||||||
|
private val coupleRepository: CoupleRepository,
|
||||||
private val userRepository: UserRepository
|
private val userRepository: UserRepository
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
|
|
||||||
|
|
@ -84,6 +86,9 @@ class DeleteAccountViewModel @Inject constructor(
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
_uiState.update { it.copy(showConfirm = false, isDeleting = true, error = null) }
|
_uiState.update { it.copy(showConfirm = false, isDeleting = true, error = null) }
|
||||||
if (uid != null) {
|
if (uid != null) {
|
||||||
|
// Leave couple first so the partner is notified and the couple doc
|
||||||
|
// is cleaned up before the auth account disappears.
|
||||||
|
runCatching { coupleRepository.leaveCouple(uid) }
|
||||||
runCatching { userRepository.deleteUserData(uid) }
|
runCatching { userRepository.deleteUserData(uid) }
|
||||||
}
|
}
|
||||||
authRepository.deleteAccount()
|
authRepository.deleteAccount()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue