Subversion Repositories Mobile Apps.GyroMouse

Rev

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

Rev Author Line No. Line
1 pmbaty 1
//
2
//  ServerDiscoveryTableViewController.swift
3
//  GyroMouse
4
//
5
//  Created by Matteo Riva on 07/08/15.
6
//  Copyright © 2015 Matteo Riva. All rights reserved.
7
//
8
 
9
import UIKit
10
 
11
class ServerDiscoveryTableViewController: UITableViewController {
12
 
13
    let client = (UIApplication.shared.delegate as! AppDelegate).client
14
 
15
    private lazy var loadingView: UIView = {
16
        let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
7 pmbaty 17
        view.center = CGPoint(x: UIScreen.main.bounds.size.width / 2, y: UIScreen.main.bounds.size.height / 2)
1 pmbaty 18
        view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.8) //black
19
        view.layer.cornerRadius = 15
20
 
21
        let spinner = UIActivityIndicatorView(style: .whiteLarge)
22
        spinner.center = CGPoint(x: 50, y: 50)
23
        spinner.color = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 0.5) //gray
24
        spinner.startAnimating()
25
        view.addSubview(spinner)
26
 
27
        return view
28
    }()
29
 
30
    override func viewDidLoad() {
31
        super.viewDidLoad()
32
 
33
        let center = NotificationCenter.default
7 pmbaty 34
        center.addObserver(forName: GyroMouseShouldRefreshServerListNotification, object: client, queue: OperationQueue.main) {[unowned self] (_) -> Void in
1 pmbaty 35
                self.tableView.reloadData()
36
        }
37
 
7 pmbaty 38
        center.addObserver(forName: GyroMouseConnectionSuccessNotification, object: client, queue: OperationQueue.main) {[unowned self] (_) -> Void in
1 pmbaty 39
            self.performSegue(withIdentifier: "mouse", sender: self)
40
            self.loadingView.removeFromSuperview()
41
        }
42
 
7 pmbaty 43
        center.addObserver(forName: GyroMouseConnectionFailedNotification, object: client, queue: OperationQueue.main) {[unowned self] (_) -> Void in
1 pmbaty 44
            self.loadingView.removeFromSuperview()
7 pmbaty 45
 
46
            let alert = UIAlertController(title: "error".localized, message: "connect_error".localized, preferredStyle: .alert)
47
            let action = UIAlertAction(title: "Ok", style: .default, handler: nil)
48
            alert.addAction(action)
49
            self.present(alert, animated: true, completion: nil)
1 pmbaty 50
        }
51
 
7 pmbaty 52
        center.addObserver(forName: GyroMouseGoingBackgroundNotification, object: nil, queue: OperationQueue.main) {[unowned self] (_) -> Void in
1 pmbaty 53
            self.tableView.reloadData()
54
        }
55
 
56
    }
57
 
58
    override func viewDidAppear(_ animated: Bool) {
59
        super.viewDidAppear(animated)
60
    }
61
 
62
    deinit {
7 pmbaty 63
         NotificationCenter.default.removeObserver(self)
1 pmbaty 64
    }
65
 
66
    // MARK: - Table view data source
67
 
68
    override func numberOfSections(in tableView: UITableView) -> Int {
69
        return 1
70
    }
71
 
72
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
73
        return client.services.count
74
    }
75
 
76
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
77
        let cell = tableView.dequeueReusableCell(withIdentifier: "server", for: indexPath)
78
        if ((indexPath.row >= 0) && (indexPath.row < client.services.count)) {
79
            cell.textLabel?.text = client.services[indexPath.row].name
80
        } else {
81
            cell.textLabel?.text = "Unknown server";
82
        }
83
        return cell
84
    }
85
 
86
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
87
        client.connectToLocalService(client.services[indexPath.row])
88
        tableView.deselectRow(at: indexPath, animated: true)
89
        navigationController!.view.addSubview(loadingView)
90
    }
91
 
92
}