Files
mowesta/ios-app/app/View/SetupView.swift
T
Marcus Handte 2c788e3329
LocationBasedServices/mowesta/pipeline/head This commit looks good
Add weather station configuration to ios app.
2022-11-03 16:47:26 +01:00

276 lines
10 KiB
Swift

//
// SetupView.swift - The view used to configure an arduino.
//
// Created by Marcus Handte on 02.11.22.
//
import SwiftUI
struct SetupView: View {
@ObservedObject var state = ArduinoState()
@State var locator = ArduinoLocator()
var body: some View {
GeometryReader { proxy in
ScrollView {
if state.phase == .configure {
ConfigureView(state: state, locator: locator)
} else {
ConnectView(state: state)
.frame(minHeight: proxy.size.height)
.frame(width: proxy.size.width)
}
}.navigationTitle("setup_title")
}
}
}
struct ConfigureView: View {
@ObservedObject var state: ArduinoState
@ObservedObject var locator: ArduinoLocator
@State var manual = false
var body: some View {
VStack {
Text("setup_explain")
Link("setup_link", destination: URL(string: "https://www.mowesta.com/weatherstation/")!)
.foregroundColor(Color("PrimaryColor"))
.padding([.leading, .trailing], 2)
.padding([.top], 5)
.padding([.bottom], 15)
HStack {
Button(action: {
if locator.disabled {
UIApplication.shared.open(URL(string:UIApplication.openSettingsURLString)!)
} else {
locator.setListener({ longitude, latitude in
state.latitude = String(format: "%.6f", latitude)
state.longitude = String(format: "%.6f", longitude)
state.updateNetwork()
})
}
}) {
HStack {
if locator.localizing {
ProgressSpinnerView(20, 3)
.padding([.leading, .trailing], 3)
} else {
Image(systemName: locator.disabled ? "location.slash" : "location")
.padding([.leading, .trailing], 3)
}
Text("setup_button_gps")
.frame(alignment: .center)
.multilineTextAlignment(.center)
}.frame(minHeight: 36)
}
.disabled(manual || locator.localizing)
.buttonStyle(MaterialButtonStyle(color: Color("PrimaryColor"), disabled: manual || locator.localizing))
.frame(maxWidth: .infinity)
Toggle(isOn: $manual) {
Text("setup_button_manual")
.frame(maxWidth: .infinity, alignment: .trailing)
}.frame(maxWidth: .infinity)
}.frame(maxWidth: 400)
.padding([.bottom], 5)
HStack(alignment: .top) {
ConfigureTextField(image: "arrow.left.arrow.right", hint: "setup_hint_longitude", text: $state.longitude, disabled: !manual, error: manual && state.lon == nil ? "setup_error_longitude" : nil, secure: false)
ConfigureTextField(image: "arrow.up.arrow.down", hint: "setup_hint_latitude", text: $state.latitude, disabled: !manual, error: manual && state.lat == nil ? "setup_error_latitude" : nil, secure: false)
}.frame(maxWidth: 400)
ConfigureTextField(image: "wifi", hint: "setup_hint_wlan_name", text: $state.network, disabled: false, error: state.network.count > 0 ? nil : "setup_error_wlan_name_too_short", secure: false)
ConfigureTextField(image: "lock", hint: "setup_hint_wlan_password", text: $state.password, disabled: false, error: state.password.count < 8 ? ( state.password.count == 0 ? "setup_error_wlan_password_none": "setup_error_wlan_password_too_short") : nil, secure: true)
ConfigureTextField(image: "cpu", hint: "setup_hint_device_name", text: $state.model, disabled: false, error: state.model.count > 0 ? nil : "setup_error_device_name_too_short", secure: false)
Text("setup_prepare")
.frame(maxWidth: 400)
.padding([.bottom], 10)
Button("setup_button_ready", action: {
ArduinoTask(taskState: state).execute()
}).disabled(!state.isExecutable())
.buttonStyle(MaterialButtonStyle(disabled: !state.isExecutable()))
.frame(maxWidth: 400)
}.padding()
.onChange(of: manual) { v in
if v {
locator.clearListener()
}
}
}
}
struct ConfigureTextField: View {
let image: String
let hint: LocalizedStringKey
let text: Binding<String>
let disabled: Bool
let error: LocalizedStringKey?
let secure: Bool
@State var hidden = true
var body: some View {
VStack(alignment: .leading, spacing: 1) {
HStack {
Image(systemName: image)
if (secure && hidden) {
SecureField(hint, text: text)
.frame(maxWidth: .infinity, minHeight: 24)
.autocapitalization(.none)
.disableAutocorrection(true)
.disabled(disabled)
} else {
TextField(hint, text: text)
.frame(maxWidth: .infinity, minHeight: 24)
.autocapitalization(.none)
.disableAutocorrection(true)
.disabled(disabled)
}
if (secure) {
Button(action: { hidden.toggle() }, label: {
Image(systemName: hidden ? "eye" : "eye.slash")
})
.buttonStyle(NullButtonStyle())
}
}
.padding([.top], 14)
.padding(8)
.frame(maxWidth: 400)
.overlay(
ZStack(alignment: .topLeading) {
RoundedRectangle(cornerRadius: 6)
.stroke(error == nil ? .gray : .red, lineWidth: 2)
.padding([.top], 10)
Text(hint)
.foregroundColor(error == nil ? .gray : .red)
.padding([.leading, .trailing], 5)
.background(Color(UIColor.systemBackground))
.font(.system(size: 14))
.padding([.leading], 10)
}
)
Text(error == nil ? LocalizedStringKey("") : error!)
.font(.caption)
.foregroundColor(.red)
.padding([.leading], 8)
.padding([.bottom], 4)
}
}
}
struct ConnectView: View {
@ObservedObject var state: ArduinoState
@Environment(\.presentationMode) var mode: Binding<PresentationMode>
var body: some View {
VStack {
Group {
if state.phase == .success {
Image(systemName: "checkmark.circle")
.resizable()
} else if state.phase == .failure {
Image(systemName: "exclamationmark.circle")
.resizable()
} else {
ProgressSpinnerView(100, 8)
}
}.frame(width: 100, height: 100)
.padding()
HStack {
Image(systemName: "plus.circle")
.foregroundColor(state.phase == .register ? .accentColor : .primary)
.padding()
Image(systemName: "link.circle")
.foregroundColor(state.phase == .connect ? .accentColor : .primary)
.padding()
Image(systemName: "paperplane.circle")
.foregroundColor(state.phase == .transfer ? .accentColor : .primary)
.padding()
}.font(.largeTitle)
if let text = getPhase() {
Text(text)
.font(.headline)
}
if state.phase == .failure {
if let text = getFailure() {
Text(text)
.frame(maxWidth: 300)
.multilineTextAlignment(.center)
}
Button(action: {
state.reset()
}) {
Text("connect_button_try_again")
}
.buttonStyle(MaterialButtonStyle(disabled: false))
.frame(maxWidth: 200)
.padding()
}
}
.padding()
.onChange(of: state.phase) { s in
if s == .success {
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
self.mode.wrappedValue.dismiss()
state.reset()
}
}
}
}
func getPhase() -> LocalizedStringKey? {
if state.phase == .register {
return "connect_message_creating"
} else if state.phase == .connect {
return "connect_message_searching"
} else if state.phase == .transfer {
return "connect_message_transfering"
} else if state.phase == .success {
return "connect_message_success"
} else {
return nil;
}
}
func getFailure() -> LocalizedStringKey? {
if state.failure == .register {
return "connect_message_create_failed"
} else if state.failure == .connect {
return "connect_message_search_failed"
} else if state.failure == .transfer {
return "connect_message_transfer_failed"
} else {
return nil
}
}
}
struct SetupView_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
SetupView()
}
}
}