Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MBL-1316: Use isAvailable to filter visible add-ons #2012

Merged
merged 6 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 14 additions & 2 deletions KsApi/GraphAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15520,6 +15520,7 @@ public enum GraphAPI {
estimatedDeliveryOn
id
isMaxPledge
available
items {
__typename
edges {
Expand Down Expand Up @@ -15570,6 +15571,7 @@ public enum GraphAPI {
GraphQLField("estimatedDeliveryOn", type: .scalar(String.self)),
GraphQLField("id", type: .nonNull(.scalar(GraphQLID.self))),
GraphQLField("isMaxPledge", type: .nonNull(.scalar(Bool.self))),
GraphQLField("available", type: .nonNull(.scalar(Bool.self))),
GraphQLField("items", type: .object(Item.selections)),
GraphQLField("limit", type: .scalar(Int.self)),
GraphQLField("limitPerBacker", type: .scalar(Int.self)),
Expand All @@ -15595,8 +15597,8 @@ public enum GraphAPI {
self.resultMap = unsafeResultMap
}

public init(amount: Amount, backersCount: Int? = nil, convertedAmount: ConvertedAmount, allowedAddons: AllowedAddon, description: String, displayName: String, endsAt: String? = nil, estimatedDeliveryOn: String? = nil, id: GraphQLID, isMaxPledge: Bool, items: Item? = nil, limit: Int? = nil, limitPerBacker: Int? = nil, localReceiptLocation: LocalReceiptLocation? = nil, name: String? = nil, postCampaignPledgingEnabled: Bool, project: Project? = nil, remainingQuantity: Int? = nil, shippingPreference: ShippingPreference? = nil, shippingSummary: String? = nil, shippingRules: [ShippingRule?]? = nil, startsAt: String? = nil) {
self.init(unsafeResultMap: ["__typename": "Reward", "amount": amount.resultMap, "backersCount": backersCount, "convertedAmount": convertedAmount.resultMap, "allowedAddons": allowedAddons.resultMap, "description": description, "displayName": displayName, "endsAt": endsAt, "estimatedDeliveryOn": estimatedDeliveryOn, "id": id, "isMaxPledge": isMaxPledge, "items": items.flatMap { (value: Item) -> ResultMap in value.resultMap }, "limit": limit, "limitPerBacker": limitPerBacker, "localReceiptLocation": localReceiptLocation.flatMap { (value: LocalReceiptLocation) -> ResultMap in value.resultMap }, "name": name, "postCampaignPledgingEnabled": postCampaignPledgingEnabled, "project": project.flatMap { (value: Project) -> ResultMap in value.resultMap }, "remainingQuantity": remainingQuantity, "shippingPreference": shippingPreference, "shippingSummary": shippingSummary, "shippingRules": shippingRules.flatMap { (value: [ShippingRule?]) -> [ResultMap?] in value.map { (value: ShippingRule?) -> ResultMap? in value.flatMap { (value: ShippingRule) -> ResultMap in value.resultMap } } }, "startsAt": startsAt])
public init(amount: Amount, backersCount: Int? = nil, convertedAmount: ConvertedAmount, allowedAddons: AllowedAddon, description: String, displayName: String, endsAt: String? = nil, estimatedDeliveryOn: String? = nil, id: GraphQLID, isMaxPledge: Bool, available: Bool, items: Item? = nil, limit: Int? = nil, limitPerBacker: Int? = nil, localReceiptLocation: LocalReceiptLocation? = nil, name: String? = nil, postCampaignPledgingEnabled: Bool, project: Project? = nil, remainingQuantity: Int? = nil, shippingPreference: ShippingPreference? = nil, shippingSummary: String? = nil, shippingRules: [ShippingRule?]? = nil, startsAt: String? = nil) {
self.init(unsafeResultMap: ["__typename": "Reward", "amount": amount.resultMap, "backersCount": backersCount, "convertedAmount": convertedAmount.resultMap, "allowedAddons": allowedAddons.resultMap, "description": description, "displayName": displayName, "endsAt": endsAt, "estimatedDeliveryOn": estimatedDeliveryOn, "id": id, "isMaxPledge": isMaxPledge, "available": available, "items": items.flatMap { (value: Item) -> ResultMap in value.resultMap }, "limit": limit, "limitPerBacker": limitPerBacker, "localReceiptLocation": localReceiptLocation.flatMap { (value: LocalReceiptLocation) -> ResultMap in value.resultMap }, "name": name, "postCampaignPledgingEnabled": postCampaignPledgingEnabled, "project": project.flatMap { (value: Project) -> ResultMap in value.resultMap }, "remainingQuantity": remainingQuantity, "shippingPreference": shippingPreference, "shippingSummary": shippingSummary, "shippingRules": shippingRules.flatMap { (value: [ShippingRule?]) -> [ResultMap?] in value.map { (value: ShippingRule?) -> ResultMap? in value.flatMap { (value: ShippingRule) -> ResultMap in value.resultMap } } }, "startsAt": startsAt])
}

public var __typename: String {
Expand Down Expand Up @@ -15709,6 +15711,16 @@ public enum GraphAPI {
}
}

/// Whether or not the reward is available for new pledges
public var available: Bool {
get {
return resultMap["available"]! as! Bool
}
set {
resultMap.updateValue(newValue, forKey: "available")
}
}

/// Items in the reward.
public var items: Item? {
get {
Expand Down
1 change: 1 addition & 0 deletions KsApi/fragments/RewardFragment.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ fragment RewardFragment on Reward {
estimatedDeliveryOn
id
isMaxPledge
available
items {
edges {
quantity
Expand Down
3 changes: 3 additions & 0 deletions KsApi/models/Reward.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public struct Reward {
public let startsAt: TimeInterval?
public let title: String?
public let localPickup: Location?
/// isAvailable is provided by GraphQL but not by API V1.
public let isAvailable: Bool?

/// Returns `true` is this is the "fake" "No reward" reward.
public var isNoReward: Bool {
Expand Down Expand Up @@ -147,6 +149,7 @@ extension Reward: Decodable {
self.title = try values.decodeIfPresent(String.self, forKey: .title)
// NOTE: `v1` is deprecated and doesn't contain any location pickup information.
self.localPickup = nil
self.isAvailable = nil
}
}

Expand Down
3 changes: 2 additions & 1 deletion KsApi/models/graphql/adapters/Reward+RewardFragment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ extension Reward {
shippingRulesExpanded: expandedShippingRules,
startsAt: rewardFragment.startsAt.flatMap(TimeInterval.init),
title: rewardFragment.name,
localPickup: location
localPickup: location,
isAvailable: rewardFragment.available
)
}
}
Expand Down
83 changes: 64 additions & 19 deletions KsApi/models/lenses/RewardLenses.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ extension Reward {
shippingRulesExpanded: $1.shippingRulesExpanded,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still formulating some thoughts about how we can replace the Foo.template.lens |> ... pattern, so I used this as an opportunity to actually get into the guts of one to learn more about it.

startsAt: $1.startsAt,
title: $1.title,
localPickup: $1.localPickup
localPickup: $1.localPickup,
isAvailable: nil
) }
)

Expand All @@ -49,7 +50,8 @@ extension Reward {
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $1.title,
localPickup: $0
localPickup: $0,
isAvailable: nil
) }
)

Expand All @@ -74,7 +76,8 @@ extension Reward {
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $1.title,
localPickup: $1.localPickup
localPickup: $1.localPickup,
isAvailable: nil
) }
)

Expand All @@ -99,7 +102,8 @@ extension Reward {
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $1.title,
localPickup: $1.localPickup
localPickup: $1.localPickup,
isAvailable: nil
) }
)

Expand All @@ -124,7 +128,8 @@ extension Reward {
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $1.title,
localPickup: $1.localPickup
localPickup: $1.localPickup,
isAvailable: nil
) }
)

Expand All @@ -149,7 +154,8 @@ extension Reward {
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $1.title,
localPickup: $1.localPickup
localPickup: $1.localPickup,
isAvailable: nil
) }
)

Expand All @@ -174,7 +180,8 @@ extension Reward {
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $1.title,
localPickup: $1.localPickup
localPickup: $1.localPickup,
isAvailable: nil
) }
)

Expand All @@ -199,7 +206,8 @@ extension Reward {
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $1.title,
localPickup: $1.localPickup
localPickup: $1.localPickup,
isAvailable: nil
) }
)

Expand All @@ -224,7 +232,8 @@ extension Reward {
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $1.title,
localPickup: $1.localPickup
localPickup: $1.localPickup,
isAvailable: nil
) }
)

Expand All @@ -249,7 +258,8 @@ extension Reward {
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $1.title,
localPickup: $1.localPickup
localPickup: $1.localPickup,
isAvailable: nil
) }
)

Expand All @@ -274,7 +284,8 @@ extension Reward {
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $1.title,
localPickup: $1.localPickup
localPickup: $1.localPickup,
isAvailable: nil
) }
)

Expand All @@ -299,7 +310,8 @@ extension Reward {
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $1.title,
localPickup: $1.localPickup
localPickup: $1.localPickup,
isAvailable: nil
) }
)

Expand All @@ -324,7 +336,8 @@ extension Reward {
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $1.title,
localPickup: $1.localPickup
localPickup: $1.localPickup,
isAvailable: nil
) }
)

Expand All @@ -349,7 +362,8 @@ extension Reward {
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $1.title,
localPickup: $1.localPickup
localPickup: $1.localPickup,
isAvailable: nil
) }
)

Expand All @@ -374,7 +388,8 @@ extension Reward {
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $1.title,
localPickup: $1.localPickup
localPickup: $1.localPickup,
isAvailable: nil
) }
)

Expand All @@ -399,7 +414,8 @@ extension Reward {
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $1.title,
localPickup: $1.localPickup
localPickup: $1.localPickup,
isAvailable: nil
) }
)

Expand All @@ -424,7 +440,8 @@ extension Reward {
shippingRulesExpanded: $0,
startsAt: $1.startsAt,
title: $1.title,
localPickup: $1.localPickup
localPickup: $1.localPickup,
isAvailable: nil
) }
)

Expand All @@ -449,7 +466,8 @@ extension Reward {
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $0,
title: $1.title,
localPickup: $1.localPickup
localPickup: $1.localPickup,
isAvailable: nil
) }
)

Expand All @@ -474,7 +492,34 @@ extension Reward {
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $0,
localPickup: $1.localPickup
localPickup: $1.localPickup,
isAvailable: nil
) }
)

public static let isAvailable = Lens<Reward, Bool?>(
view: { $0.isAvailable },
set: { Reward(
backersCount: $1.backersCount,
convertedMinimum: $1.convertedMinimum,
description: $1.description,
endsAt: $1.endsAt,
estimatedDeliveryOn: $1.estimatedDeliveryOn,
hasAddOns: $1.hasAddOns,
id: $1.id,
limit: $1.limit,
limitPerBacker: $1.limitPerBacker,
minimum: $1.minimum,
postCampaignPledgingEnabled: $1.postCampaignPledgingEnabled,
remaining: $1.remaining,
rewardsItems: $1.rewardsItems,
shipping: $1.shipping,
shippingRules: $1.shippingRules,
shippingRulesExpanded: $1.shippingRulesExpanded,
startsAt: $1.startsAt,
title: $1.title,
localPickup: $1.localPickup,
isAvailable: $0
) }
)
}
Expand Down
9 changes: 6 additions & 3 deletions KsApi/models/templates/RewardTemplates.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ extension Reward {
shippingRulesExpanded: nil,
startsAt: nil,
title: "My Reward",
localPickup: nil
localPickup: nil,
isAvailable: nil
)

public static let noReward = Reward(
Expand Down Expand Up @@ -57,7 +58,8 @@ extension Reward {
shippingRulesExpanded: nil,
startsAt: nil,
title: nil,
localPickup: nil
localPickup: nil,
isAvailable: nil
)

public static let otherReward = Reward(
Expand Down Expand Up @@ -85,7 +87,8 @@ extension Reward {
shippingRulesExpanded: nil,
startsAt: nil,
title: nil,
localPickup: nil
localPickup: nil,
isAvailable: nil
)

public static let postcards = Reward.template
Expand Down
13 changes: 1 addition & 12 deletions Library/ViewModels/RewardAddOnSelectionViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ public final class RewardAddOnSelectionViewModel: RewardAddOnSelectionViewModelT
self.endRefreshing = projectEvent.filter { $0.isTerminating }.ignoreValues()

let addOns = projectEvent.values().map(\.rewardData.addOns).skipNil()

let requestErrored = projectEvent.map(\.error).map(isNotNil)

// Quantities updated as the user selects them, merged with an empty initial value.
Expand Down Expand Up @@ -468,17 +467,7 @@ private func addOnIsAvailable(_ addOn: Reward, in project: Project) -> Bool {
return true
}

let isUnlimitedOrAvailable = addOn.limit == nil || addOn.remaining ?? 0 > 0

// Assuming the user has not backed the addOn, we only display if it's within range of the start and end date
let hasNoTimeLimitOrIsWithinRange = isStartDateBeforeToday(for: addOn) && isEndDateAfterToday(for: addOn)

return [
project.state == .live,
hasNoTimeLimitOrIsWithinRange,
isUnlimitedOrAvailable
]
.allSatisfy(isTrue)
return addOn.isAvailable ?? false
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace all the start time logic with isAvailable.

}

private func filteredAddOns(
Expand Down