214 lines
7.6 KiB
Swift
214 lines
7.6 KiB
Swift
//
|
|
// DeviceListViewController.swift
|
|
// pocloud
|
|
//
|
|
// Created by Patrick McDonagh on 5/23/18.
|
|
// Copyright © 2018 patrickjmcd. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import Alamofire
|
|
import PromiseKit
|
|
import SwiftyJSON
|
|
import RealmSwift
|
|
import SVProgressHUD
|
|
import FirebaseAuth
|
|
|
|
class DeviceListViewController: UITableViewController {
|
|
@IBOutlet weak var searchBar: UISearchBar!
|
|
let realm = try! Realm()
|
|
|
|
let user = (UIApplication.shared.delegate as! AppDelegate).user
|
|
let dataFilePath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("Auth.plist")
|
|
let appAuth = AppAuth()
|
|
|
|
var addresses : Results<Address>?
|
|
var gateways : Results<Gateway>?
|
|
var devices : Results<Device>?
|
|
var deviceTypes : Results<DeviceType>?
|
|
let baseURL = (UIApplication.shared.delegate as! AppDelegate).baseURL
|
|
let deviceTypeFilter = NSPredicate(format: "devices.@count > 0 AND NOT name IN %@", ["gen", "mainHP", "M1"])
|
|
let ignoreDeviceTypes = ["M1", "Gateway"]
|
|
|
|
var selectedDevice: Device?
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
self.refreshControl = UIRefreshControl()
|
|
refreshControl?.attributedTitle = NSAttributedString(string: "Pull to refresh")
|
|
refreshControl?.addTarget(self, action: #selector(refresh), for: UIControlEvents.valueChanged)
|
|
|
|
searchBar.delegate = self
|
|
|
|
|
|
|
|
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Log Out", style: .plain, target: self, action: #selector(self.logOut))
|
|
|
|
SVProgressHUD.show()
|
|
firstly {
|
|
self.loadRealmData()
|
|
}.then{ _ in
|
|
self.loadJSONData()
|
|
}.done { _ in
|
|
self.tableView.reloadData()
|
|
SVProgressHUD.dismiss()
|
|
}.catch { error in
|
|
print("Error in getting data in DeviceListViewController: \(error)")
|
|
}
|
|
}
|
|
|
|
@objc func logOut(){
|
|
appAuth.logOut()
|
|
navigationController?.popToRootViewController(animated: true)
|
|
}
|
|
|
|
func loadJSONData() -> Promise<Void>{
|
|
return Promise { promise in
|
|
firstly {
|
|
getDeviceTypes(baseURL: self.baseURL, authToken: self.user!.authToken)
|
|
}.then { _ in
|
|
getCompanies(baseURL: self.baseURL, authToken: self.user!.authToken)
|
|
}.then { _ in
|
|
getAddresses(baseURL: self.baseURL, authToken: self.user!.authToken)
|
|
}.then { _ in
|
|
getGateways(baseURL: self.baseURL, authToken: self.user!.authToken)
|
|
}.then { _ in
|
|
getDevices(baseURL: self.baseURL, authToken: self.user!.authToken)
|
|
}.done{ _ in
|
|
promise.fulfill(())
|
|
}.catch { error in
|
|
promise.reject(error)
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func getUniqueDeviceTypeNames() -> [String]{
|
|
var deviceTypeNames : [String] = [String]()
|
|
if let devicesList = devices {
|
|
for d in devicesList {
|
|
if let deviceParent = d.parentDeviceType.first {
|
|
deviceTypeNames.append(deviceParent.vanityName)
|
|
}
|
|
}
|
|
}
|
|
|
|
return deviceTypeNames.reduce([], { initialValue, collectionElement in
|
|
initialValue.contains(collectionElement) ? initialValue : initialValue + [collectionElement]
|
|
})
|
|
.filter({ (name) -> Bool in
|
|
!self.ignoreDeviceTypes.contains(name)
|
|
})
|
|
}
|
|
|
|
@objc func refresh() {
|
|
|
|
SVProgressHUD.show()
|
|
self.realm.refresh()
|
|
firstly {
|
|
self.loadJSONData()
|
|
}.done { _ in
|
|
self.tableView.reloadData()
|
|
self.refreshControl?.endRefreshing()
|
|
SVProgressHUD.dismiss()
|
|
}.catch { error in
|
|
print("Error in getting data in DeviceListViewController: \(error)")
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - Table view data source
|
|
|
|
override func numberOfSections(in tableView: UITableView) -> Int {
|
|
return getUniqueDeviceTypeNames().count
|
|
}
|
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
let uniqueDeviceTypes = getUniqueDeviceTypeNames()
|
|
let deviceTypesWithDevices = devices?.filter("ANY parentDeviceType.vanityName == %@", uniqueDeviceTypes[section])
|
|
return deviceTypesWithDevices?.count ?? 0
|
|
}
|
|
|
|
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
|
return getUniqueDeviceTypeNames()[section]
|
|
}
|
|
|
|
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
|
if segue.identifier == "openDeviceDetailView" {
|
|
let destinationVC = segue.destination as! DeviceDetailViewController
|
|
destinationVC.thisDevice = selectedDevice!
|
|
}
|
|
|
|
if segue.identifier == "openMaxWaterSystem" {
|
|
let destinationVC = segue.destination as! MaxWaterSystemViewController
|
|
destinationVC.thisDevice = selectedDevice!
|
|
}
|
|
}
|
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: "deviceListCell", for: indexPath)
|
|
let uniqueDeviceTypes = getUniqueDeviceTypeNames()
|
|
let deviceTypesWithDevices = devices?.filter("ANY parentDeviceType.vanityName == %@", uniqueDeviceTypes[indexPath.section]).sorted(byKeyPath: "vanityName")
|
|
if (deviceTypesWithDevices?.count)! > 0 {
|
|
cell.textLabel?.text = deviceTypesWithDevices?[indexPath.row].vanityName
|
|
cell.accessoryType = .disclosureIndicator
|
|
}
|
|
return cell
|
|
}
|
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
let uniqueDeviceTypes = getUniqueDeviceTypeNames()
|
|
let deviceTypesWithDevices = devices?.filter("ANY parentDeviceType.vanityName == %@", uniqueDeviceTypes[indexPath.section]).sorted(byKeyPath: "vanityName")
|
|
selectedDevice = deviceTypesWithDevices?[indexPath.row]
|
|
switch (selectedDevice?.parentDeviceType.first?.name){
|
|
case "advvfdipp":
|
|
performSegue(withIdentifier: "openMaxWaterSystem", sender: self)
|
|
|
|
default:
|
|
performSegue(withIdentifier: "openDeviceDetailView", sender: self)
|
|
}
|
|
}
|
|
|
|
|
|
//MARK: - Realm Functions
|
|
|
|
func loadRealmData() -> Promise<Void> {
|
|
return Promise { promise in
|
|
devices = realm.objects(Device.self)
|
|
deviceTypes = realm.objects(DeviceType.self)
|
|
gateways = realm.objects(Gateway.self)
|
|
addresses = realm.objects(Address.self)
|
|
promise.fulfill(())
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
extension DeviceListViewController : UISearchBarDelegate {
|
|
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
|
|
devices = devices?.filter("vanityName CONTAINS[cd] %@", searchBar.text!).sorted(byKeyPath: "vanityName", ascending: true)
|
|
tableView.reloadData()
|
|
}
|
|
|
|
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
|
|
if searchBar.text?.count == 0 {
|
|
firstly {
|
|
self.loadRealmData()
|
|
}.then{ _ in
|
|
self.loadJSONData()
|
|
}.done { _ in
|
|
self.tableView.reloadData()
|
|
|
|
DispatchQueue.main.async {
|
|
searchBar.resignFirstResponder()
|
|
}
|
|
}.catch { error in
|
|
print("Error in getting data in DeviceListViewController: \(error)")
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|