Skip to content

Commit

Permalink
Support .onOpenURL
Browse files Browse the repository at this point in the history
  • Loading branch information
aabewhite committed Aug 12, 2024
1 parent eb0894d commit a8b473a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,10 @@ Support levels:
<td>✅</td>
<td><code>.onMove</code></td>
</tr>
<tr>
<td>✅</td>
<td><code>.onOpenURL</code></td>
</tr>
<tr>
<td>✅</td>
<td><code>.onReceive</code></td>
Expand Down
30 changes: 28 additions & 2 deletions Sources/SkipUI/SkipUI/System/UserActivity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
// under the terms of the GNU Lesser General Public License 3.0
// as published by the Free Software Foundation https://fsf.org

import Foundation
#if SKIP
import android.app.Activity
import android.content.Intent
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.platform.LocalContext
#endif

extension View {
@available(*, unavailable)
public func userActivity(_ activityType: String, isActive: Bool = true, _ update: @escaping (Any /* NSUserActivity */) -> ()) -> some View {
Expand All @@ -20,9 +28,27 @@ extension View {
return self
}

@available(*, unavailable)
public func onOpenURL(perform action: @escaping (URL) -> ()) -> some View {
public func onOpenURL(perform action: @escaping (URL) -> Void) -> some View {
#if SKIP
return ComposeModifierView(targetView: self) { context in
guard let activity = LocalContext.current as? Activity, let intent = activity.intent, intent.action == Intent.ACTION_VIEW else {
return ComposeResult.ok
}
guard let dataString = intent.dataString, let url = URL(string: dataString) else {
return ComposeResult.ok
}
SideEffect {
action(url)
// Clear the intent so that we don't process it on recompose. We also considered remembering the last
// processed intent in each `onOpenURL`, but then navigation to a new `onOpenURL` modifier would process
// any existing intent as new because it wouldn't be remembered for that modifier
activity.intent = nil
}
return ComposeResult.ok
}
#else
return self
#endif
}
}

Expand Down

0 comments on commit a8b473a

Please sign in to comment.