Skip to content

Commit

Permalink
logging + cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
maxvol committed Sep 28, 2018
1 parent 690ad4a commit a0c89b5
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 128 deletions.
6 changes: 4 additions & 2 deletions RxVision.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
94DF277A215D69AC00051F68 /* RxVNRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94DF2779215D69AC00051F68 /* RxVNRequest.swift */; };
94DF277C215D6E9900051F68 /* VNCoreMLRequest+Rx.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94DF277B215D6E9900051F68 /* VNCoreMLRequest+Rx.swift */; };
94DF277E215D6F1400051F68 /* RxVNCoreMLRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94DF277D215D6F1400051F68 /* RxVNCoreMLRequest.swift */; };
94DF2792215E973600051F68 /* Log.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94DF2791215E973600051F68 /* Log.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -45,7 +46,6 @@
9482DE2D2045A42900A809BC /* Cartfile */ = {isa = PBXFileReference; lastKnownFileType = text; path = Cartfile; sourceTree = "<group>"; };
9482DE2F2045A47800A809BC /* RxSwift.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = RxSwift.framework; path = ../../Carthage/Build/iOS/RxSwift.framework; sourceTree = "<group>"; };
9482DE332045A5B300A809BC /* VNImageRequestHandler+Rx.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "VNImageRequestHandler+Rx.swift"; sourceTree = "<group>"; };
9482DE372045AA9100A809BC /* stuff.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = stuff.swift; sourceTree = "<group>"; };
94DF276F215D580300051F68 /* VNRequest+Rx.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "VNRequest+Rx.swift"; sourceTree = "<group>"; };
94DF2771215D65D700051F68 /* RequestCompletion.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RequestCompletion.swift; sourceTree = "<group>"; };
94DF2773215D65E600051F68 /* RxVNRequestCompletionHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RxVNRequestCompletionHandler.swift; sourceTree = "<group>"; };
Expand All @@ -54,6 +54,7 @@
94DF2779215D69AC00051F68 /* RxVNRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RxVNRequest.swift; sourceTree = "<group>"; };
94DF277B215D6E9900051F68 /* VNCoreMLRequest+Rx.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "VNCoreMLRequest+Rx.swift"; sourceTree = "<group>"; };
94DF277D215D6F1400051F68 /* RxVNCoreMLRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RxVNCoreMLRequest.swift; sourceTree = "<group>"; };
94DF2791215E973600051F68 /* Log.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Log.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -111,9 +112,9 @@
94DF2775215D67DA00051F68 /* RxVNDetectTextRectanglesRequest.swift */,
94DF2777215D681700051F68 /* VNDetectTextRectanglesRequest+Rx.swift */,
9482DE332045A5B300A809BC /* VNImageRequestHandler+Rx.swift */,
94DF2791215E973600051F68 /* Log.swift */,
9482DE132045A33B00A809BC /* RxVision.h */,
9482DE142045A33B00A809BC /* Info.plist */,
9482DE372045AA9100A809BC /* stuff.swift */,
);
path = RxVision;
sourceTree = "<group>";
Expand Down Expand Up @@ -282,6 +283,7 @@
files = (
94DF2774215D65E600051F68 /* RxVNRequestCompletionHandler.swift in Sources */,
9482DE342045A5B300A809BC /* VNImageRequestHandler+Rx.swift in Sources */,
94DF2792215E973600051F68 /* Log.swift in Sources */,
94DF2772215D65D700051F68 /* RequestCompletion.swift in Sources */,
94DF2778215D681700051F68 /* VNDetectTextRectanglesRequest+Rx.swift in Sources */,
94DF277E215D6F1400051F68 /* RxVNCoreMLRequest.swift in Sources */,
Expand Down
15 changes: 15 additions & 0 deletions RxVision/Log.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// Log.swift
// RxVision
//
// Created by Maxim Volgin on 28/09/2018.
// Copyright © 2018 Maxim Volgin. All rights reserved.
//

import os.log

struct Log {
fileprivate static let subsystem: String = Bundle.main.bundleIdentifier ?? ""

static let vn = OSLog(subsystem: subsystem, category: "vn")
}
16 changes: 4 additions & 12 deletions RxVision/RxVNCoreMLRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,11 @@ import RxSwift

public final class RxVNCoreMLRequest<T>: RxVNRequest<T> {

private lazy var _request: VNCoreMLRequest = VNCoreMLRequest(model: self.model, completionHandler: self.handler?.requestCompletionHandler)
private let model: VNCoreMLModel

public init(model: VNCoreMLModel, imageCropAndScaleOption: VNImageCropAndScaleOption) {
self.model = model
super.init()
_request.imageCropAndScaleOption = imageCropAndScaleOption
}

override public var request: VNRequest {
get {
return _request
}
let handler = RxVNRequestCompletionHandler<T>()
let request: VNCoreMLRequest = VNCoreMLRequest(model: model, completionHandler: handler.requestCompletionHandler)
request.imageCropAndScaleOption = imageCropAndScaleOption
super.init(request: request, handler: handler)
}

}
14 changes: 4 additions & 10 deletions RxVision/RxVNDetectTextRectanglesRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,11 @@ import RxSwift

public final class RxVNDetectTextRectanglesRequest<T>: RxVNRequest<T> {

private lazy var _request: VNDetectTextRectanglesRequest = VNDetectTextRectanglesRequest(completionHandler: self.handler?.requestCompletionHandler)

public init(reportCharacterBoxes: Bool) {
super.init()
_request.reportCharacterBoxes = reportCharacterBoxes
}

override public var request: VNRequest {
get {
return _request
}
let handler = RxVNRequestCompletionHandler<T>()
let request: VNDetectTextRectanglesRequest = VNDetectTextRectanglesRequest(completionHandler: handler.requestCompletionHandler)
request.reportCharacterBoxes = reportCharacterBoxes
super.init(request: request, handler: handler)
}

}
33 changes: 14 additions & 19 deletions RxVision/RxVNRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,27 @@

import Vision
import RxSwift
import os.log

public class RxVNRequest<T> {

typealias Observer = RxVNRequestCompletionHandler<T>.Observer

var handler: RxVNRequestCompletionHandler<T>?

public var value: T? {
get {
return self.handler?.value
}
set {
self.handler?.value = newValue
}
}
public lazy var observable: Observable<RequestCompletion<T>> = Observable.create { (observer) in
self.handler = RxVNRequestCompletionHandler<T>(observer: observer)
self.handler.observer = observer
os_log("RxVNRequest.observable %@", log: Log.vn, type: .debug, "\(self.handler)")
return Disposables.create()
}
public var request: VNRequest { get {
preconditionFailure("This method must be overridden")
}
}

init() {

public var value: T? = nil
public let request: VNRequest
private let handler: RxVNRequestCompletionHandler<T>

init(request: VNRequest, handler: RxVNRequestCompletionHandler<T>) {
self.request = request
self.handler = handler
}

func requestCompletionHandler(request: VNRequest, error: Error?) {
preconditionFailure("This method must be overridden")
}

}
9 changes: 5 additions & 4 deletions RxVision/RxVNRequestCompletionHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@

import Vision
import RxSwift
import os.log

final class RxVNRequestCompletionHandler<T> {

typealias Observer = AnyObserver<RequestCompletion<T>>

private let observer: Observer
var observer: Observer? = nil

public var value: T? = nil
public lazy var requestCompletionHandler: VNRequestCompletionHandler = { (request: VNRequest, error: Error?) in
self.observer.onNext(RequestCompletion(self.value, request, error))
os_log("RxVNRequestCompletionHandler.requestCompletionHandler %@ %@", log: Log.vn, type: .debug, request, "\(error)")
self.observer?.onNext(RequestCompletion(self.value, request, error))
}

init(observer: Observer) {
self.observer = observer
init() {
}

}
7 changes: 5 additions & 2 deletions RxVision/VNImageRequestHandler+Rx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
// Copyright © 2018 Maxim Volgin. All rights reserved.
//

import os.log
import Vision
import RxSwift

extension Reactive where Base: VNImageRequestHandler {

public func perform<T>(_ requests: [RxVNRequest<T>], with value: T? = nil) throws {
requests.forEach { (request) in
let _requests = requests.map { request -> VNRequest in
request.value = value
return request.request
}
try self.base.perform(requests.map { $0.request })
os_log("VNImageRequestHandler.rx.perform %@ %@", log: Log.vn, type: .debug, "\(value)", _requests)
try self.base.perform(_requests)
}

// deprecated
Expand Down
79 changes: 0 additions & 79 deletions RxVision/stuff.swift

This file was deleted.

0 comments on commit a0c89b5

Please sign in to comment.