From ba2c16d298f2a3c75777f3c75d741cb6ba134569 Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Sat, 26 Apr 2025 18:57:41 -0700 Subject: [PATCH] First commit --- .gitignore | 8 +++ License.md | 7 ++ Package.swift | 28 ++++++++ Readme.md | 43 +++++++++++ .../BetterNotification.swift | 71 +++++++++++++++++++ .../BetterNotificationTests.swift | 6 ++ 6 files changed, 163 insertions(+) create mode 100644 .gitignore create mode 100644 License.md create mode 100644 Package.swift create mode 100644 Readme.md create mode 100644 Sources/BetterNotification/BetterNotification.swift create mode 100644 Tests/BetterNotificationTests/BetterNotificationTests.swift diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0023a53 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +.DS_Store +/.build +/Packages +xcuserdata/ +DerivedData/ +.swiftpm/configuration/registries.json +.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata +.netrc diff --git a/License.md b/License.md new file mode 100644 index 0000000..1f05728 --- /dev/null +++ b/License.md @@ -0,0 +1,7 @@ +Copyright © 2025 Sami Samhuri, https://samhuri.net + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..1e4b0a5 --- /dev/null +++ b/Package.swift @@ -0,0 +1,28 @@ +// swift-tools-version: 6.0 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "BetterNotification", + platforms: [ + .iOS(.v18), + .macOS(.v15), + ], + products: [ + // Products define the executables and libraries a package produces, making them visible to other packages. + .library( + name: "BetterNotification", + targets: ["BetterNotification"]), + ], + targets: [ + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. + .target( + name: "BetterNotification"), + .testTarget( + name: "BetterNotificationTests", + dependencies: ["BetterNotification"] + ), + ] +) diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..27972b7 --- /dev/null +++ b/Readme.md @@ -0,0 +1,43 @@ +# BetterNotification + +[![0 dependencies!](https://0dependencies.dev/0dependencies.svg)](https://0dependencies.dev) +[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fsamsonjs%2FBetterNotification%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/samsonjs/BetterNotification) +[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fsamsonjs%2FBetterNotification%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/samsonjs/BetterNotification) + +## Overview + +tkt + +## Usage + +tktk + +## Installation + +The only way to install this package is with Swift Package Manager (SPM). Please [file a new issue][] or submit a pull-request if you want to use something else. + +[file a new issue]: https://github.com/samsonjs/BetterNotification/issues/new + +### Supported Platforms + +This package is supported on iOS 16.0+ and macOS 12.0+. + +### Xcode + +When you're integrating this into an app with Xcode then go to your project's Package Dependencies and enter the URL `https://github.com/samsonjs/BetterNotification` and then go through the usual flow for adding packages. + +### Swift Package Manager (SPM) + +When you're integrating this using SPM on its own then add this to the list of dependencies your Package.swift file: + +```swift +.package(url: "https://github.com/samsonjs/BetterNotification.git", .upToNextMajor(from: "0.1.0")) +``` + +and then add `"BetterNotification"` to the list of dependencies in your target as well. + +## License + +Copyright © 2025 [Sami Samhuri](https://samhuri.net) . Released under the terms of the [MIT License][MIT]. + +[MIT]: https://sjs.mit-license.org diff --git a/Sources/BetterNotification/BetterNotification.swift b/Sources/BetterNotification/BetterNotification.swift new file mode 100644 index 0000000..4f41bf8 --- /dev/null +++ b/Sources/BetterNotification/BetterNotification.swift @@ -0,0 +1,71 @@ +import Combine +import Foundation + +/// A marker protocol for types that represent notifications with associated data. Conforming types gain a default notification name and +/// user info key. They can be used with extension methods like ``NotificationCenter.notifications(for:)`` and +/// ``NotificationCenter.publisher(for:)`` which automatically extract and cast this type from user info. +/// +/// When posting better notifications you can use ``Notification.better(:object:)`` to build up a notification with the correct +/// user info automatically. +public protocol BetterNotification {} + +public extension BetterNotification { + /// The notification name associated with the conforming type. + /// + /// Uses the type's name to create a unique raw value in the format: + /// `"BetterNotification:{SelfType}"`. + static var notificationName: Notification.Name { + Notification.Name(rawValue: "BetterNotification:\(Self.self)") + } + + /// The key used in the notification's `userInfo` dictionary to store the notification value. + /// + /// Matches the raw value of `notificationName`. + static var userInfoKey: String { notificationName.rawValue } +} + +public extension Notification { + /// Creates a `Notification` instance for the given `BetterNotification` value. + /// + /// - Parameters: + /// - betterNotification: The `BetterNotification` value to send. + /// - object: An optional sender object. + /// - Returns: A configured `Notification` with `name`, `object`, and `userInfo`. + static func better( + _ betterNotification: BN, + object: Any? = nil + ) -> Notification { + Notification( + name: BN.notificationName, + object: object, + userInfo: [BN.userInfoKey: betterNotification] + ) + } +} + +public extension NotificationCenter { + /// Returns an `AsyncSequence` of notifications of a specific `BetterNotification` type. + /// + /// Each element of the sequence is a `BetterNotification` value. + /// + /// - Parameter betterType: The `BetterNotification` type to observe.. + /// - Returns: An `AsyncSequence` of `BetterNotification` values. + func notifications( + for betterType: BN.Type + ) -> any AsyncSequence { + notifications(named: BN.notificationName) + .compactMap { $0.userInfo?[BN.userInfoKey] as? BN } + } + + /// Returns a Combine publisher that emits `BetterNotification` values of a specific type. + /// + /// - Parameter betterType: The `BetterNotification` type to observe. + /// - Returns: A publisher emitting `BetterNotification` values. + func publisher( + for betterType: T.Type + ) -> AnyPublisher { + publisher(for: betterType.notificationName) + .compactMap { $0.userInfo?[T.userInfoKey] as? T } + .eraseToAnyPublisher() + } +} diff --git a/Tests/BetterNotificationTests/BetterNotificationTests.swift b/Tests/BetterNotificationTests/BetterNotificationTests.swift new file mode 100644 index 0000000..1995410 --- /dev/null +++ b/Tests/BetterNotificationTests/BetterNotificationTests.swift @@ -0,0 +1,6 @@ +import Testing +@testable import BetterNotification + +@Test func example() async throws { + // Write your test here and use APIs like `#expect(...)` to check expected conditions. +}