Skip to content

Commit

Permalink
Singular topic and category in predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
stigi committed Oct 1, 2024
1 parent ba9513a commit db17887
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Example/Example/UIKit/MagicBellStoreViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class MagicBellStoreViewController: UITableViewController,
self.configureStore(predicate: StorePredicate(archived: true))
})
alert.addAction(UIAlertAction(title: "By Category", style: .default) { _ in
self.configureStore(predicate: StorePredicate(categories: ["order_created"]))
self.configureStore(predicate: StorePredicate(category: "order_created"))
})
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
Expand Down
4 changes: 2 additions & 2 deletions Source/Features/Store/Data/StoreContext+QueryItems.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ extension StorePredicate {
}
}

for category in categories {
if let category = category {
queryItems.append(URLQueryItem(name: "category", value: category))
}
for topic in topics {
if let topic = topic {
queryItems.append(URLQueryItem(name: "topic", value: topic))
}

Expand Down
8 changes: 4 additions & 4 deletions Source/Features/Store/NotificationValidator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,20 @@ struct NotificationValidator {
}

func validateCategory(_ notification: Notification) -> Bool {
if predicate.categories.isEmpty {
if predicate.category == nil {
return true
} else if let category = notification.category {
return predicate.categories.contains(category)
return predicate.category == category
} else {
return false
}
}

func validateTopic(_ notification: Notification) -> Bool {
if predicate.topics.isEmpty {
if predicate.topic == nil {
return true
} else if let topic = notification.topic {
return predicate.topics.contains(topic)
return predicate.topic == topic
} else {
return false
}
Expand Down
8 changes: 4 additions & 4 deletions Source/Features/Store/StoreDirector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ public extension StoreDirector {

/// Build a store based on the category of notifications
/// - Returns: A notification store
func build(categories: [String]) -> NotificationStore {
return build(predicate: StorePredicate(categories: categories))
func build(category: String) -> NotificationStore {
return build(predicate: StorePredicate(category: category))
}

/// Build a store based on the topic of notifications
/// - Returns: A notification store
func build(topics: [String]) -> NotificationStore {
return build(predicate: StorePredicate(topics: topics))
func build(topic: String) -> NotificationStore {
return build(predicate: StorePredicate(topic: topic))
}
}

Expand Down
30 changes: 17 additions & 13 deletions Source/Features/Store/StorePredicate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,44 @@
import Foundation

/// The notificaiton store predicate
public struct StorePredicate: Hashable, Equatable {
public struct StorePredicate {
public let read: Bool?
public let seen: Bool?
public let archived: Bool
public let categories: [String]
public let topics: [String]
public let category: String?
public let topic: String?

/// Predicate default initializer
/// - Parameters:
/// - read: The read status. Defaults to `nil` (not specified).
/// - seen: The seen status. Defaults to `nil` (not specified).
/// - archived: The archived status. Defaults to `false` (unarchived).
/// - categories: The list of categories. Defaults to empty array.
/// - topics: The list of topics. Defaults to empty array.
/// - category: The category. Defaults to nil.
/// - topic: The topic. Defaults to nil.
public init(read: Bool? = nil,
seen: Bool? = nil,
archived: Bool = false,
categories: [String] = [],
topics: [String] = []) {
category: String? = nil,
topic: String? = nil) {
self.read = read
self.seen = seen
self.archived = archived
self.categories = categories
self.topics = topics
self.category = category
self.topic = topic
}
}

extension StorePredicate: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(read.hashValue)
hasher.combine(seen.hashValue)
hasher.combine(archived.hashValue)
hasher.combine(categories.hashValue)
hasher.combine(topics.hashValue)
hasher.combine(category.hashValue)
hasher.combine(topic.hashValue)
}
}

extension StorePredicate: Equatable {
public static func == (lhs: StorePredicate, rhs: StorePredicate) -> Bool {
if lhs.read != rhs.read {
return false
Expand All @@ -58,10 +62,10 @@ public struct StorePredicate: Hashable, Equatable {
if lhs.archived != rhs.archived {
return false
}
if lhs.categories != rhs.categories {
if lhs.category != rhs.category {
return false
}
if lhs.topics != rhs.topics {
if lhs.topic != rhs.topic {
return false
}
return true
Expand Down

0 comments on commit db17887

Please sign in to comment.