-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #197 from cocoatype/192-add-generic-web-deep-link
Support web deep links on iOS
- Loading branch information
Showing
21 changed files
with
336 additions
and
97 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
11 changes: 11 additions & 0 deletions
11
Modules/Capabilities/URLParsing/Sources/URLParseResult.swift
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,11 @@ | ||
// Created by Geoff Pado on 7/25/24. | ||
// Copyright © 2024 Cocoatype, LLC. All rights reserved. | ||
|
||
import UIKit | ||
|
||
public enum URLParseResult { | ||
case callbackAction(CallbackAction) | ||
case image(URL) | ||
case website(URL) | ||
case invalid | ||
} |
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,30 @@ | ||
// Created by Geoff Pado on 7/25/24. | ||
// Copyright © 2024 Cocoatype, LLC. All rights reserved. | ||
|
||
import UIKit | ||
|
||
public struct URLParser { | ||
public init() {} | ||
|
||
public func parse(_ url: URL) -> URLParseResult { | ||
if let action = CallbackAction(url: url) { | ||
return .callbackAction(action) | ||
} else if url.isFileURL, FileManager.default.fileExists(atPath: url.path) { | ||
return .image(url) | ||
} else if let webURL = webURL(from: url) { | ||
return .website(webURL) | ||
} else { | ||
return .invalid | ||
} | ||
} | ||
|
||
func webURL(from url: URL) -> URL? { | ||
guard var components = URLComponents(url: url, resolvingAgainstBaseURL: true), | ||
components.host == "blackhighlighter.app" | ||
else { return nil } | ||
|
||
components.scheme = "https" | ||
|
||
return components.url | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
70 changes: 70 additions & 0 deletions
70
Modules/Capabilities/URLParsing/Tests/CallbackActionTests.swift
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,70 @@ | ||
// Created by Geoff Pado on 7/26/24. | ||
// Copyright © 2024 Cocoatype, LLC. All rights reserved. | ||
|
||
import XCTest | ||
|
||
@testable import URLParsing | ||
|
||
class CallbackActionTests: XCTestCase { | ||
func testCreatingOpenAction() throws { | ||
let url = try CallbackActionURLGenerator().openURL(imageURL: imageURL) | ||
let expectedImage = try XCTUnwrap(UIImage(contentsOfFile: imageURL.path)) | ||
let action = try XCTUnwrap(CallbackAction(url: url)) | ||
XCTAssert(action.isOpen) | ||
XCTAssertEqual(action.image.size, expectedImage.size) | ||
} | ||
|
||
func testCreatingEditActionWithoutCallback() throws { | ||
let url = try CallbackActionURLGenerator().editURL(imageURL: imageURL, successURL: nil) | ||
let expectedImage = try XCTUnwrap(UIImage(contentsOfFile: imageURL.path)) | ||
let action = try XCTUnwrap(CallbackAction(url: url)) | ||
XCTAssert(action.isEdit) | ||
XCTAssertNil(action.callbackURL) | ||
XCTAssertEqual(action.image.size, expectedImage.size) | ||
} | ||
|
||
func testCreatingEditActionWithCallback() throws { | ||
let successURL = try XCTUnwrap(URL(string: "https://blackhiglighter.app")) | ||
let expectedImage = try XCTUnwrap(UIImage(contentsOfFile: imageURL.path)) | ||
let url = try CallbackActionURLGenerator().editURL(imageURL: imageURL, successURL: successURL) | ||
let action = try XCTUnwrap(CallbackAction(url: url)) | ||
XCTAssert(action.isEdit) | ||
XCTAssertEqual(action.callbackURL, successURL) | ||
XCTAssertEqual(action.image.size, expectedImage.size) | ||
} | ||
|
||
func testCreatingNonCallbackAction() throws { | ||
let url = try XCTUnwrap(URL(string: "highlighter://bad-host/")) | ||
let action = CallbackAction(url: url) | ||
XCTAssertNil(action) | ||
} | ||
|
||
func testCreatingInvalidCallbackAction() throws { | ||
let url = try CallbackActionURLGenerator().url(action: "bad-action", imageURL: imageURL, successURL: nil) | ||
let action = CallbackAction(url: url) | ||
XCTAssertNil(action) | ||
} | ||
} | ||
|
||
extension CallbackAction { | ||
var isEdit: Bool { | ||
switch self { | ||
case .edit: true | ||
case .open: false | ||
} | ||
} | ||
|
||
var isOpen: Bool { | ||
switch self { | ||
case .open: true | ||
case .edit: false | ||
} | ||
} | ||
|
||
var callbackURL: URL? { | ||
switch self { | ||
case .edit(_, let url): url | ||
case .open: nil | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
Modules/Capabilities/URLParsing/Tests/Helpers/CallbackActionURLGenerator.swift
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,38 @@ | ||
// Created by Geoff Pado on 7/26/24. | ||
// Copyright © 2024 Cocoatype, LLC. All rights reserved. | ||
|
||
import XCTest | ||
|
||
struct CallbackActionURLGenerator { | ||
func openURL(imageURL: URL) throws -> URL { | ||
try url(action: "open", imageURL: imageURL, successURL: nil) | ||
} | ||
|
||
func editURL(imageURL: URL, successURL: URL?) throws -> URL { | ||
try url(action: "edit", imageURL: imageURL, successURL: successURL) | ||
} | ||
|
||
// MARK: Intermediate Builders | ||
|
||
func url(action: String, imageURL: URL, successURL: URL?) throws -> URL { | ||
let imageString = try imageDataString(forImageAt: imageURL) | ||
var queryItems = [URLQueryItem(name: "imageData", value: imageString)] | ||
if let successURL { | ||
let encodedURL = try XCTUnwrap(successURL.absoluteString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)) | ||
queryItems.append(URLQueryItem(name: "x-success", value: encodedURL)) | ||
} | ||
return try url(path: "/\(action)", queryItems: queryItems) | ||
} | ||
|
||
private func url(path: String, queryItems: [URLQueryItem]) throws -> URL { | ||
var components = try XCTUnwrap(URLComponents(string: "highlighter://x-callback-url")) | ||
components.path = path | ||
components.queryItems = queryItems | ||
return try XCTUnwrap(components.url) | ||
} | ||
|
||
private func imageDataString(forImageAt url: URL) throws -> String { | ||
let imageData = try XCTUnwrap(FileManager.default.contents(atPath: url.path)) | ||
return imageData.base64EncodedString() | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
Modules/Capabilities/URLParsing/Tests/Helpers/URLParseResultExtensions.swift
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,35 @@ | ||
// Created by Geoff Pado on 7/26/24. | ||
// Copyright © 2024 Cocoatype, LLC. All rights reserved. | ||
|
||
import Foundation | ||
import URLParsing | ||
|
||
extension URLParseResult { | ||
var imageURL: URL? { | ||
switch self { | ||
case .image(let imageURL): imageURL | ||
case .callbackAction, .website, .invalid: nil | ||
} | ||
} | ||
|
||
var isInvalid: Bool { | ||
switch self { | ||
case .invalid: true | ||
case .callbackAction, .website, .image: false | ||
} | ||
} | ||
|
||
var isCallbackAction: Bool { | ||
switch self { | ||
case .callbackAction: true | ||
case .invalid, .website, .image: false | ||
} | ||
} | ||
|
||
var webURL: URL? { | ||
switch self { | ||
case .website(let webURL): webURL | ||
case .callbackAction, .image, .invalid: nil | ||
} | ||
} | ||
} |
Oops, something went wrong.