Просмотр исходного кода

Show error messages with SwiftMessages

Ivan Valkou 4 лет назад
Родитель
Сommit
a25e9c49d2

+ 17 - 0
FreeAPS.xcodeproj/project.pbxproj

@@ -166,6 +166,7 @@
 		38DAB280260CBB7F00F74C1A /* PumpView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 38DAB27F260CBB7F00F74C1A /* PumpView.swift */; };
 		38DAB28A260D349500F74C1A /* FetchGlucoseManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 38DAB289260D349500F74C1A /* FetchGlucoseManager.swift */; };
 		38DF1786276A73D400B3528F /* TagCloudView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 38DF1785276A73D400B3528F /* TagCloudView.swift */; };
+		38DF1789276FC8C400B3528F /* SwiftMessages in Frameworks */ = {isa = PBXBuildFile; productRef = 38DF1788276FC8C400B3528F /* SwiftMessages */; };
 		38E4451E274DB04600EC9A94 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 38E4451D274DB04600EC9A94 /* AppDelegate.swift */; };
 		38E44522274E3DDC00EC9A94 /* NetworkReachabilityManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 38E44521274E3DDC00EC9A94 /* NetworkReachabilityManager.swift */; };
 		38E44528274E401C00EC9A94 /* Protected.swift in Sources */ = {isa = PBXBuildFile; fileRef = 38E44527274E401C00EC9A94 /* Protected.swift */; };
@@ -737,6 +738,7 @@
 				38B17B6625DD90E0005CAE3D /* SwiftDate in Frameworks */,
 				3818AA60274C26A300843DB3 /* Crypto.framework in Frameworks */,
 				3833B46D26012030003021B3 /* Algorithms in Frameworks */,
+				38DF1789276FC8C400B3528F /* SwiftMessages in Frameworks */,
 				E0CC2C5C275B9F0F00A7BC71 /* HealthKit.framework in Frameworks */,
 				3818AA62274C26A400843DB3 /* MinimedKit.framework in Frameworks */,
 				3818AA58274C26A300843DB3 /* LoopKit.framework in Frameworks */,
@@ -1847,6 +1849,7 @@
 				38B17B6525DD90E0005CAE3D /* SwiftDate */,
 				3833B46C26012030003021B3 /* Algorithms */,
 				3818AA46274C255A00843DB3 /* LibreTransmitter */,
+				38DF1788276FC8C400B3528F /* SwiftMessages */,
 			);
 			productName = FreeAPS;
 			productReference = 388E595825AD948C0019842D /* FreeAPS.app */;
@@ -1964,6 +1967,7 @@
 				3811DE0E25C9D37700A708ED /* XCRemoteSwiftPackageReference "Swinject" */,
 				38B17B6425DD90E0005CAE3D /* XCRemoteSwiftPackageReference "SwiftDate" */,
 				3833B46B26012030003021B3 /* XCRemoteSwiftPackageReference "swift-algorithms" */,
+				38DF1787276FC8C300B3528F /* XCRemoteSwiftPackageReference "SwiftMessages" */,
 			);
 			productRefGroup = 388E595925AD948C0019842D /* Products */;
 			projectDirPath = "";
@@ -2838,6 +2842,14 @@
 				minimumVersion = 6.3.1;
 			};
 		};
+		38DF1787276FC8C300B3528F /* XCRemoteSwiftPackageReference "SwiftMessages" */ = {
+			isa = XCRemoteSwiftPackageReference;
+			repositoryURL = "https://github.com/SwiftKickMobile/SwiftMessages";
+			requirement = {
+				kind = upToNextMajorVersion;
+				minimumVersion = 9.0.0;
+			};
+		};
 /* End XCRemoteSwiftPackageReference section */
 
 /* Begin XCSwiftPackageProductDependency section */
@@ -2860,6 +2872,11 @@
 			package = 38B17B6425DD90E0005CAE3D /* XCRemoteSwiftPackageReference "SwiftDate" */;
 			productName = SwiftDate;
 		};
+		38DF1788276FC8C400B3528F /* SwiftMessages */ = {
+			isa = XCSwiftPackageProductDependency;
+			package = 38DF1787276FC8C300B3528F /* XCRemoteSwiftPackageReference "SwiftMessages" */;
+			productName = SwiftMessages;
+		};
 		38E8755727567AE400975559 /* SwiftDate */ = {
 			isa = XCSwiftPackageProductDependency;
 			package = 38B17B6425DD90E0005CAE3D /* XCRemoteSwiftPackageReference "SwiftDate" */;

+ 9 - 0
FreeAPS.xcworkspace/xcshareddata/swiftpm/Package.resolved

@@ -29,6 +29,15 @@
         }
       },
       {
+        "package": "SwiftMessages",
+        "repositoryURL": "https://github.com/SwiftKickMobile/SwiftMessages",
+        "state": {
+          "branch": null,
+          "revision": "1e49de7b3780b69927bc3e61903d8ec0693a3dc5",
+          "version": "9.0.5"
+        }
+      },
+      {
         "package": "Swinject",
         "repositoryURL": "https://github.com/Swinject/Swinject",
         "state": {

+ 3 - 3
FreeAPS/Sources/Logger/Logger.swift

@@ -224,9 +224,9 @@ final class Logger {
         function: String = #function,
         line: UInt = #line
     ) {
-        let message = "INFO: \(message)"
-        os_log("%@ - %@ - %d %{public}@", log: log, type: .info, file.file, function, line, message)
-        reporter.log(category.name, message, file: file, function: function, line: line)
+        let printedMessage = "INFO: \(message)"
+        os_log("%@ - %@ - %d %{public}@", log: log, type: .info, file.file, function, line, printedMessage)
+        reporter.log(category.name, printedMessage, file: file, function: function, line: line)
 
         showAlert(message)
     }

+ 1 - 0
FreeAPS/Sources/Modules/Calibrations/CalibrationsStateModel.swift

@@ -45,6 +45,7 @@ extension Calibrations {
                   lastGlucose.dateString.addingTimeInterval(60 * 4.5) > Date(),
                   let unfiltered = lastGlucose.unfiltered
             else {
+                info(.service, "Glucose is stale for calibration")
                 return
             }
 

+ 3 - 0
FreeAPS/Sources/Modules/Home/HomeStateModel.swift

@@ -115,6 +115,9 @@ extension Home {
                 .receive(on: DispatchQueue.main)
                 .map { [weak self] error in
                     self?.errorDate = error == nil ? nil : Date()
+                    if let error = error {
+                        info(.default, error.localizedDescription)
+                    }
                     return error?.localizedDescription
                 }
                 .weakAssign(to: \.errorMessage, on: self)

+ 19 - 4
FreeAPS/Sources/Modules/Main/MainStateModel.swift

@@ -1,3 +1,4 @@
+import SwiftMessages
 import SwiftUI
 import Swinject
 
@@ -6,8 +7,6 @@ extension Main {
         private(set) var modal: Modal?
         @Published var isModalPresented = false
         @Published var isSecondaryModalPresented = false
-        @Published var isAlertPresented = false
-        @Published var alertMessage = ""
         @Published var secondaryModalView: AnyView? = nil
 
         override func subscribe() {
@@ -31,8 +30,24 @@ extension Main {
             router.alertMessage
                 .receive(on: DispatchQueue.main)
                 .sink { message in
-                    self.isAlertPresented = message.isNotEmpty
-                    self.alertMessage = message
+                    SwiftMessages.show {
+                        let view = MessageView.viewFromNib(layout: .messageView)
+                        view.backgroundColor = .secondarySystemGroupedBackground
+                        view.titleLabel?.textColor = .label
+                        view.bodyLabel?.textColor = .label
+                        view.configureContent(
+                            title: NSLocalizedString("Info", comment: "Info title"),
+                            body: NSLocalizedString(message, comment: "Info message"),
+                            iconImage: nil,
+                            iconText: nil,
+                            buttonImage: nil,
+                            buttonTitle: nil,
+                            buttonTapHandler: nil
+                        )
+
+                        view.bodyLabel?.text = message
+                        return view
+                    }
                 }
                 .store(in: &lifetime)
 

+ 0 - 10
FreeAPS/Sources/Modules/Main/View/MainRootView.swift

@@ -19,16 +19,6 @@ extension Main {
                         EmptyView()
                     }
                 }
-                .alert(isPresented: $state.isAlertPresented) {
-                    Alert(
-                        title: Text("Important message"),
-                        message: Text(state.alertMessage),
-                        dismissButton: .default(Text("Dismiss")) {
-                            state.isAlertPresented = false
-                            state.alertMessage = ""
-                        }
-                    )
-                }
                 .onAppear(perform: configureView)
         }
     }