2020-01-21 01:11:55 +00:00
|
|
|
//
|
|
|
|
// AddPiHoleView.swift
|
|
|
|
// Pi-Helper
|
|
|
|
//
|
|
|
|
// Created by Billy Brawner on 10/19/19.
|
|
|
|
// Copyright © 2019 William Brawner. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2022-10-19 00:52:30 +00:00
|
|
|
import Pihelper
|
2020-01-21 01:11:55 +00:00
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct AddPiHoleView: View {
|
2022-10-19 00:52:30 +00:00
|
|
|
@EnvironmentObject var store: PihelperStore
|
|
|
|
@SwiftUI.State var ipAddress: String = ""
|
|
|
|
@SwiftUI.State var showScanFailed = false
|
|
|
|
@SwiftUI.State var showConnectFailed = false
|
2022-03-04 02:35:21 +00:00
|
|
|
var hideNavigationBar: Bool {
|
|
|
|
get {
|
2022-10-19 00:52:30 +00:00
|
|
|
if case .scan = self.store.state.route {
|
2022-03-04 02:35:21 +00:00
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-21 01:11:55 +00:00
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
NavigationView {
|
|
|
|
ScrollView {
|
|
|
|
VStack(spacing: 30) {
|
|
|
|
Image("logo")
|
2022-10-19 00:52:30 +00:00
|
|
|
ScanButton()
|
2022-03-04 02:35:21 +00:00
|
|
|
.alert(isPresented: self.$showScanFailed, content: {
|
2020-01-23 14:19:03 +00:00
|
|
|
Alert(title: Text("scan_failed"), message: Text("try_direct_connection"), dismissButton: .default(Text("OK"), action: {
|
2022-10-19 00:52:30 +00:00
|
|
|
self.store.sideEffect = nil
|
2020-01-23 14:19:03 +00:00
|
|
|
}))
|
|
|
|
})
|
2020-01-21 01:11:55 +00:00
|
|
|
OrDivider()
|
|
|
|
Text("add_pihole")
|
|
|
|
.multilineTextAlignment(.center)
|
|
|
|
TextField("ip_address", text: $ipAddress)
|
|
|
|
.textFieldStyle(RoundedBorderTextFieldStyle())
|
|
|
|
.keyboardType(.default)
|
2022-03-04 02:35:21 +00:00
|
|
|
.disableAutocorrection(true)
|
|
|
|
.autocapitalization(.none)
|
|
|
|
.onSubmit {
|
2022-10-19 00:52:30 +00:00
|
|
|
self.store.dispatch(ActionScan(deviceIp: self.ipAddress))
|
2022-03-04 02:35:21 +00:00
|
|
|
}
|
2022-10-19 00:52:30 +00:00
|
|
|
Button(action: { self.store.dispatch(ActionConnect(host: ipAddress))
|
|
|
|
}, label: { Text("connect") })
|
2020-01-21 01:11:55 +00:00
|
|
|
.buttonStyle(PiHelperButtonStyle())
|
2022-03-04 02:35:21 +00:00
|
|
|
.alert(isPresented: self.$showConnectFailed, content: {
|
2020-01-23 14:19:03 +00:00
|
|
|
Alert(title: Text("connection_failed"), message: Text("verify_ip_address"), dismissButton: .default(Text("OK"), action: {
|
2022-10-19 00:52:30 +00:00
|
|
|
self.store.sideEffect = nil
|
2020-01-23 14:19:03 +00:00
|
|
|
}))
|
|
|
|
})
|
2020-01-21 01:11:55 +00:00
|
|
|
}
|
|
|
|
.padding()
|
2020-01-24 03:32:51 +00:00
|
|
|
.keyboardAwarePadding()
|
2020-01-21 01:11:55 +00:00
|
|
|
}
|
2022-03-04 02:35:21 +00:00
|
|
|
.navigationBarHidden(self.hideNavigationBar)
|
2020-01-21 01:11:55 +00:00
|
|
|
}
|
|
|
|
.navigationViewStyle(StackNavigationViewStyle())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ScanButton: View {
|
2022-10-19 00:52:30 +00:00
|
|
|
@EnvironmentObject var store: PihelperStore
|
|
|
|
|
|
|
|
@ViewBuilder
|
|
|
|
var body: some View {
|
|
|
|
if let deviceIpAddress = resolver_get_device_ip() {
|
|
|
|
let ipAddress = String(cString: deviceIpAddress)
|
|
|
|
VStack(spacing: 30) {
|
|
|
|
Text("scan_for_pihole")
|
|
|
|
.multilineTextAlignment(.center)
|
|
|
|
Button(action: {
|
|
|
|
self.store.dispatch(ActionScan(deviceIp: ipAddress))
|
|
|
|
}, label: { Text("scan") })
|
|
|
|
.buttonStyle(PiHelperButtonStyle())
|
|
|
|
NavigationLink(
|
|
|
|
destination: ScanningView(),
|
|
|
|
isActive: .constant(store.state.route == Route.scan),
|
|
|
|
label: { EmptyView() }
|
|
|
|
)
|
|
|
|
NavigationLink(
|
|
|
|
destination: RetrieveApiKeyView(),
|
|
|
|
isActive: .constant(store.state.route == Route.auth),
|
|
|
|
label: { EmptyView() }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Text("no_wireless_connection")
|
2020-01-21 01:11:55 +00:00
|
|
|
.multilineTextAlignment(.center)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct AddPiHoleView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2022-10-19 00:52:30 +00:00
|
|
|
AddPiHoleView()
|
2020-01-21 01:11:55 +00:00
|
|
|
}
|
|
|
|
}
|