summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/DisconnectSplitButton.swift
blob: 4a7f18a3692849fd1ab508643ef114217841a50d (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
//
//  DisconnectSplitButton.swift
//  MullvadVPN
//
//  Created by pronebird on 29/07/2020.
//  Copyright © 2020 Mullvad VPN AB. All rights reserved.
//

import Foundation
import UIKit

class DisconnectSplitButton: UIView {

    private var secondaryButtonSize: CGSize {
        // TODO: make it less hardcoded
        switch self.traitCollection.userInterfaceIdiom {
        case .phone:
            return CGSize(width: 42, height: 42)
        case .pad:
            return CGSize(width: 52, height: 52)
        default:
            return .zero
        }
    }

    let primaryButton = AppButton(style: .translucentDangerSplitLeft)
    let secondaryButton = AppButton(style: .translucentDangerSplitRight)

    private let secondaryButtonWidthConstraint: NSLayoutConstraint
    private let secondaryButtonHeightConstraint: NSLayoutConstraint

    private let stackView: UIStackView

    init() {
        let primaryButtonBlurView = TranslucentButtonBlurView(button: primaryButton)
        let secondaryButtonBlurView = TranslucentButtonBlurView(button: secondaryButton)

        stackView = UIStackView(arrangedSubviews: [primaryButtonBlurView, secondaryButtonBlurView])
        stackView.translatesAutoresizingMaskIntoConstraints = false
        stackView.axis = .horizontal
        stackView.distribution = .fill
        stackView.alignment = .fill
        stackView.spacing = 1

        secondaryButton.setImage(UIImage(named: "IconReload")?.imageFlippedForRightToLeftLayoutDirection(), for: .normal)

        primaryButton.overrideContentEdgeInsets = true
        secondaryButtonWidthConstraint = secondaryButton.widthAnchor.constraint(equalToConstant: 0)
        secondaryButtonHeightConstraint = secondaryButton.heightAnchor.constraint(equalToConstant: 0)

        super.init(frame: .zero)

        addSubview(stackView)

        NSLayoutConstraint.activate([
            stackView.leadingAnchor.constraint(equalTo: leadingAnchor),
            stackView.trailingAnchor.constraint(equalTo: trailingAnchor),
            stackView.topAnchor.constraint(equalTo: topAnchor),
            stackView.bottomAnchor.constraint(equalTo: bottomAnchor),

            secondaryButtonWidthConstraint,
            secondaryButtonHeightConstraint
        ])

        updateTraitConstraints()
    }

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

    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)

        if traitCollection.userInterfaceIdiom != previousTraitCollection?.userInterfaceIdiom {
            updateTraitConstraints()
        }
    }

    private func updateTraitConstraints() {
        let newSize = self.secondaryButtonSize
        secondaryButtonWidthConstraint.constant = newSize.width
        secondaryButtonHeightConstraint.constant = newSize.height
        adjustTitleLabelPosition()
    }

    private func adjustTitleLabelPosition() {
        var contentInsets = primaryButton.defaultContentInsets

        let offset = stackView.spacing + self.secondaryButtonSize.width

        if case .leftToRight = effectiveUserInterfaceLayoutDirection {
            contentInsets.left = offset
            contentInsets.right = 0
        } else {
            contentInsets.left = 0
            contentInsets.right = offset
        }

        primaryButton.contentEdgeInsets = contentInsets
    }
}