@@ -19,7 +19,15 @@ class ContactImageUpdater {
1919 return ContactColorOption ( rawValue: rawValue) ? . uiColor ?? . white
2020 }
2121
22- func updateContactImage( bgValue: String , trend: String , delta: String , stale: Bool ) {
22+ private func textColor( for contactType: ContactType ) -> UIColor {
23+ guard contactType == . BG else { return savedTextUIColor }
24+ let colorMode = Storage . shared. contactColorMode. value
25+ // Use raw BG value in mg/dL (same units as highLine/lowLine)
26+ let bgNumeric = Double ( Observable . shared. bg. value ?? 0 )
27+ return colorMode. textColor ( for: bgNumeric, staticColor: savedTextUIColor)
28+ }
29+
30+ func updateContactImage( bgValue: String , trend: String , delta: String , iob: String , stale: Bool ) {
2331 queue. async {
2432 guard CNContactStore . authorizationStatus ( for: . contacts) == . authorized else {
2533 LogManager . shared. log ( category: . contact, message: " Access to contacts is not authorized. " )
@@ -37,9 +45,17 @@ class ContactImageUpdater {
3745 continue
3846 }
3947
48+ if contactType == . IOB, Storage . shared. contactIOB. value != . separate {
49+ continue
50+ }
51+
4052 let contactName = " \( bundleDisplayName) - \( contactType. rawValue) "
4153
42- guard let imageData = self . generateContactImage ( bgValue: bgValue, trend: trend, delta: delta, stale: stale, contactType: contactType) ? . pngData ( ) else {
54+ let includedFields = self . getIncludedFields ( for: contactType)
55+
56+ let dynamicTextColor = self . textColor ( for: contactType)
57+
58+ guard let imageData = self . generateContactImage ( bgValue: bgValue, trend: trend, delta: delta, iob: iob, stale: stale, contactType: contactType, includedFields: includedFields, textColor: dynamicTextColor) ? . pngData ( ) else {
4359 LogManager . shared. log ( category: . contact, message: " Failed to generate contact image for \( contactName) . " )
4460 continue
4561 }
@@ -100,7 +116,24 @@ class ContactImageUpdater {
100116 }
101117 }
102118
103- private func generateContactImage( bgValue: String , trend: String , delta: String , stale: Bool , contactType: ContactType ) -> UIImage ? {
119+ private func getIncludedFields( for contactType: ContactType ) -> [ ContactType ] {
120+ var included : [ ContactType ] = [ ]
121+ if Storage . shared. contactTrend. value == . include,
122+ Storage . shared. contactTrendTarget. value == contactType {
123+ included. append ( . Trend)
124+ }
125+ if Storage . shared. contactDelta. value == . include,
126+ Storage . shared. contactDeltaTarget. value == contactType {
127+ included. append ( . Delta)
128+ }
129+ if Storage . shared. contactIOB. value == . include,
130+ Storage . shared. contactIOBTarget. value == contactType {
131+ included. append ( . IOB)
132+ }
133+ return included
134+ }
135+
136+ private func generateContactImage( bgValue: String , trend: String , delta: String , iob: String , stale: Bool , contactType: ContactType , includedFields: [ ContactType ] , textColor: UIColor ) -> UIImage ? {
104137 let size = CGSize ( width: 300 , height: 300 )
105138 UIGraphicsBeginImageContextWithOptions ( size, false , 0 )
106139 guard let context = UIGraphicsGetCurrentContext ( ) else { return nil }
@@ -111,66 +144,65 @@ class ContactImageUpdater {
111144 let paragraphStyle = NSMutableParagraphStyle ( )
112145 paragraphStyle. alignment = . center
113146
114- // Format extraDelta based on the user's unit preference
115- let unitPreference = Storage . shared. units. value
116147 let yOffset : CGFloat = 48
117- if contactType == . Trend, Storage . shared. contactTrend. value == . separate {
118- let trendRect = CGRect ( x: 0 , y: 46 , width: size. width, height: size. height - 80 )
119- let trendFontSize = max ( 40 , 200 - CGFloat( trend. count * 15 ) )
120148
121- let trendAttributes : [ NSAttributedString . Key : Any ] = [
122- . font: UIFont . boldSystemFont ( ofSize: trendFontSize) ,
123- . foregroundColor: stale ? UIColor . gray : savedTextUIColor,
124- . paragraphStyle: paragraphStyle,
125- ]
149+ // Get the primary value for this contact type
150+ let primaryValue : String
151+ switch contactType {
152+ case . BG: primaryValue = bgValue
153+ case . Trend: primaryValue = trend
154+ case . Delta: primaryValue = delta
155+ case . IOB: primaryValue = iob
156+ }
126157
127- trend. draw ( in: trendRect, withAttributes: trendAttributes)
128- } else if contactType == . Delta, Storage . shared. contactDelta. value == . separate {
129- let deltaRect = CGRect ( x: 0 , y: yOffset, width: size. width, height: size. height - 80 )
130- let deltaFontSize = max ( 40 , 200 - CGFloat( delta. count * 15 ) )
158+ // Build extra values from included fields
159+ var extraValues : [ String ] = [ ]
160+ for field in includedFields {
161+ switch field {
162+ case . Trend: extraValues. append ( trend)
163+ case . Delta: extraValues. append ( delta)
164+ case . IOB: extraValues. append ( iob)
165+ case . BG: break
166+ }
167+ }
131168
132- let deltaAttributes : [ NSAttributedString . Key : Any ] = [
133- . font: UIFont . boldSystemFont ( ofSize: deltaFontSize) ,
134- . foregroundColor: stale ? UIColor . gray : savedTextUIColor,
135- . paragraphStyle: paragraphStyle,
136- ]
169+ let hasExtras = !extraValues. isEmpty
137170
138- delta . draw ( in : deltaRect , withAttributes : deltaAttributes )
139- } else if contactType == . BG {
140- let includesExtra = Storage . shared . contactDelta . value == . include || Storage . shared . contactTrend . value == . include
171+ // Determine font sizes based on number of extras
172+ let maxFontSize : CGFloat = extraValues . count >= 2 ? 140 : ( hasExtras ? 160 : 200 )
173+ let extraFontSize : CGFloat = extraValues . count >= 2 ? 60 : 90
141174
142- let maxFontSize : CGFloat = includesExtra ? 160 : 200
143- let fontSize = maxFontSize - CGFloat( bgValue. count * 15 )
144- var bgAttributes : [ NSAttributedString . Key : Any ] = [
145- . font: UIFont . boldSystemFont ( ofSize: fontSize) ,
146- . foregroundColor: stale ? UIColor . gray : savedTextUIColor,
147- . paragraphStyle: paragraphStyle,
148- ]
175+ let fontSize = max ( 40 , maxFontSize - CGFloat( primaryValue. count * 15 ) )
149176
150- if stale {
151- // Force background color back to black if stale
152- UIColor . black. setFill ( )
153- context. fill ( CGRect ( origin: . zero, size: size) )
154- bgAttributes [ . strikethroughStyle] = NSUnderlineStyle . single. rawValue
155- }
177+ let isBGStale = stale && contactType == . BG
156178
157- let bgRect : CGRect = includesExtra
158- ? CGRect ( x: 0 , y: yOffset - 20 , width: size. width, height: size. height / 2 )
159- : CGRect ( x: 0 , y: yOffset, width: size. width, height: size. height - 80 )
179+ var primaryAttributes : [ NSAttributedString . Key : Any ] = [
180+ . font: UIFont . boldSystemFont ( ofSize: fontSize) ,
181+ . foregroundColor: isBGStale ? UIColor . gray : textColor,
182+ . paragraphStyle: paragraphStyle,
183+ ]
160184
161- bgValue. draw ( in: bgRect, withAttributes: bgAttributes)
185+ if isBGStale {
186+ UIColor . black. setFill ( )
187+ context. fill ( CGRect ( origin: . zero, size: size) )
188+ primaryAttributes [ . strikethroughStyle] = NSUnderlineStyle . single. rawValue
189+ }
162190
163- if includesExtra {
164- let extraRect = CGRect ( x: 0 , y: size. height / 2 + 6 , width: size. width, height: size. height / 2 - 20 )
165- let extraAttributes : [ NSAttributedString . Key : Any ] = [
166- . font: UIFont . systemFont ( ofSize: 90 ) ,
167- . foregroundColor: stale ? UIColor . gray : savedTextUIColor,
168- . paragraphStyle: paragraphStyle,
169- ]
191+ let primaryRect : CGRect = hasExtras
192+ ? CGRect ( x: 0 , y: yOffset - 20 , width: size. width, height: size. height / 2 )
193+ : CGRect ( x: 0 , y: yOffset, width: size. width, height: size. height - 80 )
170194
171- let extra = Storage . shared. contactDelta. value == . include ? delta : trend
172- extra. draw ( in: extraRect, withAttributes: extraAttributes)
173- }
195+ primaryValue. draw ( in: primaryRect, withAttributes: primaryAttributes)
196+
197+ if hasExtras {
198+ let extraString = extraValues. joined ( separator: " " )
199+ let extraRect = CGRect ( x: 0 , y: size. height / 2 + 6 , width: size. width, height: size. height / 2 - 20 )
200+ let extraAttributes : [ NSAttributedString . Key : Any ] = [
201+ . font: UIFont . systemFont ( ofSize: extraFontSize) ,
202+ . foregroundColor: isBGStale ? UIColor . gray : textColor,
203+ . paragraphStyle: paragraphStyle,
204+ ]
205+ extraString. draw ( in: extraRect, withAttributes: extraAttributes)
174206 }
175207
176208 let image = UIGraphicsGetImageFromCurrentImageContext ( )
0 commit comments