Skip to content

Commit

Permalink
swiftlint autocorrect
Browse files Browse the repository at this point in the history
  • Loading branch information
e-marchand committed Feb 10, 2021
1 parent d31214d commit f66bca9
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 87 deletions.
4 changes: 2 additions & 2 deletions Clients/Ulysses.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public class Ulysses: Client {
@param index Optional. The position of the new sheet in its parent group. Use 0 to make it the first sheet. Available since Ulysses 2.8 (API version 2).
*/
public func newSheet(text: String, group: String? = nil, format: String? = nil, index: String? = nil, onSuccess: SuccessCallback? = nil, onFailure: FailureCallback? = nil, onCancel: CancelCallback? = nil) throws {

var parameters = ["text": text]
if let group = group {
parameters["group"] = group
Expand All @@ -96,7 +96,7 @@ public class Ulysses: Client {
if let index = index {
parameters["index"] = index
}

try self.perform(action: "new-sheet", parameters: parameters, onSuccess: onSuccess, onFailure: onFailure, onCancel: onCancel)
}

Expand Down
10 changes: 5 additions & 5 deletions SampleApp/CallbackURLKitDemo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
let manager = Manager.shared
manager.callbackURLScheme = Manager.urlSchemes?.first
manager[CallbackURLKitDemo.PrintActionString] = CallbackURLKitDemo.PrintAction

return true
}

Expand All @@ -27,7 +27,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}

}

#elseif os(OSX)
import Cocoa

Expand All @@ -39,15 +39,15 @@ class AppDelegate: NSObject, NSApplicationDelegate {
manager.registerToURLEvent()
manager.callbackURLScheme = Manager.urlSchemes?.first
manager[CallbackURLKitDemo.PrintActionString] = CallbackURLKitDemo.PrintAction
manager["success"] = { (parameters, success, failure, cancel) in

manager["success"] = { (_, success, _, _) in
DispatchQueue.main.async {
success(nil)
}
}

}

}

#endif
16 changes: 8 additions & 8 deletions SampleApp/CallbackURLKitDemo/CallbackURLKitDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import CallbackURLKit

enum DemoError: FailureCallbackError {
case noText

var code: Int {
switch self {
case .noText: return 0
Expand All @@ -25,32 +25,32 @@ enum DemoError: FailureCallbackError {
}

open class CallbackURLKitDemo: Client {

public static let instance = CallbackURLKitDemo()

public init() {
super.init(urlScheme: "callbackurlkit")
}

open func printMessage(_ message: String, onSuccess: SuccessCallback? = nil, onFailure: FailureCallback? = nil, onCancel: CancelCallback? = nil) throws {
let parameters = ["text": message]
try self.perform(action: CallbackURLKitDemo.PrintActionString, parameters: parameters,
onSuccess: onSuccess, onFailure: onFailure, onCancel: onCancel)
}

public static let PrintActionString = "print"
public static let PrintAction: ActionHandler = { parameters, success, failed, cancel in
public static let PrintAction: ActionHandler = { parameters, success, failed, _ in
if let text = parameters["text"] {
print(text)
let formatter = DateFormatter()
formatter.dateStyle = .long
formatter.timeStyle = .medium

let dateString = formatter.string(from: Date())
success(["text": text, "date": dateString])
} else {
failed(DemoError.noText)
}
}

}
20 changes: 9 additions & 11 deletions SampleApp/CallbackURLKitDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import CallbackURLKit
#endif

extension ViewController {

override func viewDidLoad() {
super.viewDidLoad()
}
Expand All @@ -28,19 +28,19 @@ extension ViewController {
try chrome.open(url: "http://www.google.com")
} catch CallbackURLKitError.appWithSchemeNotInstalled(let scheme) {
print("chrome(\(scheme)) not installed or not implement x-callback-url in current os")

} catch CallbackURLKitError.callbackURLSchemeNotDefined {
print("current app scheme not defined")
} catch let e {
print("exception \(e)")
}
}

@IBAction func printAction(_ sender: AnyObject!) {
do {
try CallbackURLKitDemo.instance.printMessage(
"a message %20 % = &toto=a",
onSuccess: { parameters in
onSuccess: { parameters in
print("parameters \(String(describing: parameters))")
},
onFailure: { error in
Expand All @@ -49,20 +49,20 @@ extension ViewController {
)
} catch CallbackURLKitError.appWithSchemeNotInstalled(let scheme) {
print("\(scheme) not installed or not implement x-callback-url in current os")

} catch CallbackURLKitError.callbackURLSchemeNotDefined {
print("current app scheme not defined")
} catch let e {
print("exception \(e)")
}
}

@IBAction func ulysseAuthorize(_ sender: AnyObject!) {
let ulysses = Ulysses()
do {
try ulysses.authorize(
appName: Manager.shared.callbackURLScheme ?? "callbackUrlKit",
onSuccess: { token in
onSuccess: { token in
print("token \(token)")

// try? ulysses.newSheet(text: "test")
Expand All @@ -71,17 +71,15 @@ extension ViewController {
print("\(error)")
}
)

} catch CallbackURLKitError.appWithSchemeNotInstalled(let scheme) {
print("chrome(\(scheme)) not installed or not implement x-callback-url in current os")

} catch CallbackURLKitError.callbackURLSchemeNotDefined {
print("current app scheme not defined")
} catch let e {
print("exception \(e)")
}
}


}

9 changes: 3 additions & 6 deletions Sources/CallbackURLKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ public typealias SuccessCallback = (Parameters?) -> Void
public typealias FailureCallback = (FailureCallbackError) -> Void
public typealias CancelCallback = () -> Void


// MARK: global functions


// Perform an action on client application
// - Parameter action: The action to perform.
// - Parameter urlScheme: The urlScheme for application to apply action.
Expand Down Expand Up @@ -70,7 +68,7 @@ public protocol FailureCallbackError: Error {
var message: String {get}
}
extension FailureCallbackError {
public var XCUErrorParameters: Parameters {
public var XCUErrorParameters: Parameters {
return [kXCUErrorCode: "\(self.code)", kXCUErrorMessage: self.message]
}
public var XCUErrorQuery: String {
Expand All @@ -95,7 +93,7 @@ extension FailureCallbackError {
}

// Framework errors
public enum CallbackURLKitError : Error {
public enum CallbackURLKitError: Error {
// It's seems that application with specified scheme has not installed
case appWithSchemeNotInstalled(scheme: String)
// Failed to create NSURL for request
Expand All @@ -121,10 +119,9 @@ let kXCUErrorMessage = "errorMessage"
// URL to open if the requested action is cancelled by the user. In the case where the target app offer the user the option to “cancel” the requested action, without a success or error result, this the the URL that should be opened to return the user to the source app.
let kXCUCancel = "x-cancel"


// MARK: - framework strings

let kResponse = "response"
let kResponseType = "responseType";
let kResponseType = "responseType"
let kRequestID = "requestID"
let protocolKeys = [kResponse, kResponseType, kRequestID]
14 changes: 7 additions & 7 deletions Sources/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ open class Client {

open var urlScheme: String
open var manager: Manager?

public init(urlScheme: String) {
self.urlScheme = urlScheme
}

open var appInstalled: Bool {
guard let url = URL(string:"\(self.urlScheme)://dummy") else {
guard let url = URL(string: "\(self.urlScheme)://dummy") else {
return false
}
#if os(iOS) || os(tvOS)
return UIApplication.shared.canOpenURL(url)

#elseif os(OSX)
return NSWorkspace.shared.urlForApplication(toOpen: url) != nil
#endif
Expand All @@ -60,11 +60,11 @@ open class Client {

open func perform(action: Action, parameters: Parameters = [:],
onSuccess: SuccessCallback? = nil, onFailure: FailureCallback? = nil, onCancel: CancelCallback? = nil) throws {

let request = Request(
ID: UUID().uuidString, client: self,
action: action, parameters: parameters,
successCallback: onSuccess, failureCallback: onFailure, cancelCallback: onCancel
successCallback: onSuccess, failureCallback: onFailure, cancelCallback: onCancel
)

let manager = self.manager ?? Manager.shared
Expand All @@ -74,7 +74,7 @@ open class Client {
// Return an error according to url, could be changed to fulfiled your need
open func error(code: String?, message: String?) -> FailureCallbackError {
let codeInt: Int
if let c = code, let ci = Int(c) {
if let c = code, let ci = Int(c) {
codeInt = ci
} else {
codeInt = ErrorCode.missingErrorCode.rawValue
Expand Down
32 changes: 15 additions & 17 deletions Sources/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import Foundation
// MARK: String
extension String {

var toQueryDictionary: [String : String] {
var result: [String : String] = [String : String]()
var toQueryDictionary: [String: String] {
var result: [String: String] = [String: String]()
let pairs: [String] = self.components(separatedBy: "&")
for pair in pairs {
let comps: [String] = pair.components(separatedBy: "=")
Expand All @@ -43,17 +43,17 @@ extension String {
var queryEncodeRFC3986: String {
let generalDelimitersToEncode = ":#[]@" // does not include "?" or "/" due to RFC 3986 - Section 3.4
let subDelimitersToEncode = "!$&'()*+,;="

var allowedCharacterSet = CharacterSet.urlQueryAllowed
allowedCharacterSet.remove(charactersIn : generalDelimitersToEncode + subDelimitersToEncode)
allowedCharacterSet.remove(charactersIn: generalDelimitersToEncode + subDelimitersToEncode)

return self.addingPercentEncoding(withAllowedCharacters: allowedCharacterSet) ?? self
}

var queryEncode: String {
return self.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) ?? self
}

var queryDecode: String {
return self.removingPercentEncoding ?? self
}
Expand All @@ -73,31 +73,30 @@ extension Dictionary {
}
return parts.joined(separator: "&") as String
}

fileprivate func join(_ other: Dictionary) -> Dictionary {
var joinedDictionary = Dictionary()

for (key, value) in self {
joinedDictionary.updateValue(value, forKey: key)
}

for (key, value) in other {
joinedDictionary.updateValue(value, forKey: key)
}

return joinedDictionary
}

init(_ pairs: [Element]) {
self.init()
for (k, v) in pairs {
self[k] = v
}
}

}
func +<K, V> (left: [K : V], right: [K : V]) -> [K : V] { return left.join(right) }

}
func +<K, V> (left: [K: V], right: [K: V]) -> [K: V] { return left.join(right) }

// MARK: NSURLComponents
extension URLComponents {
Expand Down Expand Up @@ -125,8 +124,7 @@ extension URLComponents {
self.percentEncodedQuery = add
}
}

}

func &= (left: inout URLComponents, right: String) { left.addToQuery(right) }

Loading

0 comments on commit f66bca9

Please sign in to comment.