diff options
| author | mojganii <mojgan.jelodar@codic.se> | 2024-03-11 16:11:33 +0100 |
|---|---|---|
| committer | Bug Magnet <marco.nikic@mullvad.net> | 2024-03-12 08:31:23 +0100 |
| commit | 8506380b6f7453720faccf692b60ffd8a1bc112e (patch) | |
| tree | d6b462d48943d756e9e29cbce2a2c3dfc5633331 /ios/MullvadVPN/View controllers/SelectLocation/LocationSectionHeaderView.swift | |
| parent | 1c89cd07fb90e23dd52a5e9441a2d144e913a7c3 (diff) | |
| download | mullvadvpn-8506380b6f7453720faccf692b60ffd8a1bc112e.tar.xz mullvadvpn-8506380b6f7453720faccf692b60ffd8a1bc112e.zip | |
adding section header view in select location
- each section should have its own distinct header view.
- custom list section has an action that should take user to add/edit custom list.it will be coming during upcoming changes.
- refactoring location cell
- removing LocationCellFactory
Diffstat (limited to 'ios/MullvadVPN/View controllers/SelectLocation/LocationSectionHeaderView.swift')
| -rw-r--r-- | ios/MullvadVPN/View controllers/SelectLocation/LocationSectionHeaderView.swift | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/ios/MullvadVPN/View controllers/SelectLocation/LocationSectionHeaderView.swift b/ios/MullvadVPN/View controllers/SelectLocation/LocationSectionHeaderView.swift new file mode 100644 index 0000000000..49c9cbce20 --- /dev/null +++ b/ios/MullvadVPN/View controllers/SelectLocation/LocationSectionHeaderView.swift @@ -0,0 +1,95 @@ +// +// LocationSectionHeaderView.swift +// MullvadVPN +// +// Created by Mojgan on 2024-01-25. +// Copyright © 2024 Mullvad VPN AB. All rights reserved. +// + +import Foundation +import UIKit + +class LocationSectionHeaderView: UIView, UIContentView { + var configuration: UIContentConfiguration { + get { + actualConfiguration + } set { + guard let newConfiguration = newValue as? Configuration, + actualConfiguration != newConfiguration else { return } + let previousConfiguration = actualConfiguration + actualConfiguration = newConfiguration + apply(configuration: previousConfiguration) + } + } + + private var actualConfiguration: Configuration + private let nameLabel: UILabel = { + let label = UILabel() + label.numberOfLines = 1 + label.textColor = .primaryTextColor + label.font = .systemFont(ofSize: 16, weight: .semibold) + return label + }() + + private let actionButton: UIButton = { + let button = UIButton(type: .system) + button.setImage(UIImage(systemName: "ellipsis"), for: .normal) + button.tintColor = UIColor(white: 1, alpha: 0.6) + return button + }() + + init(configuration: Configuration) { + self.actualConfiguration = configuration + super.init(frame: .zero) + applyAppearance() + addSubviews() + apply(configuration: configuration) + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + private func addSubviews() { + addConstrainedSubviews([nameLabel, actionButton]) { + nameLabel.pinEdgesToSuperviewMargins(.all().excluding(.trailing)) + + actionButton.pinEdgesToSuperviewMargins(PinnableEdges([.trailing(.zero)])) + actionButton.widthAnchor.constraint(equalToConstant: 24) + actionButton.heightAnchor.constraint(equalTo: actionButton.widthAnchor, multiplier: 1) + actionButton.centerYAnchor.constraint(equalTo: self.centerYAnchor) + + actionButton.leadingAnchor.constraint(equalTo: nameLabel.trailingAnchor, constant: 16) + } + } + + private func apply(configuration: Configuration) { + let isActionHidden = configuration.primaryAction == nil + nameLabel.text = configuration.name + actionButton.isHidden = isActionHidden + actualConfiguration.primaryAction.flatMap { [weak self] action in + self?.actionButton.addAction(action, for: .touchUpInside) + } + } + + private func applyAppearance() { + backgroundColor = .primaryColor + directionalLayoutMargins = NSDirectionalEdgeInsets(top: 8, leading: 16, bottom: 8, trailing: 24) + } +} + +extension LocationSectionHeaderView { + struct Configuration: UIContentConfiguration, Equatable { + let name: String + + var primaryAction: UIAction? + + func makeContentView() -> UIView & UIContentView { + LocationSectionHeaderView(configuration: self) + } + + func updated(for state: UIConfigurationState) -> Configuration { + self + } + } +} |
