Subversion Repositories Mobile Apps.GyroMouse

Rev

Rev 1 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

//
//  ServerDiscoveryTableViewController.swift
//  GyroMouse
//
//  Created by Matteo Riva on 07/08/15.
//  Copyright © 2015 Matteo Riva. All rights reserved.
//

import UIKit

class ServerDiscoveryTableViewController: UITableViewController {
    
    let client = (UIApplication.shared.delegate as! AppDelegate).client
    
    private lazy var loadingView: UIView = {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
        view.center = CGPoint(x: UIScreen.main.bounds.size.width / 2, y: UIScreen.main.bounds.size.height / 2)
        view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.8) //black
        view.layer.cornerRadius = 15
        
        let spinner = UIActivityIndicatorView(style: .whiteLarge)
        spinner.center = CGPoint(x: 50, y: 50)
        spinner.color = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 0.5) //gray
        spinner.startAnimating()
        view.addSubview(spinner)
        
        return view
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let center = NotificationCenter.default
        center.addObserver(forName: GyroMouseShouldRefreshServerListNotification, object: client, queue: OperationQueue.main) {[unowned self] (_) -> Void in
                self.tableView.reloadData()
        }
        
        center.addObserver(forName: GyroMouseConnectionSuccessNotification, object: client, queue: OperationQueue.main) {[unowned self] (_) -> Void in
            self.performSegue(withIdentifier: "mouse", sender: self)
            self.loadingView.removeFromSuperview()
        }
        
        center.addObserver(forName: GyroMouseConnectionFailedNotification, object: client, queue: OperationQueue.main) {[unowned self] (_) -> Void in
            self.loadingView.removeFromSuperview()

            let alert = UIAlertController(title: "error".localized, message: "connect_error".localized, preferredStyle: .alert)
            let action = UIAlertAction(title: "Ok", style: .default, handler: nil)
            alert.addAction(action)
            self.present(alert, animated: true, completion: nil)
        }
        
        center.addObserver(forName: GyroMouseGoingBackgroundNotification, object: nil, queue: OperationQueue.main) {[unowned self] (_) -> Void in
            self.tableView.reloadData()
        }
        
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }
    
    deinit {
         NotificationCenter.default.removeObserver(self)
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return client.services.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "server", for: indexPath)
        if ((indexPath.row >= 0) && (indexPath.row < client.services.count)) {
            cell.textLabel?.text = client.services[indexPath.row].name
        } else {
            cell.textLabel?.text = "Unknown server";
        }
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        client.connectToLocalService(client.services[indexPath.row])
        tableView.deselectRow(at: indexPath, animated: true)
        navigationController!.view.addSubview(loadingView)
    }

}