From 15ee57f23b1b57d01e9ef35f3f9825de035c74c5 Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Tue, 20 Feb 2024 19:20:21 -0800 Subject: [PATCH] Add a function to find the first missing channel index and use it. --- Meshtastic/Views/Settings/Channels.swift | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/Meshtastic/Views/Settings/Channels.swift b/Meshtastic/Views/Settings/Channels.swift index d71a4bd1..1ff1d65f 100644 --- a/Meshtastic/Views/Settings/Channels.swift +++ b/Meshtastic/Views/Settings/Channels.swift @@ -98,12 +98,15 @@ struct Channels: View { Button { let lastChannel = node?.myInfo?.channels?.lastObject as? ChannelEntity - var lastChannelIndex = lastChannel?.index ?? 0 + + let channelIndexes = node?.myInfo?.channels?.compactMap({(ch) -> Int in + return (ch as AnyObject).index + }) + var firstChannelIndex = firstMissingChannelIndex(channelIndexes ?? []) channelKeySize = 16 let key = generateChannelKey(size: channelKeySize) - channelName = "" - channelIndex = Int32(lastChannelIndex + 1) + channelIndex = Int32(firstChannelIndex) channelRole = 2 channelKey = key uplink = false @@ -365,3 +368,12 @@ struct Channels: View { } } } + +func firstMissingChannelIndex(_ indexes: [Int]) -> Int { + for element in 1...indexes.count { + if !indexes.contains(element) { + return element + } + } + return indexes.count + 1 +}