2022-06-12 07:38:08 -07:00
//
// P o s i t i o n C o n f i g . s w i f t
// M e s h t a s t i c A p p l e
//
// C o p y r i g h t ( c ) G a r t h V a n d e r H o u w e n 6 / 1 1 / 2 2 .
//
import SwiftUI
2022-06-12 10:28:26 -07:00
enum GpsUpdateIntervals : Int , CaseIterable , Identifiable {
case thirtySeconds = 0
case oneMinute = 60
case fiveMinutes = 300
case tenMinutes = 600
case fifteenMinutes = 900
case thirtyMinutes = 1800
case oneHour = 3600
case maxInt32 = 2147483647
var id : Int { self . rawValue }
var description : String {
get {
switch self {
case . thirtySeconds :
return " Thirty Seconds "
case . oneMinute :
return " One Minute "
case . fiveMinutes :
return " Five Minutes "
case . tenMinutes :
return " Ten Minutes "
case . fifteenMinutes :
return " Fifteen Minutes "
case . thirtyMinutes :
return " Thirty Minutes "
case . oneHour :
return " One Hour "
case . maxInt32 :
return " On Boot Only "
}
}
}
}
enum GpsAttemptTimes : Int , CaseIterable , Identifiable {
case thirtySeconds = 0
case oneMinute = 60
case fiveMinutes = 300
case tenMinutes = 600
case fifteenMinutes = 900
var id : Int { self . rawValue }
var description : String {
get {
switch self {
case . thirtySeconds :
return " Thirty Seconds "
case . oneMinute :
return " One Minute "
case . fiveMinutes :
return " Five Minutes "
case . tenMinutes :
return " Ten Minutes "
case . fifteenMinutes :
return " Fifteen Minutes "
}
}
}
}
enum PositionBroadcastIntervals : Int , CaseIterable , Identifiable {
case thirtySeconds = 30
case oneMinute = 60
case fiveMinutes = 300
case tenMinutes = 600
case fifteenMinutes = 0
case thirtyMinutes = 1800
case oneHour = 3600
var id : Int { self . rawValue }
var description : String {
get {
switch self {
case . thirtySeconds :
return " Thirty Seconds "
case . oneMinute :
return " One Minute "
case . fiveMinutes :
return " Five Minutes "
case . tenMinutes :
return " Ten Minutes "
case . fifteenMinutes :
return " Fifteen Minutes "
case . thirtyMinutes :
return " Thirty Minutes "
case . oneHour :
return " One Hour "
}
}
}
}
2022-06-12 07:38:08 -07:00
struct PositionConfig : View {
@ Environment ( \ . managedObjectContext ) var context
@ EnvironmentObject var bleManager : BLEManager
2022-06-12 10:28:26 -07:00
@ State var smartPositionEnabled = true
@ State var deviceGpsEnabled = true
@ State var fixedPosition = false
2022-06-12 22:07:58 -07:00
@ State var gpsUpdateInterval = 0
@ State var gpsAttemptTime = 0
@ State var positionBroadcastSeconds = 0
2022-06-12 10:28:26 -07:00
2022-06-14 16:45:43 -07:00
@ State var includeAltitude = false
@ State var includeSatInView = false
2022-06-12 07:38:08 -07:00
var body : some View {
VStack {
Form {
2022-06-12 10:28:26 -07:00
2022-06-13 20:43:51 -07:00
Section ( header : Text ( " Device GPS " ) ) {
2022-06-12 07:38:08 -07:00
2022-06-12 10:28:26 -07:00
Toggle ( isOn : $ deviceGpsEnabled ) {
Label ( " Device GPS Enabled " , systemImage : " location " )
}
. toggleStyle ( SwitchToggleStyle ( tint : . accentColor ) )
if deviceGpsEnabled {
2022-06-13 21:37:39 -07:00
Picker ( " Update Interval " , selection : $ gpsUpdateInterval ) {
2022-06-12 10:28:26 -07:00
ForEach ( GpsUpdateIntervals . allCases ) { ui in
Text ( ui . description )
}
}
. pickerStyle ( DefaultPickerStyle ( ) )
Text ( " How often should we try to get a GPS position. " )
. font ( . caption )
. listRowSeparator ( . visible )
2022-06-13 21:37:39 -07:00
Picker ( " Attempt Time " , selection : $ gpsAttemptTime ) {
2022-06-12 10:28:26 -07:00
ForEach ( GpsAttemptTimes . allCases ) { at in
Text ( at . description )
}
}
. pickerStyle ( DefaultPickerStyle ( ) )
Text ( " How long should we try to get our position during each GPS Update Interval attempt? " )
. font ( . caption )
. listRowSeparator ( . visible )
} else {
Toggle ( isOn : $ fixedPosition ) {
Label ( " Fixed Position " , systemImage : " location.square.fill " )
}
. toggleStyle ( SwitchToggleStyle ( tint : . accentColor ) )
if fixedPosition {
Text ( " Set to current location here " )
. font ( . caption )
. listRowSeparator ( . visible )
}
}
}
2022-06-13 20:43:51 -07:00
Section ( header : Text ( " Position Packet " ) ) {
2022-06-12 07:38:08 -07:00
2022-06-12 10:28:26 -07:00
Toggle ( isOn : $ smartPositionEnabled ) {
Label ( " Smart Position Broadcast " , systemImage : " location.fill.viewfinder " )
}
. toggleStyle ( SwitchToggleStyle ( tint : . accentColor ) )
if ! smartPositionEnabled {
Picker ( " Position Broadcast Interval " , selection : $ positionBroadcastSeconds ) {
ForEach ( PositionBroadcastIntervals . allCases ) { at in
Text ( at . description )
}
}
. pickerStyle ( DefaultPickerStyle ( ) )
Text ( " We should send our position this often (but only if it has changed significantly) " )
. font ( . caption )
. listRowSeparator ( . visible )
}
}
Section ( header : Text ( " Position Flags " ) ) {
2022-06-14 16:45:43 -07:00
Text ( " Optional fields to include when assembling position messages. the more fields are included, the larger the message will be - leading to longer airtime and a higher risk of packet loss " )
. font ( . caption )
. listRowSeparator ( . visible )
Toggle ( isOn : $ includeAltitude ) {
Label ( " Include Altitude " , systemImage : " arrow.up " )
}
. toggleStyle ( DefaultToggleStyle ( ) )
. listRowSeparator ( . visible )
Toggle ( isOn : $ includeSatInView ) {
Label ( " Include number of satellites in view " , systemImage : " skew " )
}
. toggleStyle ( DefaultToggleStyle ( ) )
2022-06-12 10:28:26 -07:00
. listRowSeparator ( . visible )
2022-06-14 16:45:43 -07:00
Toggle ( isOn : $ includeSatInView ) { // 6 4
Label ( " Include a sequence number incremented per packet " , systemImage : " number " )
}
. toggleStyle ( DefaultToggleStyle ( ) )
. listRowSeparator ( . visible )
Toggle ( isOn : $ includeSatInView ) { // 1 2 8
Label ( " Include positional timestamp " , systemImage : " clock " )
}
. toggleStyle ( DefaultToggleStyle ( ) )
. listRowSeparator ( . visible )
2022-06-12 07:38:08 -07:00
}
}
}
. navigationTitle ( " Position Config " )
. navigationBarItems ( trailing :
ZStack {
2022-06-17 18:20:30 -07:00
ConnectedDevice ( bluetoothOn : bleManager . isSwitchedOn , deviceConnected : bleManager . connectedPeripheral != nil , name : ( bleManager . connectedPeripheral != nil ) ? bleManager . connectedPeripheral . shortName : " ????? " )
2022-06-12 07:38:08 -07:00
} )
. onAppear {
self . bleManager . context = context
}
. navigationViewStyle ( StackNavigationViewStyle ( ) )
}
}