Skip to content

Instantly share code, notes, and snippets.

@saroar
Created May 19, 2023 08:31
Show Gist options
  • Select an option

  • Save saroar/42d3fb0cb5d97a7a4d2a2d02b1861deb to your computer and use it in GitHub Desktop.

Select an option

Save saroar/42d3fb0cb5d97a7a4d2a2d02b1861deb to your computer and use it in GitHub Desktop.
public struct PassContent: Codable, Equatable {
public var headerFields: [Field]?
public var primaryFields: [Field]
public var secondaryFields: [Field]?
public var auxiliaryFields: [Field]?
public var backFields: [Field]?
public init(
headerFields: [Field]? = nil,
primaryFields: [Field],
secondaryFields: [Field]? = nil,
auxiliaryFields: [Field]? = nil,
backFields: [Field]? = nil
) {
self.headerFields = headerFields
self.primaryFields = primaryFields
self.secondaryFields = secondaryFields
self.auxiliaryFields = auxiliaryFields
self.backFields = backFields
}
public static func == (lhs: PassContent, rhs: PassContent) -> Bool {
return lhs.primaryFields == rhs.primaryFields &&
lhs.secondaryFields == rhs.secondaryFields &&
lhs.auxiliaryFields == rhs.auxiliaryFields &&
lhs.headerFields == rhs.headerFields &&
lhs.backFields == rhs.backFields
}
public func hash(into hasher: inout Hasher) {
hasher.combine(headerFields)
hasher.combine(primaryFields)
hasher.combine(secondaryFields)
hasher.combine(auxiliaryFields)
hasher.combine(backFields)
}
}
public struct Field: Codable, Equatable, Hashable {
public var key: String
public var label: String?
public var value: String
public var dateStyle: DateTimeStyle?
public var timeStyle: DateTimeStyle?
public var textAlignment: TextAlignment?
/// only works for Auxiliary Fields
public var row: Int?
public var attributedValue: String?
public init(
key: String,
label: String?,
value: String,
dateStyle: DateTimeStyle? = nil,
timeStyle: DateTimeStyle? = nil,
textAlignment: TextAlignment? = nil,
row: Int? = nil,
attributedValue: String? = nil
) {
self.key = key
self.label = label
self.value = value
self.dateStyle = dateStyle
self.timeStyle = timeStyle
self.textAlignment = textAlignment
self.row = row
self.attributedValue = attributedValue
}
public static func == (lhs: Field, rhs: Field) -> Bool {
return lhs.key == rhs.key &&
lhs.label == rhs.label &&
lhs.value == rhs.value &&
lhs.dateStyle == rhs.dateStyle &&
lhs.timeStyle == rhs.timeStyle &&
lhs.textAlignment == rhs.textAlignment &&
lhs.row == rhs.row &&
lhs.attributedValue == rhs.attributedValue
}
public func hash(into hasher: inout Hasher) {
hasher.combine(key)
hasher.combine(label)
hasher.combine(value)
hasher.combine(dateStyle)
hasher.combine(timeStyle)
hasher.combine(textAlignment)
hasher.combine(row)
hasher.combine(attributedValue)
}
}
public enum PContent: Codable, Equatable {
case coupon(PassContent)
case boardingPass(PassContentTransit)
case storeCard(PassContent)
case eventTicket(PassContent)
case generic(PassContent)
}
/// I.e. content of the pass.json file
/// For all available fields and their documentation, see [this page](https://developer.apple.com/documentation/walvarpasses/pass)
public struct Pass: Codable, Equatable {
public var content: PContent
}
public struct WallatPassRouter: ReducerProtocol {
public struct State: Equatable {
@BindingState public var pass: Pass = .mock
}
public enum Action: BindableAction {
case binding(BindingAction<State>)
}
public init() {}
public var body: some ReducerProtocol<State, Action> {
BindingReducer()
Reduce(self.core)
}
func core(state: inout State, action: Action) -> EffectTask<Action> {
switch action {
case .binding:
return .none
}
}
}
struct GenericPassView: View {
public let store: StoreOf<WallatPassRouter>
public init(store: StoreOf<WallatPassRouter>) {
self.store = store
}
var body: some View {
WithViewStore(store, observe: {$0} ) { viewStore in
VStack {
TextField("Your nice name", text: viewStore.binding(\.$pass.content(.generic.primaryFields.first.value)))
.font(.title)
.fontWeight(.medium)
.lineLimit(2)
.padding(.horizontal)
.padding(.bottom, 5)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment