-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added UserActivityProvidable protocol to allow dragging items to crea…
…te a new window Removed accessibilityMoveableIfAvailable and accessibilityMoveableListIfAvailable Example App: Added BirdUserActivityProvidableView to handle onContinueUserActivity for Bird objects by opening a new window Added BirdDetailView to show info from a single bird Added MoreInfo view to summarize what Providable and Transferable demo views can do. Added ifAvailable modifier to help with version branching code
- Loading branch information
1 parent
983f8c1
commit dd743e9
Showing
4 changed files
with
99 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// | ||
// UserActivityProvidable.swift | ||
// ILikeToMoveIt | ||
// | ||
// Created by Ryan Lintott on 2023-07-13. | ||
// | ||
|
||
import SwiftUI | ||
|
||
/// A `Providable`object with that has an `NSUserActivity` property that will help it create new windows on iPadOS. | ||
/// | ||
/// Make sure to add your activity type string to plist under `NSUserActivityTypes` and then use the `onContinueUserActivity` overload function that takes a `UserActivityProvidable` object to handle what your app does when opened via this activity. | ||
public protocol UserActivityProvidable: Providable { | ||
/// Type identifier for an associated user activity. | ||
/// | ||
/// Add this string value to your plist under NSUserActivityTypes | ||
static var activityType: String { get } | ||
} | ||
|
||
public extension UserActivityProvidable { | ||
init?(activity: NSUserActivity) { | ||
guard activity.activityType == Self.activityType else { return nil } | ||
guard | ||
let data = activity.targetContentIdentifier?.data(using: .utf8), | ||
let item = try? JSONDecoder().decode(Self.self, from: data) | ||
else { | ||
return nil | ||
} | ||
self = item | ||
} | ||
|
||
var userActivity: NSUserActivity? { | ||
if Self.activityType.isEmpty { return nil } | ||
guard | ||
let data = try? JSONEncoder().encode(self) | ||
else { return nil } | ||
let activity = NSUserActivity(activityType: Self.activityType) | ||
let string = String(data: data, encoding: .utf8) | ||
activity.targetContentIdentifier = string | ||
return activity | ||
} | ||
} | ||
|
||
public extension View { | ||
/// Registers a handler to invoke when a new scene is created by dropping the specified `UserActivityProvidable` type. | ||
/// - Parameters: | ||
/// - item: The type of object that will envoke this handler. | ||
/// - action: The handler that will run when the new scene is created with an optional item that was dropped. The item will be nil if there was an error in the encoding or decoding process. | ||
func onContinueUserActivity<T: UserActivityProvidable>(_ item: T.Type, perform action: @escaping (T?) -> Void ) -> some View { | ||
onContinueUserActivity(T.activityType) { activity in | ||
let item = T(activity: activity) | ||
action(item) | ||
} | ||
} | ||
} |