MeViewController.swift 3.84 KB
Newer Older
Guillaume Roguez's avatar
Guillaume Roguez committed
1
/*
2
 *  Copyright (C) 2017 Savoir-faire Linux Inc.
Guillaume Roguez's avatar
Guillaume Roguez committed
3
4
 *
 *  Author: Edric Ladent-Milaret <edric.ladent-milaret@savoirfairelinux.com>
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
5
 *  Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
Guillaume Roguez's avatar
Guillaume Roguez committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */

import UIKit
23
import Reusable
24
import RxSwift
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
25
26
import RxCocoa
import RxDataSources
Guillaume Roguez's avatar
Guillaume Roguez committed
27

28
class MeViewController: EditProfileViewController, StoryboardBased, ViewModelBased {
Edric Milaret's avatar
Edric Milaret committed
29

30
    // MARK: - outlets
Guillaume Roguez's avatar
Guillaume Roguez committed
31
    @IBOutlet weak var nameLabel: UILabel!
32
    @IBOutlet weak var ringIdLabel: UILabel!
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
33
    @IBOutlet weak var settingsTable: UITableView!
Edric Milaret's avatar
Edric Milaret committed
34

35
36
    // MARK: - members
    var viewModel: MeViewModel!
37
    fileprivate let disposeBag = DisposeBag()
38
39

    // MARK: - functions
Guillaume Roguez's avatar
Guillaume Roguez committed
40
41
    override func viewDidLoad() {
        super.viewDidLoad()
42
43
        self.title = L10n.Global.meTabBarTitle
        self.navigationItem.title = L10n.Global.meTabBarTitle
44
        self.setupUI()
Guillaume Roguez's avatar
Guillaume Roguez committed
45
    }
Edric Milaret's avatar
Edric Milaret committed
46

47
    override func setupUI() {
48
        self.viewModel.userName
49
50
            .bind(to: self.nameLabel.rx.text)
            .disposed(by: disposeBag)
Edric Milaret's avatar
Edric Milaret committed
51

52
53
54
        self.viewModel.ringId.asObservable()
            .bind(to: self.ringIdLabel.rx.text)
            .disposed(by: disposeBag)
Edric Milaret's avatar
Edric Milaret committed
55

56
        super.setupUI()
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

        //setup Table
        self.settingsTable.estimatedRowHeight = 50
        self.settingsTable.rowHeight = UITableViewAutomaticDimension
        self.settingsTable.separatorStyle = .none

        //Register cell
        self.setUpDataSource()
        self.settingsTable.register(cellType: DeviceCell.self)
        self.settingsTable.register(cellType: LinkNewDeviceCell.self)
    }

    func setUpDataSource() {

        let settingsItmeDataSource = RxTableViewSectionedReloadDataSource<SettingsSection>()

        let configureCell: (TableViewSectionedDataSource, UITableView, IndexPath, SettingsSection.Item)
            -> UITableViewCell = {
                ( dataSource: TableViewSectionedDataSource<SettingsSection>,
                tableView: UITableView,
                indexPath: IndexPath,
                item: SettingsSection.Item) in
                switch dataSource[indexPath] {

                case .device(let device):
                    let cell = tableView.dequeueReusableCell(for: indexPath, cellType: DeviceCell.self)

                    cell.deviceIdLabel.text = device.deviceId
                    cell.selectionStyle = .none
                    return cell

                case .linkNew:
                    let cell = tableView.dequeueReusableCell(for: indexPath, cellType: LinkNewDeviceCell.self)

                    cell.addDeviceButton.rx.tap.subscribe(onNext: { [unowned self] in
                        self.viewModel.linkDevice()
                    }).disposed(by: cell.disposeBag)
                    cell.selectionStyle = .none
                    return cell
                }
        }

        settingsItmeDataSource.configureCell = configureCell
        self.viewModel.settings
            .bind(to: self.settingsTable.rx.items(dataSource: settingsItmeDataSource))
            .disposed(by: disposeBag)

        //Set header titles
        settingsItmeDataSource.titleForHeaderInSection = { dataSource, index in
            return dataSource.sectionModels[index].header
        }
Guillaume Roguez's avatar
Guillaume Roguez committed
108
109
    }
}