diff --git a/Habitica/src/main/java/com/habitrpg/android/habitica/models/user/SubscriptionPlan.kt b/Habitica/src/main/java/com/habitrpg/android/habitica/models/user/SubscriptionPlan.kt index 85ee447e2..67ade63df 100644 --- a/Habitica/src/main/java/com/habitrpg/android/habitica/models/user/SubscriptionPlan.kt +++ b/Habitica/src/main/java/com/habitrpg/android/habitica/models/user/SubscriptionPlan.kt @@ -47,21 +47,21 @@ open class SubscriptionPlan : RealmObject(), BaseObject { return totalNumberOfGems - (gemsBought ?: 0) } + /* + If user has a initial basic monthly subscription, receive hourglasses on fourth month, + else receive on third month (subtract 1 from total consecutive count) + */ val monthsUntilNextHourglass: Int? get() { - var renewalUntilNextHourglass = 0 - if (planId != null && dateTerminated == null && consecutive?.count != null) { - when (planId) { - // If user has a initial basic monthly subscription, receive hourglasses on fourth month, else receive on third month. - PLANID_BASIC -> renewalUntilNextHourglass = if (consecutive?.count!! < 3) { 4 } else 3 - PLANID_BASICEARNED -> renewalUntilNextHourglass = if (consecutive?.count!! < 3) { 4 } else 3 - PLANID_BASIC3MONTH -> renewalUntilNextHourglass = 3 - PLANID_BASIC6MONTH -> renewalUntilNextHourglass = 6 - PLANID_BASIC12MONTH -> renewalUntilNextHourglass = 12 + return if (consecutive?.offset == 0) { + if (consecutive?.count == 0) { + 4 + } else { + (3 - (((consecutive?.count ?: 0) - 1) % 3)) } - return renewalUntilNextHourglass - (consecutive?.count!! % renewalUntilNextHourglass) + } else { + consecutive?.offset } - return null } companion object { diff --git a/Habitica/src/test/java/com/habitrpg/android/habitica/models/user/SubscriptionPlanTest.kt b/Habitica/src/test/java/com/habitrpg/android/habitica/models/user/SubscriptionPlanTest.kt index 87de7f467..f78dd4833 100644 --- a/Habitica/src/test/java/com/habitrpg/android/habitica/models/user/SubscriptionPlanTest.kt +++ b/Habitica/src/test/java/com/habitrpg/android/habitica/models/user/SubscriptionPlanTest.kt @@ -71,34 +71,43 @@ class SubscriptionPlanTest : WordSpec({ "monthsUntilNextHourglass" should { beforeEach { plan.consecutive = SubscriptionPlanConsecutive() - plan.consecutive?.count = 1 + plan.consecutive?.count = 0 plan.dateTerminated = null } "months until next hourglass with initial basic sub" { plan.planId = SubscriptionPlan.PLANID_BASIC - plan.monthsUntilNextHourglass shouldBe 3 + plan.monthsUntilNextHourglass shouldBe 4 + } + + "months until receiving first hourglass with basic sub" { + plan.consecutive?.count = 2 + plan.planId = SubscriptionPlan.PLANID_BASIC + plan.monthsUntilNextHourglass shouldBe 1 } "months until next hourglass with basic sub after receiving initial hourglass" { - plan.consecutive?.count = 4 + plan.consecutive?.count = 3 plan.planId = SubscriptionPlan.PLANID_BASIC - plan.monthsUntilNextHourglass shouldBe 2 + plan.monthsUntilNextHourglass shouldBe 3 } "months until next hourglass with three month sub" { + plan.consecutive?.offset = 3 plan.planId = SubscriptionPlan.PLANID_BASIC3MONTH - plan.monthsUntilNextHourglass shouldBe 2 + plan.monthsUntilNextHourglass shouldBe 3 } "months until next hourglass with six month sub" { + plan.consecutive?.offset = 6 plan.planId = SubscriptionPlan.PLANID_BASIC6MONTH - plan.monthsUntilNextHourglass shouldBe 5 + plan.monthsUntilNextHourglass shouldBe 6 } "months until next hourglass with 12 month sub" { + plan.consecutive?.offset = 12 plan.planId = SubscriptionPlan.PLANID_BASIC12MONTH - plan.monthsUntilNextHourglass shouldBe 11 + plan.monthsUntilNextHourglass shouldBe 12 } } })