summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/SettingsSwitchCell.swift
blob: a504726dff5da56fb172e4408b7576a48a1a1028 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
//
//  SettingsSwitchCell.swift
//  MullvadVPN
//
//  Created by pronebird on 19/05/2021.
//  Copyright © 2021 Mullvad VPN AB. All rights reserved.
//

import UIKit

class SettingsSwitchCell: SettingsCell {

    private let switchContainer = CustomSwitchContainer()

    var action: ((Bool) -> Void)?

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        accessoryView = switchContainer

        switchContainer.control.addTarget(self, action: #selector(switchValueDidChange), for: .valueChanged)

        isAccessibilityElement = true
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setEnabled(_ isEnabled: Bool) {
        switchContainer.isEnabled = isEnabled
    }

    func setOn(_ isOn: Bool, animated: Bool) {
        switchContainer.control.setOn(isOn, animated: animated)
    }

    override func prepareForReuse() {
        super.prepareForReuse()

        setEnabled(true)
    }

    // MARK: - Actions

    @objc private func switchValueDidChange() {
        action?(self.switchContainer.control.isOn)
    }

    // MARK: -  Accessibility

    override var accessibilityTraits: UIAccessibilityTraits {
        set {
            super.accessibilityTraits = newValue
        }
        get {
            // Use UISwitch traits to make the entire cell behave as "Switch button"
            return switchContainer.control.accessibilityTraits
        }
    }

    override var accessibilityLabel: String? {
        set {
            super.accessibilityLabel = newValue
        }
        get {
            return titleLabel.text
        }
    }

    override var accessibilityValue: String? {
        set {
            super.accessibilityValue = newValue
        }
        get {
            return self.switchContainer.control.accessibilityValue
        }
    }

    override var accessibilityFrame: CGRect {
        set {
            super.accessibilityFrame = newValue
        }
        get {
            return UIAccessibility.convertToScreenCoordinates(self.bounds, in: self)
        }
    }

    override var accessibilityPath: UIBezierPath? {
        set {
            super.accessibilityPath = newValue
        }
        get {
            return UIBezierPath(roundedRect: accessibilityFrame, cornerRadius: 4)
        }
    }

    override func accessibilityActivate() -> Bool {
        guard switchContainer.isEnabled else { return false }

        let newValue = !self.switchContainer.control.isOn

        setOn(newValue, animated: true)
        action?(newValue)

        return true
    }
}