-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
53b986e
commit 073f5ec
Showing
16 changed files
with
1,548 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import SwiftUI | ||
|
||
@MainActor | ||
final class EditURLIntentHandler: NSObject, EditURLIntentHandling { | ||
func handle(intent: EditURLIntent) async -> EditURLIntentResponse { | ||
guard let url = intent.url else { | ||
return .init(code: .success, userActivity: nil) | ||
} | ||
|
||
guard var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: true) else { | ||
return .failure(failure: "“\(url)” is not a valid URL.") | ||
} | ||
|
||
let response = EditURLIntentResponse(code: .success, userActivity: nil) | ||
|
||
switch intent.action { | ||
case .unknown: | ||
return .init(code: .failure, userActivity: nil) | ||
case .addQueryItem: | ||
if | ||
let name = intent.queryItemName?.nilIfEmptyOrWhitespace, | ||
let value = intent.queryItemValue?.nilIfEmpty | ||
{ | ||
urlComponents.queryItems = (urlComponents.queryItems ?? []) | ||
.appending(.init(name: name, value: value)) | ||
} | ||
case .addPathComponent: | ||
if let pathComponent = intent.addPathComponentValue?.nilIfEmptyOrWhitespace { | ||
response.result = url.appendingPathComponent(pathComponent) | ||
} | ||
|
||
return response | ||
case .appendToQuery: | ||
if let queryPart = intent.appendToQueryValue?.nilIfEmptyOrWhitespace { | ||
urlComponents.query = (urlComponents.query ?? "") + queryPart | ||
} | ||
case .appendToFragment: | ||
if let fragmentPart = intent.appendToFragmentValue?.nilIfEmptyOrWhitespace { | ||
urlComponents.fragment = (urlComponents.fragment ?? "") + fragmentPart | ||
} | ||
case .removeQueryItemsNamed: | ||
if let name = intent.removeQueryItemsNamedValue { | ||
urlComponents.queryItems = (urlComponents.queryItems ?? []) | ||
.filter { $0.name != name } | ||
.nilIfEmpty | ||
} | ||
case .removeLastPathComponent: | ||
response.result = url.deletingLastPathComponent() | ||
return response | ||
case .removeQuery: | ||
urlComponents.query = nil | ||
case .removeFragment: | ||
urlComponents.fragment = nil | ||
case .removePath: | ||
urlComponents.path = "" | ||
case .removePort: | ||
urlComponents.port = nil | ||
case .removeUserAndPassword: | ||
urlComponents.user = nil | ||
urlComponents.password = nil | ||
case .setQuery: | ||
if let query = intent.setQueryValue?.nilIfEmptyOrWhitespace { | ||
urlComponents.query = query | ||
} | ||
case .setFragment: | ||
if let fragment = intent.setFragmentValue?.nilIfEmptyOrWhitespace { | ||
urlComponents.fragment = fragment | ||
} | ||
case .setPath: | ||
if let path = intent.setPathValue?.nilIfEmptyOrWhitespace { | ||
// Including the `/` prefix is required, but we handle it so the user does not have to care. | ||
urlComponents.path = path.trimmingCharacters(in: .whitespaces).ensurePrefix("/") | ||
} | ||
case .setScheme: | ||
if let scheme = intent.setSchemeValue?.nilIfEmptyOrWhitespace { | ||
urlComponents.scheme = scheme | ||
.replacingSuffix("://", with: "") | ||
.replacingSuffix(":", with: "") | ||
} | ||
case .setHost: | ||
if let host = intent.setHostValue?.nilIfEmptyOrWhitespace { | ||
urlComponents.host = host | ||
} | ||
case .setUsernameAndPassword: | ||
if | ||
let username = intent.username?.trimmingCharacters(in: .whitespaces).nilIfEmpty, | ||
let password = intent.password?.nilIfEmpty | ||
{ | ||
urlComponents.user = username | ||
urlComponents.password = password | ||
} | ||
} | ||
|
||
guard let url = urlComponents.url else { | ||
return .failure(failure: "The edited URL is invalid.") | ||
} | ||
|
||
response.result = url | ||
|
||
return response | ||
} | ||
} |
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
Oops, something went wrong.