Files
POCloud-iOS/pocloud/Controller/CompanyViewController.swift
2018-06-11 10:21:03 -05:00

94 lines
3.2 KiB
Swift

//
// CompanyViewController.swift
// pocloud
//
// Created by Patrick McDonagh on 6/6/18.
// Copyright © 2018 patrickjmcd. All rights reserved.
//
import UIKit
import RealmSwift
import PromiseKit
class CompanyViewController: UITableViewController {
let realm = try! Realm()
let baseURL = (UIApplication.shared.delegate as! AppDelegate).baseURL
let user = (UIApplication.shared.delegate as! AppDelegate).user
let appAuth = AppAuth()
var companiesWithGateways : Results<Company>?
var companiesNoGateways : Results<Company>?
var selectedCompany : Company?
override func viewDidLoad() {
super.viewDidLoad()
// Get company data from Realm
companiesWithGateways = realm.objects(Company.self).filter("gateways.@count > 0")
companiesNoGateways = realm.objects(Company.self).filter("gateways.@count == 0")
if (companiesWithGateways!.count + companiesNoGateways!.count) == 0{
firstly {
getAllMeshifyData(baseURL: self.baseURL, authToken: self.user!.authToken)
}.done {
self.tableView.reloadData()
}.catch { error in
print("Error getting all meshify data in CompanyViewController: \(error)")
}
}
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return companiesWithGateways?.count ?? 0
} else {
return companiesNoGateways?.count ?? 0
}
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0 {
return "Companies with Devices"
} else {
return "Companies with NO Devices"
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "companyCell", for: indexPath) as! AdminCompanyCell
if indexPath.section == 0 {
cell.companyNameLabel.text = companiesWithGateways![indexPath.row].name
cell.companyCountLabel.text = String(companiesWithGateways![indexPath.row].gateways.count)
} else {
cell.companyNameLabel.text = companiesNoGateways![indexPath.row].name
cell.companyCountLabel.isHidden = true
cell.accessoryType = .none
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.section == 0 {
selectedCompany = companiesWithGateways![indexPath.row]
performSegue(withIdentifier: "openCompanyDetailView", sender: self)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "openCompanyDetailView" {
let targetVC = segue.destination as! CompanyDetailViewController
targetVC.thisCompany = selectedCompany
}
}
}