Skip to content

Commit

Permalink
ADD: Enable/Disable & Clear data functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
kasketis committed Nov 26, 2015
1 parent 63399c4 commit 55e039b
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 41 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Very useful and handy for network related issues and bugs

Implemented in Swift 2.1 - bridged also for Objective-C

Current version: 1.2

Feel free to contribute :)

#### Overview
Expand Down Expand Up @@ -57,7 +59,10 @@ Call
<pre>
NFX.sharedInstance().stop()
</pre>
to stop netfox and clear all saved data
to stop netfox and clear all saved data.
If you stop netfox it's view will not be displayed until you call start method again.

If you want to just enable/disable logging functionality please use the switch button in the settings view

#### Custom gestures

Expand Down
4 changes: 2 additions & 2 deletions netfox.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "netfox"
s.version = "1.1.2"
s.version = "1.2"
s.summary = "A lightweight, one line setup, iOS network debugging library!"

s.description = <<-DESC
Expand All @@ -11,7 +11,7 @@ DESC
s.screenshots = "https://raw.githubusercontent.com/kasketis/netfox/master/assets/overview0_2.gif"
s.license = 'MIT'
s.author = "Christos Kasketis"
s.source = { :git => "https://github.com/kasketis/netfox.git", :tag => '1.1.2' }
s.source = { :git => "https://github.com/kasketis/netfox.git", :tag => '1.2' }

s.platform = :ios, '8.0'
s.requires_arc = true
Expand Down
40 changes: 34 additions & 6 deletions netfox/NFX.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation
import UIKit

let nfxVersion = "1.1.2"
let nfxVersion = "1.2"

@objc
public class NFX: NSObject
Expand Down Expand Up @@ -45,30 +45,58 @@ public class NFX: NSObject

private var started: Bool = false
private var presented: Bool = false
private var enabled: Bool = false
private var selectedGesture: ENFXGesture = .shake
private var ignoredURLs = [String]()
private var filters = [Bool]()

@objc public func start()
{
self.started = true
NSURLProtocol.registerClass(NFXProtocol)
showMessage("Started!")
enable()
clearOldData()
showMessage("Started!")
}

@objc public func stop()
{
disable()
clearOldData()
showMessage("Stopped!")
NSURLProtocol.unregisterClass(NFXProtocol)
self.started = false
showMessage("Stopped!")
}

private func showMessage(msg: String) {
print("netfox \(nfxVersion) - [https://github.com/kasketis/netfox]: \(msg)")
}

internal func isEnabled() -> Bool
{
return self.enabled
}

internal func enable()
{
self.enabled = true
register()
}

internal func disable()
{
self.enabled = false
unregister()
}

private func register()
{
NSURLProtocol.registerClass(NFXProtocol)
}

private func unregister()
{
NSURLProtocol.unregisterClass(NFXProtocol)
}

func motionDetected()
{
if self.started {
Expand Down Expand Up @@ -145,7 +173,7 @@ public class NFX: NSObject
})
}

private func clearOldData()
internal func clearOldData()
{
NFXHTTPModelManager.sharedInstance.clear()
do {
Expand Down
1 change: 1 addition & 0 deletions netfox/NFXDetailsController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class NFXDetailsController: NFXGenericController, MFMailComposeViewControllerDel
var actionSheet: UIActionSheet
actionSheet = UIActionSheet()
actionSheet.delegate = self
actionSheet.title = "Share"
actionSheet.addButtonWithTitle("Cancel")
actionSheet.addButtonWithTitle("Simple log")
actionSheet.addButtonWithTitle("Full log")
Expand Down
2 changes: 0 additions & 2 deletions netfox/NFXHTTPModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import Foundation

class NFXHTTPModel: NSObject
{


var requestURL: String?
var requestMethod: String?
var requestCachePolicy: String?
Expand Down
162 changes: 132 additions & 30 deletions netfox/NFXSettingsController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation
import UIKit

class NFXSettingsController: NFXGenericController, UITableViewDelegate, UITableViewDataSource
class NFXSettingsController: NFXGenericController, UITableViewDelegate, UITableViewDataSource, UIActionSheetDelegate
{
// MARK: Properties

Expand All @@ -34,21 +34,15 @@ class NFXSettingsController: NFXGenericController, UITableViewDelegate, UITableV
self.extendedLayoutIncludesOpaqueBars = false
self.automaticallyAdjustsScrollViewInsets = false

var filtersInfoLabel: UILabel
filtersInfoLabel = UILabel(frame: CGRectMake(10, 20, CGRectGetWidth(self.view.frame) - 2*10, 30))
filtersInfoLabel.font = UIFont.NFXFont(12)
filtersInfoLabel.textColor = UIColor.NFXGray44Color()
filtersInfoLabel.textAlignment = .Center
filtersInfoLabel.text = "Select the types of responses that you want to see"
filtersInfoLabel.numberOfLines = 0
self.view.addSubview(filtersInfoLabel)

self.tableView.frame = CGRectMake(0, 60, CGRectGetWidth(self.view.frame), CGFloat(self.tableData.count * 33))
self.tableView.autoresizingMask = [.FlexibleWidth]
self.tableView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) - 60)
self.tableView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
self.tableView.translatesAutoresizingMaskIntoConstraints = true
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.scrollEnabled = false
self.tableView.alwaysBounceVertical = false
self.tableView.backgroundColor = UIColor.clearColor()
self.tableView.tableFooterView = UIView(frame: CGRectZero)
self.tableView.tableFooterView?.hidden = true
self.view.addSubview(self.tableView)

self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: NSStringFromClass(UITableViewCell))
Expand Down Expand Up @@ -91,25 +85,48 @@ class NFXSettingsController: NFXGenericController, UITableViewDelegate, UITableV

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return self.tableData.count
switch section {
case 0: return 1
case 1: return self.tableData.count
case 2: return 1
default: return 0
}
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier( NSStringFromClass(UITableViewCell), forIndexPath: indexPath)

let shortType = tableData[indexPath.row]
cell.textLabel?.text = shortType.rawValue
let cell = tableView.dequeueReusableCellWithIdentifier(NSStringFromClass(UITableViewCell), forIndexPath: indexPath)
cell.textLabel?.font = UIFont.NFXFont(14)
cell.tintColor = UIColor.NFXOrangeColor()
configureCell(cell, indexPath: indexPath)

switch indexPath.section
{
case 0:
cell.textLabel?.text = "Logging"
let nfxEnabledSwitch: UISwitch
nfxEnabledSwitch = UISwitch()
nfxEnabledSwitch.setOn(NFX.sharedInstance().isEnabled(), animated: false)
nfxEnabledSwitch.addTarget(self, action: Selector("nfxEnabledSwitchValueChanged:"), forControlEvents: .ValueChanged)
cell.accessoryView = nfxEnabledSwitch
return cell

case 1:
let shortType = tableData[indexPath.row]
cell.textLabel?.text = shortType.rawValue
configureCell(cell, indexPath: indexPath)
return cell

case 2:
cell.textLabel?.textAlignment = .Center
cell.textLabel?.text = "Clear data"
cell.textLabel?.textColor = UIColor.NFXRedColor()
return cell


default: return UITableViewCell()

}

return cell
}

func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView?
{
return UIView.init(frame: CGRectZero)
}

func reloadTableData()
Expand All @@ -122,20 +139,76 @@ class NFXSettingsController: NFXGenericController, UITableViewDelegate, UITableV

func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return 1
return 3
}

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
let headerView = UIView()
headerView.backgroundColor = UIColor.NFXGray95Color()

switch section {
case 1:

var filtersInfoLabel: UILabel
filtersInfoLabel = UILabel(frame: headerView.bounds)
filtersInfoLabel.backgroundColor = UIColor.clearColor()
filtersInfoLabel.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
filtersInfoLabel.font = UIFont.NFXFont(13)
filtersInfoLabel.textColor = UIColor.NFXGray44Color()
filtersInfoLabel.textAlignment = .Center
filtersInfoLabel.text = "\nSelect the types of responses that you want to see"
filtersInfoLabel.numberOfLines = 2
headerView.addSubview(filtersInfoLabel)


default: break
}

return headerView

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
let cell = tableView.cellForRowAtIndexPath(indexPath)
self.filters[indexPath.row] = !self.filters[indexPath.row]
configureCell(cell, indexPath: indexPath)
switch indexPath.section
{
case 1:
let cell = tableView.cellForRowAtIndexPath(indexPath)
self.filters[indexPath.row] = !self.filters[indexPath.row]
configureCell(cell, indexPath: indexPath)
break

case 2:
clearDataButtonPressed()
break

default: break
}

tableView.deselectRowAtIndexPath(indexPath, animated: true)


}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return 33
switch indexPath.section {
case 0: return 44
case 1: return 33
case 2: return 44
default: return 0
}
}

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
{
switch section {
case 0: return 40
case 1: return 60
case 2: return 50
default: return 0
}
}

func configureCell(cell: UITableViewCell?, indexPath: NSIndexPath)
Expand All @@ -150,5 +223,34 @@ class NFXSettingsController: NFXGenericController, UITableViewDelegate, UITableV

}

func nfxEnabledSwitchValueChanged(sender: UISwitch)
{
if sender.on {
NFX.sharedInstance().enable()
} else {
NFX.sharedInstance().disable()
}
}

func clearDataButtonPressed()
{
var actionSheet: UIActionSheet
actionSheet = UIActionSheet()
actionSheet.delegate = self
actionSheet.title = "Clear data?"
actionSheet.addButtonWithTitle("Cancel")
actionSheet.addButtonWithTitle("Yes")
actionSheet.addButtonWithTitle("No")
actionSheet.cancelButtonIndex = 0
actionSheet.showInView(self.view)
}

func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int)
{
if buttonIndex == 1 {
NFX.sharedInstance().clearOldData()
}
}


}

0 comments on commit 55e039b

Please sign in to comment.