Skip to content

Commit

Permalink
Refactor buttons to use UIButton.Configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
mojganii committed Oct 10, 2024
1 parent 368961a commit 9fa4ad7
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 298 deletions.
16 changes: 10 additions & 6 deletions ios/MullvadVPN.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,6 @@
58FF9FE42B075BDD00E4C97D /* EditAccessMethodItemIdentifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58FF9FE32B075BDD00E4C97D /* EditAccessMethodItemIdentifier.swift */; };
58FF9FE82B07650A00E4C97D /* ButtonCellContentConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58FF9FE72B07650A00E4C97D /* ButtonCellContentConfiguration.swift */; };
58FF9FEA2B07653800E4C97D /* ButtonCellContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58FF9FE92B07653800E4C97D /* ButtonCellContentView.swift */; };
58FF9FEC2B07A7CB00E4C97D /* NSDirectionalEdgeInsets+Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58FF9FEB2B07A7CB00E4C97D /* NSDirectionalEdgeInsets+Helpers.swift */; };
58FF9FF02B07C4D300E4C97D /* PersistentAccessMethod+ViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58FF9FEF2B07C4D300E4C97D /* PersistentAccessMethod+ViewModel.swift */; };
58FF9FF42B07C61B00E4C97D /* AccessMethodValidationError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58FF9FF32B07C61B00E4C97D /* AccessMethodValidationError.swift */; };
7A02D4EB2A9CEC7A00C19E31 /* MullvadVPNScreenshots.xctestplan in Resources */ = {isa = PBXBuildFile; fileRef = 7A02D4EA2A9CEC7A00C19E31 /* MullvadVPNScreenshots.xctestplan */; };
Expand Down Expand Up @@ -571,10 +570,10 @@
7A9CCCC22A96302800DD6A34 /* SafariCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A9CCCB02A96302800DD6A34 /* SafariCoordinator.swift */; };
7A9CCCC32A96302800DD6A34 /* ApplicationCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A9CCCB12A96302800DD6A34 /* ApplicationCoordinator.swift */; };
7A9CCCC42A96302800DD6A34 /* TunnelCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A9CCCB22A96302800DD6A34 /* TunnelCoordinator.swift */; };
7A9F28FC2CA69D0C005F2089 /* DAITASettingsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A9F28FB2CA69D04005F2089 /* DAITASettingsTests.swift */; };
7A9F29392CABFAFC005F2089 /* InfoHeaderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A9F29382CABFAEC005F2089 /* InfoHeaderView.swift */; };
7A9F293B2CAC4443005F2089 /* InfoHeaderConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A9F293A2CAC4420005F2089 /* InfoHeaderConfig.swift */; };
7A9F293D2CAD2FD5005F2089 /* InfoModalConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A9F293C2CAD2FCF005F2089 /* InfoModalConfig.swift */; };
7A9F28FC2CA69D0C005F2089 /* DAITASettingsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A9F28FB2CA69D04005F2089 /* DAITASettingsTests.swift */; };
7A9FA1422A2E3306000B728D /* CheckboxView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A9FA1412A2E3306000B728D /* CheckboxView.swift */; };
7A9FA1442A2E3FE5000B728D /* CheckableSettingsCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A9FA1432A2E3FE5000B728D /* CheckableSettingsCell.swift */; };
7AA513862BC91C6B00D081A4 /* LogRotationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AA513852BC91C6B00D081A4 /* LogRotationTests.swift */; };
Expand Down Expand Up @@ -894,6 +893,8 @@
F06045E62B231EB700B2D37A /* URLSessionTransport.swift in Sources */ = {isa = PBXBuildFile; fileRef = F06045E52B231EB700B2D37A /* URLSessionTransport.swift */; };
F06045EA2B23217E00B2D37A /* ShadowsocksTransport.swift in Sources */ = {isa = PBXBuildFile; fileRef = F06045E92B23217E00B2D37A /* ShadowsocksTransport.swift */; };
F06045EC2B2322A500B2D37A /* Jittered.swift in Sources */ = {isa = PBXBuildFile; fileRef = F06045EB2B2322A500B2D37A /* Jittered.swift */; };
F062000A2CB7EB42002E6DB9 /* CGSize+Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = F06200092CB7EB42002E6DB9 /* CGSize+Helpers.swift */; };
F062000C2CB7EB5D002E6DB9 /* UIImage+Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = F062000B2CB7EB5D002E6DB9 /* UIImage+Helpers.swift */; };
F062B94D2C16E09700B6D47A /* TunnelSettingsManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F062B94C2C16E09700B6D47A /* TunnelSettingsManagerTests.swift */; };
F072D3CF2C07122400906F64 /* SettingsUpdaterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F072D3CE2C07122400906F64 /* SettingsUpdaterTests.swift */; };
F072D3D22C071AD100906F64 /* ShadowsocksLoaderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F072D3D12C071AD100906F64 /* ShadowsocksLoaderTests.swift */; };
Expand Down Expand Up @@ -1783,7 +1784,6 @@
58FF9FE32B075BDD00E4C97D /* EditAccessMethodItemIdentifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditAccessMethodItemIdentifier.swift; sourceTree = "<group>"; };
58FF9FE72B07650A00E4C97D /* ButtonCellContentConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonCellContentConfiguration.swift; sourceTree = "<group>"; };
58FF9FE92B07653800E4C97D /* ButtonCellContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonCellContentView.swift; sourceTree = "<group>"; };
58FF9FEB2B07A7CB00E4C97D /* NSDirectionalEdgeInsets+Helpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSDirectionalEdgeInsets+Helpers.swift"; sourceTree = "<group>"; };
58FF9FEF2B07C4D300E4C97D /* PersistentAccessMethod+ViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PersistentAccessMethod+ViewModel.swift"; sourceTree = "<group>"; };
58FF9FF32B07C61B00E4C97D /* AccessMethodValidationError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccessMethodValidationError.swift; sourceTree = "<group>"; };
7A02D4EA2A9CEC7A00C19E31 /* MullvadVPNScreenshots.xctestplan */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = MullvadVPNScreenshots.xctestplan; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1890,10 +1890,10 @@
7A9CCCB02A96302800DD6A34 /* SafariCoordinator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SafariCoordinator.swift; sourceTree = "<group>"; };
7A9CCCB12A96302800DD6A34 /* ApplicationCoordinator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ApplicationCoordinator.swift; sourceTree = "<group>"; };
7A9CCCB22A96302800DD6A34 /* TunnelCoordinator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TunnelCoordinator.swift; sourceTree = "<group>"; };
7A9F28FB2CA69D04005F2089 /* DAITASettingsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DAITASettingsTests.swift; sourceTree = "<group>"; };
7A9F29382CABFAEC005F2089 /* InfoHeaderView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfoHeaderView.swift; sourceTree = "<group>"; };
7A9F293A2CAC4420005F2089 /* InfoHeaderConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfoHeaderConfig.swift; sourceTree = "<group>"; };
7A9F293C2CAD2FCF005F2089 /* InfoModalConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfoModalConfig.swift; sourceTree = "<group>"; };
7A9F28FB2CA69D04005F2089 /* DAITASettingsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DAITASettingsTests.swift; sourceTree = "<group>"; };
7A9FA1412A2E3306000B728D /* CheckboxView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CheckboxView.swift; sourceTree = "<group>"; };
7A9FA1432A2E3FE5000B728D /* CheckableSettingsCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CheckableSettingsCell.swift; sourceTree = "<group>"; };
7AA513852BC91C6B00D081A4 /* LogRotationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogRotationTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2113,6 +2113,8 @@
F06045E52B231EB700B2D37A /* URLSessionTransport.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLSessionTransport.swift; sourceTree = "<group>"; };
F06045E92B23217E00B2D37A /* ShadowsocksTransport.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ShadowsocksTransport.swift; sourceTree = "<group>"; };
F06045EB2B2322A500B2D37A /* Jittered.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Jittered.swift; sourceTree = "<group>"; };
F06200092CB7EB42002E6DB9 /* CGSize+Helpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CGSize+Helpers.swift"; sourceTree = "<group>"; };
F062000B2CB7EB5D002E6DB9 /* UIImage+Helpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImage+Helpers.swift"; sourceTree = "<group>"; };
F062B94C2C16E09700B6D47A /* TunnelSettingsManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TunnelSettingsManagerTests.swift; sourceTree = "<group>"; };
F072D3CE2C07122400906F64 /* SettingsUpdaterTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsUpdaterTests.swift; sourceTree = "<group>"; };
F072D3D12C071AD100906F64 /* ShadowsocksLoaderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShadowsocksLoaderTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2953,6 +2955,7 @@
isa = PBXGroup;
children = (
5891BF1B25E3E3EB006D6FB0 /* Bundle+ProductVersion.swift */,
F06200092CB7EB42002E6DB9 /* CGSize+Helpers.swift */,
587EB669270EFACB00123C75 /* CharacterSet+IPAddress.swift */,
58E511E528DDDEAC00B0BCDE /* CodingErrors+CustomErrorDescription.swift */,
7AF9BE8F2A39F26000DBFEDB /* Collection+Sorting.swift */,
Expand All @@ -2974,6 +2977,7 @@
5891BF5025E66B1E006D6FB0 /* UIBarButtonItem+KeyboardNavigation.swift */,
587CBFE222807F530028DED3 /* UIColor+Helpers.swift */,
7ABE318C2A1CDD4500DF4963 /* UIFont+Weight.swift */,
F062000B2CB7EB5D002E6DB9 /* UIImage+Helpers.swift */,
58CEB2FA2AFD13E600E6E088 /* UIListContentConfiguration+Extensions.swift */,
58CEB2FC2AFD19D300E6E088 /* UITableView+ReuseIdentifier.swift */,
7A58699A2B482FE200640D27 /* UITableViewCell+Disable.swift */,
Expand Down Expand Up @@ -3009,7 +3013,6 @@
children = (
58CCA0152242560B004F3011 /* UIColor+Palette.swift */,
A9E034632ABB302000E59A5A /* UIEdgeInsets+Extensions.swift */,
58FF9FEB2B07A7CB00E4C97D /* NSDirectionalEdgeInsets+Helpers.swift */,
585CA70E25F8C44600B47C62 /* UIMetrics.swift */,
);
path = "UI appearance";
Expand Down Expand Up @@ -5624,6 +5627,7 @@
587EB672271451E300123C75 /* VPNSettingsViewModel.swift in Sources */,
586A950C290125EE007BAF2B /* AlertPresenter.swift in Sources */,
7A9FA1422A2E3306000B728D /* CheckboxView.swift in Sources */,
F062000A2CB7EB42002E6DB9 /* CGSize+Helpers.swift in Sources */,
586C0D892B03D5E000E7CDD7 /* TextCellContentConfiguration+Extensions.swift in Sources */,
58C3F4F92964B08300D72515 /* MapViewController.swift in Sources */,
584D26C6270C8741004EA533 /* SettingsDNSTextCell.swift in Sources */,
Expand All @@ -5637,6 +5641,7 @@
7A516C2E2B6D357500BBD33D /* URL+Scoping.swift in Sources */,
5878A27529093A310096FC88 /* StorePaymentEvent.swift in Sources */,
7A7AD28D29DC677800480EF1 /* FirstTimeLaunch.swift in Sources */,
F062000C2CB7EB5D002E6DB9 /* UIImage+Helpers.swift in Sources */,
7A6389EB2B7FAD7A008E77E1 /* SettingsFieldValidationErrorContentView.swift in Sources */,
58B26E2A2943545A00D5980C /* NotificationManagerDelegate.swift in Sources */,
58A1AA8C23F5584C009F7EA6 /* ConnectionPanelView.swift in Sources */,
Expand Down Expand Up @@ -5832,7 +5837,6 @@
7A5869B92B56E7F000640D27 /* IPOverrideViewControllerDelegate.swift in Sources */,
586C0D7A2B039CE300E7CDD7 /* ShadowsocksCipherPicker.swift in Sources */,
58B93A1326C3F13600A55733 /* TunnelState.swift in Sources */,
58FF9FEC2B07A7CB00E4C97D /* NSDirectionalEdgeInsets+Helpers.swift in Sources */,
586C0D832B03D2FF00E7CDD7 /* ShadowsocksSectionHandler.swift in Sources */,
58B26E262943522400D5980C /* NotificationProvider.swift in Sources */,
58CE5E64224146200008646E /* AppDelegate.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ class ButtonCellContentView: UIView, UIContentView {
button.titleLabel?.font = .systemFont(ofSize: 17)
button.isEnabled = actualConfiguration.isEnabled
button.style = actualConfiguration.style
button.overrideContentEdgeInsets = true
button.directionalContentEdgeInsets = actualConfiguration.directionalContentEdgeInsets
button.configuration?.contentInsets = actualConfiguration.directionalContentEdgeInsets
}

private func addSubviews() {
Expand Down
18 changes: 18 additions & 0 deletions ios/MullvadVPN/Extensions/CGSize+Helpers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// CGSize+Helpers.swift
// MullvadVPN
//
// Created by Mojgan on 2024-10-10.
// Copyright © 2024 Mullvad VPN AB. All rights reserved.
//

import UIKit

extension CGSize {
// Function to deduct insets from CGSize
func deducting(insets: NSDirectionalEdgeInsets) -> CGSize {
let newWidth = self.width - (insets.leading + insets.trailing)
let newHeight = self.height - (insets.top + insets.bottom)
return CGSize(width: newWidth, height: newHeight)
}
}
29 changes: 29 additions & 0 deletions ios/MullvadVPN/Extensions/UIImage+Helpers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// UIImage+Helpers.swift
// MullvadVPN
//
// Created by Mojgan on 2024-10-10.
// Copyright © 2024 Mullvad VPN AB. All rights reserved.
//

import UIKit

extension UIImage {
// Function to resize image while keeping aspect ratio
func resizeImage(targetSize: CGSize) -> UIImage {
let widthRatio = targetSize.width / self.size.width
let heightRatio = targetSize.height / self.size.height
let scaleFactor = min(widthRatio, heightRatio)

// Calculate new size based on the scale factor
let newSize = CGSize(width: self.size.width * scaleFactor, height: self.size.height * scaleFactor)
let renderer = UIGraphicsImageRenderer(size: newSize)

// Render the new image
let resizedImage = renderer.image { _ in
self.draw(in: CGRect(origin: .zero, size: newSize))
}

return resizedImage.withRenderingMode(self.renderingMode)
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "TranslucentDangerButton.pdf",
"idiom" : "universal",
"resizing" : {
"mode" : "9-part",
"center" : {
"mode" : "tile",
"width" : 1,
"height" : 1
},
"cap-insets" : {
"bottom" : 4,
"top" : 4,
"left" : 4,
"right" : 4,
"left" : 4
}
"top" : 4
},
"center" : {
"height" : 1,
"mode" : "tile",
"width" : 1
},
"mode" : "9-part"
}
}
],
"info" : {
"version" : 1,
"author" : "xcode"
"author" : "xcode",
"version" : 1
}
}
}
21 changes: 0 additions & 21 deletions ios/MullvadVPN/UI appearance/NSDirectionalEdgeInsets+Helpers.swift

This file was deleted.

26 changes: 16 additions & 10 deletions ios/MullvadVPN/View controllers/Login/AccountInputGroupView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,35 +92,41 @@ final class AccountInputGroupView: UIView {
}()

private let lastUsedAccountButton: UIButton = {
let button = UIButton(type: .system)
let button = UIButton(configuration: .plain())
button.translatesAutoresizingMaskIntoConstraints = false
button.titleLabel?.font = accountNumberFont()
button.setTitle(" ", for: .normal)
button.contentHorizontalAlignment = .leading
button.contentEdgeInsets = UIMetrics.textFieldMargins
button.setTitleColor(UIColor.AccountTextField.NormalState.textColor, for: .normal)
button.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
button.accessibilityLabel = NSLocalizedString(
"LAST_USED_ACCOUNT_ACCESSIBILITY_LABEL",
tableName: "AccountInput",
value: "Last used account",
comment: ""
)
button.configuration?.contentInsets = UIMetrics.textFieldMargins.toDirectionalInsets
button.configuration?.title = " "
button.configuration?
.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { attributeContainer in
var updatedAttributeContainer = attributeContainer
updatedAttributeContainer.font = AccountInputGroupView.accountNumberFont()
updatedAttributeContainer.foregroundColor = .AccountTextField.NormalState.textColor
return updatedAttributeContainer
}

return button
}()

private let removeLastUsedAccountButton: UIButton = {
let button = UIButton(type: .custom)
private let removeLastUsedAccountButton: CustomButton = {
let button = CustomButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.setImage(UIImage(named: "IconCloseSml"), for: .normal)
button.imageView?.tintColor = .primaryColor.withAlphaComponent(0.4)
button.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
button.accessibilityLabel = NSLocalizedString(
"REMOVE_LAST_USED_ACCOUNT_ACCESSIBILITY_LABEL",
tableName: "AccountInput",
value: "Remove last used account",
comment: ""
)
button.configuration?.image = UIImage(resource: .iconCloseSml).withTintColor(.primaryColor)
button.configuration?.title = " "
return button
}()

Expand Down Expand Up @@ -303,7 +309,7 @@ final class AccountInputGroupView: UIView {
)

UIView.performWithoutAnimation {
self.lastUsedAccountButton.setTitle(formattedNumber, for: .normal)
self.lastUsedAccountButton.configuration?.title = formattedNumber
self.lastUsedAccountButton.layoutIfNeeded()
}
}
Expand Down
Loading

0 comments on commit 9fa4ad7

Please sign in to comment.