Finish hooking up favorites

This commit is contained in:
Garth Vander Houwen 2024-03-31 14:45:30 -07:00
parent 52c52ae1e6
commit 515bc7fcd6
3 changed files with 22 additions and 21 deletions

View file

@ -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")

View file

@ -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<UserEntity>
@ -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

View file

@ -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