From 1dc2cf46685328397cd9122e8ebd1dc5f4664af2 Mon Sep 17 00:00:00 2001 From: Blake McAnally Date: Mon, 8 Jul 2024 00:31:45 -0500 Subject: [PATCH] Refactor show/hide alerts button --- Localizable.xcstrings | 6 ++++ Meshtastic.xcodeproj/project.pbxproj | 14 +++++++- .../{ => Actions}/FavoriteNodeButton.swift | 0 .../Helpers/Actions/NodeAlertsButton.swift | 33 +++++++++++++++++++ .../Views/Nodes/Helpers/NodeDetail.swift | 8 +++++ Meshtastic/Views/Nodes/NodeList.swift | 20 ++++------- 6 files changed, 67 insertions(+), 14 deletions(-) rename Meshtastic/Views/Nodes/Helpers/{ => Actions}/FavoriteNodeButton.swift (100%) create mode 100644 Meshtastic/Views/Nodes/Helpers/Actions/NodeAlertsButton.swift diff --git a/Localizable.xcstrings b/Localizable.xcstrings index b030b8b4..a74c28a8 100644 --- a/Localizable.xcstrings +++ b/Localizable.xcstrings @@ -8207,6 +8207,9 @@ }, "Help with App Development" : { + }, + "Hide alerts" : { + }, "Hide Alerts" : { @@ -19627,6 +19630,9 @@ }, "Short Name: %@" : { + }, + "Show alerts" : { + }, "Show Alerts" : { diff --git a/Meshtastic.xcodeproj/project.pbxproj b/Meshtastic.xcodeproj/project.pbxproj index dac78a1a..76194afc 100644 --- a/Meshtastic.xcodeproj/project.pbxproj +++ b/Meshtastic.xcodeproj/project.pbxproj @@ -8,6 +8,7 @@ /* Begin PBXBuildFile section */ 251926852C3BA97800249DF5 /* FavoriteNodeButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 251926842C3BA97800249DF5 /* FavoriteNodeButton.swift */; }; + 251926872C3BAE2200249DF5 /* NodeAlertsButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 251926862C3BAE2200249DF5 /* NodeAlertsButton.swift */; }; 259792252C2F114500AD1659 /* ChannelEntityExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD58C5F12919AD3C00D5BEFB /* ChannelEntityExtension.swift */; }; 259792262C2F114500AD1659 /* PositionEntityExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD5394FD276BA0EF00AD86B1 /* PositionEntityExtension.swift */; }; 259792272C2F114500AD1659 /* TraceRouteEntityExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDE5B4052B227E3200FCDD05 /* TraceRouteEntityExtension.swift */; }; @@ -224,6 +225,7 @@ /* Begin PBXFileReference section */ 251926842C3BA97800249DF5 /* FavoriteNodeButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FavoriteNodeButton.swift; sourceTree = ""; }; + 251926862C3BAE2200249DF5 /* NodeAlertsButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NodeAlertsButton.swift; sourceTree = ""; }; 25AECD4E2C2F723200862C8E /* Localizable.xcstrings */ = {isa = PBXFileReference; lastKnownFileType = text.json.xcstrings; path = Localizable.xcstrings; sourceTree = ""; }; 6D825E612C34786C008DBEE4 /* CommonRegex.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommonRegex.swift; sourceTree = ""; }; 6DA39D8D2A92DC52007E311C /* MeshtasticAppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MeshtasticAppDelegate.swift; sourceTree = ""; }; @@ -473,6 +475,15 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 251926882C3BAF2E00249DF5 /* Actions */ = { + isa = PBXGroup; + children = ( + 251926842C3BA97800249DF5 /* FavoriteNodeButton.swift */, + 251926862C3BAE2200249DF5 /* NodeAlertsButton.swift */, + ); + path = Actions; + sourceTree = ""; + }; C9483F6B2773016700998F6B /* MapKitMap */ = { isa = PBXGroup; children = ( @@ -876,7 +887,7 @@ DDDB26452AACC0B7003AFCB7 /* NodeInfoItem.swift */, DDDB26412AABF655003AFCB7 /* NodeListItem.swift */, DDDCD56F2BB26F5C00BE6B60 /* NodeListFilter.swift */, - 251926842C3BA97800249DF5 /* FavoriteNodeButton.swift */, + 251926882C3BAF2E00249DF5 /* Actions */, ); path = Helpers; sourceTree = ""; @@ -1191,6 +1202,7 @@ DDB6ABE028B13AC700384BA1 /* DeviceEnums.swift in Sources */, DD86D40C287F401000BAEB7A /* SaveChannelQRCode.swift in Sources */, D93068DD2B81CA820066FBC8 /* ConfigHeader.swift in Sources */, + 251926872C3BAE2200249DF5 /* NodeAlertsButton.swift in Sources */, DDA1C48E28DB49D3009933EC /* ChannelRoles.swift in Sources */, D9BC22DB2B7DE8E2006A37D5 /* TileDownloadStatus.swift in Sources */, DDD5BB092C285DDC007E03CA /* AppLog.swift in Sources */, diff --git a/Meshtastic/Views/Nodes/Helpers/FavoriteNodeButton.swift b/Meshtastic/Views/Nodes/Helpers/Actions/FavoriteNodeButton.swift similarity index 100% rename from Meshtastic/Views/Nodes/Helpers/FavoriteNodeButton.swift rename to Meshtastic/Views/Nodes/Helpers/Actions/FavoriteNodeButton.swift diff --git a/Meshtastic/Views/Nodes/Helpers/Actions/NodeAlertsButton.swift b/Meshtastic/Views/Nodes/Helpers/Actions/NodeAlertsButton.swift new file mode 100644 index 00000000..663d2900 --- /dev/null +++ b/Meshtastic/Views/Nodes/Helpers/Actions/NodeAlertsButton.swift @@ -0,0 +1,33 @@ +import CoreData +import OSLog +import SwiftUI + +struct NodeAlertsButton: View { + var context: NSManagedObjectContext + + @ObservedObject + var node: NodeInfoEntity + + @ObservedObject + var user: UserEntity + + var body: some View { + Button { + user.mute = !user.mute + context.refresh(node, mergeChanges: true) + do { + try context.save() + } catch { + context.rollback() + Logger.data.error("Save User Mute Error") + } + } label: { + Label { + Text(user.mute ? "Show alerts" : "Hide alerts") + } icon: { + Image(systemName: user.mute ? "bell.slash" : "bell") + .symbolRenderingMode(.hierarchical) + } + } + } +} diff --git a/Meshtastic/Views/Nodes/Helpers/NodeDetail.swift b/Meshtastic/Views/Nodes/Helpers/NodeDetail.swift index 757caaa5..ea427587 100644 --- a/Meshtastic/Views/Nodes/Helpers/NodeDetail.swift +++ b/Meshtastic/Views/Nodes/Helpers/NodeDetail.swift @@ -222,6 +222,14 @@ struct NodeDetail: View { context: context, node: node ) + + if let user = node.user { + NodeAlertsButton( + context: context, + node: node, + user: user + ) + } } if let metadata = node.metadata, diff --git a/Meshtastic/Views/Nodes/NodeList.swift b/Meshtastic/Views/Nodes/NodeList.swift index 54af2db4..91fee07f 100644 --- a/Meshtastic/Views/Nodes/NodeList.swift +++ b/Meshtastic/Views/Nodes/NodeList.swift @@ -69,19 +69,13 @@ struct NodeList: View { node: node ) - if node.user != nil { - Button { - node.user!.mute = !node.user!.mute - context.refresh(node, mergeChanges: true) - do { - try context.save() - } catch { - context.rollback() - Logger.data.error("Save User Mute Error") - } - } label: { - Label(node.user!.mute ? "Show Alerts" : "Hide Alerts", systemImage: node.user!.mute ? "bell" : "bell.slash") - } + if let user = node.user { + NodeAlertsButton( + context: context, + node: node, + user: user + ) + if bleManager.connectedPeripheral != nil && node.num != connectedNodeNum { Button { let positionSent = bleManager.sendPosition(