Subversion Repositories Mobile Apps.GyroMouse

Rev

Go to most recent revision | Details | 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))
17
        view.center = CGPoint(x: ScreenSize.SCREEN_WIDTH / 2, y: ScreenSize.SCREEN_HEIGHT / 2)
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
34
        center.addObserver(forName: ServerDiscoveredServicesDidChangeNotification, object: client, queue: OperationQueue.main) {[unowned self] (_) -> Void in
35
                self.tableView.reloadData()
36
        }
37
 
38
        center.addObserver(forName: ClientDidCompleteLocalConnectionNotification, object: client, queue: OperationQueue.main) {[unowned self] (_) -> Void in
39
            self.performSegue(withIdentifier: "mouse", sender: self)
40
            self.loadingView.removeFromSuperview()
41
        }
42
 
43
        center.addObserver(forName: ClientDidFailLocalConnectionNotification, object: client, queue: OperationQueue.main) {[unowned self] (_) -> Void in
44
            self.loadingView.removeFromSuperview()
45
            if #available(iOS 8.0, *) {
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)
50
            } else {
51
                let alert = UIAlertView(title: "Errore", message: "connect_error".localized, delegate: nil, cancelButtonTitle: "Ok")
52
                alert.show()
53
            }
54
        }
55
 
56
        center.addObserver(forName: DidEnterBackgroundNotification, object: nil, queue: OperationQueue.main) {[unowned self] (_) -> Void in
57
            self.tableView.reloadData()
58
        }
59
 
60
    }
61
 
62
    override func viewDidAppear(_ animated: Bool) {
63
        super.viewDidAppear(animated)
64
    }
65
 
66
    deinit {
67
        let center = NotificationCenter.default
68
        center.removeObserver(self)
69
    }
70
 
71
    // MARK: - Table view data source
72
 
73
    override func numberOfSections(in tableView: UITableView) -> Int {
74
        return 1
75
    }
76
 
77
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
78
        return client.services.count
79
    }
80
 
81
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
82
        let cell = tableView.dequeueReusableCell(withIdentifier: "server", for: indexPath)
83
        if ((indexPath.row >= 0) && (indexPath.row < client.services.count)) {
84
            cell.textLabel?.text = client.services[indexPath.row].name
85
        } else {
86
            cell.textLabel?.text = "Unknown server";
87
        }
88
        return cell
89
    }
90
 
91
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
92
        client.connectToLocalService(client.services[indexPath.row])
93
        tableView.deselectRow(at: indexPath, animated: true)
94
        navigationController!.view.addSubview(loadingView)
95
    }
96
 
97
}