Skip to content

Commit

Permalink
Implement legend support in drawer (side menu)
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandarIlic committed Nov 19, 2024
1 parent 7cacb0b commit 987bd91
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
24 changes: 21 additions & 3 deletions app/src/main/kotlin/net/primal/android/drawer/PrimalDrawer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.luminance
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.AnnotatedString
Expand All @@ -43,14 +45,15 @@ import androidx.hilt.navigation.compose.hiltViewModel
import java.text.NumberFormat
import kotlinx.coroutines.launch
import net.primal.android.R
import net.primal.android.core.compose.AvatarThumbnail
import net.primal.android.core.compose.AvatarThumbnailCustomBorder
import net.primal.android.core.compose.NostrUserText
import net.primal.android.core.compose.icons.PrimalIcons
import net.primal.android.core.compose.icons.primaliconpack.DarkMode
import net.primal.android.core.compose.icons.primaliconpack.LightMode
import net.primal.android.core.compose.icons.primaliconpack.QrCode
import net.primal.android.core.compose.preview.PrimalPreview
import net.primal.android.core.utils.formatNip05Identifier
import net.primal.android.premium.legend.LegendaryProfile
import net.primal.android.theme.AppTheme
import net.primal.android.theme.domain.PrimalTheme
import net.primal.android.user.domain.Badges
Expand Down Expand Up @@ -107,6 +110,9 @@ fun PrimalDrawer(
) {
DrawerHeader(
userAccount = state.activeUserAccount,
customBadge = state.customBadge,
avatarGlow = state.avatarGlow,
legendaryStyle = state.legendaryStyle,
onQrCodeClick = onQrCodeClick,
)

Expand Down Expand Up @@ -134,21 +140,32 @@ fun PrimalDrawer(
}

@Composable
private fun DrawerHeader(userAccount: UserAccount?, onQrCodeClick: () -> Unit) {
private fun DrawerHeader(
userAccount: UserAccount?,
customBadge: Boolean,
avatarGlow: Boolean,
legendaryStyle: LegendaryProfile?,
onQrCodeClick: () -> Unit,
) {
val numberFormat = remember { NumberFormat.getNumberInstance() }
ConstraintLayout(
modifier = Modifier.fillMaxWidth(),
) {
val startGuideline = createGuidelineFromStart(24.dp)
val (avatarRef, usernameRef, iconRef, identifierRef, statsRef) = createRefs()

AvatarThumbnail(
AvatarThumbnailCustomBorder(
modifier = Modifier.constrainAs(avatarRef) {
start.linkTo(startGuideline)
top.linkTo(parent.top, margin = 16.dp)
},
avatarSize = 52.dp,
avatarCdnImage = userAccount?.avatarCdnImage,
hasBorder = avatarGlow && legendaryStyle != null,
borderBrush = when {
legendaryStyle != null -> legendaryStyle.brush
else -> Brush.linearGradient(listOf(Color.Transparent, Color.Transparent))
},
)

NostrUserText(
Expand All @@ -160,6 +177,7 @@ private fun DrawerHeader(userAccount: UserAccount?, onQrCodeClick: () -> Unit) {
top.linkTo(avatarRef.bottom, margin = 16.dp)
width = Dimension.preferredValue(220.dp)
},
customBadge = if (customBadge) legendaryStyle else null,
)

IconButton(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.primal.android.drawer

import net.primal.android.premium.legend.LegendaryProfile
import net.primal.android.user.domain.Badges
import net.primal.android.user.domain.UserAccount

Expand All @@ -11,6 +12,9 @@ interface PrimalDrawerContract {
val activeUserAccount: UserAccount? = null,
val badges: Badges = Badges(),
val showPremiumBadge: Boolean = false,
val customBadge: Boolean = false,
val avatarGlow: Boolean = false,
val legendaryStyle: LegendaryProfile? = null,
)

sealed class UiEvent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.getAndUpdate
import kotlinx.coroutines.launch
import net.primal.android.premium.legend.LegendaryProfile
import net.primal.android.profile.repository.ProfileRepository
import net.primal.android.theme.active.ActiveThemeStore
import net.primal.android.theme.domain.PrimalTheme
import net.primal.android.user.accounts.active.ActiveAccountStore
Expand All @@ -24,6 +26,7 @@ class PrimalDrawerViewModel @Inject constructor(
private val activeAccountStore: ActiveAccountStore,
private val activeThemeStore: ActiveThemeStore,
private val subscriptionsManager: SubscriptionsManager,
private val profileRepository: ProfileRepository,
) : ViewModel() {

private val _state = MutableStateFlow(
Expand All @@ -45,6 +48,7 @@ class PrimalDrawerViewModel @Inject constructor(
init {
subscribeToEvents()
observeActiveAccount()
observeProfile()
subscribeToBadgesUpdates()
}

Expand Down Expand Up @@ -73,6 +77,20 @@ class PrimalDrawerViewModel @Inject constructor(
}
}

private fun observeProfile() {
viewModelScope.launch {
profileRepository.observeProfile(profileId = activeAccountStore.activeUserId()).collect {
setState {
copy(
avatarGlow = it.metadata?.primalLegendProfile?.avatarGlow == true,
customBadge = it.metadata?.primalLegendProfile?.customBadge == true,
legendaryStyle = LegendaryProfile.valueById(it.metadata?.primalLegendProfile?.styleId),
)
}
}
}
}

private fun UserAccount.hasNotSeenPremiumInTheLast(duration: Duration): Boolean {
val lastTimestamp = this.lastBuyPremiumTimestampInMillis ?: 0
return lastTimestamp < Instant.now().minusSeconds(duration.inWholeSeconds).epochSecond
Expand Down

0 comments on commit 987bd91

Please sign in to comment.