Skip to content

Commit

Permalink
Fix Swiftlint warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Petersson committed Dec 20, 2023
1 parent a5e9e96 commit bcd7850
Show file tree
Hide file tree
Showing 9 changed files with 281 additions and 259 deletions.
16 changes: 12 additions & 4 deletions ios/MullvadVPN.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
58677712290976FB006F721F /* SettingsInteractor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58677711290976FB006F721F /* SettingsInteractor.swift */; };
5867771429097BCD006F721F /* PaymentState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5867771329097BCD006F721F /* PaymentState.swift */; };
5867771629097C5B006F721F /* ProductState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5867771529097C5B006F721F /* ProductState.swift */; };
5868585524054096000B8131 /* AppButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5868585424054096000B8131 /* AppButton.swift */; };
5868585524054096000B8131 /* CustomButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5868585424054096000B8131 /* CustomButton.swift */; };
58695AA02A4ADA9200328DB3 /* TunnelObfuscationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58695A9F2A4ADA9200328DB3 /* TunnelObfuscationTests.swift */; };
58695AA72A4B109F00328DB3 /* TunnelObfuscation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5840231F2A406BF5007B27AC /* TunnelObfuscation.framework */; };
586A0DCB2A20E359006C731C /* MullvadTypes.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 58D223D5294C8E5E0029F5F8 /* MullvadTypes.framework */; };
Expand Down Expand Up @@ -507,6 +507,8 @@
7A3FD1B72AD54ABD0042BEA6 /* AnyTransport.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58BDEB982A98F4ED00F578F2 /* AnyTransport.swift */; };
7A3FD1B82AD54AE60042BEA6 /* TimeServerProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58BDEB9A2A98F58600F578F2 /* TimeServerProxy.swift */; };
7A42DEC92A05164100B209BE /* SettingsInputCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A42DEC82A05164100B209BE /* SettingsInputCell.swift */; };
7A5869952B32E9C700640D27 /* LinkButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A5869942B32E9C700640D27 /* LinkButton.swift */; };
7A5869972B32EA4500640D27 /* AppButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A5869962B32EA4500640D27 /* AppButton.swift */; };
7A6B4F592AB8412E00123853 /* TunnelMonitorTimings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A6B4F582AB8412E00123853 /* TunnelMonitorTimings.swift */; };
7A6F2FA52AFA3CB2006D0856 /* AccountExpiryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A6F2FA42AFA3CB2006D0856 /* AccountExpiryTests.swift */; };
7A6F2FA72AFBB9AE006D0856 /* AccountExpiry.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A6F2FA62AFBB9AE006D0856 /* AccountExpiry.swift */; };
Expand Down Expand Up @@ -1350,7 +1352,7 @@
58677711290976FB006F721F /* SettingsInteractor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsInteractor.swift; sourceTree = "<group>"; };
5867771329097BCD006F721F /* PaymentState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaymentState.swift; sourceTree = "<group>"; };
5867771529097C5B006F721F /* ProductState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductState.swift; sourceTree = "<group>"; };
5868585424054096000B8131 /* AppButton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppButton.swift; sourceTree = "<group>"; };
5868585424054096000B8131 /* CustomButton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomButton.swift; sourceTree = "<group>"; };
58695A9D2A4ADA9100328DB3 /* TunnelObfuscationTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = TunnelObfuscationTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
58695A9F2A4ADA9200328DB3 /* TunnelObfuscationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TunnelObfuscationTests.swift; sourceTree = "<group>"; };
586A95112901321B007BAF2B /* IPv6Endpoint.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IPv6Endpoint.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1646,6 +1648,8 @@
7A3353962AAA0F8600F0A71C /* OperationBlockObserverSupport.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OperationBlockObserverSupport.swift; sourceTree = "<group>"; };
7A3FD1B42AD4465A0042BEA6 /* AppMessageHandlerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppMessageHandlerTests.swift; sourceTree = "<group>"; };
7A42DEC82A05164100B209BE /* SettingsInputCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsInputCell.swift; sourceTree = "<group>"; };
7A5869942B32E9C700640D27 /* LinkButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinkButton.swift; sourceTree = "<group>"; };
7A5869962B32EA4500640D27 /* AppButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppButton.swift; sourceTree = "<group>"; };
7A6B4F582AB8412E00123853 /* TunnelMonitorTimings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TunnelMonitorTimings.swift; sourceTree = "<group>"; };
7A6F2FA42AFA3CB2006D0856 /* AccountExpiryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountExpiryTests.swift; sourceTree = "<group>"; };
7A6F2FA62AFBB9AE006D0856 /* AccountExpiry.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountExpiry.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2302,15 +2306,17 @@
583FE01F29C197ED006E85F9 /* Views */ = {
isa = PBXGroup;
children = (
5868585424054096000B8131 /* AppButton.swift */,
7A5869962B32EA4500640D27 /* AppButton.swift */,
7A9FA1412A2E3306000B728D /* CheckboxView.swift */,
5868585424054096000B8131 /* CustomButton.swift */,
58ACF64C26567A4F00ACE4B7 /* CustomSwitch.swift */,
58ACF64E26567A7100ACE4B7 /* CustomSwitchContainer.swift */,
58293FB025124117005D0BB5 /* CustomTextField.swift */,
58293FB2251241B3005D0BB5 /* CustomTextView.swift */,
5892A45D265FABFF00890742 /* EmptyTableViewHeaderFooterView.swift */,
58FD5BF32428C67600112C88 /* InAppPurchaseButton.swift */,
F03580242A13842C00E5DAFD /* IncreasedHitButton.swift */,
7A5869942B32E9C700640D27 /* LinkButton.swift */,
58F19E34228C15BA00C7710B /* SpinnerActivityIndicatorView.swift */,
E1FD0DF428AA7CE400299DB4 /* StatusActivityView.swift */,
58EF581025D69DB400AEBA94 /* StatusImageView.swift */,
Expand Down Expand Up @@ -4616,6 +4622,7 @@
5846227126E229F20035F7C2 /* StoreSubscription.swift in Sources */,
58421030282D8A3C00F24E46 /* UpdateAccountDataOperation.swift in Sources */,
F0E8E4C92A604E7400ED26A3 /* AccountDeletionInteractor.swift in Sources */,
7A5869952B32E9C700640D27 /* LinkButton.swift in Sources */,
F09A297D2A9F8A9B00EA3B6F /* RedeemVoucherContentView.swift in Sources */,
5803B4B02940A47300C23744 /* TunnelConfiguration.swift in Sources */,
587EB672271451E300123C75 /* PreferencesViewModel.swift in Sources */,
Expand All @@ -4636,6 +4643,7 @@
58A1AA8C23F5584C009F7EA6 /* ConnectionPanelView.swift in Sources */,
5878A27B2909649A0096FC88 /* CustomOverlayRenderer.swift in Sources */,
A91614D62B10B26B00F416EB /* TunnelControlViewModel.swift in Sources */,
7A5869972B32EA4500640D27 /* AppButton.swift in Sources */,
586C0D8F2B03D88100E7CDD7 /* ProxyProtocolConfigurationItemIdentifier.swift in Sources */,
588527B2276B3F0700BAA373 /* LoadTunnelConfigurationOperation.swift in Sources */,
58DFF7D22B0256A300F864E0 /* MarkdownStylingOptions.swift in Sources */,
Expand Down Expand Up @@ -4744,7 +4752,7 @@
5827B0AE2B0F4CBE00CCBBA1 /* ProxyConfigurationViewControllerDelegate.swift in Sources */,
5827B0962B0DB2C100CCBBA1 /* ProxyConfigurationItemIdentifier.swift in Sources */,
586C0D8B2B03D84400E7CDD7 /* AddAccessMethodItemIdentifier.swift in Sources */,
5868585524054096000B8131 /* AppButton.swift in Sources */,
5868585524054096000B8131 /* CustomButton.swift in Sources */,
58E25F812837BBBB002CFB2C /* SceneDelegate.swift in Sources */,
7A1A26492A29D48A00B978AA /* RelayFilterCellFactory.swift in Sources */,
5867771629097C5B006F721F /* ProductState.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class ListAccessMethodViewController: UIViewController, UITableViewDelegate {
addChild(contentController)
contentController.didMove(toParent: self)

interactor.publisher.sink { newElements in
interactor.publisher.sink { _ in
self.updateDataSource(animated: true)
}
.store(in: &cancellables)
Expand Down Expand Up @@ -104,7 +104,7 @@ class ListAccessMethodViewController: UIViewController, UITableViewDelegate {
private func configureDataSource() {
dataSource = UITableViewDiffableDataSource(
tableView: tableView,
cellProvider: { [weak self] tableView, indexPath, itemIdentifier in
cellProvider: { [weak self] _, indexPath, itemIdentifier in
self?.dequeueCell(at: indexPath, itemIdentifier: itemIdentifier)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct SettingsCellFactory: CellFactoryProtocol {
return cell
}

// swiftlint:disable:next function_body_length
func configureCell(_ cell: UITableViewCell, item: SettingsDataSource.Item, indexPath: IndexPath) {
switch item {
case .preferences:
Expand Down
230 changes: 0 additions & 230 deletions ios/MullvadVPN/Views/AppButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,96 +8,6 @@

import UIKit

enum ButtonImageAlignment {
/// Align image at the left edge of the title label
case left

/// Align image at the right edge of the title label
case right

/// Align image at the leading edge of the title label
case leading

/// Align image at the trailing edge of the title label
case trailing

/// Align image at the leading edge of content area
case leadingFixed

/// Align image at the trailing edge of the content area
case trailingFixed

/// Align image at the left edge of the content area
case leftFixed

/// Align image at the right edge of the content area
case rightFixed
}

private extension UIControl.State {
var customButtonTitleColor: UIColor? {
switch self {
case .normal:
return UIColor.AppButton.normalTitleColor
case .disabled:
return UIColor.AppButton.disabledTitleColor.withAlphaComponent(0.5)
case .highlighted:
return UIColor.AppButton.highlightedTitleColor
default:
return nil
}
}
}

/// A subclass that implements the button that visually look like URL links on the web
class LinkButton: CustomButton {
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}

required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}

var titleString: String? {
didSet {
updateAttributedTitle(string: titleString)
}
}

private func commonInit() {
imageAlignment = .trailing
contentHorizontalAlignment = .leading

accessibilityTraits.insert(.link)
}

private func updateAttributedTitle(string: String?) {
let states: [UIControl.State] = [.normal, .highlighted, .disabled]
states.forEach { state in
let attributedTitle = string.flatMap { makeAttributedTitle($0, for: state) }
self.setAttributedTitle(attributedTitle, for: state)
}
}

private func makeAttributedTitle(
_ title: String,
for state: UIControl.State
) -> NSAttributedString {
var attributes: [NSAttributedString.Key: Any] = [
.underlineStyle: NSUnderlineStyle.single.rawValue,
]

if let titleColor = state.customButtonTitleColor {
attributes[.foregroundColor] = titleColor
}

return NSAttributedString(string: title, attributes: attributes)
}
}

/// A subclass that implements action buttons used across the app
class AppButton: CustomButton {
/// Default content insets based on current trait collection.
Expand Down Expand Up @@ -260,146 +170,6 @@ class AppButton: CustomButton {
}
}

/// A custom `UIButton` subclass that implements additional layouts for the image
class CustomButton: UIButton {
var imageAlignment: ButtonImageAlignment = .leading {
didSet {
invalidateIntrinsicContentSize()
}
}

var inlineImageSpacing: CGFloat = 4 {
didSet {
invalidateIntrinsicContentSize()
}
}

override var intrinsicContentSize: CGSize {
var intrinsicSize = super.intrinsicContentSize

// Add spacing between the image and title label in intrinsic size calculation
if let imageSize = currentImage?.size, imageSize.width > 0 {
intrinsicSize.width += inlineImageSpacing
}

return intrinsicSize
}

var effectiveImageAlignment: ButtonImageAlignment {
switch (imageAlignment, effectiveUserInterfaceLayoutDirection) {
case (.left, _),
(.leading, .leftToRight),
(.trailing, .rightToLeft):
return .left

case (.right, _),
(.trailing, .leftToRight),
(.leading, .rightToLeft):
return .right

case (.leftFixed, _),
(.leadingFixed, .leftToRight),
(.trailingFixed, .rightToLeft):
return .leftFixed

case (.rightFixed, _),
(.trailingFixed, .leftToRight),
(.leadingFixed, .rightToLeft):
return .rightFixed

default:
fatalError()
}
}

override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}

required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}

private func commonInit() {
// Align the text color with the tint color which is applied to the image view
if let imageTintColor = UIControl.State.normal.customButtonTitleColor {
tintColor = imageTintColor
}
}

private func computeLayout(forContentRect contentRect: CGRect) -> (CGRect, CGRect) {
var imageRect = super.imageRect(forContentRect: contentRect)
var titleRect = super.titleRect(forContentRect: contentRect)

switch (effectiveContentHorizontalAlignment, effectiveImageAlignment) {
case (.left, .left):
imageRect.origin.x = contentRect.minX
titleRect.origin.x = imageRect.width > 0
? imageRect.maxX + inlineImageSpacing
: contentRect.minX

case (.left, .right):
titleRect.origin.x = contentRect.minX
imageRect.origin.x = titleRect.maxX + inlineImageSpacing

case (.left, .leftFixed):
imageRect.origin.x = contentRect.minX
titleRect.origin.x = imageRect.width > 0
? imageRect.maxX + inlineImageSpacing
: contentRect.minX

case (.left, .rightFixed):
imageRect.origin.x = contentRect.maxX - imageRect.width
titleRect.origin.x = contentRect.minX

case (.center, .leftFixed):
imageRect.origin.x = contentRect.minX
titleRect.origin.x = contentRect.midX - titleRect.width * 0.5

case (.center, .rightFixed):
imageRect.origin.x = contentRect.maxX - imageRect.width
titleRect.origin.x = contentRect.midX - titleRect.width * 0.5

case (.center, .left):
titleRect.origin.x = contentRect.midX - titleRect.width * 0.5
imageRect.origin.x = titleRect.minX - inlineImageSpacing - imageRect.width

case (.center, .right):
titleRect.origin.x = contentRect.midX - titleRect.width * 0.5
imageRect.origin.x = titleRect.maxX + inlineImageSpacing

case (.right, .left):
titleRect.origin.x = contentRect.maxX - titleRect.width
imageRect.origin.x = titleRect.minX - imageRect.width - inlineImageSpacing

case (.right, .leftFixed):
imageRect.origin.x = contentRect.minX
titleRect.origin.x = contentRect.maxX - titleRect.width

case (.right, .rightFixed):
imageRect.origin.x = contentRect.maxX - imageRect.width
titleRect.origin.x = imageRect.width > 0
? imageRect.minX - inlineImageSpacing - titleRect.width
: contentRect.maxX - titleRect.width

default:
fatalError()
}

return (titleRect, imageRect)
}

override func imageRect(forContentRect contentRect: CGRect) -> CGRect {
computeLayout(forContentRect: contentRect).1
}

override func titleRect(forContentRect contentRect: CGRect) -> CGRect {
computeLayout(forContentRect: contentRect).0
}
}

private extension AppButton {
class DynamicAssets {
static let shared = DynamicAssets()
Expand Down
Loading

0 comments on commit bcd7850

Please sign in to comment.