2020-01-21 01:11:55 +00:00
|
|
|
//
|
|
|
|
// PiHoleDetailsView.swift
|
|
|
|
// Pi-Helper
|
|
|
|
//
|
|
|
|
// Created by Billy Brawner on 10/19/19.
|
|
|
|
// Copyright © 2019 William Brawner. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct PiHoleDetailsView: View {
|
|
|
|
var stateContent: AnyView {
|
|
|
|
switch self.dataStore.pihole {
|
|
|
|
case .success(let pihole):
|
2020-10-20 00:46:05 +00:00
|
|
|
return PiHoleActionsView(dataStore: self.dataStore, status: pihole).toAnyView()
|
|
|
|
case .failure(.loading(let previousStatus)):
|
|
|
|
if let status = previousStatus {
|
|
|
|
return PiHoleActionsView(dataStore: self.dataStore, status: status).toAnyView()
|
|
|
|
} else {
|
|
|
|
return ActivityIndicatorView(.constant(true)).toAnyView()
|
|
|
|
}
|
|
|
|
case .failure(.networkError(let error)):
|
|
|
|
switch (error) {
|
|
|
|
default:
|
|
|
|
return PiHoleActionsView(dataStore: self.dataStore, status: .unknown).toAnyView()
|
2020-01-21 01:11:55 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return ActivityIndicatorView(.constant(true)).toAnyView()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
NavigationView {
|
2020-01-26 20:43:26 +00:00
|
|
|
stateContent
|
|
|
|
.navigationBarTitle("PiHelper")
|
2020-01-31 15:29:15 +00:00
|
|
|
.navigationBarItems(trailing: NavigationLink(destination: AboutView(self.dataStore), label: {
|
|
|
|
Image(systemName: "info.circle")
|
|
|
|
.padding()
|
|
|
|
}))
|
2020-01-21 01:11:55 +00:00
|
|
|
}
|
|
|
|
.navigationViewStyle(StackNavigationViewStyle())
|
|
|
|
.onAppear {
|
|
|
|
self.dataStore.monitorStatus()
|
|
|
|
}
|
|
|
|
.onDisappear {
|
|
|
|
self.dataStore.stopMonitoring()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ObservedObject var dataStore: PiHoleDataStore
|
|
|
|
init(_ dataStore: PiHoleDataStore) {
|
|
|
|
self.dataStore = dataStore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PiHoleActionsView: View {
|
|
|
|
var body: some View {
|
|
|
|
VStack {
|
|
|
|
HStack {
|
|
|
|
Text("status")
|
2020-10-20 00:46:05 +00:00
|
|
|
PiHoleStatusView(status)
|
2020-01-21 01:11:55 +00:00
|
|
|
}
|
2020-10-20 00:46:05 +00:00
|
|
|
DurationView(status)
|
|
|
|
PiHoleActions(self.dataStore, status: status)
|
2020-01-21 01:11:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let dataStore: PiHoleDataStore
|
2020-10-20 00:46:05 +00:00
|
|
|
let status: PiHoleStatus
|
2020-01-21 01:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct PiHoleStatusView: View {
|
|
|
|
var body: some View {
|
2020-10-20 00:46:05 +00:00
|
|
|
Text(status.localizedStringKey).foregroundColor(status.foregroundColor)
|
2020-01-21 01:11:55 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 00:46:05 +00:00
|
|
|
let status: PiHoleStatus
|
|
|
|
init(_ status: PiHoleStatus) {
|
|
|
|
self.status = status
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct DurationView: View {
|
|
|
|
var body: some View {
|
|
|
|
stateContent
|
2020-01-21 01:11:55 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 00:46:05 +00:00
|
|
|
var stateContent: AnyView {
|
|
|
|
switch status {
|
|
|
|
case .disabled(let duration):
|
|
|
|
if let durationString = duration {
|
|
|
|
return HStack {
|
|
|
|
Text("time_remaining")
|
|
|
|
Text(durationString)
|
|
|
|
.frame(minWidth: 30)
|
|
|
|
}
|
|
|
|
.toAnyView()
|
|
|
|
} else {
|
|
|
|
return EmptyView().toAnyView()
|
|
|
|
}
|
2020-01-21 01:11:55 +00:00
|
|
|
default:
|
2020-10-20 00:46:05 +00:00
|
|
|
return EmptyView().toAnyView()
|
2020-01-21 01:11:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 00:46:05 +00:00
|
|
|
let status: PiHoleStatus
|
|
|
|
init(_ status: PiHoleStatus) {
|
|
|
|
self.status = status
|
2020-01-21 01:11:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PiHoleActions: View {
|
|
|
|
var body: some View {
|
|
|
|
stateContent.padding()
|
|
|
|
}
|
|
|
|
|
|
|
|
var stateContent: AnyView {
|
2020-10-20 00:46:05 +00:00
|
|
|
switch status {
|
|
|
|
case .disabled:
|
2020-01-26 20:43:54 +00:00
|
|
|
return Button(action: { self.dataStore.enable() }, label: { Text("enable") })
|
|
|
|
.buttonStyle(PiHelperButtonStyle(.green))
|
|
|
|
.padding(Edge.Set(arrayLiteral: [.bottom, .top]), 5.0)
|
|
|
|
.toAnyView()
|
2020-10-20 00:46:05 +00:00
|
|
|
case .enabled:
|
2020-01-26 20:43:54 +00:00
|
|
|
return VStack {
|
2020-01-21 01:11:55 +00:00
|
|
|
Button(action: { self.dataStore.disable(10) }, label: { Text("disable_10_sec") })
|
|
|
|
.buttonStyle(PiHelperButtonStyle())
|
|
|
|
.padding(Edge.Set(arrayLiteral: [.bottom, .top]), 5.0)
|
|
|
|
Button(action: { self.dataStore.disable(30) }, label: { Text("disable_30_sec") })
|
|
|
|
.buttonStyle(PiHelperButtonStyle())
|
|
|
|
.padding(Edge.Set(arrayLiteral: [.bottom, .top]), 5.0)
|
|
|
|
Button(action: { self.dataStore.disable(300) }, label: { Text("disable_5_min") })
|
|
|
|
.buttonStyle(PiHelperButtonStyle())
|
|
|
|
.padding(Edge.Set(arrayLiteral: [.bottom, .top]), 5.0)
|
2020-07-04 17:51:51 +00:00
|
|
|
NavigationLink(
|
|
|
|
destination: DisableCustomTimeView(self.dataStore),
|
|
|
|
isActive: .constant(self.dataStore.showCustomDisableView),
|
|
|
|
label: { EmptyView() }
|
|
|
|
)
|
|
|
|
Button(action: {
|
|
|
|
self.dataStore.showCustomDisableView = true
|
|
|
|
}, label: { Text("disable_custom") })
|
|
|
|
.buttonStyle(PiHelperButtonStyle())
|
|
|
|
.padding(Edge.Set(arrayLiteral: [.bottom, .top]), 5.0)
|
2020-01-21 01:11:55 +00:00
|
|
|
Button(action: { self.dataStore.disable() }, label: { Text("disable_permanent") })
|
|
|
|
.buttonStyle(PiHelperButtonStyle())
|
|
|
|
.padding(Edge.Set(arrayLiteral: [.bottom, .top]), 5.0)
|
2020-01-26 20:43:54 +00:00
|
|
|
}.toAnyView()
|
|
|
|
default:
|
|
|
|
return Text("Unable to load Pi-hole status. Please verify your credentials and ensure the Pi-hole is accessible from your current network.")
|
2020-01-31 15:29:15 +00:00
|
|
|
.toAnyView()
|
2020-01-21 01:11:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ObservedObject var dataStore: PiHoleDataStore
|
2020-10-20 00:46:05 +00:00
|
|
|
let status: PiHoleStatus
|
|
|
|
init(_ dataStore: PiHoleDataStore, status: PiHoleStatus) {
|
2020-01-21 01:11:55 +00:00
|
|
|
self.dataStore = dataStore
|
2020-10-20 00:46:05 +00:00
|
|
|
self.status = status
|
2020-01-21 01:11:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PiHoleDetailsView_Previews: PreviewProvider {
|
|
|
|
static var dataStore: PiHoleDataStore {
|
|
|
|
get {
|
|
|
|
let _dataStore = PiHoleDataStore()
|
2020-10-20 00:46:05 +00:00
|
|
|
_dataStore.pihole = .success(.disabled("20"))
|
2020-01-21 01:11:55 +00:00
|
|
|
return _dataStore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static var previews: some View {
|
|
|
|
PiHoleDetailsView(self.dataStore)
|
|
|
|
}
|
|
|
|
}
|