summaryrefslogtreecommitdiff
path: root/LearnMapKit/LearnMapKitApp.swift
blob: b10764d8025c051a1515378816d328971bc70c49 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
//  LearnMapKitApp.swift
//  LearnMapKit
//
//  Created by Jacky Jack on 05/06/2024.
//

import SwiftUI
import Collections
import ArgumentParser

//https://www.hackingwithswift.com/quick-start/swiftui/how-to-run-code-when-your-app-launches

struct CommandLineArgs: ParsableCommand {
    @Option(name: .shortAndLong) var hostname: String? = nil
    @Option(name: .shortAndLong) var port: Int? = nil
    @Option(name: .shortAndLong) var inputfile: String? = nil
    @Flag(name: .shortAndLong) var debug:Bool = false
    @Flag(name: .shortAndLong) var version:Bool = false
}

//@EnvironmentObject var flightState: FlightState

@main
struct LearnMapKitApp: App {
    var network_mode = false
    var file_mode = false
    @State var queue: Deque<ADSBLocation> = []
    @State var netconfig: NetworkConfigure = NetworkConfigure()
    @State var new_config: Bool = false
    
    //@StateObject private var flightState:FlightState = FlightState()
    //@State var flightState:FlightState = FlightState()
    var flightState:FlightState = FlightState()
    //@Published var list_of_plains:[FlightTracker]?
    //@StateObject var flightState:FlightState = FlightState()
    
    
    var default_hostname = "192.168.4.233"
    var default_port = 30002
    var default_input_file = ""
    
    
    
    init() {
        print("Init ")
        if let args = CommandLineArgs.parseNotExit()
        {
            if args.hostname != nil {
                default_hostname = args.hostname!
                network_mode = true
            }
            if args.port != nil {
                default_port = args.port!
                network_mode = true
            }
            
            if args.inputfile != nil {
                default_input_file = args.inputfile!
                file_mode = true
            }
        }
        
        if network_mode {
            netconfig.servername = default_hostname
            netconfig.serverport = default_port
        }
    }
    
    var body: some Scene {
        WindowGroup {
            ContentView(net_config: $netconfig).onAppear(perform: {
                if network_mode {
                    print("Network mode")
                    flightState.networkMode(hostname: self.default_hostname, port: self.default_port)
                } else if (file_mode){
                    print("Run file mode")
                    flightState.fileMode(filepath: self.default_input_file)
                } else {
                    print("Run default mode")
                    flightState.defaultMode(hostname: default_hostname, port: default_port)
                }
                 flightState.run()
            })
        }.environmentObject(flightState)
        
        /*WindowGroup("Network", id: "net-config") {
            NetConfigView(net_config: $netconfig)
        }*/
    }
}

extension ParsableArguments {
    static func parseNotExit(
        _ arguments: [String]? = nil
      ) -> Self! {
        do {
          return try parse(arguments)
        } catch {
          print("Ignore error")
          return nil
        }
      }
}