Skip to content

Commit

Permalink
Improve style cache behavior - addStyleNamed() should never use the c…
Browse files Browse the repository at this point in the history
…ached default style
  • Loading branch information
calimarkus committed Mar 28, 2024
1 parent 866d730 commit b9f8a56
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
15 changes: 11 additions & 4 deletions JDStatusBarNotification/Private/StyleCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,45 @@ import Foundation
import UIKit

class StyleCache: NSObject {
var defaultStyle: StatusBarNotificationStyle = StatusBarNotificationStyle()
var userStyles: [String: StatusBarNotificationStyle] = [:]
private var defaultStyle: StatusBarNotificationStyle = StatusBarNotificationStyle()
private var userStyles: [String: StatusBarNotificationStyle] = [:]

// default to cached (potentially modified) default style
func style(forName styleName: String?) -> StatusBarNotificationStyle {
if let styleName, let style = userStyles[styleName] {
return style
}
return defaultStyle
}

// default to cached (potentially modified) default style
func style(forIncludedStyle includedStyle: IncludedStatusBarNotificationStyle) -> StatusBarNotificationStyle {
if includedStyle == .defaultStyle {
return defaultStyle
}
return buildStyleForIncludedStyle(includedStyle)
}

func updateDefaultStyle(_ styleBuilder: NotificationPresenter.PrepareStyleClosure) {
defaultStyle = styleBuilder(defaultStyle)
}

// never base this on the (potentially modified) default style
func addStyleNamed(_ styleName: String,
basedOnStyle includedStyle: IncludedStatusBarNotificationStyle,
prepare styleBuilder: NotificationPresenter.PrepareStyleClosure) -> String
{
userStyles[styleName] = styleBuilder(style(forIncludedStyle: includedStyle))
userStyles[styleName] = styleBuilder(buildStyleForIncludedStyle(includedStyle))
return styleName
}

// MARK: - Included Styles

// Always creates a new Style instance
private func buildStyleForIncludedStyle(_ includedStyle: IncludedStatusBarNotificationStyle) -> StatusBarNotificationStyle {
switch includedStyle {
case .defaultStyle:
return defaultStyle
return StatusBarNotificationStyle()

case .light:
let style = StatusBarNotificationStyle()
Expand Down
2 changes: 1 addition & 1 deletion JDStatusBarNotification/Public/NotificationPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public class NotificationPresenter: NSObject, NotificationWindowDelegate {
styleCache.updateDefaultStyle(prepare)
}

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

0 comments on commit b9f8a56

Please sign in to comment.