Skip to content

Commit fecb37e

Browse files
committed
Tweaking state to remove labels from VM state to help with all the screen variations we have
1 parent 3fdb855 commit fecb37e

File tree

5 files changed

+70
-42
lines changed

5 files changed

+70
-42
lines changed
Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,16 @@ import kotlinx.coroutines.launch
3636
import network.loki.messenger.R
3737
import org.session.libsession.utilities.NonTranslatableStringConstants
3838
import org.session.libsession.utilities.StringSubstitutionConstants.APP_PRO_KEY
39+
import org.session.libsession.utilities.StringSubstitutionConstants.CURRENT_PLAN_KEY
40+
import org.session.libsession.utilities.StringSubstitutionConstants.DATE_KEY
3941
import org.session.libsession.utilities.StringSubstitutionConstants.ICON_KEY
4042
import org.session.libsession.utilities.StringSubstitutionConstants.MONTHLY_PRICE_KEY
4143
import org.session.libsession.utilities.StringSubstitutionConstants.PRICE_KEY
44+
import org.session.libsession.utilities.StringSubstitutionConstants.PRO_KEY
4245
import org.thoughtcrime.securesms.preferences.prosettings.ProSettingsViewModel.ProPlan
4346
import org.thoughtcrime.securesms.preferences.prosettings.ProSettingsViewModel.ProPlanBadge
4447
import org.thoughtcrime.securesms.preferences.prosettings.ProSettingsViewModel.Commands.*
48+
import org.thoughtcrime.securesms.pro.SubscriptionState
4549
import org.thoughtcrime.securesms.pro.subscription.ProSubscriptionDuration
4650
import org.thoughtcrime.securesms.ui.SpeechBubbleTooltip
4751
import org.thoughtcrime.securesms.ui.components.AccentFillButtonRect
@@ -62,13 +66,13 @@ import org.thoughtcrime.securesms.ui.theme.bold
6266

6367
@OptIn(ExperimentalSharedTransitionApi::class)
6468
@Composable
65-
fun UpdatePlanScreen(
69+
fun ChoosePlanScreen(
6670
viewModel: ProSettingsViewModel,
6771
onBack: () -> Unit,
6872
) {
69-
val planData by viewModel.proPlanUIState.collectAsState()
73+
val planData by viewModel.choose.collectAsState()
7074

71-
UpdatePlan(
75+
ChoosePlan(
7276
planData = planData,
7377
sendCommand = viewModel::onCommand,
7478
onBack = onBack,
@@ -77,8 +81,8 @@ fun UpdatePlanScreen(
7781

7882
@OptIn(ExperimentalMaterial3Api::class, ExperimentalSharedTransitionApi::class)
7983
@Composable
80-
fun UpdatePlan(
81-
planData: ProSettingsViewModel.ProPlanUIState,
84+
fun ChoosePlan(
85+
planData: ProSettingsViewModel.ChoosePlanState,
8286
sendCommand: (ProSettingsViewModel.Commands) -> Unit,
8387
onBack: () -> Unit,
8488
) {
@@ -93,9 +97,30 @@ fun UpdatePlan(
9397

9498
Spacer(Modifier.height(LocalDimensions.current.spacing))
9599

100+
val context = LocalContext.current
101+
val title = when(planData.subscriptionState) {
102+
is SubscriptionState.Expired ->
103+
Phrase.from(context.getText(R.string.proPlanRenewStart))
104+
.put(APP_PRO_KEY, NonTranslatableStringConstants.APP_PRO)
105+
.put(APP_PRO_KEY, NonTranslatableStringConstants.APP_PRO)
106+
.format()
107+
108+
is SubscriptionState.Active.Expiring -> Phrase.from(context.getText(R.string.proPlanActivatedNotAuto))
109+
.put(APP_PRO_KEY, NonTranslatableStringConstants.APP_PRO)
110+
.put(DATE_KEY, "May 21st, 2025") //todo PRO implement properly
111+
.format()
112+
113+
else -> Phrase.from(context.getText(R.string.proPlanActivatedAuto))
114+
.put(APP_PRO_KEY, NonTranslatableStringConstants.APP_PRO)
115+
.put(CURRENT_PLAN_KEY, "3 months") //todo PRO implement properly
116+
.put(DATE_KEY, "May 21st, 2025") //todo PRO implement properly
117+
.put(PRO_KEY, NonTranslatableStringConstants.PRO)
118+
.format()
119+
}
120+
96121
Text(
97122
modifier = Modifier.fillMaxWidth(),
98-
text = annotatedStringResource(planData.title),
123+
text = annotatedStringResource(title),
99124
textAlign = TextAlign.Center,
100125
style = LocalType.current.base,
101126
color = LocalColors.current.text,
@@ -123,10 +148,16 @@ fun UpdatePlan(
123148

124149
Spacer(Modifier.height(LocalDimensions.current.contentSpacing))
125150

151+
val buttonLabel = when(planData.subscriptionState) {
152+
is SubscriptionState.Expired -> context.getString(R.string.renew)
153+
is SubscriptionState.Active.Expiring -> context.getString(R.string.updatePlan)
154+
else -> context.getString(R.string.updatePlan)
155+
}
156+
126157
AccentFillButtonRect(
127158
modifier = Modifier.fillMaxWidth()
128159
.widthIn(max = LocalDimensions.current.maxContentWidth),
129-
text = planData.buttonLabel,
160+
text = buttonLabel,
130161
enabled = planData.enableButton,
131162
onClick = {
132163
sendCommand(GetProPlan)
@@ -377,9 +408,8 @@ private fun PreviewUpdatePlan(
377408
) {
378409
PreviewTheme(colors) {
379410
val context = LocalContext.current
380-
UpdatePlan(
381-
planData = ProSettingsViewModel.ProPlanUIState(
382-
title = "This is a title",
411+
ChoosePlan(
412+
planData = ProSettingsViewModel.ChoosePlanState(
383413
enableButton = true,
384414
plans = listOf(
385415
ProPlan(

app/src/main/java/org/thoughtcrime/securesms/preferences/prosettings/PlanConfirmationScreen.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ fun PlanConfirmationScreen(
9696
@OptIn(ExperimentalMaterial3Api::class, ExperimentalSharedTransitionApi::class)
9797
@Composable
9898
fun PlanConfirmation(
99-
proData: ProSettingsViewModel.ProSettingsUIState,
99+
proData: ProSettingsViewModel.ProSettingsState,
100100
sendCommand: (ProSettingsViewModel.Commands) -> Unit,
101101
onBack: () -> Unit,
102102
) {
@@ -172,13 +172,14 @@ private fun PreviewPlanConfirmation(
172172
) {
173173
PreviewTheme(colors) {
174174
PlanConfirmation(
175-
proData = ProSettingsViewModel.ProSettingsUIState(
175+
proData = ProSettingsViewModel.ProSettingsState(
176176
subscriptionState = SubscriptionState.Active.AutoRenewing(
177177
proStatus = ProStatus.Pro(
178178
visible = true,
179179
validUntil = Instant.now() + Duration.ofDays(14),
180180
),
181-
type = ProSubscriptionDuration.THREE_MONTHS
181+
type = ProSubscriptionDuration.THREE_MONTHS,
182+
nonOriginatingSubscription = null
182183
),
183184
),
184185
sendCommand = {},

app/src/main/java/org/thoughtcrime/securesms/preferences/prosettings/ProSettingsHomeScreen.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ fun ProSettingsHomeScreen(
101101
@OptIn(ExperimentalMaterial3Api::class, ExperimentalSharedTransitionApi::class)
102102
@Composable
103103
fun ProSettingsHome(
104-
data: ProSettingsViewModel.ProSettingsUIState,
104+
data: ProSettingsViewModel.ProSettingsState,
105105
sendCommand: (ProSettingsViewModel.Commands) -> Unit,
106106
onBack: () -> Unit,
107107
) {
@@ -640,13 +640,14 @@ fun PreviewProSettingsPro(
640640
) {
641641
PreviewTheme(colors) {
642642
ProSettingsHome(
643-
data = ProSettingsViewModel.ProSettingsUIState(
643+
data = ProSettingsViewModel.ProSettingsState(
644644
subscriptionState = SubscriptionState.Active.AutoRenewing(
645645
proStatus = ProStatus.Pro(
646646
visible = true,
647647
validUntil = Instant.now() + Duration.ofDays(14),
648648
),
649-
type = ProSubscriptionDuration.THREE_MONTHS
649+
type = ProSubscriptionDuration.THREE_MONTHS,
650+
nonOriginatingSubscription = null
650651
),
651652
),
652653
sendCommand = {},
@@ -662,7 +663,7 @@ fun PreviewProSettingsExpired(
662663
) {
663664
PreviewTheme(colors) {
664665
ProSettingsHome(
665-
data = ProSettingsViewModel.ProSettingsUIState(
666+
data = ProSettingsViewModel.ProSettingsState(
666667
subscriptionState = SubscriptionState.Expired,
667668
),
668669
sendCommand = {},
@@ -678,7 +679,7 @@ fun PreviewProSettingsNonPro(
678679
) {
679680
PreviewTheme(colors) {
680681
ProSettingsHome(
681-
data = ProSettingsViewModel.ProSettingsUIState(
682+
data = ProSettingsViewModel.ProSettingsState(
682683
subscriptionState = SubscriptionState.NeverSubscribed,
683684
),
684685
sendCommand = {},

app/src/main/java/org/thoughtcrime/securesms/preferences/prosettings/ProSettingsNavHost.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ sealed interface ProSettingsDestination {
2424
data object Home: ProSettingsDestination
2525

2626
@Serializable
27-
data object UpdatePlan: ProSettingsDestination
27+
data object ChoosePlan: ProSettingsDestination
2828

2929
@Serializable
3030
data object PlanConfirmation: ProSettingsDestination
@@ -74,8 +74,8 @@ fun ProSettingsNavHost(
7474
}
7575

7676
// Subscription plan selection
77-
horizontalSlideComposable<UpdatePlan> {
78-
UpdatePlanScreen(
77+
horizontalSlideComposable<ChoosePlan> {
78+
ChoosePlanScreen(
7979
viewModel = viewModel,
8080
onBack = { scope.launch { navigator.navigateUp() }},
8181
)

app/src/main/java/org/thoughtcrime/securesms/preferences/prosettings/ProSettingsViewModel.kt

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import android.content.Context
44
import android.icu.util.MeasureUnit
55
import androidx.lifecycle.ViewModel
66
import androidx.lifecycle.viewModelScope
7-
import androidx.media3.common.Label
87
import com.squareup.phrase.Phrase
98
import dagger.hilt.android.lifecycle.HiltViewModel
109
import dagger.hilt.android.qualifiers.ApplicationContext
@@ -46,14 +45,14 @@ class ProSettingsViewModel @Inject constructor(
4645
private val dateUtils: DateUtils
4746
) : ViewModel() {
4847

49-
private val _proSettingsUIState: MutableStateFlow<ProSettingsUIState> = MutableStateFlow(ProSettingsUIState())
50-
val proSettingsUIState: StateFlow<ProSettingsUIState> = _proSettingsUIState
48+
private val _proSettingsUIState: MutableStateFlow<ProSettingsState> = MutableStateFlow(ProSettingsState())
49+
val proSettingsUIState: StateFlow<ProSettingsState> = _proSettingsUIState
5150

5251
private val _dialogState: MutableStateFlow<DialogsState> = MutableStateFlow(DialogsState())
5352
val dialogState: StateFlow<DialogsState> = _dialogState
5453

55-
private val _proPlanUIState: MutableStateFlow<ProPlanUIState> = MutableStateFlow(ProPlanUIState())
56-
val proPlanUIState: StateFlow<ProPlanUIState> = _proPlanUIState
54+
private val _choosePlanState: MutableStateFlow<ChoosePlanState> = MutableStateFlow(ChoosePlanState())
55+
val choose: StateFlow<ChoosePlanState> = _choosePlanState
5756

5857
private val proSettingsDateFormat = "MMMM d, yyyy"
5958

@@ -66,7 +65,7 @@ class ProSettingsViewModel @Inject constructor(
6665
val subscriptionState = proStatusManager.getCurrentSubscriptionState()
6766

6867
_proSettingsUIState.update {
69-
ProSettingsUIState(
68+
ProSettingsState(
7069
subscriptionState = if(proStatusManager.isCurrentUserPro())
7170
subscriptionState
7271
else SubscriptionState.NeverSubscribed,
@@ -98,7 +97,7 @@ class ProSettingsViewModel @Inject constructor(
9897
)
9998
}
10099

101-
_proPlanUIState.update {
100+
_choosePlanState.update {
102101
// sort out the title and button label for the plan screen based on subscription status
103102
val (title, buttonLabel) = when(subscriptionState) {
104103
is SubscriptionState.Expired ->
@@ -127,9 +126,7 @@ class ProSettingsViewModel @Inject constructor(
127126
val currentPlan3Months = isActive && subscriptionState.type == ProSubscriptionDuration.THREE_MONTHS
128127
val currentPlan1Month = isActive && subscriptionState.type == ProSubscriptionDuration.ONE_MONTH
129128

130-
ProPlanUIState(
131-
title = title,
132-
buttonLabel = buttonLabel,
129+
ChoosePlanState(
133130
enableButton = subscriptionState !is SubscriptionState.Active.AutoRenewing, // only the auto-renew can have a disabled state
134131
plans = listOf(
135132
ProPlan(
@@ -219,15 +216,15 @@ class ProSettingsViewModel @Inject constructor(
219216
}
220217

221218
Commands.ShowPlanUpdate -> {
222-
navigateTo(ProSettingsDestination.UpdatePlan)
219+
navigateTo(ProSettingsDestination.ChoosePlan)
223220
}
224221

225222
is Commands.SetShowProBadge -> {
226223
//todo PRO implement
227224
}
228225

229226
is Commands.SelectProPlan -> {
230-
_proPlanUIState.update { data ->
227+
_choosePlanState.update { data ->
231228
data.copy(
232229
plans = data.plans.map {
233230
it.copy(selected = it == command.plan)
@@ -316,7 +313,7 @@ class ProSettingsViewModel @Inject constructor(
316313
}
317314

318315
private fun getSelectedPlan(): ProPlan {
319-
return _proPlanUIState.value.plans.first { it.selected }
316+
return _choosePlanState.value.plans.first { it.selected }
320317
}
321318

322319
private fun getPlanFromProvider(){
@@ -345,27 +342,26 @@ class ProSettingsViewModel @Inject constructor(
345342
data object ConfirmProPlan: Commands
346343
}
347344

348-
data class ProSettingsUIState(
345+
data class ProSettingsState(
349346
val subscriptionState: SubscriptionState = SubscriptionState.NeverSubscribed,
350347
val proStats: ProStats = ProStats(),
351348
val subscriptionExpiryLabel: CharSequence = "", // eg: "Pro auto renewing in 3 days"
352349
val subscriptionExpiryDate: CharSequence = "" // eg: "May 21st, 2025"
353350
)
354351

352+
data class ChoosePlanState(
353+
val subscriptionState: SubscriptionState = SubscriptionState.NeverSubscribed,
354+
val plans: List<ProPlan> = emptyList(),
355+
val enableButton: Boolean = false,
356+
)
357+
355358
data class ProStats(
356359
val groupsUpdated: Int = 0,
357360
val pinnedConversations: Int = 0,
358361
val proBadges: Int = 0,
359362
val longMessages: Int = 0
360363
)
361364

362-
data class ProPlanUIState(
363-
val plans: List<ProPlan> = emptyList(),
364-
val enableButton: Boolean = false,
365-
val title: CharSequence = "",
366-
val buttonLabel: String = "",
367-
)
368-
369365
data class ProPlan(
370366
val title: String,
371367
val subtitle: String,

0 commit comments

Comments
 (0)