diff --git a/README.md b/README.md index 0457846..912762f 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 -``` diff --git a/RIBsTreeViewerClient.xcodeproj/project.pbxproj b/RIBsTreeViewerClient.xcodeproj/project.pbxproj index 7ecc1fb..a5fd211 100644 --- a/RIBsTreeViewerClient.xcodeproj/project.pbxproj +++ b/RIBsTreeViewerClient.xcodeproj/project.pbxproj @@ -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; @@ -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; diff --git a/RIBsTreeViewerClient/Sources/RIBsTreeViewer.swift b/RIBsTreeViewerClient/Sources/RIBsTreeViewer.swift index 4853f66..9c08574 100644 --- a/RIBsTreeViewerClient/Sources/RIBsTreeViewer.swift +++ b/RIBsTreeViewerClient/Sources/RIBsTreeViewer.swift @@ -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.interval(RxTimeInterval.microseconds(200), scheduler: MainScheduler.instance) + let watchingInterval: Int + if let interval = option?[.monitoringInterval] as? Int { + watchingInterval = interval + } else { + watchingInterval = 1000 + } + + watchingDisposable = Observable.interval(.milliseconds(watchingInterval), scheduler: MainScheduler.instance) .map { [unowned self] _ in self.tree(router: self.router) } @@ -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] {