Skip to content

Commit 9fa4ad7

Browse files
committed
Refactor buttons to use UIButton.Configuration
1 parent 368961a commit 9fa4ad7

File tree

11 files changed

+162
-298
lines changed

11 files changed

+162
-298
lines changed

Diff for: ios/MullvadVPN.xcodeproj/project.pbxproj

+10-6
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,6 @@
449449
58FF9FE42B075BDD00E4C97D /* EditAccessMethodItemIdentifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58FF9FE32B075BDD00E4C97D /* EditAccessMethodItemIdentifier.swift */; };
450450
58FF9FE82B07650A00E4C97D /* ButtonCellContentConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58FF9FE72B07650A00E4C97D /* ButtonCellContentConfiguration.swift */; };
451451
58FF9FEA2B07653800E4C97D /* ButtonCellContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58FF9FE92B07653800E4C97D /* ButtonCellContentView.swift */; };
452-
58FF9FEC2B07A7CB00E4C97D /* NSDirectionalEdgeInsets+Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58FF9FEB2B07A7CB00E4C97D /* NSDirectionalEdgeInsets+Helpers.swift */; };
453452
58FF9FF02B07C4D300E4C97D /* PersistentAccessMethod+ViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58FF9FEF2B07C4D300E4C97D /* PersistentAccessMethod+ViewModel.swift */; };
454453
58FF9FF42B07C61B00E4C97D /* AccessMethodValidationError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58FF9FF32B07C61B00E4C97D /* AccessMethodValidationError.swift */; };
455454
7A02D4EB2A9CEC7A00C19E31 /* MullvadVPNScreenshots.xctestplan in Resources */ = {isa = PBXBuildFile; fileRef = 7A02D4EA2A9CEC7A00C19E31 /* MullvadVPNScreenshots.xctestplan */; };
@@ -571,10 +570,10 @@
571570
7A9CCCC22A96302800DD6A34 /* SafariCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A9CCCB02A96302800DD6A34 /* SafariCoordinator.swift */; };
572571
7A9CCCC32A96302800DD6A34 /* ApplicationCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A9CCCB12A96302800DD6A34 /* ApplicationCoordinator.swift */; };
573572
7A9CCCC42A96302800DD6A34 /* TunnelCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A9CCCB22A96302800DD6A34 /* TunnelCoordinator.swift */; };
573+
7A9F28FC2CA69D0C005F2089 /* DAITASettingsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A9F28FB2CA69D04005F2089 /* DAITASettingsTests.swift */; };
574574
7A9F29392CABFAFC005F2089 /* InfoHeaderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A9F29382CABFAEC005F2089 /* InfoHeaderView.swift */; };
575575
7A9F293B2CAC4443005F2089 /* InfoHeaderConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A9F293A2CAC4420005F2089 /* InfoHeaderConfig.swift */; };
576576
7A9F293D2CAD2FD5005F2089 /* InfoModalConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A9F293C2CAD2FCF005F2089 /* InfoModalConfig.swift */; };
577-
7A9F28FC2CA69D0C005F2089 /* DAITASettingsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A9F28FB2CA69D04005F2089 /* DAITASettingsTests.swift */; };
578577
7A9FA1422A2E3306000B728D /* CheckboxView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A9FA1412A2E3306000B728D /* CheckboxView.swift */; };
579578
7A9FA1442A2E3FE5000B728D /* CheckableSettingsCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A9FA1432A2E3FE5000B728D /* CheckableSettingsCell.swift */; };
580579
7AA513862BC91C6B00D081A4 /* LogRotationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AA513852BC91C6B00D081A4 /* LogRotationTests.swift */; };
@@ -894,6 +893,8 @@
894893
F06045E62B231EB700B2D37A /* URLSessionTransport.swift in Sources */ = {isa = PBXBuildFile; fileRef = F06045E52B231EB700B2D37A /* URLSessionTransport.swift */; };
895894
F06045EA2B23217E00B2D37A /* ShadowsocksTransport.swift in Sources */ = {isa = PBXBuildFile; fileRef = F06045E92B23217E00B2D37A /* ShadowsocksTransport.swift */; };
896895
F06045EC2B2322A500B2D37A /* Jittered.swift in Sources */ = {isa = PBXBuildFile; fileRef = F06045EB2B2322A500B2D37A /* Jittered.swift */; };
896+
F062000A2CB7EB42002E6DB9 /* CGSize+Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = F06200092CB7EB42002E6DB9 /* CGSize+Helpers.swift */; };
897+
F062000C2CB7EB5D002E6DB9 /* UIImage+Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = F062000B2CB7EB5D002E6DB9 /* UIImage+Helpers.swift */; };
897898
F062B94D2C16E09700B6D47A /* TunnelSettingsManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F062B94C2C16E09700B6D47A /* TunnelSettingsManagerTests.swift */; };
898899
F072D3CF2C07122400906F64 /* SettingsUpdaterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F072D3CE2C07122400906F64 /* SettingsUpdaterTests.swift */; };
899900
F072D3D22C071AD100906F64 /* ShadowsocksLoaderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F072D3D12C071AD100906F64 /* ShadowsocksLoaderTests.swift */; };
@@ -1783,7 +1784,6 @@
17831784
58FF9FE32B075BDD00E4C97D /* EditAccessMethodItemIdentifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditAccessMethodItemIdentifier.swift; sourceTree = "<group>"; };
17841785
58FF9FE72B07650A00E4C97D /* ButtonCellContentConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonCellContentConfiguration.swift; sourceTree = "<group>"; };
17851786
58FF9FE92B07653800E4C97D /* ButtonCellContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonCellContentView.swift; sourceTree = "<group>"; };
1786-
58FF9FEB2B07A7CB00E4C97D /* NSDirectionalEdgeInsets+Helpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSDirectionalEdgeInsets+Helpers.swift"; sourceTree = "<group>"; };
17871787
58FF9FEF2B07C4D300E4C97D /* PersistentAccessMethod+ViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PersistentAccessMethod+ViewModel.swift"; sourceTree = "<group>"; };
17881788
58FF9FF32B07C61B00E4C97D /* AccessMethodValidationError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccessMethodValidationError.swift; sourceTree = "<group>"; };
17891789
7A02D4EA2A9CEC7A00C19E31 /* MullvadVPNScreenshots.xctestplan */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = MullvadVPNScreenshots.xctestplan; sourceTree = "<group>"; };
@@ -1890,10 +1890,10 @@
18901890
7A9CCCB02A96302800DD6A34 /* SafariCoordinator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SafariCoordinator.swift; sourceTree = "<group>"; };
18911891
7A9CCCB12A96302800DD6A34 /* ApplicationCoordinator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ApplicationCoordinator.swift; sourceTree = "<group>"; };
18921892
7A9CCCB22A96302800DD6A34 /* TunnelCoordinator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TunnelCoordinator.swift; sourceTree = "<group>"; };
1893+
7A9F28FB2CA69D04005F2089 /* DAITASettingsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DAITASettingsTests.swift; sourceTree = "<group>"; };
18931894
7A9F29382CABFAEC005F2089 /* InfoHeaderView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfoHeaderView.swift; sourceTree = "<group>"; };
18941895
7A9F293A2CAC4420005F2089 /* InfoHeaderConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfoHeaderConfig.swift; sourceTree = "<group>"; };
18951896
7A9F293C2CAD2FCF005F2089 /* InfoModalConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfoModalConfig.swift; sourceTree = "<group>"; };
1896-
7A9F28FB2CA69D04005F2089 /* DAITASettingsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DAITASettingsTests.swift; sourceTree = "<group>"; };
18971897
7A9FA1412A2E3306000B728D /* CheckboxView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CheckboxView.swift; sourceTree = "<group>"; };
18981898
7A9FA1432A2E3FE5000B728D /* CheckableSettingsCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CheckableSettingsCell.swift; sourceTree = "<group>"; };
18991899
7AA513852BC91C6B00D081A4 /* LogRotationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogRotationTests.swift; sourceTree = "<group>"; };
@@ -2113,6 +2113,8 @@
21132113
F06045E52B231EB700B2D37A /* URLSessionTransport.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLSessionTransport.swift; sourceTree = "<group>"; };
21142114
F06045E92B23217E00B2D37A /* ShadowsocksTransport.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ShadowsocksTransport.swift; sourceTree = "<group>"; };
21152115
F06045EB2B2322A500B2D37A /* Jittered.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Jittered.swift; sourceTree = "<group>"; };
2116+
F06200092CB7EB42002E6DB9 /* CGSize+Helpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CGSize+Helpers.swift"; sourceTree = "<group>"; };
2117+
F062000B2CB7EB5D002E6DB9 /* UIImage+Helpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImage+Helpers.swift"; sourceTree = "<group>"; };
21162118
F062B94C2C16E09700B6D47A /* TunnelSettingsManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TunnelSettingsManagerTests.swift; sourceTree = "<group>"; };
21172119
F072D3CE2C07122400906F64 /* SettingsUpdaterTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsUpdaterTests.swift; sourceTree = "<group>"; };
21182120
F072D3D12C071AD100906F64 /* ShadowsocksLoaderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShadowsocksLoaderTests.swift; sourceTree = "<group>"; };
@@ -2953,6 +2955,7 @@
29532955
isa = PBXGroup;
29542956
children = (
29552957
5891BF1B25E3E3EB006D6FB0 /* Bundle+ProductVersion.swift */,
2958+
F06200092CB7EB42002E6DB9 /* CGSize+Helpers.swift */,
29562959
587EB669270EFACB00123C75 /* CharacterSet+IPAddress.swift */,
29572960
58E511E528DDDEAC00B0BCDE /* CodingErrors+CustomErrorDescription.swift */,
29582961
7AF9BE8F2A39F26000DBFEDB /* Collection+Sorting.swift */,
@@ -2974,6 +2977,7 @@
29742977
5891BF5025E66B1E006D6FB0 /* UIBarButtonItem+KeyboardNavigation.swift */,
29752978
587CBFE222807F530028DED3 /* UIColor+Helpers.swift */,
29762979
7ABE318C2A1CDD4500DF4963 /* UIFont+Weight.swift */,
2980+
F062000B2CB7EB5D002E6DB9 /* UIImage+Helpers.swift */,
29772981
58CEB2FA2AFD13E600E6E088 /* UIListContentConfiguration+Extensions.swift */,
29782982
58CEB2FC2AFD19D300E6E088 /* UITableView+ReuseIdentifier.swift */,
29792983
7A58699A2B482FE200640D27 /* UITableViewCell+Disable.swift */,
@@ -3009,7 +3013,6 @@
30093013
children = (
30103014
58CCA0152242560B004F3011 /* UIColor+Palette.swift */,
30113015
A9E034632ABB302000E59A5A /* UIEdgeInsets+Extensions.swift */,
3012-
58FF9FEB2B07A7CB00E4C97D /* NSDirectionalEdgeInsets+Helpers.swift */,
30133016
585CA70E25F8C44600B47C62 /* UIMetrics.swift */,
30143017
);
30153018
path = "UI appearance";
@@ -5624,6 +5627,7 @@
56245627
587EB672271451E300123C75 /* VPNSettingsViewModel.swift in Sources */,
56255628
586A950C290125EE007BAF2B /* AlertPresenter.swift in Sources */,
56265629
7A9FA1422A2E3306000B728D /* CheckboxView.swift in Sources */,
5630+
F062000A2CB7EB42002E6DB9 /* CGSize+Helpers.swift in Sources */,
56275631
586C0D892B03D5E000E7CDD7 /* TextCellContentConfiguration+Extensions.swift in Sources */,
56285632
58C3F4F92964B08300D72515 /* MapViewController.swift in Sources */,
56295633
584D26C6270C8741004EA533 /* SettingsDNSTextCell.swift in Sources */,
@@ -5637,6 +5641,7 @@
56375641
7A516C2E2B6D357500BBD33D /* URL+Scoping.swift in Sources */,
56385642
5878A27529093A310096FC88 /* StorePaymentEvent.swift in Sources */,
56395643
7A7AD28D29DC677800480EF1 /* FirstTimeLaunch.swift in Sources */,
5644+
F062000C2CB7EB5D002E6DB9 /* UIImage+Helpers.swift in Sources */,
56405645
7A6389EB2B7FAD7A008E77E1 /* SettingsFieldValidationErrorContentView.swift in Sources */,
56415646
58B26E2A2943545A00D5980C /* NotificationManagerDelegate.swift in Sources */,
56425647
58A1AA8C23F5584C009F7EA6 /* ConnectionPanelView.swift in Sources */,
@@ -5832,7 +5837,6 @@
58325837
7A5869B92B56E7F000640D27 /* IPOverrideViewControllerDelegate.swift in Sources */,
58335838
586C0D7A2B039CE300E7CDD7 /* ShadowsocksCipherPicker.swift in Sources */,
58345839
58B93A1326C3F13600A55733 /* TunnelState.swift in Sources */,
5835-
58FF9FEC2B07A7CB00E4C97D /* NSDirectionalEdgeInsets+Helpers.swift in Sources */,
58365840
586C0D832B03D2FF00E7CDD7 /* ShadowsocksSectionHandler.swift in Sources */,
58375841
58B26E262943522400D5980C /* NotificationProvider.swift in Sources */,
58385842
58CE5E64224146200008646E /* AppDelegate.swift in Sources */,

Diff for: ios/MullvadVPN/Coordinators/Settings/APIAccess/Cells/ButtonCellContentView.swift

+1-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ class ButtonCellContentView: UIView, UIContentView {
6666
button.titleLabel?.font = .systemFont(ofSize: 17)
6767
button.isEnabled = actualConfiguration.isEnabled
6868
button.style = actualConfiguration.style
69-
button.overrideContentEdgeInsets = true
70-
button.directionalContentEdgeInsets = actualConfiguration.directionalContentEdgeInsets
69+
button.configuration?.contentInsets = actualConfiguration.directionalContentEdgeInsets
7170
}
7271

7372
private func addSubviews() {

Diff for: ios/MullvadVPN/Extensions/CGSize+Helpers.swift

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// CGSize+Helpers.swift
3+
// MullvadVPN
4+
//
5+
// Created by Mojgan on 2024-10-10.
6+
// Copyright © 2024 Mullvad VPN AB. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
extension CGSize {
12+
// Function to deduct insets from CGSize
13+
func deducting(insets: NSDirectionalEdgeInsets) -> CGSize {
14+
let newWidth = self.width - (insets.leading + insets.trailing)
15+
let newHeight = self.height - (insets.top + insets.bottom)
16+
return CGSize(width: newWidth, height: newHeight)
17+
}
18+
}

Diff for: ios/MullvadVPN/Extensions/UIImage+Helpers.swift

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// UIImage+Helpers.swift
3+
// MullvadVPN
4+
//
5+
// Created by Mojgan on 2024-10-10.
6+
// Copyright © 2024 Mullvad VPN AB. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
extension UIImage {
12+
// Function to resize image while keeping aspect ratio
13+
func resizeImage(targetSize: CGSize) -> UIImage {
14+
let widthRatio = targetSize.width / self.size.width
15+
let heightRatio = targetSize.height / self.size.height
16+
let scaleFactor = min(widthRatio, heightRatio)
17+
18+
// Calculate new size based on the scale factor
19+
let newSize = CGSize(width: self.size.width * scaleFactor, height: self.size.height * scaleFactor)
20+
let renderer = UIGraphicsImageRenderer(size: newSize)
21+
22+
// Render the new image
23+
let resizedImage = renderer.image { _ in
24+
self.draw(in: CGRect(origin: .zero, size: newSize))
25+
}
26+
27+
return resizedImage.withRenderingMode(self.renderingMode)
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
{
22
"images" : [
33
{
4-
"idiom" : "universal",
54
"filename" : "TranslucentDangerButton.pdf",
5+
"idiom" : "universal",
66
"resizing" : {
7-
"mode" : "9-part",
8-
"center" : {
9-
"mode" : "tile",
10-
"width" : 1,
11-
"height" : 1
12-
},
137
"cap-insets" : {
148
"bottom" : 4,
15-
"top" : 4,
9+
"left" : 4,
1610
"right" : 4,
17-
"left" : 4
18-
}
11+
"top" : 4
12+
},
13+
"center" : {
14+
"height" : 1,
15+
"mode" : "tile",
16+
"width" : 1
17+
},
18+
"mode" : "9-part"
1919
}
2020
}
2121
],
2222
"info" : {
23-
"version" : 1,
24-
"author" : "xcode"
23+
"author" : "xcode",
24+
"version" : 1
2525
}
26-
}
26+
}

Diff for: ios/MullvadVPN/UI appearance/NSDirectionalEdgeInsets+Helpers.swift

-21
This file was deleted.

Diff for: ios/MullvadVPN/View controllers/Login/AccountInputGroupView.swift

+16-10
Original file line numberDiff line numberDiff line change
@@ -92,35 +92,41 @@ final class AccountInputGroupView: UIView {
9292
}()
9393

9494
private let lastUsedAccountButton: UIButton = {
95-
let button = UIButton(type: .system)
95+
let button = UIButton(configuration: .plain())
9696
button.translatesAutoresizingMaskIntoConstraints = false
97-
button.titleLabel?.font = accountNumberFont()
98-
button.setTitle(" ", for: .normal)
9997
button.contentHorizontalAlignment = .leading
100-
button.contentEdgeInsets = UIMetrics.textFieldMargins
101-
button.setTitleColor(UIColor.AccountTextField.NormalState.textColor, for: .normal)
10298
button.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
10399
button.accessibilityLabel = NSLocalizedString(
104100
"LAST_USED_ACCOUNT_ACCESSIBILITY_LABEL",
105101
tableName: "AccountInput",
106102
value: "Last used account",
107103
comment: ""
108104
)
105+
button.configuration?.contentInsets = UIMetrics.textFieldMargins.toDirectionalInsets
106+
button.configuration?.title = " "
107+
button.configuration?
108+
.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { attributeContainer in
109+
var updatedAttributeContainer = attributeContainer
110+
updatedAttributeContainer.font = AccountInputGroupView.accountNumberFont()
111+
updatedAttributeContainer.foregroundColor = .AccountTextField.NormalState.textColor
112+
return updatedAttributeContainer
113+
}
114+
109115
return button
110116
}()
111117

112-
private let removeLastUsedAccountButton: UIButton = {
113-
let button = UIButton(type: .custom)
118+
private let removeLastUsedAccountButton: CustomButton = {
119+
let button = CustomButton()
114120
button.translatesAutoresizingMaskIntoConstraints = false
115-
button.setImage(UIImage(named: "IconCloseSml"), for: .normal)
116-
button.imageView?.tintColor = .primaryColor.withAlphaComponent(0.4)
117121
button.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
118122
button.accessibilityLabel = NSLocalizedString(
119123
"REMOVE_LAST_USED_ACCOUNT_ACCESSIBILITY_LABEL",
120124
tableName: "AccountInput",
121125
value: "Remove last used account",
122126
comment: ""
123127
)
128+
button.configuration?.image = UIImage(resource: .iconCloseSml).withTintColor(.primaryColor)
129+
button.configuration?.title = " "
124130
return button
125131
}()
126132

@@ -303,7 +309,7 @@ final class AccountInputGroupView: UIView {
303309
)
304310

305311
UIView.performWithoutAnimation {
306-
self.lastUsedAccountButton.setTitle(formattedNumber, for: .normal)
312+
self.lastUsedAccountButton.configuration?.title = formattedNumber
307313
self.lastUsedAccountButton.layoutIfNeeded()
308314
}
309315
}

0 commit comments

Comments
 (0)