Skip to content

Commit

Permalink
Added multiple mode support (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcousillas6 authored Dec 9, 2017
1 parent 2728f47 commit 0f0b9ab
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 39 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ Xniffer.setup(with: configuration)
let sessionManager = Alamofire.SessionManager(configuration: configuration)
```
### UI Modes
The Xniffer currently supports three modes, `.window`, `.console` and `.custom`. The default value is `.window` but this can be changed easily on the `Xniffer.setup(configuration: URLSessionConfiguration, mode: XnifferUI = .window)`.
Each one of this uses a different implementation of the `XnifferDelegate`.
The Xniffer currently supports three modes which could be used at the same time, `.window`, `.console` and `.custom`. The default value is only`.window` but this can be changed easily on the `Xniffer.setup(configuration: URLSessionConfiguration, modes: [XnifferUI] = [.window])`.

Each one of this uses a different implementation of the `XnifferObserver`.

* `.window` : Displays a window on top of the status bar which can be expanded to display a list of the profiled requests. This is the default value.
* `.console` : Prints the results on the Xcode console.
* `.custom` : This one receives a closure of type `() -> ()` so you can use your own implementation of the `XnifferDelegate`.
* `.custom` : This one receives a closure of type `() -> ()` so you can use your own implementation of the `XnifferObserver`.


## Requirements
Expand Down Expand Up @@ -80,7 +81,7 @@ Follow these 3 steps to run Example project: Clone Xniffer repository, open Xnif
To install Xniffer, simply add the following line to your Podfile:

```ruby
pod 'Xniffer', '~> 2.0'
pod 'Xniffer', '~> 3.0'
```

#### Carthage
Expand All @@ -90,7 +91,7 @@ pod 'Xniffer', '~> 2.0'
To install Xniffer, simply add the following line to your Cartfile:

```ogdl
github "xmartlabs/Xniffer" ~> 2.0
github "xmartlabs/Xniffer" ~> 3.0
```

## Author
Expand Down
60 changes: 33 additions & 27 deletions Sources/Core/Xniffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public enum XnifferUI {

}

public protocol XnifferDelegate: class {
public protocol XnifferObserver: class {

func displayResult(for result: XnifferResult)
func queueUpdated(queueCount: Int)
Expand All @@ -80,24 +80,26 @@ public protocol XnifferDelegate: class {

public final class Xniffer {

public var delegate: XnifferDelegate?
public var observers = [XnifferObserver]()
private var requests = [RequestWrapper]()
private static var profilerWindow: XnifferWindow?

public static let shared = Xniffer()

private init () {}

public static func setup(with configuration: URLSessionConfiguration, mode: XnifferUI = .window) {
public static func setup(with configuration: URLSessionConfiguration, modes: [XnifferUI] = [.window]) {
Xniffer.enableInURLSessionConfiguration(configuration: configuration)
switch mode {
case .console:
Xniffer.shared.delegate = ConsoleXniffer()
case .window:
Xniffer.profilerWindow = XnifferWindow()
Xniffer.profilerWindow?.makeKeyAndVisible()
case .custom(let callback):
callback()
modes.forEach {
switch $0 {
case .console:
Xniffer.shared.observers.append(ConsoleXniffer())
case .window:
Xniffer.profilerWindow = XnifferWindow()
Xniffer.profilerWindow?.makeKeyAndVisible()
case .custom(let callback):
callback()
}
}
}

Expand All @@ -110,14 +112,16 @@ public final class Xniffer {
let timeInterval = response.timestamp.uptimeNanoseconds - request.timestamp.uptimeNanoseconds
let latency = Double(timeInterval) / 1_000_000
DispatchQueue.main.async { [weak self] in
self?.delegate?.displayResult(for:
XnifferResult(request: request.request,
response: response.response,
error: nil,
session: response.session,
latency: latency
self?.observers.forEach{
$0.displayResult(for:
XnifferResult(request: request.request,
response: response.response,
error: nil,
session: response.session,
latency: latency
)
)
)
}
}
}

Expand All @@ -126,14 +130,16 @@ public final class Xniffer {
let timeInterval = error.timestamp.uptimeNanoseconds - request.timestamp.uptimeNanoseconds
let latency = Double(timeInterval) / 1_000_000
DispatchQueue.main.sync { [weak self] in
self?.delegate?.displayResult(for:
XnifferResult(request: request.request,
response: error.response,
error: error.error,
session: error.session,
latency: latency
self?.observers.forEach{
$0.displayResult(for:
XnifferResult(request: request.request,
response: error.response,
error: error.error,
session: error.session,
latency: latency
)
)
)
}
}
}

Expand All @@ -144,7 +150,7 @@ public final class Xniffer {
func enqueue(request: RequestWrapper) {
requests.append(request)
DispatchQueue.main.sync { [weak self] in
self?.delegate?.queueUpdated(queueCount: self?.requests.count ?? 0)
self?.observers.forEach{ $0.queueUpdated(queueCount: self?.requests.count ?? 0) }
}
}

Expand All @@ -153,7 +159,7 @@ public final class Xniffer {
let request = requests[index]
requests.remove(at: index)
DispatchQueue.main.sync { [weak self] in
self?.delegate?.queueUpdated(queueCount: self?.requests.count ?? 0)
self?.observers.forEach{ $0.queueUpdated(queueCount: self?.requests.count ?? 0) }
}
return request
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/UI/ConsoleXniffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import Foundation

class ConsoleXniffer: XnifferDelegate {
class ConsoleXniffer: XnifferObserver {

func displayResult(for result: XnifferResult) {
print("======================================================================")
Expand Down
6 changes: 3 additions & 3 deletions Sources/UI/XnifferViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class XnifferViewController: UIViewController {
let labelWidth = 150
let x = Int(UIScreen.main.bounds.width / 2) - labelWidth / 2
let label = UILabel(frame: CGRect(x: x, y: 0, width: labelWidth, height: 44))
label.text = ""
label.text = "No requests yet"
label.textColor = .white
label.textAlignment = .center
return label
Expand Down Expand Up @@ -108,7 +108,7 @@ class XnifferViewController: UIViewController {
}

func setupXniffer() {
Xniffer.shared.delegate = self
Xniffer.shared.observers.append(self)
}

private func setupTable() {
Expand Down Expand Up @@ -187,7 +187,7 @@ extension XnifferViewController: UITableViewDelegate {

}

extension XnifferViewController: XnifferDelegate {
extension XnifferViewController: XnifferObserver {

func displayResult(for result: XnifferResult) {
historic.insert(result, at: 0)
Expand Down
4 changes: 2 additions & 2 deletions Sources/UI/XnifferWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class XnifferWindow: UIWindow, XnifferControllerDelegate {
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 200, height: 20))
button.backgroundColor = .black
button.titleLabel?.font = UIFont.systemFont(ofSize: 12)
button.setTitle("", for: .normal)
button.setTitleColor(.red, for: .normal)
button.setTitle("No requests yet", for: .normal)
button.setTitleColor(.green, for: .normal)
button.addTarget(self, action: #selector(displayResultController), for: .touchUpInside)
button.layer.cornerRadius = 10
return button
Expand Down
2 changes: 1 addition & 1 deletion Xniffer.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "Xniffer"
s.version = "2.0.0"
s.version = "3.0.0"
s.summary = "A swift network profiler built on top URLSession."
s.homepage = "https://github.com/xmartlabs/Xniffer"
s.license = { type: 'MIT', file: 'LICENSE' }
Expand Down

0 comments on commit 0f0b9ab

Please sign in to comment.