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-1495] [MBL-1502] PPO UI implementation #2186

Merged
merged 19 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct PPOAlertFlag: View {
}

var image: Image {
switch self.alert.type {
switch self.alert.icon {
case .time:
Image(PPOCardStyles.timeImage)
case .alert:
Expand All @@ -36,7 +36,7 @@ struct PPOAlertFlag: View {
}

var foregroundColor: Color {
switch self.alert.icon {
switch self.alert.type {
case .warning:
Color(uiColor: PPOCardStyles.warningColor.foreground)
case .alert:
Expand All @@ -45,7 +45,7 @@ struct PPOAlertFlag: View {
}

var backgroundColor: Color {
switch self.alert.icon {
switch self.alert.type {
case .warning:
Color(uiColor: PPOCardStyles.warningColor.background)
case .alert:
Expand All @@ -64,10 +64,10 @@ struct PPOAlertFlag: View {

#Preview("Stack of flags") {
VStack(alignment: .leading, spacing: 8) {
PPOAlertFlag(alert: .init(type: .time, icon: .warning, message: "Address locks in 8 hours"))
PPOAlertFlag(alert: .init(type: .alert, icon: .warning, message: "Survey available"))
PPOAlertFlag(alert: .init(type: .warning, icon: .time, message: "Address locks in 8 hours"))
PPOAlertFlag(alert: .init(type: .warning, icon: .alert, message: "Survey available"))
PPOAlertFlag(alert: .init(type: .alert, icon: .alert, message: "Payment failed"))
PPOAlertFlag(alert: .init(type: .time, icon: .alert, message: "Pledge will be dropped in 6 days"))
PPOAlertFlag(alert: .init(type: .alert, icon: .time, message: "Pledge will be dropped in 6 days"))
PPOAlertFlag(alert: .init(type: .alert, icon: .alert, message: "Card needs authentication"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ enum PPOCardStyles {
color: UIColor.ksr_black
)

static let background = UIColor.ksr_white

static let timeImage = ImageResource.iconLimitedTime
static let alertImage = ImageResource.iconNotice

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ import SwiftUI

struct PPOProjectCard: View {
@StateObject var viewModel: PPOProjectCardViewModel
var parentSize: CGSize

var onShowProject: ((PPOProjectCardModel) -> Void)? = nil
stevestreza-ksr marked this conversation as resolved.
Show resolved Hide resolved
var onSendMessage: ((PPOProjectCardModel) -> Void)? = nil
var onPerformAction: ((PPOProjectCardModel, PPOProjectCardModel.Action) -> Void)? = nil
stevestreza-ksr marked this conversation as resolved.
Show resolved Hide resolved

var body: some View {
VStack(spacing: Constants.spacing) {
self.flagList
self.projectDetails(leadingColumnWidth: self.viewModel.parentSize.width * Constants.firstColumnWidth)
self.projectDetails(leadingColumnWidth: self.parentSize.width * Constants.firstColumnWidth)
self.divider
self.projectCreator
self.divider
self.addressDetails(leadingColumnWidth: self.viewModel.parentSize.width * Constants.firstColumnWidth)
self.addressDetails(leadingColumnWidth: self.parentSize.width * Constants.firstColumnWidth)
self.actionButtons
}
.padding(.vertical)
Expand All @@ -32,8 +37,16 @@ struct PPOProjectCard: View {
content: { self.badge.opacity(self.viewModel.card.isUnread ? 1 : 0) }
)

// insets
.padding(.horizontal, Constants.outerPadding)
// Handle actions
.onReceive(self.viewModel.showProjectTapped) {
self.onShowProject?(self.viewModel.card)
}
.onReceive(self.viewModel.sendMessageTapped) {
self.onSendMessage?(self.viewModel.card)
}
.onReceive(self.viewModel.actionPerformed) { action in
self.onPerformAction?(self.viewModel.card, action)
}
}

@ViewBuilder
Expand Down Expand Up @@ -67,7 +80,7 @@ struct PPOProjectCard: View {
@ViewBuilder
private func projectDetails(leadingColumnWidth: CGFloat) -> some View {
PPOProjectDetails(
imageUrl: self.viewModel.card.imageURL,
image: self.viewModel.card.image,
title: self.viewModel.card.title,
pledge: self.viewModel.card.pledge,
leadingColumnWidth: leadingColumnWidth
Expand All @@ -77,8 +90,13 @@ struct PPOProjectCard: View {

@ViewBuilder
private var projectCreator: some View {
PPOProjectCreator(creatorName: self.viewModel.card.creatorName)
.padding([.horizontal])
PPOProjectCreator(
creatorName: self.viewModel.card.creatorName,
onSendMessage: { [weak viewModel] () in
viewModel?.sendCreatorMessage()
}
)
.padding([.horizontal])
}

@ViewBuilder
Expand Down Expand Up @@ -146,7 +164,10 @@ struct PPOProjectCard: View {
ScrollView(.vertical) {
VStack(spacing: 16) {
ForEach(PPOProjectCardModel.previewTemplates) { template in
PPOProjectCard(viewModel: PPOProjectCardViewModel(card: template, parentSize: geometry.size))
PPOProjectCard(
viewModel: PPOProjectCardViewModel(card: template),
parentSize: geometry.size
)
}
}
}
Expand Down
stevestreza-ksr marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import Foundation
import Kingfisher
import KsApi
import Library

public struct PPOProjectCardModel: Identifiable, Equatable {
public struct PPOProjectCardModel: Identifiable, Equatable, Hashable {
public let isUnread: Bool
public let alerts: [Alert]
public let imageURL: URL
public let image: Kingfisher.Source
public let title: String
public let pledge: GraphAPI.MoneyFragment
public let creatorName: String
Expand All @@ -14,6 +15,18 @@ public struct PPOProjectCardModel: Identifiable, Equatable {
public let tierType: TierType
public let projectAnalytics: GraphAPI.ProjectAnalyticsFragment

public func hash(into hasher: inout Hasher) {
hasher.combine(self.isUnread)
hasher.combine(self.alerts)
hasher.combine(self.image)
hasher.combine(self.title)
hasher.combine(self.creatorName)
hasher.combine(self.address)
stevestreza-ksr marked this conversation as resolved.
Show resolved Hide resolved
hasher.combine(self.actions.0)
hasher.combine(self.actions.1)
hasher.combine(self.tierType)
}

// MARK: - Identifiable

public let id = UUID()
Expand All @@ -25,7 +38,7 @@ public struct PPOProjectCardModel: Identifiable, Equatable {
public static func == (lhs: PPOProjectCardModel, rhs: PPOProjectCardModel) -> Bool {
lhs.isUnread == rhs.isUnread &&
lhs.alerts == rhs.alerts &&
lhs.imageURL == rhs.imageURL &&
lhs.image == rhs.image &&
lhs.title == rhs.title &&
lhs.pledge == rhs.pledge &&
lhs.creatorName == rhs.creatorName &&
Expand All @@ -40,7 +53,7 @@ public struct PPOProjectCardModel: Identifiable, Equatable {
case confirmAddress
}

public enum Action: Identifiable, Equatable {
public enum Action: Identifiable, Equatable, Hashable {
case confirmAddress
case editAddress
case completeSurvey
Expand Down Expand Up @@ -97,7 +110,7 @@ public struct PPOProjectCardModel: Identifiable, Equatable {
}
}

public struct Alert: Identifiable, Equatable {
public struct Alert: Identifiable, Equatable, Hashable {
public let type: AlertType
public let icon: AlertIcon
public let message: String
Expand All @@ -112,7 +125,7 @@ public struct PPOProjectCardModel: Identifiable, Equatable {
"\(self.type)-\(self.icon)-\(self.message)"
}

public enum AlertType: Identifiable, Equatable {
public enum AlertIcon: Identifiable, Equatable {
case time
case alert

Expand All @@ -126,7 +139,7 @@ public struct PPOProjectCardModel: Identifiable, Equatable {
}
}

public enum AlertIcon: Identifiable, Equatable {
public enum AlertType: Identifiable, Equatable {
case warning
case alert

Expand All @@ -144,7 +157,7 @@ public struct PPOProjectCardModel: Identifiable, Equatable {

extension PPOProjectCardModel.Alert {
init?(flag: GraphAPI.PpoCardFragment.Flag) {
let alertType: PPOProjectCardModel.Alert.AlertType? = switch flag.type {
let alertIcon: PPOProjectCardModel.Alert.AlertIcon? = switch flag.icon {
case "alert":
.alert
case "time":
Expand All @@ -153,7 +166,7 @@ extension PPOProjectCardModel.Alert {
nil
}

let alertIcon: PPOProjectCardModel.Alert.AlertIcon? = switch flag.icon {
let alertType: PPOProjectCardModel.Alert.AlertType? = switch flag.type {
case "alert":
.alert
case "warning":
Expand Down Expand Up @@ -193,9 +206,9 @@ extension PPOProjectCardModel {
internal static let confirmAddressTemplate = PPOProjectCardModel(
isUnread: true,
alerts: [
.init(type: .time, icon: .warning, message: "Address locks in 8 hours")
.init(type: .warning, icon: .time, message: "Address locks in 8 hours")
],
imageURL: URL(string: "http://localhost/")!,
image: .network(URL(string: "https:///")!),
title: "Sugardew Island - Your cozy farm shop let’s pretend this is a way way way longer title",
pledge: .init(amount: "50.00", currency: .usd, symbol: "$"),
creatorName: "rokaplay truncate if longer than",
Expand All @@ -213,10 +226,10 @@ extension PPOProjectCardModel {
internal static let addressLockTemplate = PPOProjectCardModel(
isUnread: true,
alerts: [
.init(type: .alert, icon: .warning, message: "Survey available"),
.init(type: .time, icon: .warning, message: "Address locks in 48 hours")
.init(type: .warning, icon: .alert, message: "Survey available"),
.init(type: .warning, icon: .time, message: "Address locks in 48 hours")
],
imageURL: URL(string: "http://localhost/")!,
image: .network(URL(string: "https:///")!),
title: "Sugardew Island - Your cozy farm shop let’s pretend this is a way way way longer title",
pledge: .init(amount: "50.00", currency: .usd, symbol: "$"),
creatorName: "rokaplay truncate if longer than",
Expand All @@ -231,12 +244,12 @@ extension PPOProjectCardModel {
alerts: [
.init(type: .alert, icon: .alert, message: "Payment failed"),
.init(
type: .time,
icon: .alert,
type: .alert,
icon: .time,
message: "Pledge will be dropped in 6 days"
)
],
imageURL: URL(string: "http://localhost/")!,
image: .network(URL(string: "https:///")!),
title: "Sugardew Island - Your cozy farm shop let’s pretend this is a way way way longer title",
pledge: .init(amount: "50.00", currency: .usd, symbol: "$"),
creatorName: "rokaplay truncate if longer than",
Expand All @@ -251,12 +264,12 @@ extension PPOProjectCardModel {
alerts: [
.init(type: .alert, icon: .alert, message: "Card needs authentication"),
.init(
type: .time,
icon: .alert,
type: .alert,
icon: .time,
message: "Pledge will be dropped in 6 days"
)
],
imageURL: URL(string: "http://localhost/")!,
image: .network(URL(string: "https:///")!),
title: "Sugardew Island - Your cozy farm shop let’s pretend this is a way way way longer title",
pledge: .init(amount: "50.00", currency: .usd, symbol: "$"),
creatorName: "rokaplay truncate if longer than",
Expand All @@ -269,9 +282,9 @@ extension PPOProjectCardModel {
internal static let completeSurveyTemplate = PPOProjectCardModel(
isUnread: true,
alerts: [
.init(type: .alert, icon: .warning, message: "Survey available")
.init(type: .warning, icon: .alert, message: "Survey available")
],
imageURL: URL(string: "http://localhost/")!,
image: .network(URL(string: "https:///")!),
title: "Sugardew Island - Your cozy farm shop let’s pretend this is a way way way longer title",
pledge: .init(amount: "50.00", currency: .usd, symbol: "$"),
creatorName: "rokaplay truncate if longer than",
Expand Down Expand Up @@ -324,8 +337,9 @@ extension PPOProjectCardModel {
let backing = card.backing?.fragments.ppoBackingFragment
let ppoProject = backing?.project?.fragments.ppoProjectFragment

let imageURL = ppoProject?.image?.url
let image = ppoProject?.image?.url
.flatMap { URL(string: $0) }
.map { Kingfisher.Source.network($0) }

let title = ppoProject?.name
let pledge = backing?.amount.fragments.moneyFragment
Expand Down Expand Up @@ -364,11 +378,11 @@ extension PPOProjectCardModel {

let projectAnalyticsFragment = backing?.project?.fragments.projectAnalyticsFragment

if let imageURL, let title, let pledge, let creatorName, let projectAnalyticsFragment {
if let image, let title, let pledge, let creatorName, let projectAnalyticsFragment {
self.init(
isUnread: true,
alerts: alerts,
imageURL: imageURL,
image: image,
title: title,
pledge: pledge,
creatorName: creatorName,
Expand Down
Loading