Skip to content

Commit 21a4216

Browse files
committed
Send action message correctly
1 parent 3f07cc1 commit 21a4216

File tree

18 files changed

+108
-100
lines changed

18 files changed

+108
-100
lines changed

MonitoredAppMiddleware/Sources/MonitoredAppMiddleware/CodeGen/EnumCodable.generated.swift

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -47,63 +47,3 @@ extension MessageType: Codable {
4747
}
4848
}
4949
}
50-
extension PayloadTree: Codable {
51-
enum CodingKeys: String, CodingKey {
52-
case type
53-
case associatedValues
54-
55-
enum UnkeyedKeys: String, CodingKey {
56-
case associatedValue0
57-
}
58-
enum KeyedKeys: String, CodingKey {
59-
case key
60-
case value
61-
}
62-
enum ArrayKeys: String, CodingKey {
63-
case key
64-
case values
65-
}
66-
}
67-
68-
public init(from decoder: Decoder) throws {
69-
let container = try decoder.container(keyedBy: CodingKeys.self)
70-
switch try container.decode(String.self, forKey: .type) {
71-
case "unkeyed":
72-
let subContainer = try container.nestedContainer(keyedBy: CodingKeys.UnkeyedKeys.self, forKey: .associatedValues)
73-
let associatedValues0 = try subContainer.decode(String.self, forKey: .associatedValue0)
74-
self = .unkeyed(associatedValues0)
75-
case "keyed":
76-
let subContainer = try container.nestedContainer(keyedBy: CodingKeys.KeyedKeys.self, forKey: .associatedValues)
77-
let associatedValues0 = try subContainer.decode(String.self, forKey: .key)
78-
let associatedValues1 = try subContainer.decode(String.self, forKey: .value)
79-
self = .keyed(key: associatedValues0, value: associatedValues1)
80-
case "array":
81-
let subContainer = try container.nestedContainer(keyedBy: CodingKeys.ArrayKeys.self, forKey: .associatedValues)
82-
let associatedValues0 = try subContainer.decode(String.self, forKey: .key)
83-
let associatedValues1 = try subContainer.decode([PayloadTree].self, forKey: .values)
84-
self = .array(key: associatedValues0, values: associatedValues1)
85-
default:
86-
throw DecodingError.keyNotFound(CodingKeys.type, .init(codingPath: container.codingPath, debugDescription: "Unknown key"))
87-
}
88-
}
89-
90-
public func encode(to encoder: Encoder) throws {
91-
var container = encoder.container(keyedBy: CodingKeys.self)
92-
switch self {
93-
case let .unkeyed(associatedValue0):
94-
try container.encode("unkeyed", forKey: .type)
95-
var subContainer = container.nestedContainer(keyedBy: CodingKeys.UnkeyedKeys.self, forKey: .associatedValues)
96-
try subContainer.encode(associatedValue0, forKey: .associatedValue0)
97-
case let .keyed(key, value):
98-
try container.encode("keyed", forKey: .type)
99-
var subContainer = container.nestedContainer(keyedBy: CodingKeys.KeyedKeys.self, forKey: .associatedValues)
100-
try subContainer.encode(key, forKey: .key)
101-
try subContainer.encode(value, forKey: .value)
102-
case let .array(key, values):
103-
try container.encode("array", forKey: .type)
104-
var subContainer = container.nestedContainer(keyedBy: CodingKeys.ArrayKeys.self, forKey: .associatedValues)
105-
try subContainer.encode(key, forKey: .key)
106-
try subContainer.encode(values, forKey: .values)
107-
}
108-
}
109-
}

MonitoredAppMiddleware/Sources/MonitoredAppMiddleware/Model/ActionMessage.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import SwiftRex
44
public struct ActionMessage: Codable, Equatable {
55
public let remoteDate: Date
66
public let action: String
7-
public let actionPayload: PayloadTree
8-
public let state: PayloadTree
7+
public let state: Data?
98
public let actionSource: ActionSource
109
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import Foundation
22

33
// sourcery: EnumCodable
4-
public indirect enum PayloadTree: Equatable {
5-
case unkeyed(String)
6-
case keyed(key: String, value: String)
7-
case array(key: String, values: [PayloadTree])
8-
}
4+
//public indirect enum PayloadTree: Equatable {
5+
// case unkeyed(String)
6+
// case keyed(key: String, value: String)
7+
// case array(key: String, values: [PayloadTree])
8+
//}

MonitoredAppMiddleware/Sources/MonitoredAppMiddleware/MonitoredAppMiddleware.swift

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import MultipeerConnectivity
55
import MultipeerMiddleware
66
import SwiftRex
77

8-
public final class MonitoredAppMiddleware<Action, State>: Middleware {
8+
public final class MonitoredAppMiddleware<Action, State: Encodable & Equatable>: Middleware {
99
public typealias InputActionType = Action
1010
public typealias OutputActionType = Action
1111
public typealias StateType = State
@@ -20,7 +20,7 @@ public final class MonitoredAppMiddleware<Action, State>: Middleware {
2020
public init(
2121
multipeerSession: (() -> MultipeerSession)? = nil,
2222
advertiser: (() -> MultipeerAdvertiserPublisher)? = nil,
23-
encoder: @escaping () -> JSONEncoder = JSONEncoder.init
23+
encoder: @escaping () -> JSONEncoder = sortedOrderEncoder
2424
) {
2525
let name = "\(appName()) 📱 \(deviceName())"
2626
let myselfAsPeer = MCPeerID(displayName: name)
@@ -52,8 +52,10 @@ public final class MonitoredAppMiddleware<Action, State>: Middleware {
5252
}
5353

5454
public func handle(action: InputActionType, from dispatcher: ActionSource, afterReducer: inout AfterReducer) {
55+
let oldState = getState?()
5556
afterReducer = .do {
56-
self.sendAction(action: action, from: dispatcher)
57+
let newState = self.getState?()
58+
self.sendAction(action: action, from: dispatcher, changedState: oldState == newState ? nil : newState)
5759
}
5860
}
5961

@@ -78,14 +80,13 @@ public final class MonitoredAppMiddleware<Action, State>: Middleware {
7880
)
7981
}
8082

81-
private func sendAction(action: InputActionType, from dispatcher: ActionSource) {
83+
private func sendAction(action: InputActionType, from dispatcher: ActionSource, changedState: State?) {
8284
send(message:
8385
MessageType.action(
8486
ActionMessage(
8587
remoteDate: Date(),
8688
action: "\(action)",
87-
actionPayload: .unkeyed(""),
88-
state: .unkeyed(""),
89+
state: changedState.flatMap { try? encoder().encode($0) },
8990
actionSource: dispatcher
9091
)
9192
)
@@ -101,6 +102,15 @@ public final class MonitoredAppMiddleware<Action, State>: Middleware {
101102
}
102103
}
103104

105+
extension MonitoredAppMiddleware {
106+
public static func sortedOrderEncoder() -> JSONEncoder {
107+
let encoder = JSONEncoder()
108+
encoder.outputFormatting = [.sortedKeys, .withoutEscapingSlashes]
109+
encoder.dateEncodingStrategy = .iso8601
110+
return encoder
111+
}
112+
}
113+
104114
private func mainBundle() -> Bundle { Bundle.main }
105115
private func serviceType() -> String { "swiftrex-mon" }
106116
private func appName() -> String { mainBundle().infoDictionary?["CFBundleName"] as? String ?? "" }

SampleApp/PointFreePrimeTime/Counter/Counter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public let counterViewReducer: Reducer<CounterViewAction, CounterViewState> =
117117
stateSetter: setter(\CounterViewState.primeModal)
118118
)
119119

120-
public struct PrimeAlert: Equatable, Identifiable {
120+
public struct PrimeAlert: Equatable, Identifiable, Encodable {
121121
let prime: Int
122122
public var id: Int { self.prime }
123123
}

SampleApp/PointFreePrimeTime/PrimeTime/ContentView.swift

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import SwiftRex
66
import SwiftUI
77
import Utils
88

9-
struct AppState: Equatable {
9+
struct AppState: Equatable, Encodable {
1010
var count = 0
1111
var favoritePrimes: [Int] = []
1212
var loggedInUser: User?
@@ -15,17 +15,32 @@ struct AppState: Equatable {
1515
var isNthPrimeButtonDisabled: Bool = false
1616
var isPrimeModalShown: Bool = false
1717

18-
struct Activity: Equatable {
18+
struct Activity: Equatable, Encodable {
1919
let timestamp: Date
2020
let type: ActivityType
2121

22-
enum ActivityType: Equatable {
22+
enum ActivityType: Equatable, Encodable {
2323
case addedFavoritePrime(Int)
2424
case removedFavoritePrime(Int)
25+
26+
enum CodingKeys: String, CodingKey {
27+
case addedFavoritePrime
28+
case removedFavoritePrime
29+
}
30+
31+
func encode(to encoder: Encoder) throws {
32+
var container = encoder.container(keyedBy: CodingKeys.self)
33+
switch self {
34+
case let .addedFavoritePrime(addedFavoritePrime):
35+
try container.encode(addedFavoritePrime, forKey: .addedFavoritePrime)
36+
case let .removedFavoritePrime(removedFavoritePrime):
37+
try container.encode(removedFavoritePrime, forKey: .removedFavoritePrime)
38+
}
39+
}
2540
}
2641
}
2742

28-
struct User: Equatable {
43+
struct User: Equatable, Encodable {
2944
let id: Int
3045
let name: String
3146
let bio: String

SwiftRexMonitor/.sourcery.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ templates:
55
output:
66
CodeGen
77
args:
8-
imports: ["MonitoredAppMiddleware", "MultipeerConnectivity", "MultipeerMiddleware"]
8+
imports: ["MonitoredAppMiddleware", "MultipeerConnectivity", "MultipeerMiddleware", "SwiftRex"]
99
testable-imports: []

SwiftRexMonitor/CodeGen/AutoMockable.generated.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ import AppKit
1313
import MonitoredAppMiddleware
1414
import MultipeerConnectivity
1515
import MultipeerMiddleware
16+
import SwiftRex
1617

SwiftRexMonitor/CodeGen/EnumCodable.generated.swift

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import MonitoredAppMiddleware
66
import MultipeerConnectivity
77
import MultipeerMiddleware
8+
import SwiftRex
89
extension AppAction: Encodable {
910
enum CodingKeys: String, CodingKey {
1011
case type
@@ -48,7 +49,10 @@ extension MonitorAction: Codable {
4849
case from
4950
}
5051
enum GotActionKeys: String, CodingKey {
51-
case associatedValue0
52+
case action
53+
case remoteDate
54+
case state
55+
case actionSource
5256
case peer
5357
}
5458
enum GotGreetingsKeys: String, CodingKey {
@@ -75,9 +79,12 @@ extension MonitorAction: Codable {
7579
self = .evaluateData(associatedValues0, from: associatedValues1)
7680
case "gotAction":
7781
let subContainer = try container.nestedContainer(keyedBy: CodingKeys.GotActionKeys.self, forKey: .associatedValues)
78-
let associatedValues0 = try subContainer.decode(ActionMessage.self, forKey: .associatedValue0)
79-
let associatedValues1 = try subContainer.decode(Peer.self, forKey: .peer)
80-
self = .gotAction(associatedValues0, peer: associatedValues1)
82+
let associatedValues0 = try subContainer.decode(String.self, forKey: .action)
83+
let associatedValues1 = try subContainer.decode(Date.self, forKey: .remoteDate)
84+
let associatedValues2 = try subContainer.decode(String?.self, forKey: .state)
85+
let associatedValues3 = try subContainer.decode(ActionSource.self, forKey: .actionSource)
86+
let associatedValues4 = try subContainer.decode(Peer.self, forKey: .peer)
87+
self = .gotAction(action: associatedValues0, remoteDate: associatedValues1, state: associatedValues2, actionSource: associatedValues3, peer: associatedValues4)
8188
case "gotGreetings":
8289
let subContainer = try container.nestedContainer(keyedBy: CodingKeys.GotGreetingsKeys.self, forKey: .associatedValues)
8390
let associatedValues0 = try subContainer.decode(PeerMetadata.self, forKey: .associatedValue0)
@@ -104,10 +111,13 @@ extension MonitorAction: Codable {
104111
var subContainer = container.nestedContainer(keyedBy: CodingKeys.EvaluateDataKeys.self, forKey: .associatedValues)
105112
try subContainer.encode(associatedValue0, forKey: .associatedValue0)
106113
try subContainer.encode(from, forKey: .from)
107-
case let .gotAction(associatedValue0, peer):
114+
case let .gotAction(action, remoteDate, state, actionSource, peer):
108115
try container.encode("gotAction", forKey: .type)
109116
var subContainer = container.nestedContainer(keyedBy: CodingKeys.GotActionKeys.self, forKey: .associatedValues)
110-
try subContainer.encode(associatedValue0, forKey: .associatedValue0)
117+
try subContainer.encode(action, forKey: .action)
118+
try subContainer.encode(remoteDate, forKey: .remoteDate)
119+
try subContainer.encode(state, forKey: .state)
120+
try subContainer.encode(actionSource, forKey: .actionSource)
111121
try subContainer.encode(peer, forKey: .peer)
112122
case let .gotGreetings(associatedValue0, peer):
113123
try container.encode("gotGreetings", forKey: .type)

SwiftRexMonitor/CodeGen/Prism.generated.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import MonitoredAppMiddleware
66
import MultipeerConnectivity
77
import MultipeerMiddleware
8+
import SwiftRex
89
extension AppAction {
910
public var start: Void? {
1011
get {
@@ -101,14 +102,14 @@ extension MonitorAction {
101102
self.evaluateData != nil
102103
}
103104

104-
public var gotAction: (ActionMessage, peer: Peer)? {
105+
public var gotAction: (action: String, remoteDate: Date, state: String?, actionSource: ActionSource, peer: Peer)? {
105106
get {
106-
guard case let .gotAction(associatedValue0, peer) = self else { return nil }
107-
return (associatedValue0, peer)
107+
guard case let .gotAction(action, remoteDate, state, actionSource, peer) = self else { return nil }
108+
return (action, remoteDate, state, actionSource, peer)
108109
}
109110
set {
110111
guard case .gotAction = self, let newValue = newValue else { return }
111-
self = .gotAction(newValue.0, peer: newValue.1)
112+
self = .gotAction(action: newValue.0, remoteDate: newValue.1, state: newValue.2, actionSource: newValue.3, peer: newValue.4)
112113
}
113114
}
114115

0 commit comments

Comments
 (0)