1
1
package org.hyperskill.app.step_quiz.presentation
2
2
3
+ import kotlinx.coroutines.flow.distinctUntilChanged
4
+ import kotlinx.coroutines.flow.launchIn
5
+ import kotlinx.coroutines.flow.map
6
+ import kotlinx.coroutines.flow.onEach
3
7
import org.hyperskill.app.SharedResources
4
8
import org.hyperskill.app.analytic.domain.interactor.AnalyticInteractor
5
9
import org.hyperskill.app.core.domain.platform.Platform
@@ -30,6 +34,7 @@ import org.hyperskill.app.subscriptions.domain.interactor.SubscriptionsInteracto
30
34
import org.hyperskill.app.subscriptions.domain.model.Subscription
31
35
import org.hyperskill.app.subscriptions.domain.model.isFreemium
32
36
import org.hyperskill.app.subscriptions.domain.model.isProblemsLimitReached
37
+ import org.hyperskill.app.subscriptions.domain.repository.CurrentSubscriptionStateRepository
33
38
import ru.nobird.app.presentation.redux.dispatcher.CoroutineActionDispatcher
34
39
35
40
internal class StepQuizActionDispatcher (
@@ -43,8 +48,23 @@ internal class StepQuizActionDispatcher(
43
48
private val sentryInteractor : SentryInteractor ,
44
49
private val onboardingInteractor : OnboardingInteractor ,
45
50
private val resourceProvider : ResourceProvider ,
46
- private val platform : Platform
51
+ private val platform : Platform ,
52
+ currentSubscriptionStateRepository : CurrentSubscriptionStateRepository
47
53
) : CoroutineActionDispatcher<Action, Message>(config.createConfig()) {
54
+
55
+ init {
56
+ currentSubscriptionStateRepository
57
+ .changes
58
+ .map { it.isProblemsLimitReached }
59
+ .distinctUntilChanged()
60
+ .onEach { isProblemsLimitReached ->
61
+ onNewMessage(
62
+ InternalMessage .ProblemsLimitChanged (isProblemsLimitReached)
63
+ )
64
+ }
65
+ .launchIn(actionScope)
66
+ }
67
+
48
68
override suspend fun doSuspendableAction (action : Action ) {
49
69
when (action) {
50
70
is Action .FetchAttempt ->
@@ -200,21 +220,13 @@ internal class StepQuizActionDispatcher(
200
220
getSubmissionState(attempt.id, action.step.id, currentProfile.id)
201
221
.getOrThrow()
202
222
203
- val isProblemsLimitReached = currentSubscription.isProblemsLimitReached
204
- val problemsLimitReachedModalData = if (isProblemsLimitReached) {
205
- getProblemsLimitReachedModalData(
206
- currentSubscription,
207
- isSubscriptionPurchaseEnabled(currentProfile, currentSubscription)
208
- )
209
- } else {
210
- null
211
- }
223
+ val problemsLimitReachedModalData = getProblemsLimitReachedModalData(currentProfile, currentSubscription)
212
224
213
225
Message .FetchAttemptSuccess (
214
226
step = action.step,
215
227
attempt = attempt,
216
228
submissionState = submissionState,
217
- isProblemsLimitReached = isProblemsLimitReached,
229
+ isProblemsLimitReached = currentSubscription. isProblemsLimitReached,
218
230
problemsLimitReachedModalData = problemsLimitReachedModalData,
219
231
problemsOnboardingFlags = onboardingInteractor.getProblemsOnboardingFlags()
220
232
)
@@ -236,20 +248,25 @@ internal class StepQuizActionDispatcher(
236
248
}
237
249
}
238
250
239
- internal fun isSubscriptionPurchaseEnabled (
240
- currentProfile : Profile ,
241
- currentSubscription : Subscription
251
+ private fun isSubscriptionPurchaseEnabled (
252
+ profile : Profile ,
253
+ subscription : Subscription
242
254
): Boolean =
243
255
platform.isSubscriptionPurchaseEnabled &&
244
- currentProfile .features.isMobileOnlySubscriptionEnabled &&
245
- currentSubscription .isFreemium
256
+ profile .features.isMobileOnlySubscriptionEnabled &&
257
+ subscription .isFreemium
246
258
259
+ // TODO: ALTAPPS-1171: Extract ProblemsLimitReachedModal into a separate feature
247
260
private suspend fun getProblemsLimitReachedModalData (
248
- subscription : Subscription ,
249
- isSubscriptionPurchaseEnabled : Boolean
261
+ profile : Profile ,
262
+ subscription : Subscription
250
263
): StepQuizFeature .ProblemsLimitReachedModalData ? {
264
+ if (! subscription.isProblemsLimitReached) return null
265
+
251
266
val stepsLimitTotal = subscription.stepsLimitTotal ? : return null
252
267
268
+ val isSubscriptionPurchaseEnabled = isSubscriptionPurchaseEnabled(profile, subscription)
269
+
253
270
return if (currentProfileStateRepository.isFreemiumWrongSubmissionChargeLimitsEnabled()) {
254
271
StepQuizFeature .ProblemsLimitReachedModalData (
255
272
title = resourceProvider.getString(
@@ -303,15 +320,7 @@ internal class StepQuizActionDispatcher(
303
320
subscriptionsInteractor.chargeProblemsLimits(action.chargeStrategy)
304
321
305
322
val subscription = subscriptionsInteractor.getCurrentSubscription().getOrElse { return }
306
- val problemsLimitReachedModalData =
307
- if (subscription.isProblemsLimitReached) {
308
- getProblemsLimitReachedModalData(
309
- subscription = subscription,
310
- isSubscriptionPurchaseEnabled = isSubscriptionPurchaseEnabled(currentProfile, subscription)
311
- )
312
- } else {
313
- null
314
- }
323
+ val problemsLimitReachedModalData = getProblemsLimitReachedModalData(currentProfile, subscription)
315
324
316
325
onNewMessage(
317
326
InternalMessage .UpdateProblemsLimitResult (
0 commit comments