/
PledgeFragmentViewModel.kt
1881 lines (1505 loc) · 84.4 KB
/
PledgeFragmentViewModel.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.kickstarter.viewmodels
import android.content.SharedPreferences
import android.text.SpannableString
import android.util.Pair
import androidx.annotation.NonNull
import com.kickstarter.R
import com.kickstarter.libs.Config
import com.kickstarter.libs.Environment
import com.kickstarter.libs.FragmentViewModel
import com.kickstarter.libs.NumberOptions
import com.kickstarter.libs.models.Country
import com.kickstarter.libs.models.OptimizelyExperiment
import com.kickstarter.libs.rx.transformers.Transformers.combineLatestPair
import com.kickstarter.libs.rx.transformers.Transformers.errors
import com.kickstarter.libs.rx.transformers.Transformers.ignoreValues
import com.kickstarter.libs.rx.transformers.Transformers.neverError
import com.kickstarter.libs.rx.transformers.Transformers.takeWhen
import com.kickstarter.libs.rx.transformers.Transformers.values
import com.kickstarter.libs.rx.transformers.Transformers.zipPair
import com.kickstarter.libs.utils.BooleanUtils
import com.kickstarter.libs.utils.DateTimeUtils
import com.kickstarter.libs.utils.ExperimentData
import com.kickstarter.libs.utils.NumberUtils
import com.kickstarter.libs.utils.ObjectUtils
import com.kickstarter.libs.utils.ProjectUtils
import com.kickstarter.libs.utils.ProjectViewUtils
import com.kickstarter.libs.utils.RefTagUtils
import com.kickstarter.libs.utils.RewardUtils
import com.kickstarter.libs.utils.extensions.parseToDouble
import com.kickstarter.models.Backing
import com.kickstarter.models.Checkout
import com.kickstarter.models.Project
import com.kickstarter.models.Reward
import com.kickstarter.models.ShippingRule
import com.kickstarter.models.StoredCard
import com.kickstarter.services.apiresponses.ShippingRulesEnvelope
import com.kickstarter.services.mutations.CreateBackingData
import com.kickstarter.services.mutations.UpdateBackingData
import com.kickstarter.ui.ArgumentsKey
import com.kickstarter.ui.data.CardState
import com.kickstarter.ui.data.CheckoutData
import com.kickstarter.ui.data.PledgeData
import com.kickstarter.ui.data.PledgeFlowContext
import com.kickstarter.ui.data.PledgeReason
import com.kickstarter.ui.data.ProjectData
import com.kickstarter.ui.fragments.PledgeFragment
import com.stripe.android.StripeIntentResult
import rx.Observable
import rx.subjects.BehaviorSubject
import rx.subjects.PublishSubject
import type.CreditCardPaymentType
import java.math.RoundingMode
import java.net.CookieManager
import kotlin.math.max
interface PledgeFragmentViewModel {
interface Inputs {
/** Call when a card has been inserted into the stored cards list. */
fun addedCardPosition(position: Int)
/** Call when a card has successfully saved. */
fun cardSaved(storedCard: StoredCard)
/** Call when user selects a card they want to pledge with. */
fun cardSelected(storedCard: StoredCard, position: Int)
/** Call when logged out user clicks the continue button. */
fun continueButtonClicked()
/** Call when user clicks the decrease pledge button. */
fun decreasePledgeButtonClicked()
/** Call when user clicks the increase pledge button. */
fun increasePledgeButtonClicked()
/** Call when user clicks the decrease bonus button. */
fun decreaseBonusButtonClicked()
/** Call when user clicks the increase bonus button. */
fun increaseBonusButtonClicked()
/** Call when user clicks a url. */
fun linkClicked(url: String)
/** Call when user clicks the mini reward. */
fun miniRewardClicked()
/** Call when the new card button is clicked. */
fun newCardButtonClicked()
/** Call when the user updates the pledge amount. */
fun pledgeInput(amount: String)
/** Call when the user updates the bonus amount. */
fun bonusInput(amount: String)
/** Call when user clicks the pledge button. */
fun pledgeButtonClicked()
/** Call when user selects a shipping location. */
fun shippingRuleSelected(shippingRule: ShippingRule)
/** Call when Stripe SCA is successful. */
fun stripeSetupResultSuccessful(outcome: Int)
/** Call when Stripe SCA is unsuccessful. */
fun stripeSetupResultUnsuccessful(exception: Exception)
}
interface Outputs {
/** Emits a newly added stored card and the project. */
fun addedCard(): Observable<Pair<StoredCard, Project>>
/** Emits the additional pledge amount string. */
fun additionalPledgeAmount(): Observable<String>
/** Emits when the additional pledge amount should be hidden. */
fun additionalPledgeAmountIsGone(): Observable<Boolean>
/** Emits the base URL to build terms URLs. */
fun baseUrlForTerms(): Observable<String>
/** Emits a list of stored cards for a user. */
fun cardsAndProject(): Observable<Pair<List<StoredCard>, Project>>
/** Emits a boolean determining if the continue button should be enabled. */
fun continueButtonIsEnabled(): Observable<Boolean>
/** Emits a boolean determining if the continue button should be hidden. */
fun continueButtonIsGone(): Observable<Boolean>
/** Emits a string representing the total pledge amount in the user's preferred currency. */
fun conversionText(): Observable<String>
/** Returns `true` if the conversion should be hidden, `false` otherwise. */
fun conversionTextViewIsGone(): Observable<Boolean>
/** Emits a boolean determining if the decrease pledge button should be enabled. */
fun decreasePledgeButtonIsEnabled(): Observable<Boolean>
/** Emits the estimated delivery date string of the reward. */
fun estimatedDelivery(): Observable<String>
/** Emits a boolean determining if the estimated delivery info should be hidden. */
fun estimatedDeliveryInfoIsGone(): Observable<Boolean>
/** Emits a boolean determining if the increase pledge button should be enabled.*/
fun increasePledgeButtonIsEnabled(): Observable<Boolean>
/** Emits a boolean determining if the payment container should be hidden. */
fun paymentContainerIsGone(): Observable<Boolean>
/** Emits the pledge amount string of the reward or backing. */
fun pledgeAmount(): Observable<String>
/** Emits the string resource ID of the pledge button. */
fun pledgeButtonCTA(): Observable<Int>
/** Emits a boolean determining if the pledge button should be enabled. */
fun pledgeButtonIsEnabled(): Observable<Boolean>
/** Emits a boolean determining if the pledge button should be hidden. */
fun pledgeButtonIsGone(): Observable<Boolean>
/** Emits a boolean determining if the pledge progress bar should be hidden. */
fun pledgeProgressIsGone(): Observable<Boolean>
/** Emits the hint text for the pledge amount. */
fun pledgeHint(): Observable<String>
/** Emits the maximum pledge amount in the project's currency. */
fun pledgeMaximum(): Observable<String>
/** Emits a boolean determining if the pledge maximum should be hidden. */
fun pledgeMaximumIsGone(): Observable<Boolean>
/** Emits the minimum pledge amount in the project's currency. */
fun pledgeMinimum(): Observable<String>
/** Emits a boolean determining if the pledge section should be hidden. */
fun pledgeSectionIsGone(): Observable<Boolean>
/** Emits the pledge amount string of the backing. */
fun pledgeSummaryAmount(): Observable<CharSequence>
/** Emits a boolean determining if the pledge summary section should be hidden. */
fun pledgeSummaryIsGone(): Observable<Boolean>
/** Emits the color resource ID of the pledge amount. */
fun pledgeTextColor(): Observable<Int>
/** Emits the currency symbol string of the project. */
fun projectCurrencySymbol(): Observable<Pair<SpannableString, Boolean>>
/** Emits a boolean determining if the delivery section should be hidden. */
fun rewardSummaryIsGone(): Observable<Boolean>
/** Emits the title of the current reward. */
fun rewardTitle(): Observable<String>
/** Emits the currently selected shipping rule. */
fun selectedShippingRule(): Observable<ShippingRule>
/** Emits the shipping amount of the selected shipping rule. */
fun shippingAmount(): Observable<CharSequence>
/** Emits the shipping rule. */
fun shippingRule(): Observable<ShippingRule>
/** Emits a pair of list of shipping rules to be selected and the project. */
fun shippingRulesAndProject(): Observable<Pair<List<ShippingRule>, Project>>
/** Emits when the shipping rules section should be hidden. */
fun shippingRulesSectionIsGone(): Observable<Boolean>
/** Emits the shipping amount of the selected shipping rule. */
fun shippingSummaryAmount(): Observable<CharSequence>
/** Emits a boolean determining if the shipping summary should be hidden. */
fun shippingSummaryIsGone(): Observable<Boolean>
/** Emits the displayable name of the backing's location. */
fun shippingSummaryLocation(): Observable<String>
/** Emits when we should show the [com.kickstarter.ui.fragments.NewCardFragment]. */
fun showNewCardFragment(): Observable<Project>
/** Emits when the pledge call was unsuccessful. */
fun showPledgeError(): Observable<Void>
/** Emits when the creating backing mutation was successful. */
fun showPledgeSuccess(): Observable<Pair<CheckoutData, PledgeData>>
/** Emits when the cards adapter should update the selected position. */
fun showSelectedCard(): Observable<Pair<Int, CardState>>
/** Emits when we should show the SCA flow with the client secret. */
fun showSCAFlow(): Observable<String>
/** Emits when the update payment source mutation was unsuccessful. */
fun showUpdatePaymentError(): Observable<Void>
/** Emits when the update payment source mutation was successful. */
fun showUpdatePaymentSuccess(): Observable<Void>
/** Emits when the update pledge call was unsuccessful. */
fun showUpdatePledgeError(): Observable<Void>
/** Emits when the update pledge call was successful. */
fun showUpdatePledgeSuccess(): Observable<Void>
/** Emits when we should start a Chrome tab. */
fun startChromeTab(): Observable<String>
/** Emits when we should start the [com.kickstarter.ui.activities.LoginToutActivity]. */
fun startLoginToutActivity(): Observable<Void>
/** Emits the total amount string of the pledge. */
fun totalAmount(): Observable<CharSequence>
/** Emits the total pledge amount in the project's currency and the project's deadline. */
fun totalAndDeadline(): Observable<Pair<String, String>>
/** Emits when the total and deadline warning should be shown. */
fun totalAndDeadlineIsVisible(): Observable<Void>
/** Emits a boolean determining if the divider above the total should be hidden. */
fun totalDividerIsGone(): Observable<Boolean>
/** Emits a boolean determining if the header whould be hidden */
fun headerSectionIsGone(): Observable<Boolean>
/** Emits a Pair containing reward/add-on title and the amount */
fun headerSelectedItems(): Observable<List<Pair<Project, Reward>>>
/** Emits a boolean determining if the minimum pledge amount subtitle should be shown */
fun isPledgeMinimumSubtitleGone(): Observable<Boolean>
/** Emits a boolean determining if the bonus support section is visible */
fun isBonusSupportSectionGone(): Observable<Boolean>
/** Emits a boolean determining if the decrease bonus button should be enabled. */
fun decreaseBonusButtonIsEnabled(): Observable<Boolean>
/** Emits a boolean determining if the increase bonus button should be enabled.*/
fun increaseBonusButtonIsEnabled(): Observable<Boolean>
/** Emits the bonus amount string of the reward or backing. */
fun bonusAmount(): Observable<String>
/** Emits the hint text for the bonus amount. */
fun bonusHint(): Observable<String>
/** Emits if a reward is a No Reward. */
fun isNoReward(): Observable<Boolean>
/** Emits the title of the project for `No Reward` use case */
fun projectTitle(): Observable<String>
/** Emits the selected reward + addOns list*/
fun rewardAndAddOns(): Observable<List<Reward>>
/** Emits if the static shipping selection area should be gone */
fun shippingRuleStaticIsGone(): Observable<Boolean>
/** Emits if the bonus summary section area should be gone */
fun bonusSummaryIsGone(): Observable<Boolean>
/** Emits the bonus summary amount */
fun bonusSummaryAmount(): Observable<CharSequence>
/** Emits the total pledgeAmount for Rewards + AddOns **/
fun pledgeAmountHeader(): Observable<CharSequence>
}
class ViewModel(@NonNull val environment: Environment) : FragmentViewModel<PledgeFragment>(environment), Inputs, Outputs {
private val addedCardPosition = PublishSubject.create<Int>()
private val cardSaved = PublishSubject.create<StoredCard>()
private val cardSelected = PublishSubject.create<Pair<StoredCard, Int>>()
private val continueButtonClicked = PublishSubject.create<Void>()
private val decreasePledgeButtonClicked = PublishSubject.create<Void>()
private val increasePledgeButtonClicked = PublishSubject.create<Void>()
private val linkClicked = PublishSubject.create<String>()
private val miniRewardClicked = PublishSubject.create<Void>()
private val newCardButtonClicked = PublishSubject.create<Void>()
private val pledgeButtonClicked = PublishSubject.create<Void>()
private val pledgeInput = PublishSubject.create<String>()
private val shippingRule = BehaviorSubject.create<ShippingRule>()
private val stripeSetupResultSuccessful = PublishSubject.create<Int>()
private val stripeSetupResultUnsuccessful = PublishSubject.create<Exception>()
private val decreaseBonusButtonClicked = PublishSubject.create<Void>()
private val increaseBonusButtonClicked = PublishSubject.create<Void>()
private val bonusInput = PublishSubject.create<String>()
private val addedCard = BehaviorSubject.create<Pair<StoredCard, Project>>()
private val additionalPledgeAmount = BehaviorSubject.create<String>()
private val additionalPledgeAmountIsGone = BehaviorSubject.create<Boolean>()
private val baseUrlForTerms = BehaviorSubject.create<String>()
private val cardsAndProject = BehaviorSubject.create<Pair<List<StoredCard>, Project>>()
private val continueButtonIsEnabled = BehaviorSubject.create<Boolean>()
private val continueButtonIsGone = BehaviorSubject.create<Boolean>()
private val conversionText = BehaviorSubject.create<String>()
private val conversionTextViewIsGone = BehaviorSubject.create<Boolean>()
private val decreasePledgeButtonIsEnabled = BehaviorSubject.create<Boolean>()
private val rewardSummaryIsGone = BehaviorSubject.create<Boolean>()
private val estimatedDelivery = BehaviorSubject.create<String>()
private val estimatedDeliveryInfoIsGone = BehaviorSubject.create<Boolean>()
private val increasePledgeButtonIsEnabled = BehaviorSubject.create<Boolean>()
private val paymentContainerIsGone = BehaviorSubject.create<Boolean>()
private val pledgeAmount = BehaviorSubject.create<String>()
private val pledgeButtonCTA = BehaviorSubject.create<Int>()
private val pledgeButtonIsGone = BehaviorSubject.create<Boolean>()
private val pledgeButtonIsEnabled = BehaviorSubject.create<Boolean>()
private val pledgeHint = BehaviorSubject.create<String>()
private val pledgeMaximum = BehaviorSubject.create<String>()
private val pledgeMaximumIsGone = BehaviorSubject.create<Boolean>()
private val pledgeMinimum = BehaviorSubject.create<String>()
private val pledgeProgressIsGone = BehaviorSubject.create<Boolean>()
private val pledgeSectionIsGone = BehaviorSubject.create<Boolean>()
private val pledgeSummaryAmount = BehaviorSubject.create<CharSequence>()
private val pledgeSummaryIsGone = BehaviorSubject.create<Boolean>()
private val pledgeTextColor = BehaviorSubject.create<Int>()
private val projectCurrencySymbol = BehaviorSubject.create<Pair<SpannableString, Boolean>>()
private val rewardTitle = BehaviorSubject.create<String>()
private val shippingAmount = BehaviorSubject.create<CharSequence>()
private val shippingRulesAndProject = BehaviorSubject.create<Pair<List<ShippingRule>, Project>>()
private val shippingRulesSectionIsGone = BehaviorSubject.create<Boolean>()
// - when having add-ons the shipping location is an static field, no changes allowed in there
private val shippingRuleStaticIsGone = BehaviorSubject.create<Boolean>()
private val shippingSummaryAmount = BehaviorSubject.create<CharSequence>()
private val shippingSummaryIsGone = BehaviorSubject.create<Boolean>()
private val shippingSummaryLocation = BehaviorSubject.create<String>()
private val showNewCardFragment = PublishSubject.create<Project>()
private val showPledgeError = PublishSubject.create<Void>()
private val showPledgeSuccess = PublishSubject.create<Pair<CheckoutData, PledgeData>>()
private val showSelectedCard = BehaviorSubject.create<Pair<Int, CardState>>()
private val showSCAFlow = PublishSubject.create<String>()
private val showUpdatePaymentError = PublishSubject.create<Void>()
private val showUpdatePaymentSuccess = PublishSubject.create<Void>()
private val showUpdatePledgeError = PublishSubject.create<Void>()
private val showUpdatePledgeSuccess = PublishSubject.create<Void>()
private val startChromeTab = PublishSubject.create<String>()
private val startLoginToutActivity = PublishSubject.create<Void>()
private val totalAmount = BehaviorSubject.create<CharSequence>()
private val totalAndDeadline = BehaviorSubject.create<Pair<String, String>>()
private val totalAndDeadlineIsVisible = BehaviorSubject.create<Void>()
private val totalDividerIsGone = BehaviorSubject.create<Boolean>()
private val headerSectionIsGone = BehaviorSubject.create<Boolean>()
private val headerSelectedItems = BehaviorSubject.create<List<Pair<Project, Reward>>>()
private val isPledgeMinimumSubtitleGone = BehaviorSubject.create<Boolean>()
private val isBonusSupportSectionGone = BehaviorSubject.create<Boolean>()
private val bonusAmount = BehaviorSubject.create<String>()
private val decreaseBonusButtonIsEnabled = BehaviorSubject.create<Boolean>()
private val increaseBonusButtonIsEnabled = BehaviorSubject.create<Boolean>()
private val bonusHint = BehaviorSubject.create<String>()
// - Keep track if the bonus stepper increase/decrease has being pressed at some point
private val bonusAmountHasChanged = BehaviorSubject.create<Boolean>(false)
private val isNoReward = BehaviorSubject.create<Boolean>()
private val projectTitle = BehaviorSubject.create<String>()
private val apiClient = environment.apiClient()
private val apolloClient = environment.apolloClient()
private val optimizely = environment.optimizely()
private val cookieManager: CookieManager = environment.cookieManager()
private val currentConfig = environment.currentConfig()
private val currentUser = environment.currentUser()
private val ksCurrency = environment.ksCurrency()
private val sharedPreferences: SharedPreferences = environment.sharedPreferences()
private val variantSuggestedAmount = BehaviorSubject.create<Double>()
private val shippingRuleUpdated = BehaviorSubject.create<Boolean>(false)
private val selectedReward = BehaviorSubject.create<Reward>()
private val rewardAndAddOns = BehaviorSubject.create<List<Reward>>()
private val shippingAmountSelectedRw = BehaviorSubject.create<Double>(0.0)
private val bonusSummaryIsGone = BehaviorSubject.create<Boolean>()
private val bonusSummaryAmount = BehaviorSubject.create<CharSequence>()
// - Flag to know if the shipping location should be the default one,
// - meaning we don't have shipping location selected yet
// - Use case: (Reward shippable without addOns in new pledge or updating pledge with restricted location)
private val shouldLoadDefaultLocation = PublishSubject.create<Boolean>()
private val pledgeAmountHeader = BehaviorSubject.create<CharSequence>()
private val stepperAmount = 1
val inputs: Inputs = this
val outputs: Outputs = this
init {
val userIsLoggedIn = this.currentUser.isLoggedIn
.distinctUntilChanged()
val pledgeData = arguments()
.map { it.getParcelable(ArgumentsKey.PLEDGE_PLEDGE_DATA) as PledgeData? }
.ofType(PledgeData::class.java)
pledgeData
.map { it.reward() }
.compose(bindToLifecycle())
.subscribe { this.selectedReward.onNext(it) }
val projectData = pledgeData
.map { it.projectData() }
val project = projectData
.map { it.project() }
// Shipping rules section
val shippingRules = project
.compose<Pair<Project, Reward>>(combineLatestPair(this.selectedReward))
.filter { RewardUtils.isShippable(it.second) }
.switchMap<ShippingRulesEnvelope> { this.apiClient.fetchShippingRules(it.first, it.second).compose(neverError()) }
.map { it.shippingRules() }
.distinctUntilChanged()
.share()
val pledgeReason = arguments()
.map { it.getSerializable(ArgumentsKey.PLEDGE_PLEDGE_REASON) as PledgeReason }
val updatingPayment = pledgeReason
.map { it == PledgeReason.UPDATE_PAYMENT || it == PledgeReason.FIX_PLEDGE }
.distinctUntilChanged()
val updatingPaymentOrUpdatingPledge = pledgeReason
.map { it == PledgeReason.UPDATE_PAYMENT || it == PledgeReason.UPDATE_PLEDGE || it == PledgeReason.FIX_PLEDGE }
.distinctUntilChanged()
val addOns = pledgeData
.map { if (it.addOns().isNullOrEmpty()) emptyList() else it.addOns() as List<Reward> }
val backing = projectData
.compose<Pair<ProjectData, PledgeReason>>(combineLatestPair(pledgeReason))
.filter { it.second != PledgeReason.PLEDGE }
.map { it.first.backing() ?: it.first.project().backing() }
.filter { ObjectUtils.isNotNull(it) }
.map { requireNotNull(it) }
backing
.map { it.locationId() == null }
.subscribe {
this.shouldLoadDefaultLocation.onNext(it)
}
val backingShippingRule = backing
.compose<Pair<Backing, PledgeData>>(combineLatestPair(pledgeData))
.filter {
shouldLoadShippingRuleFromBacking(it)
}
.map { requireNotNull(it.first.locationId()) }
.compose<Pair<Long, List<ShippingRule>>>(combineLatestPair(shippingRules))
.map { shippingInfo ->
selectedShippingRule(shippingInfo)
}
val backingWhenPledgeReasonUpdatePayment = backing
.compose<Pair<Backing, PledgeReason>>(combineLatestPair(pledgeReason))
.filter { PledgeReason.UPDATE_PAYMENT == it.second }
.map { it.first }
val backingShippingRuleUpdatePayment = backingWhenPledgeReasonUpdatePayment
.filter { it.reward()?.let { reward -> !RewardUtils.isNoReward(reward) } ?: false }
.compose<Pair<Backing, PledgeData>>(combineLatestPair(pledgeData))
.map { requireNotNull(it.first.locationId()) }
.compose<Pair<Long, List<ShippingRule>>>(combineLatestPair(shippingRules))
.map { shippingInfo ->
selectedShippingRule(shippingInfo)
}
val initShippingRule = pledgeData
.distinctUntilChanged()
.map {
it.shippingRule()
}
pledgeData
.map { it.shippingRule() == null && RewardUtils.isShippable(it.reward()) }
.subscribe { this.shouldLoadDefaultLocation.onNext(it) }
val preSelectedShippingRule = Observable.merge(initShippingRule, backingShippingRule, backingShippingRuleUpdatePayment)
.distinctUntilChanged()
preSelectedShippingRule
.filter { ObjectUtils.isNotNull(it) }
.map { requireNotNull(it) }
.compose(bindToLifecycle())
.subscribe {
this.shippingRule.onNext(it)
}
backing
.map { if (it.addOns().isNullOrEmpty()) emptyList() else requireNotNull(it.addOns()) }
.compose<Pair<List<Reward>, Reward>>(combineLatestPair(this.selectedReward))
.compose(bindToLifecycle())
.subscribe {
val updatedList = it.first.toMutableList()
updatedList.add(0, it.second)
this.rewardAndAddOns.onNext(updatedList.toList())
}
backing
.compose<Pair<Backing, Reward>>(combineLatestPair(this.selectedReward))
.compose(bindToLifecycle())
.filter { it.first != null && it.second != null }
backing
.map { it.bonusAmount() }
.filter { it > 0 }
.compose(bindToLifecycle())
.subscribe {
this.bonusAmount.onNext(it.toString())
}
val projectDataAndReward = projectData
.compose<Pair<ProjectData, Reward>>(combineLatestPair(this.selectedReward))
this.selectedReward
.compose<Pair<Reward, PledgeReason>>(combineLatestPair(pledgeReason))
.filter { it.second == PledgeReason.PLEDGE || it.second == PledgeReason.UPDATE_REWARD }
.map { it.first }
.compose<Pair<Reward, List<Reward>>>(combineLatestPair(addOns))
.map {
joinRewardAndAddOns(it.first, it.second)
}
.compose(bindToLifecycle())
.subscribe(this.rewardAndAddOns)
val pledgeAmountHeader = this.rewardAndAddOns
.filter { !RewardUtils.isNoReward(it.first()) }
.map { getPledgeAmount(it) }
pledgeAmountHeader
.compose<Pair<Double, Project>>(combineLatestPair(project))
.map { ProjectViewUtils.styleCurrency(it.first, it.second, this.ksCurrency) }
.compose(bindToLifecycle())
.subscribe { this.pledgeAmountHeader.onNext(it) }
val projectAndReward = project
.compose<Pair<Project, Reward>>(combineLatestPair(this.selectedReward))
val country = project
.map { Country.findByCurrencyCode(it.currency()) }
.filter { it != null }
.distinctUntilChanged()
.ofType(Country::class.java)
Observable.combineLatest(projectDataAndReward, this.currentUser.observable(), country) { data, user, c ->
val experimentData = ExperimentData(user, data.first.refTagFromIntent(), data.first.refTagFromCookie())
val variant = this.optimizely.variant(OptimizelyExperiment.Key.SUGGESTED_NO_REWARD_AMOUNT, experimentData)
RewardUtils.rewardAmountByVariant(variant, data.second, c.minPledge)
}
.compose<Pair<Double, Reward>>(combineLatestPair(this.selectedReward))
.filter { RewardUtils.isNoReward(it.second) }
.map { it.first }
.distinctUntilChanged()
.compose(bindToLifecycle())
.subscribe {
variantSuggestedAmount.onNext(it)
}
val fullProjectDataAndPledgeData = projectData
.compose<Pair<ProjectData, PledgeData>>(combineLatestPair(pledgeData))
projectAndReward
.map { rewardTitle(it.first, it.second) }
.distinctUntilChanged()
.compose(bindToLifecycle())
.subscribe(this.rewardTitle)
this.selectedReward
.map { it.estimatedDeliveryOn() }
.filter { ObjectUtils.isNotNull(it) }
.map { dateTime -> dateTime?.let { DateTimeUtils.estimatedDeliveryOn(it) } }
.distinctUntilChanged()
.compose(bindToLifecycle())
.subscribe(this.estimatedDelivery)
this.selectedReward
.map { ObjectUtils.isNull(it.estimatedDeliveryOn()) || RewardUtils.isNoReward(it) }
.compose(bindToLifecycle())
.subscribe(this.estimatedDeliveryInfoIsGone)
val minRw = this.selectedReward
.filter { !RewardUtils.isNoReward(it) }
.map { it.minimum() }
.distinctUntilChanged()
val rewardMinimum = Observable.merge(minRw, variantSuggestedAmount)
rewardMinimum
.map { NumberUtils.format(it.toInt()) }
.compose(bindToLifecycle())
.subscribe(this.pledgeHint)
Observable.combineLatest(rewardMinimum, project) { amount, project ->
return@combineLatest this.ksCurrency.format(amount, project)
}
.distinctUntilChanged()
.compose(bindToLifecycle())
.subscribe(this.pledgeMinimum)
this.rewardAndAddOns
.compose<Pair<List<Reward>, Project>>(combineLatestPair(project))
.map { joinProject(it) }
.compose(bindToLifecycle())
.subscribe(this.headerSelectedItems)
project
.map { ProjectViewUtils.currencySymbolAndPosition(it, this.ksCurrency) }
.compose(bindToLifecycle())
.subscribe(this.projectCurrencySymbol)
project
.map { it.name() }
.compose(bindToLifecycle())
.subscribe(this.projectTitle)
// Pledge stepper section
val additionalPledgeAmount = BehaviorSubject.create(0.0)
val additionalAmountOrZero = additionalPledgeAmount
.map { max(0.0, it) }
val stepAmount = country
.map { it.minPledge }
additionalAmountOrZero
.compose<Pair<Double, Project>>(combineLatestPair(project))
.map { this.ksCurrency.format(it.first, it.second, RoundingMode.HALF_UP) }
.distinctUntilChanged()
.compose(bindToLifecycle())
.subscribe(this.additionalPledgeAmount)
additionalAmountOrZero
.map { it <= 0.0 }
.distinctUntilChanged()
.compose(bindToLifecycle())
.subscribe(this.additionalPledgeAmountIsGone)
val initialAmount = rewardMinimum
.compose<Pair<Double, Boolean>>(combineLatestPair(updatingPaymentOrUpdatingPledge))
.filter { BooleanUtils.isFalse(it.second) }
.map { it.first }
// - For no Reward the amount of the RW and the bonus amount are the same value
val backingAmountNR = backing
.filter { it.reward() == null }
.map { it.bonusAmount() }
.distinctUntilChanged()
val backingAmountRW = backing
.filter { it.reward()?.let { rw -> !RewardUtils.isNoReward(rw) } ?: false }
.map { it.amount() - it.shippingAmount() - it.bonusAmount() }
.distinctUntilChanged()
val backingAmount = Observable.merge(backingAmountNR, backingAmountRW)
val pledgeInput = Observable.merge(initialAmount, this.pledgeInput.map { it.parseToDouble() }, backingAmount)
.map { it }
.distinctUntilChanged()
pledgeInput
.compose<Double>(takeWhen(this.increasePledgeButtonClicked))
.map { it + this.stepperAmount }
.map { it.toString() }
.compose(bindToLifecycle())
.subscribe(this.pledgeInput)
pledgeInput
.compose<Double>(takeWhen(this.decreasePledgeButtonClicked))
.map { it - this.stepperAmount }
.map { it.toString() }
.compose(bindToLifecycle())
.subscribe(this.pledgeInput)
pledgeInput
.compose<Pair<Double, Double>>(combineLatestPair(variantSuggestedAmount))
.map { it.first - it.second }
.compose(bindToLifecycle())
.subscribe { additionalPledgeAmount.onNext(it) }
pledgeInput
.compose<Pair<Double, Double>>(combineLatestPair(variantSuggestedAmount))
.map { max(it.first, it.second) > it.second }
.distinctUntilChanged()
.compose(bindToLifecycle())
.subscribe(this.decreasePledgeButtonIsEnabled)
pledgeInput
.map { NumberUtils.format(it.toFloat(), NumberOptions.builder().precision(NumberUtils.precision(it, RoundingMode.HALF_UP)).build()) }
.compose(bindToLifecycle())
.subscribe(this.pledgeAmount)
// Bonus stepper action
val bonusMinimum = Observable.just(0.0)
val bonusStepAmount = Observable.just(1.0)
val bonusInput = Observable.merge(bonusMinimum, this.bonusInput.map { it.parseToDouble() })
bonusMinimum
.map { NumberUtils.format(it.toInt()) }
.distinctUntilChanged()
.compose(bindToLifecycle())
.subscribe(this.bonusHint)
bonusInput
.compose<Double>(takeWhen(this.increaseBonusButtonClicked))
.compose<Pair<Double, Double>>(combineLatestPair(bonusStepAmount))
.map { it.first + it.second }
.map { it.toString() }
.compose(bindToLifecycle())
.subscribe(this.bonusInput)
bonusInput
.compose<Double>(takeWhen(this.decreaseBonusButtonClicked))
.compose<Pair<Double, Double>>(combineLatestPair(bonusStepAmount))
.map { it.first - it.second }
.map { it.toString() }
.compose(bindToLifecycle())
.subscribe(this.bonusInput)
bonusInput
.compose<Pair<Double, Double>>(combineLatestPair(bonusMinimum))
.map { max(it.first, it.second) > it.second }
.distinctUntilChanged()
.compose(bindToLifecycle())
.subscribe(this.decreaseBonusButtonIsEnabled)
bonusInput
.map { NumberUtils.format(it.toFloat(), NumberOptions.builder().precision(NumberUtils.precision(it, RoundingMode.HALF_UP)).build()) }
.distinctUntilChanged()
.compose(bindToLifecycle())
.subscribe(this.bonusAmount)
Observable.merge(this.decreaseBonusButtonClicked, this.decreasePledgeButtonClicked, this.increaseBonusButtonClicked, this.increasePledgeButtonClicked)
.distinctUntilChanged()
.subscribe {
this.bonusAmountHasChanged.onNext(true)
}
val rulesAndProject = shippingRules
.compose<Pair<List<ShippingRule>, Project>>(combineLatestPair(project))
rulesAndProject
.compose(bindToLifecycle())
.subscribe(this.shippingRulesAndProject)
Observable.combineLatest(shippingRules, this.currentConfig.observable(), shouldLoadDefaultLocation) { rules, config, isDefault ->
return@combineLatest if (isDefault) defaultConfigShippingRule(rules, config) else null
}
.filter { ObjectUtils.isNotNull(it) }
.map { requireNotNull(it) }
.compose<Pair<ShippingRule, PledgeReason>>(combineLatestPair(pledgeReason))
.filter { it.second == PledgeReason.PLEDGE || it.second == PledgeReason.UPDATE_REWARD }
.map { it.first }
.compose<Pair<ShippingRule, Project>>(combineLatestPair(project))
.compose(bindToLifecycle())
.subscribe {
this.shippingRule.onNext(it.first)
}
val backingShippingAmount = backing
.map { it.shippingAmount() }
val shippingAmountWhenBacking = Observable.combineLatest(this.shippingRule, pledgeReason, backingShippingAmount, rewardAndAddOns) { rule, reason, bShippingAmount, listRw ->
return@combineLatest getShippingAmount(rule, reason, bShippingAmount, listRw)
}
.distinctUntilChanged()
val newPledge = pledgeReason
.filter { it == PledgeReason.PLEDGE || it == PledgeReason.UPDATE_REWARD }
.map { it }
// - Shipping amount when no backing
val shippingAmountNewPledge = Observable.combineLatest(this.shippingRule, newPledge, rewardAndAddOns) { rule, reason, listRw ->
return@combineLatest getShippingAmount(rule, reason, listRw = listRw)
}
.distinctUntilChanged()
val shippingAmount = Observable.merge(shippingAmountNewPledge, shippingAmountWhenBacking)
shippingAmount
.compose<Pair<Double, Project>>(combineLatestPair(project))
.compose(bindToLifecycle())
.distinctUntilChanged()
.subscribe {
shippingAmountSelectedRw.onNext(it.first)
this.shippingAmount.onNext(ProjectViewUtils.styleCurrency(it.first, it.second, this.ksCurrency))
}
// - When updating payment, shipping location area should always be gone
updatingPayment
.compose<Pair<Boolean, Reward>>(combineLatestPair(this.selectedReward))
.filter { it.first == true && RewardUtils.isShippable(it.second) }
.compose(bindToLifecycle())
.subscribe {
this.shippingRulesSectionIsGone.onNext(true)
}
val isRewardWithShipping = this.selectedReward
.filter { RewardUtils.isShippable(it) }
val isDigitalRw = this.selectedReward
.filter { RewardUtils.isDigital(it) }
// - Calculate total for Reward || Rewards + AddOns with Shipping location
val totalWShipping = Observable.combineLatest(isRewardWithShipping, pledgeAmountHeader, shippingAmount, this.bonusAmount, pledgeReason) {
_, pAmount, shippingAmount, bAmount, pReason ->
return@combineLatest getAmount(pAmount, shippingAmount, bAmount, pReason)
}
.distinctUntilChanged()
// - Calculate total for NoReward
val totalNR = this.selectedReward
.filter { RewardUtils.isNoReward(it) }
.compose<Pair<Reward, String>>(combineLatestPair(this.pledgeInput.startWith("")))
.map { if (it.second.isNotEmpty()) NumberUtils.parse(it.second) else it.first.minimum() }
// - Calculate total for DigitalRewards || DigitalReward + DigitalAddOns
val totalDigital = Observable.combineLatest(isDigitalRw, pledgeAmountHeader, this.bonusAmount, pledgeReason) { _, pledgeAmount, bonusAmount, pReason ->
return@combineLatest getAmountDigital(pledgeAmount, NumberUtils.parse(bonusAmount), pReason)
}
.distinctUntilChanged()
val total = Observable.merge(totalWShipping, totalNR, totalDigital)
total
.compose<Pair<Double, Project>>(combineLatestPair(project))
.map { ProjectViewUtils.styleCurrency(it.first, it.second, this.ksCurrency) }
.compose(bindToLifecycle())
.subscribe {
this.totalAmount.onNext(it)
}
total
.compose<Pair<Double, Project>>(combineLatestPair(project))
.map { Pair(this.ksCurrency.format(it.first, it.second, RoundingMode.HALF_UP), it.second) }
.filter { ObjectUtils.isNotNull(it.second.deadline()) }
.map { totalAndProject -> totalAndProject.second.deadline()?.let { Pair(totalAndProject.first, DateTimeUtils.longDate(it)) } }
.distinctUntilChanged()
.compose(bindToLifecycle())
.subscribe(this.totalAndDeadline)
this.totalAndDeadline
.compose(ignoreValues())
.compose(bindToLifecycle())
.subscribe(this.totalAndDeadlineIsVisible)
total
.compose<Pair<Double, Project>>(combineLatestPair(project))
.filter { it.second.currency() != it.second.currentCurrency() }
.map { this.ksCurrency.formatWithUserPreference(it.first, it.second, RoundingMode.UP, 2) }
.compose(bindToLifecycle())
.subscribe(this.conversionText)
projectAndReward
.map { it.first.currency() != it.first.currentCurrency() }
.map { BooleanUtils.negate(it) }
.distinctUntilChanged()
.compose(bindToLifecycle())
.subscribe(this.conversionTextViewIsGone)
val currencyMaximum = country
.map { it.maxPledge.toDouble() }
.distinctUntilChanged()
val threshold = pledgeAmountHeader
.compose<Pair<Double, Double>>(combineLatestPair(shippingAmount))
.map { it.first + it.second }
.distinctUntilChanged()
val selectedPledgeAmount = Observable.merge(pledgeAmountHeader, threshold, variantSuggestedAmount)
val bonusSupportMaximum = currencyMaximum
.compose<Pair<Double, Double>>(combineLatestPair(selectedPledgeAmount))
.compose<Pair<Pair<Double, Double>, Reward>>(combineLatestPair(this.selectedReward))
.map { if (RewardUtils.isNoReward(it.second)) it.first.first else it.first.first - it.first.second }
val pledgeMaximumIsGone = currencyMaximum
.compose<Pair<Double, Double>>(combineLatestPair(total))
.map { it.first >= it.second }
.distinctUntilChanged()
pledgeMaximumIsGone
.distinctUntilChanged()
.compose(bindToLifecycle())
.subscribe {
this.pledgeMaximumIsGone.onNext(it)
}
bonusSupportMaximum
.distinctUntilChanged()
.compose<Pair<Double, Project>>(combineLatestPair(project))
.map { this.ksCurrency.format(it.first, it.second, RoundingMode.HALF_UP) }
.compose(bindToLifecycle())
.subscribe {
this.pledgeMaximum.onNext(it)
}
val minAndMaxPledge = rewardMinimum
.compose<Pair<Double, Double>>(combineLatestPair(currencyMaximum))
pledgeInput
.compose<Pair<Double, Pair<Double, Double>>>(combineLatestPair(minAndMaxPledge))
.map { it.first in it.second.first..it.second.second }
.map { if (it) R.color.kds_create_700 else R.color.kds_alert }
.distinctUntilChanged()
.compose(bindToLifecycle())
.subscribe(this.pledgeTextColor)
val stepAndMaxPledge = stepAmount
.map { it.toDouble() }
.compose<Pair<Double, Double>>(combineLatestPair(currencyMaximum))
pledgeInput
.compose<Pair<Double, Pair<Double, Double>>>(combineLatestPair(stepAndMaxPledge))
.map { it.second.second - it.first >= it.second.first }
.distinctUntilChanged()
.compose(bindToLifecycle())
.subscribe(this.increasePledgeButtonIsEnabled)
// Manage pledge section
backingAmount
.compose<Pair<Double, Project>>(combineLatestPair(project))
.map { ProjectViewUtils.styleCurrency(it.first, it.second, this.ksCurrency) }
.distinctUntilChanged()
.compose(bindToLifecycle())
.subscribe(this.pledgeSummaryAmount)
updatingPayment
.compose(bindToLifecycle())
.subscribe(this.totalDividerIsGone)
backing
.map { it.shippingAmount().toDouble() }
.compose<Pair<Double, Project>>(combineLatestPair(project))
.map { ProjectViewUtils.styleCurrency(it.first, it.second, this.ksCurrency) }
.compose(bindToLifecycle())
.subscribe(this.shippingSummaryAmount)
backing
.map { it.bonusAmount() }
.compose<Pair<Double, PledgeReason>>(combineLatestPair(pledgeReason))
.filter { it.second == PledgeReason.UPDATE_PAYMENT }
.map { it.first }
.compose<Pair<Double, Project>>(combineLatestPair(project))
.map { ProjectViewUtils.styleCurrency(it.first, it.second, this.ksCurrency) }
.compose(bindToLifecycle())
.subscribe {
this.bonusSummaryAmount.onNext(it)
}
this.shippingRule
.map { it.location().displayableName() }
.distinctUntilChanged()
.compose(bindToLifecycle())
.subscribe(this.shippingSummaryLocation)