Skip to content

Commit b9f8a56

Browse files
committed
Improve style cache behavior - addStyleNamed() should never use the cached default style
1 parent 866d730 commit b9f8a56

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

JDStatusBarNotification/Private/StyleCache.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,45 @@ import Foundation
1010
import UIKit
1111

1212
class StyleCache: NSObject {
13-
var defaultStyle: StatusBarNotificationStyle = StatusBarNotificationStyle()
14-
var userStyles: [String: StatusBarNotificationStyle] = [:]
13+
private var defaultStyle: StatusBarNotificationStyle = StatusBarNotificationStyle()
14+
private var userStyles: [String: StatusBarNotificationStyle] = [:]
1515

16+
// default to cached (potentially modified) default style
1617
func style(forName styleName: String?) -> StatusBarNotificationStyle {
1718
if let styleName, let style = userStyles[styleName] {
1819
return style
1920
}
2021
return defaultStyle
2122
}
2223

24+
// default to cached (potentially modified) default style
2325
func style(forIncludedStyle includedStyle: IncludedStatusBarNotificationStyle) -> StatusBarNotificationStyle {
26+
if includedStyle == .defaultStyle {
27+
return defaultStyle
28+
}
2429
return buildStyleForIncludedStyle(includedStyle)
2530
}
2631

2732
func updateDefaultStyle(_ styleBuilder: NotificationPresenter.PrepareStyleClosure) {
2833
defaultStyle = styleBuilder(defaultStyle)
2934
}
3035

36+
// never base this on the (potentially modified) default style
3137
func addStyleNamed(_ styleName: String,
3238
basedOnStyle includedStyle: IncludedStatusBarNotificationStyle,
3339
prepare styleBuilder: NotificationPresenter.PrepareStyleClosure) -> String
3440
{
35-
userStyles[styleName] = styleBuilder(style(forIncludedStyle: includedStyle))
41+
userStyles[styleName] = styleBuilder(buildStyleForIncludedStyle(includedStyle))
3642
return styleName
3743
}
3844

3945
// MARK: - Included Styles
4046

47+
// Always creates a new Style instance
4148
private func buildStyleForIncludedStyle(_ includedStyle: IncludedStatusBarNotificationStyle) -> StatusBarNotificationStyle {
4249
switch includedStyle {
4350
case .defaultStyle:
44-
return defaultStyle
51+
return StatusBarNotificationStyle()
4552

4653
case .light:
4754
let style = StatusBarNotificationStyle()

JDStatusBarNotification/Public/NotificationPresenter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public class NotificationPresenter: NSObject, NotificationWindowDelegate {
217217
styleCache.updateDefaultStyle(prepare)
218218
}
219219

220-
/// Adds a new named style - based on an included style, if given.
220+
/// Adds a new named style - based on an included style, if given.(otherwise based on the default style)
221221
/// This can later be used by referencing it using the `styleName`.
222222
///
223223
/// The added style can be used in future presentations by utilizing the same `styleName` in e.g. ``present(_:subtitle:styleName:duration:completion:)``.

0 commit comments

Comments
 (0)