From 515bc7fcd6faa927140ad3ae40616f0ea3503a49 Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Sun, 31 Mar 2024 14:45:30 -0700 Subject: [PATCH] Finish hooking up favorites --- Meshtastic/Helpers/BLEManager.swift | 14 ------------- Meshtastic/Views/Messages/UserList.swift | 26 +++++++++++++++++++----- Meshtastic/Views/Nodes/NodeList.swift | 3 +-- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/Meshtastic/Helpers/BLEManager.swift b/Meshtastic/Helpers/BLEManager.swift index abf65753..d5ca39ef 100644 --- a/Meshtastic/Helpers/BLEManager.swift +++ b/Meshtastic/Helpers/BLEManager.swift @@ -759,20 +759,6 @@ class BLEManager: NSObject, CBPeripheralDelegate, MqttClientProxyManagerDelegate if fetchedNodeInfo.count == 1 && fetchedNodeInfo[0].storeForwardConfig?.enabled == true { wantStoreAndForwardPackets = true; } - if fetchedNodeInfo.count == 1 { - if !(fetchedNodeInfo[0].user?.vip ?? false) { - fetchedNodeInfo[0].favorite = true - fetchedNodeInfo[0].user?.vip = true - do { - try context!.save() - - } catch { - context!.rollback() - let nsError = error as NSError - print("💥 Core Data error. Error: \(nsError)") - } - } - } } catch { print("Failed to find a node info for the connected node") diff --git a/Meshtastic/Views/Messages/UserList.swift b/Meshtastic/Views/Messages/UserList.swift index 978e8c08..3f10b3c0 100644 --- a/Meshtastic/Views/Messages/UserList.swift +++ b/Meshtastic/Views/Messages/UserList.swift @@ -33,7 +33,9 @@ struct UserList: View { } } @FetchRequest( - sortDescriptors: [NSSortDescriptor(key: "lastMessage", ascending: false), NSSortDescriptor(key: "vip", ascending: false), NSSortDescriptor(key: "longName", ascending: true)], + sortDescriptors: [NSSortDescriptor(key: "lastMessage", ascending: false), + NSSortDescriptor(key: "userNode.favorite", ascending: false), + NSSortDescriptor(key: "longName", ascending: true)], animation: .default) private var users: FetchedResults @@ -71,7 +73,7 @@ struct UserList: View { Text(user.longName ?? "unknown".localized) .font(.headline) Spacer() - if user.vip { + if (user.userNode?.favorite ?? false) { Image(systemName: "star.fill") .foregroundColor(.yellow) } @@ -108,15 +110,29 @@ struct UserList: View { .frame(height: 62) .contextMenu { Button { - user.vip = !user.vip + + if node != nil && !(user.userNode?.favorite ?? false) { + let success = bleManager.setFavoriteNode(node: user.userNode!, connectedNodeNum: Int64(node!.num)) + if success { + user.userNode?.favorite = !(user.userNode?.favorite ?? true) + print("Favorited a node") + } + } else { + let success = bleManager.removeFavoriteNode(node: user.userNode!, connectedNodeNum: Int64(node!.num)) + if success { + user.userNode?.favorite = !(user.userNode?.favorite ?? true) + print("Favorited a node") + } + } + context.refresh(user, mergeChanges: true) do { try context.save() } catch { context.rollback() - print("💥 Save User VIP Error") + print("💥 Save Node Favorite Error") } } label: { - Label(user.vip ? "Un-Favorite" : "Favorite", systemImage: user.vip ? "star.slash.fill" : "star.fill") + Label((user.userNode?.favorite ?? false) ? "Un-Favorite" : "Favorite", systemImage: (user.userNode?.favorite ?? false) ? "star.slash.fill" : "star.fill") } Button { user.mute = !user.mute diff --git a/Meshtastic/Views/Nodes/NodeList.swift b/Meshtastic/Views/Nodes/NodeList.swift index 1cb3508e..1dfec72d 100644 --- a/Meshtastic/Views/Nodes/NodeList.swift +++ b/Meshtastic/Views/Nodes/NodeList.swift @@ -51,8 +51,7 @@ struct NodeList: View { .contextMenu { Button { - node.user!.vip = !node.user!.vip - if !node.favorite { + if node.favorite { let success = bleManager.setFavoriteNode(node: node, connectedNodeNum: Int64(connectedNodeNum)) if success { node.favorite = !node.favorite