mirror of
https://github.com/meshtastic/Meshtastic-Apple.git
synced 2026-04-20 22:13:56 +00:00
* Add SwiftUI previews for simple helper views Agent-Logs-Url: https://github.com/meshtastic/Meshtastic-Apple/sessions/a2a43e8c-24fd-443a-8a98-13b678770edd Co-authored-by: garthvh <1795163+garthvh@users.noreply.github.com> * Add previews for action buttons, ChannelForm, MetricsColumnDetail, and DeviceOnboarding Agent-Logs-Url: https://github.com/meshtastic/Meshtastic-Apple/sessions/a2a43e8c-24fd-443a-8a98-13b678770edd Co-authored-by: garthvh <1795163+garthvh@users.noreply.github.com> * Add previews for config views, log views, AppLog, Firmware, AppData, and UserConfig Agent-Logs-Url: https://github.com/meshtastic/Meshtastic-Apple/sessions/a2a43e8c-24fd-443a-8a98-13b678770edd Co-authored-by: garthvh <1795163+garthvh@users.noreply.github.com> * Add preview for PositionConfig Agent-Logs-Url: https://github.com/meshtastic/Meshtastic-Apple/sessions/a2a43e8c-24fd-443a-8a98-13b678770edd Co-authored-by: garthvh <1795163+garthvh@users.noreply.github.com> * Fix formatting bugs in #Preview blocks: restore missing .environmentObject/.environment modifiers and add proper tab indentation Agent-Logs-Url: https://github.com/meshtastic/Meshtastic-Apple/sessions/7eeb7a54-7928-466f-8e39-b00d0012a09d Co-authored-by: garthvh <1795163+garthvh@users.noreply.github.com> * Linting fixes --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: garthvh <1795163+garthvh@users.noreply.github.com> Co-authored-by: Garth Vander Houwen <garthvh@yahoo.com>
94 lines
2.6 KiB
Swift
94 lines
2.6 KiB
Swift
import CoreData
|
|
import OSLog
|
|
import SwiftUI
|
|
|
|
struct FavoriteNodeButton: View {
|
|
|
|
@EnvironmentObject var accessoryManager: AccessoryManager
|
|
@Environment(\.managedObjectContext) var context
|
|
|
|
@ObservedObject var node: NodeInfoEntity
|
|
@State var isShowingClientBaseConfirmation = false
|
|
|
|
var body: some View {
|
|
let connectedRoleIsClientBase = accessoryManager.connectedDeviceRole == DeviceRoles.clientBase
|
|
Button {
|
|
// Special case for CLIENT_BASE: show confirmation when attempting to favorite a node
|
|
if connectedRoleIsClientBase && !node.favorite {
|
|
isShowingClientBaseConfirmation = true
|
|
return
|
|
}
|
|
// Normal case: perform action immediately
|
|
guard let connectedNodeNum = accessoryManager.activeDeviceNum else { return }
|
|
Task {
|
|
await assignFavorite(node: node, setToFavorite: !node.favorite, connectedNodeNum: Int64(connectedNodeNum))
|
|
}
|
|
} label: {
|
|
Label {
|
|
Text(node.favorite ? "Remove from favorites" : "Add to favorites")
|
|
} icon: {
|
|
Image(systemName: node.favorite ? "star.fill" : "star")
|
|
.symbolRenderingMode(.multicolor)
|
|
}
|
|
}
|
|
.confirmationDialog(
|
|
"Are you sure?",
|
|
isPresented: $isShowingClientBaseConfirmation,
|
|
titleVisibility: .visible
|
|
) {
|
|
Button("Yes, I control this node") {
|
|
guard let connectedNodeNum = accessoryManager.activeDeviceNum else { return }
|
|
Task {
|
|
await assignFavorite(node: node, setToFavorite: true, connectedNodeNum: Int64(connectedNodeNum))
|
|
}
|
|
}
|
|
Button("Cancel", role: .cancel) { }
|
|
} message: {
|
|
Text("Client Base should only favorite other nodes you control. Improper use will hurt your local mesh.")
|
|
}
|
|
}
|
|
|
|
private func assignFavorite (node: NodeInfoEntity, setToFavorite: Bool, connectedNodeNum: Int64) async {
|
|
do {
|
|
if setToFavorite {
|
|
try await accessoryManager.setFavoriteNode(
|
|
node: node,
|
|
connectedNodeNum: Int64(connectedNodeNum)
|
|
)
|
|
} else {
|
|
try await accessoryManager.removeFavoriteNode(
|
|
node: node,
|
|
connectedNodeNum: Int64(connectedNodeNum)
|
|
)
|
|
}
|
|
|
|
Task { @MainActor in
|
|
// Update CoreData
|
|
node.favorite = setToFavorite
|
|
|
|
do {
|
|
try context.save()
|
|
} catch {
|
|
context.rollback()
|
|
Logger.data.error("Save Node Favorite Error")
|
|
}
|
|
Logger.data.debug("Favorited a node")
|
|
}
|
|
} catch {
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
let context = PersistenceController.preview.container.viewContext
|
|
let node = NodeInfoEntity(context: context)
|
|
node.num = 123456789
|
|
let user = UserEntity(context: context)
|
|
user.longName = "Test Node"
|
|
user.shortName = "TN"
|
|
node.user = user
|
|
return FavoriteNodeButton(node: node)
|
|
.environmentObject(AccessoryManager.shared)
|
|
.environment(\.managedObjectContext, context)
|
|
}
|