pihelper-ios/Pi-helper/AddPiHoleView.swift

104 lines
3.7 KiB
Swift
Raw Normal View History

//
// AddPiHoleView.swift
// Pi-Helper
//
// Created by Billy Brawner on 10/19/19.
// Copyright © 2019 William Brawner. All rights reserved.
//
import Pihelper
import SwiftUI
struct AddPiHoleView: View {
@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 {
if case .scan = self.store.state.route {
2022-03-04 02:35:21 +00:00
return true
} else {
return false
}
}
}
var body: some View {
NavigationView {
ScrollView {
VStack(spacing: 30) {
Image("logo")
ScanButton()
2022-03-04 02:35:21 +00:00
.alert(isPresented: self.$showScanFailed, content: {
Alert(title: Text("scan_failed"), message: Text("try_direct_connection"), dismissButton: .default(Text("OK"), action: {
self.store.sideEffect = nil
}))
})
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 {
self.store.dispatch(ActionScan(deviceIp: self.ipAddress))
2022-03-04 02:35:21 +00:00
}
Button(action: { self.store.dispatch(ActionConnect(host: ipAddress))
}, label: { Text("connect") })
.buttonStyle(PiHelperButtonStyle())
2022-03-04 02:35:21 +00:00
.alert(isPresented: self.$showConnectFailed, content: {
Alert(title: Text("connection_failed"), message: Text("verify_ip_address"), dismissButton: .default(Text("OK"), action: {
self.store.sideEffect = nil
}))
})
}
.padding()
.keyboardAwarePadding()
}
2022-03-04 02:35:21 +00:00
.navigationBarHidden(self.hideNavigationBar)
}
.navigationViewStyle(StackNavigationViewStyle())
}
}
struct ScanButton: View {
@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")
.multilineTextAlignment(.center)
}
}
}
struct AddPiHoleView_Previews: PreviewProvider {
static var previews: some View {
AddPiHoleView()
}
}