Skip to content

Commit

Permalink
Swiftlint Code cleanup (#21)
Browse files Browse the repository at this point in the history
* Cleaned up Swift Lint issues

* Cleaned up message processing code
  • Loading branch information
dsarfati authored Jan 7, 2018
1 parent fedbd58 commit b963a06
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 54 deletions.
7 changes: 5 additions & 2 deletions Examples/iOS Example/iOS Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
651624311F16DA9F00932E26 = {
CreatedOnToolsVersion = 8.3.3;
DevelopmentTeam = 24QXGCES3E;
LastSwiftMigration = 0920;
ProvisioningStyle = Automatic;
};
};
Expand Down Expand Up @@ -388,7 +389,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.zapic.iOS-Example";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -404,7 +406,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.zapic.iOS-Example";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand Down
8 changes: 4 additions & 4 deletions Examples/iOS Example/iOS Example/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSPhotoLibraryUsageDescription</key>
<string>Zapic will only use the Photos you select.</string>
<key>NSContactsUsageDescription</key>
<string>We&apos;ll use your Contacts to find people you know on Zapic.</string>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
Expand All @@ -29,6 +25,10 @@
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>NSContactsUsageDescription</key>
<string>We'll use your Contacts to find people you know on Zapic.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Zapic will only use the Photos you select.</string>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
Expand Down
4 changes: 2 additions & 2 deletions Zapic/Banner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ class Banner: UIView {
self.didTapBlock = didTapBlock

if let subText = subtitle {
bannerContent = NotificationBannerView(title: title, text: subText, icon:icon)
bannerContent = NotificationBannerView(title: title, text: subText, icon: icon)
} else {
bannerContent = MessageBannerView(title, icon:icon)
bannerContent = MessageBannerView(title, icon: icon)
}

super.init(frame: CGRect.zero)
Expand Down
4 changes: 2 additions & 2 deletions Zapic/BannerContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class MessageBannerView: ZapicBanner {

init(_ text: String, icon: UIImage?) {

super.init(icon:icon)
super.init(icon: icon)

let label = UILabel()
label.font = UIFont.systemFont(ofSize: 16)
Expand All @@ -94,7 +94,7 @@ class NotificationBannerView: ZapicBanner {

init(title: String, text: String, icon: UIImage?) {

super.init(contentRightPadding:20, icon:icon)
super.init(contentRightPadding: 20, icon: icon)

//Title
let titleLabel = UILabel()
Expand Down
6 changes: 3 additions & 3 deletions Zapic/ContactManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ContactManager {

let contactStore = CNContactStore()

func getContacts(completionHandler: @escaping (_ contacts: [[String:Any]]?) -> Void) {
func getContacts(completionHandler: @escaping (_ contacts: [[String: Any]]?) -> Void) {

requestForAccess { (accessGranted) -> Void in
if accessGranted {
Expand Down Expand Up @@ -108,7 +108,7 @@ class ContactManager {
return results
}

private func processContacts(_ input: [CNContact]) -> [[String:Any]] {
private func processContacts(_ input: [CNContact]) -> [[String: Any]] {
var contacts = [[String: Any]]()
for contact in input {

Expand All @@ -123,7 +123,7 @@ class ContactManager {
emailAddresses.append(String(email.value))
}

let dict: [String:Any] =
let dict: [String: Any] =
["first": contact.givenName,
"last": contact.familyName,
"emails": emailAddresses,
Expand Down
4 changes: 2 additions & 2 deletions Zapic/GameCenterHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class GameCenterHelper {
/// Generates the current player's GameCenter signature
///
/// - Parameter completionHandler: Callback when the signature has been generated
static func generateSignature(completionHandler: @escaping ([String:Any]?, Error?) -> Void) {
static func generateSignature(completionHandler: @escaping ([String: Any]?, Error?) -> Void) {
gameCenterAuthenticate { (player, error) in
guard error == nil else {
ZLog.error("Error authenticaing player")
Expand Down Expand Up @@ -76,7 +76,7 @@ class GameCenterHelper {
}
}

private static func generateIdentityInfo(player: GKLocalPlayer, completionHandler: @escaping ([String:Any]?, Error?) -> Void) {
private static func generateIdentityInfo(player: GKLocalPlayer, completionHandler: @escaping ([String: Any]?, Error?) -> Void) {
ZLog.info("Generating identity signature")

player.generateIdentityVerificationSignature { (publicKeyUrl: URL!, signature: Data!, salt: Data!, timestamp: UInt64, error: Error!) -> Void in
Expand Down
2 changes: 1 addition & 1 deletion Zapic/Views/ZapicView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ZapicView: UIView {
let iconSize: CGFloat = 64
icon.translatesAutoresizingMaskIntoConstraints = false
icon.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
icon.centerYAnchor.constraint(equalTo: self.centerYAnchor, constant:-100).isActive = true
icon.centerYAnchor.constraint(equalTo: self.centerYAnchor, constant: -100).isActive = true
icon.heightAnchor.constraint(equalToConstant: iconSize).isActive = true
icon.widthAnchor.constraint(equalToConstant: iconSize).isActive = true

Expand Down
40 changes: 17 additions & 23 deletions Zapic/Views/ZapicWebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct Event {
}
}

protocol ZapicViewControllerDelegate : class {
protocol ZapicViewControllerDelegate: class {
func onPageReady()
func onAppError(error: Error)
func closePage()
Expand Down Expand Up @@ -124,12 +124,12 @@ class ZapicWebView: WKWebView, WKScriptMessageHandler, UIScrollViewDelegate, Zap
return
}

super.load(URLRequest(url: myURL, timeoutInterval:30))
super.load(URLRequest(url: myURL, timeoutInterval: 30))
}

func onAppError(error: Error) {
status = .error
controllerDelegate?.onAppError(error:error)
controllerDelegate?.onAppError(error: error)
zapicDelegate?.onAppError(error: error)
}

Expand All @@ -141,7 +141,6 @@ class ZapicWebView: WKWebView, WKScriptMessageHandler, UIScrollViewDelegate, Zap
switch methodName {
case "dispatch":
handleDispatch(message)
break
case "console":
handleConsole(message)
default:
Expand Down Expand Up @@ -173,8 +172,6 @@ class ZapicWebView: WKWebView, WKScriptMessageHandler, UIScrollViewDelegate, Zap
level = ZLogLevel.error
case "WARN":
level = ZLogLevel.warn
case "INFO":
fallthrough
default:
level = ZLogLevel.info
}
Expand Down Expand Up @@ -204,35 +201,32 @@ class ZapicWebView: WKWebView, WKScriptMessageHandler, UIScrollViewDelegate, Zap
return
}

handleMessage(type: type, json: json)
}

private func handleMessage(type: WebEvent, json: [String: Any]) {

ZLog.info("Received from JS: \(type) ")

switch type {
case .login:
zapicDelegate?.getVerificationSignature()
break
case .getContacts:
zapicDelegate?.getContacts()
break
case .appStarted:
status = .ready
zapicDelegate?.onAppReady()
break
case .loggedIn:
loggedIn(json)
break
case .pageReady:
isPageReady = true
controllerDelegate?.onPageReady()
break
case .showBanner:
receiveBanner(json)
break
case .closePageRequest:
controllerDelegate?.closePage()
break
default:
ZLog.warn("Unhandled message type \(type)")
break
}
}

Expand All @@ -252,8 +246,8 @@ class ZapicWebView: WKWebView, WKScriptMessageHandler, UIScrollViewDelegate, Zap

// MARK: - Receive Messages

private func receiveBanner(_ json: [String:Any]) {
guard let msg = json["payload"] as? [String:Any] else {
private func receiveBanner(_ json: [String: Any]) {
guard let msg = json["payload"] as? [String: Any] else {
ZLog.warn("Received invalid ShowBanner payload")
return
}
Expand All @@ -263,7 +257,7 @@ class ZapicWebView: WKWebView, WKScriptMessageHandler, UIScrollViewDelegate, Zap
return
}

let icon: UIImage? = decode(base64:msg["icon"] as? String)
let icon: UIImage? = decode(base64: msg["icon"] as? String)

let subTitle = msg["subtitle"] as? String

Expand All @@ -272,7 +266,7 @@ class ZapicWebView: WKWebView, WKScriptMessageHandler, UIScrollViewDelegate, Zap

private func loggedIn(_ json: [String: Any]) {

guard let msg = json["payload"] as? [String:Any] else {
guard let msg = json["payload"] as? [String: Any] else {
ZLog.warn("Received invalid SetPlayerId payload")
return
}
Expand All @@ -292,11 +286,11 @@ class ZapicWebView: WKWebView, WKScriptMessageHandler, UIScrollViewDelegate, Zap

// MARK: - Events

func dispatchToJS(type: WebFunction, payload:Any) {
func dispatchToJS(type: WebFunction, payload: Any) {
dispatchToJS(type: type, payload: payload, isError: false)
}

func dispatchToJS(type: WebFunction, payload:Any, isError: Bool) {
func dispatchToJS(type: WebFunction, payload: Any, isError: Bool) {

//Ensure setSignature is the only method that can be sent before
//the app is ready
Expand Down Expand Up @@ -332,7 +326,7 @@ class ZapicWebView: WKWebView, WKScriptMessageHandler, UIScrollViewDelegate, Zap
}
}

func submitEvent(eventType: EventType, params: [String:Any]) {
func submitEvent(eventType: EventType, params: [String: Any]) {

if status != .ready {
ZLog.info("Web client is not ready to accept events")
Expand All @@ -341,11 +335,11 @@ class ZapicWebView: WKWebView, WKScriptMessageHandler, UIScrollViewDelegate, Zap

ZLog.info("Submitting event to web client")

let msg: [String:Any] = ["type": eventType.rawValue,
let msg: [String: Any] = ["type": eventType.rawValue,
"params": params,
"timestamp": Date().iso8601]

dispatchToJS(type:.submitEvent, payload: msg)
dispatchToJS(type: .submitEvent, payload: msg)
}

/// Attempt to resend all events that we unable to send
Expand Down
2 changes: 1 addition & 1 deletion Zapic/ZLog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class ZLog: NSObject {
}

static func log(_ message: String, level: ZLogLevel, source: ZLogSource = .sdk) {
writeLog(message:message, level: level, source: source)
writeLog(message: message, level: level, source: source)
}

private static func writeLog(message: String, level: ZLogLevel, source: ZLogSource) {
Expand Down
4 changes: 2 additions & 2 deletions Zapic/Zapic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class Zapic: NSObject {
core.submitEvent(eventType: .gameplay, params: params)
}

@objc public static func submitEvent(_ params: [String:Any]) {
@objc public static func submitEvent(_ params: [String: Any]) {
core.submitEvent(eventType: .gameplay, params: params)
}

Expand All @@ -58,7 +58,7 @@ public class Zapic: NSObject {
ZLog.error("Invalid view name \(viewName)")
return
}
show(view:view)
show(view: view)
}

public static func show(view: ZapicViews) {
Expand Down
6 changes: 3 additions & 3 deletions Zapic/ZapicColors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import Foundation
import UIKit

class ZapicColors {
static let blue = UIColor(red:0.00, green:0.87, blue:0.68, alpha:1.0)
static let green = UIColor(red:0.00, green:0.52, blue:0.89, alpha:1.0)
static let midPoint = UIColor(red:0.00, green:0.69, blue:0.79, alpha:1.0)
static let blue = UIColor(red: 0.00, green: 0.87, blue: 0.68, alpha: 1.0)
static let green = UIColor(red: 0.00, green: 0.52, blue: 0.89, alpha: 1.0)
static let midPoint = UIColor(red: 0.00, green: 0.69, blue: 0.79, alpha: 1.0)
static let background = UIColor.white// UIColor(red:0.11, green:0.13, blue:0.15, alpha:1.0)
static let primaryText = UIColor.black
static let secondaryText = UIColor.black.withAlphaComponent(0.5)
Expand Down
4 changes: 2 additions & 2 deletions Zapic/ZapicController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ZapicController: UIViewController, ZapicViewControllerDelegate {
fatalError("RootViewController not found, ensure this is called at the correct time")
}

super.init(nibName:nil, bundle:nil)
super.init(nibName: nil, bundle: nil)

loading.controllerDelegate = self
offline.controllerDelegate = self
Expand Down Expand Up @@ -62,7 +62,7 @@ class ZapicController: UIViewController, ZapicViewControllerDelegate {

func closePage() {
self.closeView {
self.webView.dispatchToJS(type: .closePage, payload:"")
self.webView.dispatchToJS(type: .closePage, payload: "")
}
}

Expand Down
8 changes: 4 additions & 4 deletions Zapic/ZapicCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class ZapicCore: ZapicDelegate {
if let contacts = contacts {
self.webClient.dispatchToJS(type: .setContacts, payload: contacts)
} else {
self.webClient.dispatchToJS(type: .setContacts, payload: "Unable to get contacts", isError:true)
self.webClient.dispatchToJS(type: .setContacts, payload: "Unable to get contacts", isError: true)
}
}
}
Expand All @@ -73,7 +73,7 @@ class ZapicCore: ZapicDelegate {

func show(view: ZapicViews) {
ZLog.info("Show \(view.rawValue)")
zapicController.show(view:view)
zapicController.show(view: view)
}

func getVerificationSignature() {
Expand All @@ -84,15 +84,15 @@ class ZapicCore: ZapicDelegate {
if let sig = signature {
self.webClient.dispatchToJS(type: .setSignature, payload: sig)
} else {
self.webClient.dispatchToJS(type: .setSignature, payload: "Error with Game Center", isError:true)
self.webClient.dispatchToJS(type: .setSignature, payload: "Error with Game Center", isError: true)
}
}
}

/// Trigger when a banner should be shown
func showBanner(title: String, subTitle: String?, icon: UIImage?) {

let banner = Banner(title: title, subtitle:subTitle, icon:icon)
let banner = Banner(title: title, subtitle: subTitle, icon: icon)

banner.dismissesOnTap = true
banner.show(duration: 3.0)
Expand Down
2 changes: 1 addition & 1 deletion Zapic/ZapicDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Foundation

protocol ZapicDelegate : class {
protocol ZapicDelegate: class {

/// Triggers the generation of the
/// Game Center verification signature
Expand Down
4 changes: 2 additions & 2 deletions Zapic/ZapicWebClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import Foundation
protocol ZapicWebClient {
var zapicDelegate: ZapicDelegate? { get set }
func submitEvent(eventType: EventType, params: [String: Any])
func dispatchToJS(type: WebFunction, payload:Any)
func dispatchToJS(type: WebFunction, payload:Any, isError: Bool)
func dispatchToJS(type: WebFunction, payload: Any)
func dispatchToJS(type: WebFunction, payload: Any, isError: Bool)

/// Load the web client
func load()
Expand Down

0 comments on commit b963a06

Please sign in to comment.