Skip to content

Commit

Permalink
Merge pull request #7 from srea/feature/add-interface-and-options
Browse files Browse the repository at this point in the history
Stop method, interval option
  • Loading branch information
srea committed Dec 20, 2019
2 parents 5d9b1e4 + 8e37a98 commit 2ceb721
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 53 deletions.
51 changes: 12 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,37 +34,24 @@ Carthage CopyFrameworks (ONLY DEBUG)
### Implementation

```swift
// MARK: - RIBsTreeViewer

#if DEBUG
import RIBsTreeViewerClient
#endif

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

#if DEBUG
private var ribsTreeViewer: RIBsTreeViewer?
#endif

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let window = UIWindow(frame: UIScreen.main.bounds)
self.window = window

...
@available(iOS 13.0, *)
var RIBsTreeViewerHolder: RIBsTreeViewer? = nil

launchRouter.launch(from: window)
#if DEBUG
startRIBsTreeViewer(launchRouter: launchRouter)
#endif
return true
}

}

#if DEBUG
extension AppDelegate {
private func startRIBsTreeViewer(launchRouter: Routing) {
ribsTreeViewer = RIBsTreeViewer.init(router: launchRouter)
ribsTreeViewer?.start()
if #available(iOS 13.0, *) {
RIBsTreeViewerHolder = RIBsTreeViewerImpl.init(router: launchRouter,
option: [.webSocketURL: "ws://0.0.0.0:8080",
.monitoringInterval: 1000]])
RIBsTreeViewerHolder?.start()
} else {
DEBUGLOG { "RIBsTreeViewer is not supported OS version." }
}
}
}
#endif
Expand All @@ -84,17 +71,3 @@ $ yarn install
$ npx webpack
$ open ./public/index.html
```

## Options

### .webSocketURL

```swift
#if DEBUG
if #available(iOS 13.0, *) {
ribsTreeViewer = RIBsTreeViewerImpl.init(router: launchRouter,
option: [.webSocketURL: "ws://0.0.0.0:8080"])
ribsTreeViewer?.start()
}
#endif
```
4 changes: 2 additions & 2 deletions RIBsTreeViewerClient.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@
"@executable_path/Frameworks",
"@loader_path/Frameworks",
);
MARKETING_VERSION = 1.0.2;
MARKETING_VERSION = 1.0.3;
PRODUCT_BUNDLE_IDENTIFIER = co.minipro.app.RIBsTreeViewerClient;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
Expand Down Expand Up @@ -352,7 +352,7 @@
"@executable_path/Frameworks",
"@loader_path/Frameworks",
);
MARKETING_VERSION = 1.0.2;
MARKETING_VERSION = 1.0.3;
PRODUCT_BUNDLE_IDENTIFIER = co.minipro.app.RIBsTreeViewerClient;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
Expand Down
44 changes: 32 additions & 12 deletions RIBsTreeViewerClient/Sources/RIBsTreeViewer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,50 @@ import Foundation
import RxSwift
import RIBs

protocol RIBsTreeViewer {
public protocol RIBsTreeViewer {
init(router: Routing, option: [RIBsTreeViewerOption: Any]?)
func start()
func stop()
}

public enum RIBsTreeViewerOptions: String {
public enum RIBsTreeViewerOption {
case webSocketURL
case monitoringInterval
}

@available(iOS 13.0, *)
public class RIBsTreeViewerImpl {
public class RIBsTreeViewerImpl: RIBsTreeViewer {

private let router: Routing
private let webSocket: WebSocketClient
private let disposeBag = DisposeBag()
private var watchingDisposable: Disposable?
private let option: [RIBsTreeViewerOption: Any]?

public init(router: Routing, option: [RIBsTreeViewerOptions: String]? = nil) {
let url = option?[.webSocketURL]
required public init(router: Routing, option: [RIBsTreeViewerOption: Any]?) {
self.option = option
self.router = router

if let url = url {
self.webSocket = WebSocketClient.init(url: URL(string: url)!)
let webSocketURL: String
if let url = option?[.webSocketURL] as? String {
webSocketURL = url
} else {
self.webSocket = WebSocketClient.init(url: URL(string: "ws://0.0.0.0:8080")!)
webSocketURL = "ws://0.0.0.0:8080"
}

self.webSocket = WebSocketClient.init(url: URL(string: webSocketURL)!)
self.webSocket.delegate = self
self.webSocket.connect()
}

public func start() {
Observable<Int>.interval(RxTimeInterval.microseconds(200), scheduler: MainScheduler.instance)
let watchingInterval: Int
if let interval = option?[.monitoringInterval] as? Int {
watchingInterval = interval
} else {
watchingInterval = 1000
}

watchingDisposable = Observable<Int>.interval(.milliseconds(watchingInterval), scheduler: MainScheduler.instance)
.map { [unowned self] _ in
self.tree(router: self.router)
}
Expand All @@ -52,10 +66,16 @@ public class RIBsTreeViewerImpl {
let jsonString = String(bytes: jsonData, encoding: .utf8)!
self?.webSocket.send(text: jsonString)
} catch {
// print(error)
// TODO: Error Handling
}
})
.disposed(by: disposeBag)

}

public func stop() {
watchingDisposable?.dispose()
watchingDisposable = nil
webSocket.disconnect()
}

private func tree(router: Routing, appendImage: Bool = false) -> [String: Any] {
Expand Down

0 comments on commit 2ceb721

Please sign in to comment.