passepartout-apple/Passepartout/App/Views/OnDemandView+SSID.swift

147 lines
4.3 KiB
Swift
Raw Normal View History

2022-04-12 13:09:14 +00:00
//
// OnDemandView+SSID.swift
// Passepartout
//
// Created by Davide De Rosa on 2/23/22.
2024-01-14 13:34:21 +00:00
// Copyright (c) 2024 Davide De Rosa. All rights reserved.
2022-04-12 13:09:14 +00:00
//
// https://github.com/passepartoutvpn
//
// This file is part of Passepartout.
//
// Passepartout is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Passepartout is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
//
2022-06-23 21:31:01 +00:00
import PassepartoutLibrary
import SwiftUI
2022-04-12 13:09:14 +00:00
extension OnDemandView {
struct SSIDList: View {
@Binding var withSSIDs: [String: Bool]
2023-03-17 20:55:47 +00:00
2023-05-24 16:19:47 +00:00
// FIXME: arch, this is a candidate for DI
@StateObject private var reader = Wifi(observer: CoreLocationWifiObserver())
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
var body: some View {
EditableTextList(elements: allSSIDs, allowsDuplicates: false, mapping: mapElements) { text in
requestSSID(text)
2022-04-12 13:09:14 +00:00
} textField: {
ssidRow(callback: $0)
} addLabel: {
Text(L10n.OnDemand.Items.AddSsid.caption)
} commitLabel: {
Text(L10n.Global.Strings.add)
}
}
}
}
2023-03-17 20:55:47 +00:00
// MARK: -
private extension OnDemandView.SSIDList {
func mapElements(elements: [IdentifiableString]) -> [IdentifiableString] {
elements
.filter { !$0.string.isEmpty }
.sorted { $0.string.lowercased() < $1.string.lowercased() }
}
2023-03-17 20:55:47 +00:00
func ssidRow(callback: EditableTextFieldCallback) -> some View {
Group {
if callback.isNewElement {
ssidField(callback: callback)
} else {
Toggle(isOn: isSSIDOn(callback.text.wrappedValue)) {
2022-04-12 13:09:14 +00:00
ssidField(callback: callback)
}
}
}
}
2023-03-17 20:55:47 +00:00
func ssidField(callback: EditableTextFieldCallback) -> some View {
TextField(
Unlocalized.Network.ssid,
text: callback.text,
onEditingChanged: callback.onEditingChanged,
onCommit: callback.onCommit
).themeValidSSID(callback.text.wrappedValue)
2022-04-12 13:09:14 +00:00
}
var allSSIDs: Binding<[String]> {
2022-04-12 13:09:14 +00:00
.init {
Array(withSSIDs.keys)
} set: { newValue in
withSSIDs.forEach {
guard newValue.contains($0.key) else {
withSSIDs.removeValue(forKey: $0.key)
return
}
}
newValue.forEach {
guard withSSIDs[$0] == nil else {
return
}
withSSIDs[$0] = false
}
// print(">>> withSSIDs (allSSIDs): \(withSSIDs)")
}
}
2023-03-17 20:55:47 +00:00
var onSSIDs: Binding<Set<String>> {
2022-04-12 13:09:14 +00:00
.init {
Set(withSSIDs.filter {
$0.value
}.map(\.key))
} set: { newValue in
withSSIDs.forEach {
guard newValue.contains($0.key) else {
2023-03-19 13:41:53 +00:00
if withSSIDs[$0.key] != nil {
2022-04-12 13:09:14 +00:00
withSSIDs[$0.key] = false
} else {
withSSIDs.removeValue(forKey: $0.key)
}
return
}
}
newValue.forEach {
guard !(withSSIDs[$0] ?? false) else {
return
}
withSSIDs[$0] = true
}
// print(">>> withSSIDs (onSSIDs): \(withSSIDs)")
}
}
2023-03-17 20:55:47 +00:00
func isSSIDOn(_ ssid: String) -> Binding<Bool> {
2022-04-12 13:09:14 +00:00
.init {
withSSIDs[ssid] ?? false
} set: {
withSSIDs[ssid] = $0
}
}
}
// MARK: -
private extension OnDemandView.SSIDList {
func requestSSID(_ text: Binding<String>) {
Task { @MainActor in
let ssid = try await reader.currentSSID()
if !withSSIDs.keys.contains(ssid) {
text.wrappedValue = ssid
}
}
}
}