Skip to content
This repository has been archived by the owner on Nov 29, 2022. It is now read-only.

Commit

Permalink
Merge branch 'release/2.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Svirsky committed Apr 19, 2019
2 parents 4c6699a + c00a6d9 commit 3dc1c83
Show file tree
Hide file tree
Showing 60 changed files with 535 additions and 147 deletions.
6 changes: 3 additions & 3 deletions CerebellumWidget.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
Pod::Spec.new do |s|

s.name = "CerebellumWidget"
s.version = "1.1.5"
s.version = "2.0.0"
s.summary = "Cerebellum widget SDK for iOS."
s.description = "The library includes all the stuff required to embed Cerebellum widget to your iOS application."
s.homepage = "http://cere.io"

s.license = "MIT"

s.author = { "Cerebellum Network Inc." => "widget@cere.io" }
s.author = { "Cerebellum Network, Inc." => "info@cere.io" }

s.platform = :ios, "9.0"

s.source = { :git => "[email protected]:funler/widget-ios.git", :tag => s.version }
s.source = { :git => "[email protected]:cere-io/widget-ios.git", :tag => s.version }

s.source_files = "CerebellumWidget/**/*.swift"

Expand Down
22 changes: 19 additions & 3 deletions CerebellumWidget.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
B5259954221ABF1C001D90EE /* GetReferralsInfoWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5259953221ABF1C001D90EE /* GetReferralsInfoWrapper.swift */; };
B5259956221AC311001D90EE /* ShowNativeMessageWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5259955221AC311001D90EE /* ShowNativeMessageWrapper.swift */; };
B551C676223EECEE0099CBAA /* CerebellumWidgetProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = B551C675223EECEE0099CBAA /* CerebellumWidgetProtocol.swift */; };
B597635E2269BF1800685161 /* WKWebView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B597635D2269BF1800685161 /* WKWebView.swift */; };
B59763602269BF8700685161 /* UIApplication.swift in Sources */ = {isa = PBXBuildFile; fileRef = B597635F2269BF8700685161 /* UIApplication.swift */; };
B5C93EEA220F88C0001A9BD5 /* ShareWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5C93EE9220F88C0001A9BD5 /* ShareWrapper.swift */; };
B5C93EEC2211A778001A9BD5 /* ShareWithWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5C93EEB2211A778001A9BD5 /* ShareWithWrapper.swift */; };
B5C93EEF221416CA001A9BD5 /* ClaimedRewardItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5C93EEE221416CA001A9BD5 /* ClaimedRewardItem.swift */; };
Expand Down Expand Up @@ -57,6 +59,8 @@
B5259953221ABF1C001D90EE /* GetReferralsInfoWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetReferralsInfoWrapper.swift; sourceTree = "<group>"; };
B5259955221AC311001D90EE /* ShowNativeMessageWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShowNativeMessageWrapper.swift; sourceTree = "<group>"; };
B551C675223EECEE0099CBAA /* CerebellumWidgetProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CerebellumWidgetProtocol.swift; sourceTree = "<group>"; };
B597635D2269BF1800685161 /* WKWebView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WKWebView.swift; sourceTree = "<group>"; };
B597635F2269BF8700685161 /* UIApplication.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIApplication.swift; sourceTree = "<group>"; };
B5C93EE9220F88C0001A9BD5 /* ShareWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShareWrapper.swift; sourceTree = "<group>"; };
B5C93EEB2211A778001A9BD5 /* ShareWithWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShareWithWrapper.swift; sourceTree = "<group>"; };
B5C93EEE221416CA001A9BD5 /* ClaimedRewardItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClaimedRewardItem.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -116,6 +120,15 @@
name = Frameworks;
sourceTree = "<group>";
};
B597635C2269BEF700685161 /* Extensions */ = {
isa = PBXGroup;
children = (
B597635D2269BF1800685161 /* WKWebView.swift */,
B597635F2269BF8700685161 /* UIApplication.swift */,
);
path = Extensions;
sourceTree = "<group>";
};
B5EB4EB322033530004EA1B5 = {
isa = PBXGroup;
children = (
Expand All @@ -138,6 +151,7 @@
B5EB4EBF22033530004EA1B5 /* CerebellumWidget */ = {
isa = PBXGroup;
children = (
B597635C2269BEF700685161 /* Extensions */,
B5EB4F31220870CD004EA1B5 /* Handlers */,
B5EB4F1122045225004EA1B5 /* Services */,
B5EB4F0822044C91004EA1B5 /* JSWrappers */,
Expand Down Expand Up @@ -273,7 +287,7 @@
attributes = {
LastSwiftUpdateCheck = 1010;
LastUpgradeCheck = 1010;
ORGANIZATIONNAME = "Funler LLC";
ORGANIZATIONNAME = "Cerebellum Network, Inc.";
TargetAttributes = {
B5EB4EBC22033530004EA1B5 = {
CreatedOnToolsVersion = 10.1;
Expand Down Expand Up @@ -395,6 +409,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
B59763602269BF8700685161 /* UIApplication.swift in Sources */,
B5EB4F3922088533004EA1B5 /* OnSignInHandlerMapper.swift in Sources */,
B5C93EEA220F88C0001A9BD5 /* ShareWrapper.swift in Sources */,
B5EB4F0E22044D55004EA1B5 /* JSProtocolWithResponse.swift in Sources */,
Expand All @@ -416,6 +431,7 @@
B5EB4F33220870E3004EA1B5 /* OnHideHandlerMapper.swift in Sources */,
B5EB4F4022089618004EA1B5 /* OnGetUserByEmailHandlerMapper.swift in Sources */,
B5EB4F1322045232004EA1B5 /* StorageService.swift in Sources */,
B597635E2269BF1800685161 /* WKWebView.swift in Sources */,
B5EB4F65220AE7BC004EA1B5 /* LogoutWrapper.swift in Sources */,
B5259954221ABF1C001D90EE /* GetReferralsInfoWrapper.swift in Sources */,
B5EB4F25220470C2004EA1B5 /* JSCallbackList.swift in Sources */,
Expand Down Expand Up @@ -582,7 +598,7 @@
"@executable_path/Frameworks",
"@loader_path/Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = io.cere.CerebellumWidget;
PRODUCT_BUNDLE_IDENTIFIER = io.cere.RewardsModule;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
Expand Down Expand Up @@ -611,7 +627,7 @@
"@executable_path/Frameworks",
"@loader_path/Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = io.cere.CerebellumWidget;
PRODUCT_BUNDLE_IDENTIFIER = io.cere.RewardsModule;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
Expand Down
2 changes: 1 addition & 1 deletion CerebellumWidget/CerebellumWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// CerebellumWidget
//
// Created by Konstantin on 1/31/19.
// Copyright © 2019 Funler LLC. All rights reserved.
// Copyright © 2019 Cerebellum Network, Inc. All rights reserved.
//

#import <UIKit/UIKit.h>
Expand Down
72 changes: 56 additions & 16 deletions CerebellumWidget/CerebellumWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
// CerebellumWidget
//
// Created by Konstantin on 1/31/19.
// Copyright © 2019 Funler LLC. All rights reserved.
// Copyright © 2019 Cerebellum Network, Inc. All rights reserved.
//

import Foundation
import UIKit
import WebKit
import WebViewJavascriptBridge
import SwiftyJSON

/// Class that implements CerebellumWidgetProtocol and provides all the functionality of the widget.
///
public class CerebellumWidget: NSObject, CerebellumWidgetProtocol, WKNavigationDelegate {
var webView: WKWebView?;
var bridge: WebViewJavascriptBridge?;
var parentController: UIViewController?;
var _parentController: UIViewController?;
var onInitializationFinishedHandler: OnInitializationFinishedHandler?;
var onGetUserByEmailHandler: OnGetUserByEmailHandler?;
var onSignInHandler: OnSignInHandler?;
Expand All @@ -39,19 +40,15 @@ public class CerebellumWidget: NSObject, CerebellumWidgetProtocol, WKNavigationD
private var heightPercentage: CGFloat = 90;

/// Initializes and prepares the widget for usage.
/// - Parameter parentController: controller that will host the widget view and is responsible for showing/hiding the widget.
/// - Parameter applicationId: identifier of the application from RMS.
/// - Parameter env: Environment for running the widget (`PRODUCTION` is default).
public init(parentController: UIViewController, applicationId: String, env: Environment) {
super.init();

public func initialize(applicationId: String, env: Environment = Environment.PRODUCTION) {
determineCurrentVersion();

if (widgetInitialized) {
return;
}

self.parentController = parentController;
self.appId = applicationId;
self.env = env;

Expand All @@ -63,10 +60,32 @@ public class CerebellumWidget: NSObject, CerebellumWidgetProtocol, WKNavigationD
public func show(placement: String) {
self.queueHandler({() in
self.setView(visible: true);
self.executeJS(method: "__showOnNative", withParams: placement);
_ = self.executeJS(method: "__showOnNative", withParams: placement);
});
}

/// Checks whether widget has items in specified placement. If nothing is specified then it checks if there are items in any placement.
public func hasItems(forPlacement: String) -> Bool {
if (self.widgetInitialized) {
return self.evaluateJS(method: "hasItems", withParams: forPlacement) == "true";
}

return false;
}

/// Returns array of placements that are available for current RMS configuration
public func getPlacements() -> [String] {
if (self.widgetInitialized) {
let list = self.evaluateJS(method: "getPlacements");

if (list != nil) {
return JSON(parseJSON: list!).arrayValue.map { value in value.stringValue };
}
}

return [];
}

/// Hides the widget.
public func hide() {
self.queueHandler({() in
Expand All @@ -77,20 +96,20 @@ public class CerebellumWidget: NSObject, CerebellumWidgetProtocol, WKNavigationD
/// Sets user email.
public func setEmail(email: String) {
self.queueHandler({() in
self.executeJS(method: "sendToField", withParams: "'email', '\(email)'");
_ = self.executeJS(method: "sendToField", withParams: "'email', '\(email)'");
});
}

/// Sets widget to sign-up mode and shows it.
public func showOnboarding() {
self.setMode(mode: WidgetMode.LOGIN);
_ = self.executeJS(method: "showOnboarding");
}

private func setMode(mode: WidgetMode) {
self.mode = mode;

self.queueHandler({() in
self.executeJS(method: "setMode", withParams: "'\(String(describing: self.mode).lowercased())'");
_ = self.executeJS(method: "setMode", withParams: "'\(String(describing: self.mode).lowercased())'");
});
}

Expand All @@ -106,7 +125,7 @@ public class CerebellumWidget: NSObject, CerebellumWidgetProtocol, WKNavigationD
/// }
public func setUserData(data: String) {
self.queueHandler({() in
self.executeJS(method: "setUserData", withParams: data);
_ = self.executeJS(method: "setUserData", withParams: data);
});
}

Expand Down Expand Up @@ -138,14 +157,18 @@ public class CerebellumWidget: NSObject, CerebellumWidgetProtocol, WKNavigationD
/// Logs out of the widget.
public func logout() {
self.queueHandler({() in
self.executeJS(method: "logout");
_ = self.executeJS(method: "logout");
});
}

/// Sets handler that is called when widget is finished initialization.
public func onInitializationFinished(_ handler: @escaping OnInitializationFinishedHandler) -> CerebellumWidget {
self.onInitializationFinishedHandler = handler;

if (self.widgetInitialized) {
handler();
}

return self;
}

Expand Down Expand Up @@ -187,6 +210,17 @@ public class CerebellumWidget: NSObject, CerebellumWidgetProtocol, WKNavigationD
return self;
}

/// Sets parent view controller that will host the widget view and is responsible for showing/hiding the widget. If the property is not set then current top most view controller is used.
public var parentController: UIViewController {
get {
return self._parentController != nil ? self._parentController! : UIApplication.getTopMostViewController()!;
}

set(controller) {
self._parentController = controller;
}
}

func queueHandler(_ handler: @escaping () -> Void) {
if (widgetInitialized) {
handler();
Expand All @@ -202,7 +236,13 @@ public class CerebellumWidget: NSObject, CerebellumWidgetProtocol, WKNavigationD
func executeJS(method: String, withParams: String = "") {
let js = "window.CRBWidget." + method + "(" + withParams + ");";

self.bridge?._evaluateJavascript(js);
self.bridge!._evaluateJavascript(js);
}

func evaluateJS(method: String, withParams: String = "") -> String? {
let js = "window.CRBWidget." + method + "(" + withParams + ");";

return self.webView?.evaluate(script: js);
}

func load() {
Expand All @@ -211,13 +251,13 @@ public class CerebellumWidget: NSObject, CerebellumWidgetProtocol, WKNavigationD
attachBridge();
loadContent();
}

func createViewAndAddAsSubview() {
let configuration = WKWebViewConfiguration();

self.webView = WKWebView(frame: .zero, configuration: configuration);
self.webView!.navigationDelegate = self;
self.parentController!.view.addSubview(self.webView!);
self.parentController.view.addSubview(self.webView!);
}

func initWidgetSize() {
Expand Down
8 changes: 7 additions & 1 deletion CerebellumWidget/CerebellumWidgetProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// CerebellumWidget
//
// Created by Konstantin on 3/18/19.
// Copyright © 2019 Funler LLC. All rights reserved.
// Copyright © 2019 Cerebellum Network, Inc. All rights reserved.
//

import Foundation
Expand All @@ -13,6 +13,12 @@ public protocol CerebellumWidgetProtocol {

/// This method should show the widget for specified placement.
func show(placement: String);

/// This method should check whether widget has items in specified placement.
func hasItems(forPlacement: String) -> Bool;

/// This method should return array of placements that are available for current RMS configuration.
func getPlacements() -> [String];

/// This method should hide the widget.
func hide();
Expand Down
4 changes: 2 additions & 2 deletions CerebellumWidget/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// CerebellumWidget
//
// Created by Konstantin on 2/2/19.
// Copyright © 2019 Funler LLC. All rights reserved.
// Copyright © 2019 Cerebellum Network, Inc. All rights reserved.
//

import Foundation
Expand All @@ -12,7 +12,7 @@ import Foundation
/// `STAGE` and `PRODUCTION` should only be used.
public struct Environment {
/// Configuration property for Local environment.
public static let LOCAL: Environment = Environment("local", "http://192.168.100.2:3002");
public static let LOCAL: Environment = Environment("local", "http://localhost:3002");

/// Configuration property for Dev1 environment.
static let DEV1: Environment = Environment("dev1", "https://widget.dev.cere.io");
Expand Down
26 changes: 26 additions & 0 deletions CerebellumWidget/Extensions/UIApplication.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// UIApplication.swift
// CerebellumWidget
//
// Created by Konstantin on 4/19/19.
// Copyright © 2019 Cerebellum Network, Inc. All rights reserved.
//

import Foundation

extension UIApplication {
class func getTopMostViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return getTopMostViewController(base: nav.visibleViewController)
}
if let tab = base as? UITabBarController {
if let selected = tab.selectedViewController {
return getTopMostViewController(base: selected)
}
}
if let presented = base?.presentedViewController {
return getTopMostViewController(base: presented)
}
return base
}
}
27 changes: 27 additions & 0 deletions CerebellumWidget/Extensions/WKWebView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// WKWebView.swift
// CerebellumWidget
//
// Created by Konstantin on 4/19/19.
// Copyright © 2019 Cerebellum Network, Inc. All rights reserved.
//

import WebKit

extension WKWebView {
func evaluate(script: String) -> String? {
var finished = false;
var result: String?;

evaluateJavaScript("var ____r____ = \(script); JSON.stringify(____r____);") { (r, e) in
result = r as! String?;
finished = true;
}

while !finished {
RunLoop.current.run(mode: .default, before: Date.distantFuture);
}

return result;
}
}
Loading

0 comments on commit 3dc1c83

Please sign in to comment.