diff --git a/RippleChat/BTPeripheral.swift b/RippleChat/BTPeripheral.swift index 37e457d..de02a6f 100644 --- a/RippleChat/BTPeripheral.swift +++ b/RippleChat/BTPeripheral.swift @@ -98,6 +98,7 @@ extension BluetoothPeripheral: CBPeripheralManagerDelegate { let receivedObject = try decoder.decode(WantMessage.self, from: receivedData) // Use the received object to update your app state as needed print("Received Write") + self.incomingMsg = "" self.incomingMsg = receivedObject.printMsg() self.wantVector = receivedObject print(receivedObject.printMsg()) diff --git a/RippleChat/Views/PeeringView.swift b/RippleChat/Views/PeeringView.swift index 2ea0636..2b9903b 100644 --- a/RippleChat/Views/PeeringView.swift +++ b/RippleChat/Views/PeeringView.swift @@ -22,13 +22,17 @@ struct PeeringView: View { List { ForEach(btPeripheral.wantVector.friends.keys.sorted(), id: \.self) { friend in Text("Feed: \(friend.description), SEQ: \(btPeripheral.wantVector.friends[friend] ?? -1)") - // Send new log Entries... } } Text("Incoming msg: \(btPeripheral.incomingMsg)") + .onChange(of: btPeripheral.incomingMsg) { newValue in + compareWithSavedFeeds(newVector: btPeripheral.wantVector) + } Button(action: { do { - let WANT_msg = WantMessage(friends: dataStore.friends) + var combinedDict = dataStore.friends + combinedDict[dataStore.personalFeed.feedID] = dataStore.personalFeed.feed.count + let WANT_msg = WantMessage(friends: combinedDict) let encoded_msg = try JSONEncoder().encode(WANT_msg) btController.writeToCharacteristics(message: String(data: encoded_msg, encoding: .utf8)!) //btController.writeToCharacteristics(message: "Test") @@ -42,6 +46,20 @@ struct PeeringView: View { .padding() } } + + func compareWithSavedFeeds(newVector: WantMessage) { + print("comparing with saved Feeds...") + for friend in newVector.friends.keys.sorted() { + if(dataStore.friends.keys.contains(friend.description)) { + print("Found friend \(friend.description) in own Feeds!") + var missingFeedEntries: Int = -1 + if let ownCount = dataStore.friends[friend] { + missingFeedEntries = newVector.friends[friend]! - ownCount + } + print("You are \(missingFeedEntries) behind on the feed of \(friend.description)") + } + } + } } struct PeeringView_Previews: PreviewProvider {