// // DeviceDetailViewController.swift // pocloud // // Created by Patrick McDonagh on 5/24/18. // Copyright © 2018 patrickjmcd. All rights reserved. // import UIKit import Alamofire import PromiseKit import RealmSwift import SwiftyJSON import SVProgressHUD import FirebaseDatabase import ChameleonFramework class DeviceDetailViewController: UITableViewController { let realm = try! Realm() var ref: DatabaseReference! let baseURL = (UIApplication.shared.delegate as! AppDelegate).baseURL let user = (UIApplication.shared.delegate as! AppDelegate).user var thisDevice : Device? var deviceTypes : Results? var selectedChannel : Channel? var channelLookup : [String : Int] = [:] var values : [String : MeshifyValue] = [:] var updatedChannelNames : [String] = [String]() var changedChannelNames : [String] = [String]() let numFormatter = NumberFormatter() override func viewDidLoad() { super.viewDidLoad() self.tableView.estimatedRowHeight = 44 self.tableView.rowHeight = UITableViewAutomaticDimension ref = Database.database().reference() let macAddress = String((thisDevice?.macAddress.replacingOccurrences(of: ":", with: "").uppercased().dropLast(4))!) let deviceTypeName = (thisDevice?.parentDeviceType.first?.name)! numFormatter.minimumFractionDigits = 0 numFormatter.maximumFractionDigits = 3 numFormatter.minimumIntegerDigits = 1 SVProgressHUD.show() firstly { loadData() }.done { _ in self.tableView.reloadData() }.catch{ error in print("Error in loadData: \(error)") } self.navigationItem.title = thisDevice?.vanityName self.refreshControl = UIRefreshControl() refreshControl?.attributedTitle = NSAttributedString(string: "Pull to refresh") refreshControl?.addTarget(self, action: #selector(refresh), for: UIControlEvents.valueChanged) // tableView.addSubview(refreshControl!) firstly { getChannels(deviceTypeId: thisDevice!.deviceTypeId, baseURL: baseURL, authToken: (user?.authToken)!) }.then { _ in getChannelValues(deviceId: self.thisDevice!.id, baseURL: self.baseURL, authToken: (self.user?.authToken)!) }.done { (valueDict) in self.values = valueDict self.tableView.reloadData() SVProgressHUD.dismiss() if let dev = self.thisDevice { for chanIndex in 0.. String { if let doubledValue = Double(value) { if let formattedString = numFormatter.string(from: NSNumber(value: doubledValue)) { return formattedString } else { return value } } else { return value } } // MARK: - Table view data source override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // return (thisDevice?.parentDeviceType.first?.channels.count)! return values.count } // MARK: - Table View Delegate Method override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "deviceDetailCell", for: indexPath) as! DeviceDetailChannelCell cell.channelNameLabel?.text = thisDevice?.parentDeviceType.first?.channels[indexPath.row].subTitle let chanName = thisDevice?.parentDeviceType.first?.channels[indexPath.row].name let chanVal = values[chanName!] if let i = changedChannelNames.index(of: chanName!) { changedChannelNames.remove(at: i) cell.backgroundColor = UIColor.flatLime() DispatchQueue.main.asyncAfter(deadline: .now() + 5) { UIView.animate(withDuration: 1, animations: { cell.backgroundColor = UIColor.white }) } } if let i = updatedChannelNames.index(of: chanName!) { updatedChannelNames.remove(at: i) cell.backgroundColor = UIColor.flatYellow() DispatchQueue.main.asyncAfter(deadline: .now() + 5) { UIView.animate(withDuration: 1, animations: { cell.backgroundColor = UIColor.white }) } } cell.channelValueLabel?.text = tryRound(value: chanVal!.value, places: 3) cell.accessoryType = .disclosureIndicator return cell } //MARK : - Table Cell Selected override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { selectedChannel = thisDevice?.parentDeviceType.first?.channels[indexPath.row] performSegue(withIdentifier: "goToChannelView", sender: self) } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "goToChannelView" { let destinationVC = segue.destination as! ChannelDetailViewController destinationVC.thisDevice = thisDevice! destinationVC.thisChannel = selectedChannel! } } func loadData() -> Promise { return Promise { promise in deviceTypes = realm.objects(DeviceType.self) thisDevice = realm.objects(Device.self).filter("id == %d", thisDevice!.id).first! promise.fulfill(()) } } }