Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Rev 62 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
//
2
//  twogetskillsApp.swift
3
//  twogetskills
4
//
5
//  Created by Efrain Yanez Recanatini on 1/12/22.
6
//
7
 
8
import SwiftUI
9
import BackgroundTasks
10
 
11
 
12
@main
13
struct TwoGetSkillsApp: App {
14
    @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
15
    @Environment(\.scenePhase) var scenePhase
8 efrain 16
 
17 efrain 17
 
8 efrain 18
    @ObservedObject var syncForeground = SyncForegroundObservableObject()
19
    private let syncAdapter = SyncAdapter()
17 efrain 20
    private var appData = AppData.sharedInstance
21
 
1 efrain 22
    init() {
23
      let coloredAppearance = UINavigationBarAppearance()
24
      coloredAppearance.configureWithOpaqueBackground()
25
      coloredAppearance.backgroundColor = UIColor(Color("color_app_bar_backgroud"))
26
      coloredAppearance.titleTextAttributes = [.foregroundColor: Color("color_app_bar_foreground")]
27
      coloredAppearance.largeTitleTextAttributes = [.foregroundColor: Color("color_app_bar_foreground")]
28
      coloredAppearance.backButtonAppearance.normal.titleTextAttributes = [.foregroundColor: Color("color_app_bar_foreground")]
29
 
30
      UINavigationBar.appearance().standardAppearance = coloredAppearance
31
      UINavigationBar.appearance().compactAppearance = coloredAppearance
32
      UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance
33
 
34
      UINavigationBar.appearance().tintColor = UIColor(Color("color_app_bar_foreground"))
35
 
36
      UIPageControl.appearance().currentPageIndicatorTintColor = .systemBlue
37
      UIPageControl.appearance().pageIndicatorTintColor = .systemGray2
38
 
61 efrain 39
 
8 efrain 40
 
11 efrain 41
 
61 efrain 42
        if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
43
 
44
            let fileURL = dir.appendingPathComponent(Config.DEVICE_UUID_FILENAME)
45
            if !FileManager.default.fileExists(atPath: fileURL.path) {
62 efrain 46
 
47
                if appData.deviceUuid.isEmpty {
48
 
49
                    if let new_uuid = UIDevice.current.identifierForVendor?.uuidString {
50
                        do {
51
                            try new_uuid.write(to: fileURL, atomically: false, encoding: .utf8)
61 efrain 52
 
62 efrain 53
                            var sync = SyncModel()
54
                            sync.data = new_uuid
55
                            sync.type = Constants.SYNC_ADAPTER_TYPE_DEVICE
56
 
57
                            let syncDao = SyncDao()
63 efrain 58
                            let syncId = syncDao.insert(record: sync)
59
 
60
                            if syncId > 0 {
62 efrain 61
                                appData.deviceUuid = new_uuid
62
                                appData.save()
63
 
63 efrain 64
                                sync.id = syncId
65
 
62 efrain 66
                                let syncAdapter = SyncAdapter()
63 efrain 67
                                syncAdapter.registerDevice(record: sync)
61 efrain 68
                            }
69
                        }
62 efrain 70
                        catch {/* error handling here */}
61 efrain 71
                    }
62 efrain 72
                } else {
73
                    do {
74
                        try appData.deviceUuid.write(to: fileURL, atomically: false, encoding: .utf8)
75
                    }
61 efrain 76
                    catch {/* error handling here */}
77
                }
78
            }
79
        }
1 efrain 80
    }
81
 
82
    var body: some Scene {
83
 
84
        WindowGroup {
85
            MainView()
17 efrain 86
        }
87
 
88
        .onChange(of: scenePhase) { newScenePhase in
1 efrain 89
            switch newScenePhase {
8 efrain 90
                case .active:
91
                    print("App is active")
92
                    syncForeground.timer.invalidate()
17 efrain 93
                    syncForeground.timer = Timer.scheduledTimer(timeInterval: 30.0, target: syncAdapter, selector: #selector(syncAdapter.updateTimerForeground), userInfo: nil, repeats: true)
8 efrain 94
                    break
95
 
96
                case .inactive:
97
                  print("App is inactive")
98
                    syncForeground.timer.invalidate()
99
                    break
100
 
101
                case .background:
102
                  print("App is in background")
103
                    syncForeground.timer.invalidate()
104
                    break
105
 
106
                @unknown default:
107
                    print("Oh - interesting: I received an unexpected new value.")
108
                    syncForeground.timer.invalidate()
109
                    break
110
                }
1 efrain 111
          }
112
    }
113
 
59 efrain 114
 
115
}
1 efrain 116
 
117
 
61 efrain 118
class SyncForegroundObservableObject: ObservableObject
119
{
120
    @Published var timer : Timer = Timer()
121
}