Skip to content

Commit

Permalink
Fix edited shoutout not visible (#307)
Browse files Browse the repository at this point in the history
Optimize and refetch membership status on update customization
  • Loading branch information
markocic authored Feb 5, 2025
1 parent 314ae5c commit a8ff430
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ interface LegendaryProfileCustomizationContract {
val avatarCdnImage: CdnImage? = null,
val membership: PremiumMembership? = null,
val avatarLegendaryCustomization: LegendaryCustomization = LegendaryCustomization(),
val applyingChanges: Boolean = false,
) {
fun computeShoutout() = membership?.editedShoutout ?: avatarLegendaryCustomization.currentShoutout ?: ""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@ import net.primal.android.premium.legend.customization.LegendaryProfileCustomiza
import net.primal.android.premium.legend.domain.LegendaryCustomization
import net.primal.android.premium.legend.domain.asLegendaryCustomization
import net.primal.android.premium.repository.PremiumRepository
import net.primal.android.profile.repository.ProfileRepository
import net.primal.android.user.accounts.active.ActiveAccountStore
import net.primal.android.user.repository.UserRepository
import timber.log.Timber

@HiltViewModel
class LegendaryProfileCustomizationViewModel @Inject constructor(
private val activeAccountStore: ActiveAccountStore,
private val profileRepository: ProfileRepository,
private val premiumRepository: PremiumRepository,
private val userRepository: UserRepository,
) : ViewModel() {
Expand All @@ -37,10 +35,9 @@ class LegendaryProfileCustomizationViewModel @Inject constructor(
fun setEvent(event: UiEvent) = viewModelScope.launch { events.emit(event) }

init {
requestUpdateActiveAccount()
observeActiveAccount()
observeProfile()
observeEvents()
requestProfileUpdate()
}

private fun observeEvents() {
Expand All @@ -55,7 +52,6 @@ class LegendaryProfileCustomizationViewModel @Inject constructor(

private fun applyCustomization(event: UiEvent.ApplyCustomization) {
viewModelScope.launch {
setState { copy(applyingChanges = true) }
event.optimisticallyUpdateCustomization()

try {
Expand All @@ -72,8 +68,7 @@ class LegendaryProfileCustomizationViewModel @Inject constructor(
} catch (error: WssException) {
Timber.e(error)
} finally {
runCatching { userRepository.fetchAndUpdateUserAccount(userId = activeAccountStore.activeUserId()) }
setState { copy(applyingChanges = false) }
requestUpdateActiveAccount()
}
}
}
Expand All @@ -85,29 +80,20 @@ class LegendaryProfileCustomizationViewModel @Inject constructor(
copy(
avatarCdnImage = it.avatarCdnImage,
membership = it.premiumMembership,
avatarLegendaryCustomization = it.primalLegendProfile?.asLegendaryCustomization()
?: LegendaryCustomization(),
)
}
}
}

private fun observeProfile() {
private fun requestUpdateActiveAccount() =
viewModelScope.launch {
profileRepository.observeProfile(profileId = activeAccountStore.activeUserId()).collect {
setState {
copy(
avatarLegendaryCustomization = it.metadata?.primalPremiumInfo
?.legendProfile?.asLegendaryCustomization() ?: LegendaryCustomization(),
)
}
runCatching {
userRepository.fetchAndUpdateUserAccount(userId = activeAccountStore.activeUserId())
premiumRepository.fetchMembershipStatus(userId = activeAccountStore.activeUserId())
}
}
}

private fun requestProfileUpdate() {
viewModelScope.launch {
profileRepository.requestProfileUpdate(profileId = activeAccountStore.activeUserId())
}
}

private fun UiEvent.ApplyCustomization.optimisticallyUpdateCustomization() {
val data = this
Expand Down

0 comments on commit a8ff430

Please sign in to comment.