Skip to content

Commit c22b37f

Browse files
committed
Code cleanup
1 parent 33337f2 commit c22b37f

File tree

13 files changed

+151
-154
lines changed

13 files changed

+151
-154
lines changed

Loop Widget Extension/Components/BasalView.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import SwiftUI
1010

1111
struct BasalView: View {
1212
let netBasal: NetBasalContext
13-
let isOld: Bool
14-
13+
let isStale: Bool
1514

1615
var body: some View {
1716
let percent = netBasal.percentage
@@ -21,20 +20,20 @@ struct BasalView: View {
2120
BasalRateView(percent: percent)
2221
.overlay(
2322
BasalRateView(percent: percent)
24-
.stroke(isOld ? Color(UIColor.systemGray3) : Color("insulin"), lineWidth: 2)
23+
.stroke(isStale ? Color.staleGray : Color.insulin, lineWidth: 2)
2524
)
26-
.foregroundColor((isOld ? Color(UIColor.systemGray3) : Color("insulin")).opacity(0.5))
25+
.foregroundColor((isStale ? Color.staleGray : Color.insulin).opacity(0.5))
2726
.frame(width: 44, height: 22)
2827

2928
if let rateString = decimalFormatter.string(from: NSNumber(value: rate)) {
3029
Text("\(rateString) U")
3130
.font(.footnote)
32-
.foregroundColor(Color(isOld ? UIColor.systemGray3 : UIColor.secondaryLabel))
31+
.foregroundColor(isStale ? .staleGray : .secondary)
3332
}
3433
else {
3534
Text("-U")
3635
.font(.footnote)
37-
.foregroundColor(Color(isOld ? UIColor.systemGray3 : UIColor.secondaryLabel))
36+
.foregroundColor(isStale ? .staleGray : .secondary)
3837
}
3938
}
4039
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//
2+
// DeeplinkView.swift
3+
// Loop Widget Extension
4+
//
5+
// Created by Noah Brauner on 8/9/24.
6+
// Copyright © 2024 LoopKit Authors. All rights reserved.
7+
//
8+
9+
import SwiftUI
10+
11+
fileprivate extension Deeplink {
12+
var deeplinkURL: URL {
13+
URL(string: "loop://\(rawValue)")!
14+
}
15+
16+
var accentColor: Color {
17+
switch self {
18+
case .carbEntry:
19+
return .carbs
20+
case .bolus:
21+
return .insulin
22+
case .preMeal:
23+
return .carbs
24+
case .customPresets:
25+
return .glucose
26+
}
27+
}
28+
29+
var icon: Image {
30+
switch self {
31+
case .carbEntry:
32+
return Image(.carbs)
33+
case .bolus:
34+
return Image(.bolus)
35+
case .preMeal:
36+
return Image(.premeal)
37+
case .customPresets:
38+
return Image(.workout)
39+
}
40+
}
41+
}
42+
43+
struct DeeplinkView: View {
44+
let destination: Deeplink
45+
var isActive: Bool = false
46+
47+
var body: some View {
48+
Link(destination: destination.deeplinkURL) {
49+
destination.icon
50+
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
51+
.foregroundColor(isActive ? .white : destination.accentColor)
52+
.containerRelativeBackground(color: isActive ? destination.accentColor : .widgetSecondaryBackground)
53+
}
54+
}
55+
}

Loop Widget Extension/Components/EventualGlucoseView.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ struct EventualGlucoseView: View {
1818
VStack {
1919
Text("Eventual")
2020
.font(.footnote)
21-
.foregroundColor(entry.contextIsStale ? Color(UIColor.systemGray3) : Color(UIColor.secondaryLabel))
22-
21+
.foregroundColor(entry.contextIsStale ? .staleGray : .secondary)
22+
2323
Text("\(glucoseString)")
2424
.font(.subheadline)
2525
.fontWeight(.heavy)
2626

2727
Text(eventualGlucose.unit.shortLocalizedUnitString())
2828
.font(.footnote)
29-
.foregroundColor(entry.contextIsStale ? Color(UIColor.systemGray3) : Color(UIColor.secondaryLabel))
29+
.foregroundColor(entry.contextIsStale ? .staleGray : .secondary)
3030
}
3131
}
3232
}

Loop Widget Extension/Components/GlucoseView.swift

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,17 @@ import HealthKit
1212
import LoopCore
1313

1414
struct GlucoseView: View {
15-
1615
var entry: StatusWidgetTimelimeEntry
1716

1817
var body: some View {
1918
VStack(alignment: .center, spacing: 0) {
2019
HStack(spacing: 2) {
21-
if let glucose = entry.currentGlucose,
22-
!entry.glucoseIsStale,
23-
let unit = entry.unit
24-
{
25-
let quantity = glucose.quantity
26-
let glucoseFormatter = NumberFormatter.glucoseFormatter(for: unit)
27-
if let glucoseString = glucoseFormatter.string(from: quantity.doubleValue(for: unit)) {
28-
Text(glucoseString)
29-
.font(.system(size: 24, weight: .heavy, design: .default))
30-
}
31-
else {
32-
Text("??")
33-
.font(.system(size: 24, weight: .heavy, design: .default))
34-
}
20+
if !entry.glucoseIsStale,
21+
let glucoseQuantity = entry.currentGlucose?.quantity,
22+
let unit = entry.unit,
23+
let glucoseString = NumberFormatter.glucoseFormatter(for: unit).string(from: glucoseQuantity.doubleValue(for: unit)) {
24+
Text(glucoseString)
25+
.font(.system(size: 24, weight: .heavy, design: .default))
3526
}
3627
else {
3728
Text("---")
@@ -42,26 +33,22 @@ struct GlucoseView: View {
4233
Image(systemName: trendImageName)
4334
}
4435
}
45-
// Prevent truncation of text
46-
.fixedSize(horizontal: true, vertical: false)
47-
.foregroundColor(entry.glucoseStatusIsStale ? Color(UIColor.systemGray3) : .primary)
36+
.foregroundColor(entry.glucoseStatusIsStale ? .staleGray : .primary)
4837

49-
let unitString = entry.unit == nil ? "-" : entry.unit!.localizedShortUnitString
38+
let unitString = entry.unit?.localizedShortUnitString ?? "-"
5039
if let delta = entry.delta, let unit = entry.unit {
5140
let deltaValue = delta.doubleValue(for: unit)
5241
let numberFormatter = NumberFormatter.glucoseFormatter(for: unit)
5342
let deltaString = (deltaValue < 0 ? "-" : "+") + numberFormatter.string(from: abs(deltaValue))!
5443

5544
Text(deltaString + " " + unitString)
56-
// Dynamic text causes string to be cut off
57-
.font(.system(size: 13))
58-
.foregroundColor(entry.glucoseStatusIsStale ? Color(UIColor.systemGray3) : Color(UIColor.secondaryLabel))
59-
.fixedSize(horizontal: true, vertical: true)
45+
.font(.footnote)
46+
.foregroundColor(entry.glucoseStatusIsStale ? .staleGray : .secondary)
6047
}
6148
else {
6249
Text(unitString)
6350
.font(.footnote)
64-
.foregroundColor(entry.glucoseStatusIsStale ? Color(UIColor.systemGray3) : Color(UIColor.secondaryLabel))
51+
.foregroundColor(entry.glucoseStatusIsStale ? .staleGray : .secondary)
6552
}
6653
}
6754
}

Loop Widget Extension/Components/PumpView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct PumpView: View {
2121
}
2222
}
2323
else if let netBasal = entry.netBasal {
24-
BasalView(netBasal: netBasal, isOld: entry.contextIsStale)
24+
BasalView(netBasal: netBasal, isStale: entry.contextIsStale)
2525
}
2626
}
2727
}

Loop Widget Extension/Components/SystemActionLink.swift

Lines changed: 0 additions & 82 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// Color.swift
3+
// Loop
4+
//
5+
// Created by Noah Brauner on 8/9/24.
6+
// Copyright © 2024 LoopKit Authors. All rights reserved.
7+
//
8+
9+
import SwiftUI
10+
11+
extension Color {
12+
static let widgetBackground = Color(.widgetBackground)
13+
static let widgetSecondaryBackground = Color(.widgetSecondaryBackground)
14+
static let staleGray = Color(.systemGray3)
15+
16+
static let insulin = Color(.insulin)
17+
static let glucose = Color(.glucose)
18+
static let carbs = Color(.fresh)
19+
}

Loop Widget Extension/Helpers/WidgetBackground.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,18 @@ extension View {
1313
func widgetBackground() -> some View {
1414
if #available(iOSApplicationExtension 17.0, *) {
1515
containerBackground(for: .widget) {
16-
background { Color("WidgetBackground") }
16+
background { Color.widgetBackground }
1717
}
1818
} else {
19-
background { Color("WidgetBackground") }
19+
background { Color.widgetBackground }
2020
}
2121
}
22+
23+
@ViewBuilder
24+
func containerRelativeBackground(color: Color = .widgetSecondaryBackground) -> some View {
25+
background(
26+
ContainerRelativeShape()
27+
.fill(color)
28+
)
29+
}
2230
}

Loop Widget Extension/LoopWidgets.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import SwiftUI
1010

1111
@main
1212
struct LoopWidgets: WidgetBundle {
13-
1413
@WidgetBundleBuilder
1514
var body: some Widget {
1615
SystemStatusWidget()

Loop Widget Extension/Widgets/SystemStatusWidget.swift

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ import LoopUI
1212
import SwiftUI
1313
import WidgetKit
1414

15-
struct SystemStatusWidgetEntryView : View {
16-
15+
struct SystemStatusWidgetEntryView: View {
1716
@Environment(\.widgetFamily) private var widgetFamily
1817

19-
var entry: StatusWidgetTimelineProvider.Entry
18+
var entry: StatusWidgetTimelimeEntry
2019

2120
var freshness: LoopCompletionFreshness {
2221
let lastLoopCompleted = entry.lastLoopCompleted ?? Date().addingTimeInterval(.minutes(16))
@@ -38,10 +37,7 @@ struct SystemStatusWidgetEntryView : View {
3837
}
3938
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
4039
.padding(5)
41-
.background(
42-
ContainerRelativeShape()
43-
.fill(Color("WidgetSecondaryBackground"))
44-
)
40+
.containerRelativeBackground()
4541

4642
HStack(alignment: .center, spacing: 0) {
4743
PumpView(entry: entry)
@@ -52,33 +48,30 @@ struct SystemStatusWidgetEntryView : View {
5248
}
5349
.frame(maxHeight: .infinity, alignment: .center)
5450
.padding(.vertical, 5)
55-
.background(
56-
ContainerRelativeShape()
57-
.fill(Color("WidgetSecondaryBackground"))
58-
)
51+
.containerRelativeBackground()
5952
}
6053

6154
if widgetFamily != .systemSmall {
6255
VStack(alignment: .center, spacing: 5) {
6356
HStack(alignment: .center, spacing: 5) {
64-
SystemActionLink(to: .carbEntry)
57+
DeeplinkView(destination: .carbEntry)
6558

66-
SystemActionLink(to: .bolus)
59+
DeeplinkView(destination: .bolus)
6760
}
6861

6962
HStack(alignment: .center, spacing: 5) {
7063
if entry.preMealPresetAllowed {
71-
SystemActionLink(to: .preMeal, active: entry.preMealPresetActive)
64+
DeeplinkView(destination: .preMeal, isActive: entry.preMealPresetActive)
7265
}
7366

74-
SystemActionLink(to: .customPreset, active: entry.customPresetActive)
67+
DeeplinkView(destination: .customPresets, isActive: entry.customPresetActive)
7568
}
7669
}
7770
.buttonStyle(.plain)
7871
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
7972
}
8073
}
81-
.foregroundColor(entry.contextIsStale ? Color(UIColor.systemGray3) : nil)
74+
.foregroundColor(entry.contextIsStale ? .staleGray : nil)
8275
.padding(5)
8376
.widgetBackground()
8477
}

0 commit comments

Comments
 (0)