mirror of
https://github.com/samsonjs/NotificationSmuggler.git
synced 2026-03-25 08:25:48 +00:00
First commit
This commit is contained in:
commit
ba2c16d298
6 changed files with 163 additions and 0 deletions
8
.gitignore
vendored
Normal file
8
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
.DS_Store
|
||||
/.build
|
||||
/Packages
|
||||
xcuserdata/
|
||||
DerivedData/
|
||||
.swiftpm/configuration/registries.json
|
||||
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
|
||||
.netrc
|
||||
7
License.md
Normal file
7
License.md
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
Copyright © 2025 Sami Samhuri, https://samhuri.net <sami@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.
|
||||
28
Package.swift
Normal file
28
Package.swift
Normal file
|
|
@ -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"]
|
||||
),
|
||||
]
|
||||
)
|
||||
43
Readme.md
Normal file
43
Readme.md
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
# BetterNotification
|
||||
|
||||
[](https://0dependencies.dev)
|
||||
[](https://swiftpackageindex.com/samsonjs/BetterNotification)
|
||||
[](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) <sami@samhuri.net>. Released under the terms of the [MIT License][MIT].
|
||||
|
||||
[MIT]: https://sjs.mit-license.org
|
||||
71
Sources/BetterNotification/BetterNotification.swift
Normal file
71
Sources/BetterNotification/BetterNotification.swift
Normal file
|
|
@ -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<BN: BetterNotification>(
|
||||
_ 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<BN: BetterNotification>(
|
||||
for betterType: BN.Type
|
||||
) -> any AsyncSequence<BN, Never> {
|
||||
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<T: BetterNotification>(
|
||||
for betterType: T.Type
|
||||
) -> AnyPublisher<T, Never> {
|
||||
publisher(for: betterType.notificationName)
|
||||
.compactMap { $0.userInfo?[T.userInfoKey] as? T }
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
}
|
||||
|
|
@ -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.
|
||||
}
|
||||
Loading…
Reference in a new issue