Add a function to find the first missing channel index and use it.

This commit is contained in:
Garth Vander Houwen 2024-02-20 19:20:21 -08:00
parent 3c623d3441
commit 15ee57f23b

View file

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