ChangePasswordViewController.swift 4.51 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
//
//  ChangePasswordViewController.swift
//  Akamu_iOS
//
//  Created by Maurice Kraus on 19.07.20.
//  Copyright © 2020 Akamu e.V. All rights reserved.
//

import AkamuUIKit
import UIKit
11
import Logger
12
13
14
15
16
17
18
19
20
21

class ChangePasswordViewController: UITableViewController {
    var firstPasswordCell: UITableViewCell = UITableViewCell()
    var secondPasswordCell: UITableViewCell = UITableViewCell()
    
    var firstPasswordField: UITextField = UITextField()
    var secondPasswordField: UITextField = UITextField()
    
    override func viewDidLoad() {
        super.viewDidLoad()
22
        logger.event("User visited \(String(describing: ChangePasswordViewController.self))")
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
        tableView = UITableView(frame: tableView.frame, style: .grouped)
    }
    
    override func loadView() {
        super.loadView()
        // construct first name cell, section 0, row 0
        firstPasswordCell.backgroundColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.5)
        firstPasswordField = UITextField(frame: firstPasswordCell.contentView.bounds.insetBy(dx: 15, dy: 0))
        firstPasswordField.placeholder = "New Password"
        firstPasswordField.isSecureTextEntry = true
        firstPasswordCell.addSubview(firstPasswordField)
        
        // construct last name cell, section 0, row 1
        secondPasswordCell.backgroundColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.5)
        secondPasswordField = UITextField(frame: secondPasswordCell.contentView.bounds.insetBy(dx: 15, dy: 0))
        secondPasswordField.placeholder = "Retype New Password"
        secondPasswordField.isSecureTextEntry = true
        secondPasswordCell.addSubview(secondPasswordField)
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        switch indexPath.section {
        case 0:
            switch indexPath.row {
            case 0:
                return firstPasswordCell // section 0, row 0 is the first name
            default:
                fatalError("Unknown row in section 0")
            }
        case 1:
            switch indexPath.row {
            case 0:
                return secondPasswordCell // section 1, row 0 is the share option
            default:
                fatalError("Unknown row in section 1")
            }
        default:
            fatalError("Unknown section")
        }
    }
    
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        if section == 1 {
            return 100
        } else {
            return 0
        }
    }
    
    override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        guard section != 0 else { return nil }
        let footerView = UIView()
        footerView.backgroundColor = .clear
        let button = UIButton()
        button.setTitle("Update Password", for: .normal)
        button.setTitleColor(Colors.akamuBlue, for: .normal)
        button.layer.borderWidth = 1
        button.layer.borderColor = Colors.akamuBlue.cgColor
        button.layer.cornerRadius = 5
        footerView.addSubview(button)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.centerInSuperview()
        button.heightAnchor.constraint(equalToConstant: 50).isActive = true
        button.titleEdgeInsets = .init(top: 0, left: 10, bottom: 0, right: 10)
        button.widthAnchor.constraint(greaterThanOrEqualToConstant: 200).isActive = true
        return footerView
    }
    
    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        switch section {
        case 0:
            return "New Password"
        case 1:
            return "Retype Password"
        default:
            fatalError("Unknown section")
        }
    }
}

#if canImport(SwiftUI) && PREVIEW
import SwiftUI
import Swinject

@available(iOS 13, *)
struct ChangePasswordViewControllerPreview: PreviewProvider {
    static var previews: some View {
        ForEach(Devices.all, id: \.identity) { deviceName in
            NavigationView {
                ChangePasswordViewController().toPreview()
                    .navigationBarTitle(Text("Change Password"))
            }
            .previewDevice(PreviewDevice(rawValue: deviceName)).previewDisplayName(deviceName)
        }
    }
}
#endif