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

Refactor minutes ago to display 'm' and '< 1 m' in header

Deniz Cengiz 1 год назад
Родитель
Сommit
59971141ca

+ 8 - 0
Trio/Sources/Helpers/Formatters.swift

@@ -86,6 +86,14 @@ extension Formatter {
         formatter.decimalSeparator = "."
         return formatter
     }()
+
+    static let timaAgoFormatter: NumberFormatter = {
+        let formatter = NumberFormatter()
+        formatter.numberStyle = .decimal
+        formatter.maximumFractionDigits = 0
+        formatter.negativePrefix = ""
+        return formatter
+    }()
 }
 
 extension JSONDecoder.DateDecodingStrategy {

+ 2 - 2
Trio/Sources/Localizations/Main/Localizable.xcstrings

@@ -111042,7 +111042,7 @@
       }
     },
     "m" : {
-      "comment" : "abbreviation for minutes",
+      "comment" : "Abbreviation for Minutes\nabbreviation for minutes",
       "localizations" : {
         "bg" : {
           "stringUnit" : {
@@ -117299,7 +117299,7 @@
       }
     },
     "min" : {
-      "comment" : "Minutes abbreviation\nMinutes ago since last loop\nShort form for minutes",
+      "comment" : "Minutes abbreviation\nShort form for minutes",
       "localizations" : {
         "bg" : {
           "stringUnit" : {

+ 17 - 22
Trio/Sources/Modules/Home/View/Header/CurrentGlucoseView.swift

@@ -39,14 +39,6 @@ struct CurrentGlucoseView: View {
         return formatter
     }
 
-    private var timaAgoFormatter: NumberFormatter {
-        let formatter = NumberFormatter()
-        formatter.numberStyle = .decimal
-        formatter.maximumFractionDigits = 0
-        formatter.negativePrefix = ""
-        return formatter
-    }
-
     var body: some View {
         let triangleColor = Color(red: 0.262745098, green: 0.7333333333, blue: 0.9137254902)
 
@@ -91,20 +83,23 @@ struct CurrentGlucoseView: View {
                     }
                     HStack {
                         let minutesAgo = -1 * (glucose.last?.date?.timeIntervalSinceNow ?? 0) / 60
-                        let text = timaAgoFormatter.string(for: Double(minutesAgo)) ?? ""
-                        Text(
-                            minutesAgo <= 1 ? "< 1 " + String(localized: "min", comment: "Short form for minutes") : (
-                                text + " " +
-                                    String(localized: "min", comment: "Short form for minutes") + " "
-                            )
-                        )
-                        .font(.caption2).foregroundStyle(colorScheme == .dark ? Color.white.opacity(0.9) : Color.secondary)
-
-                        Text(
-                            delta
-                        )
-                        .font(.caption2).foregroundStyle(colorScheme == .dark ? Color.white.opacity(0.9) : Color.secondary)
-                    }.frame(alignment: .top)
+                        var minutesAgoString: String {
+                            if minutesAgo > 1 {
+                                let minuteString = Formatter.timaAgoFormatter.string(for: Double(minutesAgo)) ?? ""
+                                return minuteString + " " + String(localized: "m", comment: "Abbreviation for Minutes")
+                            } else {
+                                return "< 1 " + String(localized: "m", comment: "Abbreviation for Minutes")
+                            }
+                        }
+
+                        Group {
+                            Text(minutesAgoString)
+                            Text(delta)
+                        }
+                        .font(.callout).fontWeight(.bold)
+                        .foregroundStyle(colorScheme == .dark ? Color.white.opacity(0.9) : Color.secondary)
+                    }
+                    .frame(alignment: .top)
                 }
             }
             .onChange(of: glucose.last?.directionEnum) {

+ 8 - 3
Trio/Sources/Modules/Home/View/Header/LoopView.swift

@@ -57,11 +57,16 @@ struct LoopView: View {
     }
 
     private var timeString: String {
-        let minAgo = Int((timerDate.timeIntervalSince(lastLoopDate) - Config.lag) / 60) + 1
-        if minAgo > 1440 {
+        let minutesAgo = -1 * lastLoopDate.timeIntervalSinceNow / 60
+        let minuteString = Formatter.timaAgoFormatter.string(for: Double(minutesAgo)) ?? ""
+
+        if minutesAgo > 1440 {
             return "--"
+        } else if minutesAgo <= 1 {
+            return "< 1 " + String(localized: "m", comment: "Abbreviation for Minutes")
+        } else {
+            return minuteString + " " + String(localized: "m", comment: "Abbreviation for Minutes")
         }
-        return "\(minAgo) " + String(localized: "min", comment: "Minutes ago since last loop")
     }
 
     private var color: Color {