Skip to content

Commit

Permalink
General refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamajabu committed Feb 19, 2019
1 parent d350aca commit 15468ad
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 66 deletions.
46 changes: 13 additions & 33 deletions mac/Bagel/Components/Packets/PacketsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class PacketsViewController: BaseViewController {
self.filterTextField.delegate = self

self.viewModel?.onChange = { [weak self] in

self?.refresh()
}

Expand All @@ -44,56 +43,44 @@ class PacketsViewController: BaseViewController {


func refresh() {

let isScrolledToBottom = self.isScrolledToBottom()

self.tableView.reloadData()

if let selectedItemIndex = self.viewModel?.selectedItemIndex {

self.tableView.selectRowIndexes(IndexSet(integer: selectedItemIndex), byExtendingSelection: false)
}

if isScrolledToBottom {

if isScrolledToBottom() {
self.scrollToBottom()
}
}


func setupTableViewHeaders() {

for tableColumn in self.tableView.tableColumns {

if tableColumn.identifier.rawValue == "statusCode" {
switch tableColumn.identifier.rawValue {
case "statusCode":
tableColumn.headerCell = FlatTableHeaderCell(textCell: "Status")
tableColumn.width = PacketsViewController.statusColumnWidth

}else if tableColumn.identifier.rawValue == "method" {

case "method":
tableColumn.headerCell = FlatTableHeaderCell(textCell: "Method")
tableColumn.width = PacketsViewController.methodColumnWidth

}else if tableColumn.identifier.rawValue == "url" {

case "url":
tableColumn.headerCell = FlatTableHeaderCell(textCell: "URL")
tableColumn.width = self.view.frame.size.width - PacketsViewController.statusColumnWidth - PacketsViewController.dateColumnWidth - PacketsViewController.methodColumnWidth

}else if tableColumn.identifier.rawValue == "date" {

tableColumn.width = self.view.frame.size.width - PacketsViewController.statusColumnWidth - PacketsViewController.dateColumnWidth - PacketsViewController.methodColumnWidth
case "date":
tableColumn.headerCell = FlatTableHeaderCell(textCell: "Date")
tableColumn.width = PacketsViewController.dateColumnWidth
default:
break
}
}
}

@IBAction func clearButtonAction(_ sender: Any) {

self.viewModel?.clearPackets()
}


}

extension PacketsViewController: NSTableViewDelegate, NSTableViewDataSource
Expand Down Expand Up @@ -166,8 +153,7 @@ extension PacketsViewController: NSTableViewDelegate, NSTableViewDataSource
extension PacketsViewController: NSTextFieldDelegate {

func controlTextDidChange(_ obj: Notification) {

self.viewModel?.filterTerm = self.filterTextField.stringValue
viewModel?.filterTerm = filterTextField.stringValue
}

}
Expand All @@ -176,17 +162,11 @@ extension PacketsViewController: NSTextFieldDelegate {
extension PacketsViewController {

func isScrolledToBottom() -> Bool {

if self.tableView.enclosingScrollView?.verticalScroller?.floatValue ?? 0 > 0.9 {

return true
}

return false
return tableView.enclosingScrollView?.verticalScroller?.floatValue ?? 0 > 0.9
}

func scrollToBottom() {

self.tableView.scrollToEndOfDocument(nil)
tableView.scrollToEndOfDocument(nil)
}

}
39 changes: 12 additions & 27 deletions mac/Bagel/Components/Packets/PacketsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
import Cocoa

class PacketsViewModel: BaseListViewModel<BagelPacket> {

var filterTerm = "" {
didSet {
self.refreshItems()
}
}

func register() {

NotificationCenter.default.addObserver(self, selector: #selector(self.refreshItems), name: BagelNotifications.didGetPacket, object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(self.refreshItems), name: BagelNotifications.didUpdatePacket, object: nil)
Expand All @@ -27,46 +26,32 @@ class PacketsViewModel: BaseListViewModel<BagelPacket> {
NotificationCenter.default.addObserver(self, selector: #selector(self.refreshItems), name: BagelNotifications.didSelectDevice, object: nil)
}


var selectedItem: BagelPacket? {

return BagelController.shared.selectedProjectController?.selectedDeviceController?.selectedPacket
}


var selectedItemIndex: Int? {
guard let selectedItem = self.selectedItem else { return nil }

if let selectedItem = self.selectedItem {

return self.items.firstIndex { $0 === selectedItem }
}

return nil
return self.items.firstIndex { $0 === selectedItem }
}

@objc func refreshItems() {

self.filter(items: BagelController.shared.selectedProjectController?.selectedDeviceController?.packets ?? [])
self.onChange?()
items = filter(items: BagelController.shared.selectedProjectController?.selectedDeviceController?.packets ?? [])
onChange?()
}

func filter(items: [BagelPacket]?) {

if let items = items, filterTerm.count > 0 {

self.items = items.filter({ (packet) -> Bool in

return packet.requestInfo?.url?.contains(self.filterTerm) ?? true
})

}else{

self.items = BagelController.shared.selectedProjectController?.selectedDeviceController?.packets ?? []
func filter(items: [BagelPacket]?) -> [BagelPacket] {
guard let items = items, filterTerm.count > 0 else {
return BagelController.shared.selectedProjectController?.selectedDeviceController?.packets ?? []
}

return items.filter({ (packet) -> Bool in
return packet.requestInfo?.url?.contains(self.filterTerm) ?? true
})
}

func clearPackets() {

BagelController.shared.selectedProjectController?.selectedDeviceController?.clear()
self.refreshItems()
}
Expand Down
5 changes: 1 addition & 4 deletions mac/Bagel/Components/Projects/ProjectsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@ class ProjectsViewController: BaseViewController {
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.backgroundColor = ThemeColor.projectListBackgroundColor




self.viewModel?.onChange = { [weak self] in

self?.refresh()
}
}
Expand Down
1 change: 0 additions & 1 deletion mac/Bagel/Workers/BagelController/Models/BagelPacket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import Cocoa

class BagelPacket: Codable {

var packetId: String?

var requestInfo: BagelRequestInfo?
Expand Down
2 changes: 1 addition & 1 deletion mac/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: ebe70cf0430bb9673b9d2acef3c2a16142460de8

COCOAPODS: 1.6.0.beta.2
COCOAPODS: 1.6.0

0 comments on commit 15468ad

Please sign in to comment.