Skip to content

Commit

Permalink
Update to Swift 4.2 (material-components#7166)
Browse files Browse the repository at this point in the history
This PR updates the Swift version to 4.2.

Partially resolves #6874.
  • Loading branch information
andrewoverton authored Apr 17, 2019
1 parent 5d74de1 commit b383721
Show file tree
Hide file tree
Showing 104 changed files with 469 additions and 966 deletions.
4 changes: 2 additions & 2 deletions catalog/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ swift_library(
srcs = native.glob(["MDCCatalog/*.swift"]),
copts = [
"-swift-version",
"3",
"4.2",
],
resources = ["MDCDragons/HeaderView.xib"],
deps = [
Expand Down Expand Up @@ -120,7 +120,7 @@ swift_library(
srcs = native.glob(["MDCDragons/*.swift"]),
copts = [
"-swift-version",
"3",
"4.2",
],
data = glob(["MDCCatalog/Assets.xcassets/*.imageset/**"]),
resources = ["MDCDragons/HeaderView.xib"],
Expand Down
8 changes: 4 additions & 4 deletions catalog/MDCCatalog.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "com.google.mdc-catalog.MDCActionExtension";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.2;
};
name = Debug;
};
Expand All @@ -613,7 +613,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "com.google.mdc-catalog.MDCActionExtension";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.2;
};
name = Release;
};
Expand Down Expand Up @@ -738,7 +738,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "MDCCatalog/MDCCatalog-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.2;
};
name = Debug;
};
Expand All @@ -756,7 +756,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.google.MDCCatalog;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "MDCCatalog/MDCCatalog-Bridging-Header.h";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.2;
};
name = Release;
};
Expand Down
8 changes: 4 additions & 4 deletions catalog/MDCCatalog/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, MDCAppBarNavigationContro
let navigationController = MDCAppBarNavigationController()

func application(_ application: UIApplication, didFinishLaunchingWithOptions
launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.window = MDCCatalogWindow(frame: UIScreen.main.bounds)

// The navigation tree will only take examples that implement
Expand Down Expand Up @@ -66,9 +66,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate, MDCAppBarNavigationContro
return true
}

func themeDidChange(notification: NSNotification) {
@objc func themeDidChange(notification: NSNotification) {
let colorScheme = AppTheme.globalTheme.colorScheme
for viewController in navigationController.childViewControllers {
for viewController in navigationController.children {
guard let appBar = navigationController.appBar(for: viewController) else {
continue
}
Expand Down Expand Up @@ -104,7 +104,7 @@ protocol CatalogAppBarInjectee {
}

extension UINavigationController {
func presentMenu() {
@objc func presentMenu() {
let menuViewController = MDCMenuViewController(style: .plain)
let bottomSheet = MDCBottomSheetController(contentViewController: menuViewController)
self.present(bottomSheet, animated: true, completion: nil)
Expand Down
6 changes: 4 additions & 2 deletions catalog/MDCCatalog/MDCCatalogCollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ class MDCCatalogCollectionViewCell: UICollectionViewCell {
contentView.clipsToBounds = true
contentView.addSubview(tile)
self.isAccessibilityElement = true
self.accessibilityTraits |= UIAccessibilityTraitButton
let rawAccessibilityTraits =
accessibilityTraits.rawValue | UIAccessibilityTraits.button.rawValue
self.accessibilityTraits = UIAccessibilityTraits(rawValue: rawAccessibilityTraits)

updateTheme()

Expand Down Expand Up @@ -85,7 +87,7 @@ class MDCCatalogCollectionViewCell: UICollectionViewCell {
label.textColor = AppTheme.globalTheme.colorScheme.onBackgroundColor
}

func themeDidChange(notification: NSNotification) {
@objc func themeDidChange(notification: NSNotification) {
updateTheme()
}

Expand Down
22 changes: 6 additions & 16 deletions catalog/MDCCatalog/MDCCatalogComponentsController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import MaterialComponents.MaterialTypography

import UIKit

class MDCCatalogComponentsController: UICollectionViewController, MDCInkTouchControllerDelegate {
class MDCCatalogComponentsController: UICollectionViewController, UICollectionViewDelegateFlowLayout, MDCInkTouchControllerDelegate {

fileprivate struct Constants {
static let headerScrollThreshold: CGFloat = 30
Expand Down Expand Up @@ -94,7 +94,7 @@ class MDCCatalogComponentsController: UICollectionViewController, MDCInkTouchCon

title = "Material Components for iOS"

addChildViewController(headerViewController)
addChild(headerViewController)

headerViewController.isTopLayoutGuideAdjustmentEnabled = true
headerViewController.inferTopSafeAreaInsetFromViewController = true
Expand All @@ -115,7 +115,7 @@ class MDCCatalogComponentsController: UICollectionViewController, MDCInkTouchCon
object: nil)
}

func themeDidChange(notification: NSNotification) {
@objc func themeDidChange(notification: NSNotification) {
let colorScheme = AppTheme.globalTheme.colorScheme
MDCFlexibleHeaderColorThemer.applySemanticColorScheme(colorScheme,
to: headerViewController.headerView)
Expand Down Expand Up @@ -198,18 +198,12 @@ class MDCCatalogComponentsController: UICollectionViewController, MDCInkTouchCon
}

view.addSubview(headerViewController.view)
#if swift(>=4.2)
headerViewController.didMove(toParent: self)
#else
headerViewController.didMove(toParentViewController: self)
#endif

collectionView?.accessibilityIdentifier = "collectionView"
#if swift(>=3.2)
if #available(iOS 11.0, *) {
collectionView?.contentInsetAdjustmentBehavior = .always
}
#endif
}

override func viewWillAppear(_ animated: Bool) {
Expand All @@ -223,23 +217,21 @@ class MDCCatalogComponentsController: UICollectionViewController, MDCInkTouchCon
collectionView?.collectionViewLayout.invalidateLayout()
}

override var childViewControllerForStatusBarStyle: UIViewController? {
override var childForStatusBarStyle: UIViewController? {
return headerViewController
}

override var childViewControllerForStatusBarHidden: UIViewController? {
override var childForStatusBarHidden: UIViewController? {
return headerViewController
}

#if swift(>=3.2)
@available(iOS 11, *)
override func viewSafeAreaInsetsDidChange() {
// Re-constraint the title label to account for changes in safeAreaInsets's left and right.
logoLeftPaddingConstraint?.constant = Constants.inset + view.safeAreaInsets.left
menuButtonRightPaddingConstraint?.constant = -1 * (Constants.inset + view.safeAreaInsets.right)
menuTopPaddingConstraint?.constant = Constants.inset + view.safeAreaInsets.top
}
#endif

func setupFlexibleHeaderContentConstraints() {

Expand Down Expand Up @@ -364,14 +356,12 @@ class MDCCatalogComponentsController: UICollectionViewController, MDCInkTouchCon

func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
sizeForItemAt indexPath: IndexPath) -> CGSize {
let dividerWidth: CGFloat = 1
var safeInsets: CGFloat = 0
#if swift(>=3.2)
if #available(iOS 11, *) {
safeInsets = view.safeAreaInsets.left + view.safeAreaInsets.right
}
#endif
var cellWidthHeight: CGFloat

// iPhones have 2 columns in portrait and 3 in landscape
Expand Down
2 changes: 1 addition & 1 deletion catalog/MDCCatalog/MDCCatalogDebugAlert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class MDCCatalogDebugDismissCell: UICollectionViewCell {
label.text = "DISMISS"
label.textAlignment = .center
isAccessibilityElement = true
accessibilityTraits = UIAccessibilityTraitButton
accessibilityTraits = .button
accessibilityLabel = label.text
contentView.addSubview(label)
}
Expand Down
2 changes: 1 addition & 1 deletion catalog/MDCCatalog/MDCCatalogTileView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class MDCCatalogTileView: UIView {
super.init(coder: aDecoder)
}

func themeDidChange(notification: NSNotification) {
@objc func themeDidChange(notification: NSNotification) {
imageCache.removeAllObjects()
}

Expand Down
4 changes: 0 additions & 4 deletions catalog/MDCCatalog/MDCDebugSafeAreaInsetsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,16 @@ class MDCDebugSafeAreaInsetsView: UIView {
fatalError("init(coder:) has not been implemented")
}

#if swift(>=3.2)
override open func safeAreaInsetsDidChange() {
setNeedsLayout()
layoutIfNeeded()
}
#endif

override func layoutSubviews() {
var safeAreaInsets = UIEdgeInsets.zero
#if swift(>=3.2)
if #available(iOS 11, *) {
safeAreaInsets = self.safeAreaInsets
}
#endif

let width = frame.width
let height = frame.height
Expand Down
32 changes: 10 additions & 22 deletions catalog/MDCCatalog/MDCNodeListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class NodeViewTableViewDemoCell: UITableViewCell {

let label = UILabel()

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
textLabel!.font = MDCTypography.subheadFont()
imageView!.image = UIImage(named: "Demo")
Expand Down Expand Up @@ -95,7 +95,7 @@ class NodeViewTableViewPrimaryDemoCell: UITableViewCell {

let containedButton = MDCButton()

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupContainedButton()
}
Expand Down Expand Up @@ -213,14 +213,10 @@ class MDCNodeListViewController: CBCNodeListViewController {
additionalExamplesSectionHeader = createAdditionalExamplesSectionHeader()
self.tableView.backgroundColor = UIColor.white
self.tableView.separatorStyle = .none
self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension
self.tableView.sectionHeaderHeight = UITableView.automaticDimension

var charactersCount = 0
#if swift(>=3.2)
charactersCount = node.title.count
#else
charactersCount = node.title.characters.count
#endif
charactersCount = node.title.count
if charactersCount > 0 {
self.tableView.accessibilityIdentifier = "Table" + node.title
} else {
Expand All @@ -239,7 +235,7 @@ class MDCNodeListViewController: CBCNodeListViewController {
self.navigationController?.setNavigationBarHidden(true, animated: animated)
}

func themeDidChange(notification: NSNotification) {
@objc func themeDidChange(notification: NSNotification) {
setNeedsStatusBarAppearanceUpdate()
self.tableView.reloadData()
}
Expand Down Expand Up @@ -298,7 +294,7 @@ extension MDCNodeListViewController {
sectionTitleLabel.translatesAutoresizingMaskIntoConstraints = false
sectionTitleLabel.text = sectionNames[1]
sectionTitleLabel.numberOfLines = 0
sectionTitleLabel.setContentCompressionResistancePriority(1000, for: .vertical)
sectionTitleLabel.setContentCompressionResistancePriority(.required, for: .vertical)

sectionView.addSubview(lineDivider)
sectionView.addSubview(sectionTitleLabel)
Expand Down Expand Up @@ -368,7 +364,6 @@ extension MDCNodeListViewController {
constant: self.padding).isActive = true
}
// Title Label to Section View
#if swift(>=3.2)
if #available(iOS 11.0, *) {
// Align to the safe area insets.
sectionTitleLabel.leadingAnchor
Expand All @@ -380,9 +375,6 @@ extension MDCNodeListViewController {
} else {
preiOS11Behavior()
}
#else
preiOS11Behavior()
#endif

NSLayoutConstraint(
item: sectionView,
Expand All @@ -405,22 +397,22 @@ extension MDCNodeListViewController {
sectionTitleLabel.translatesAutoresizingMaskIntoConstraints = false
sectionTitleLabel.text = sectionNames[0]
sectionTitleLabel.numberOfLines = 0
sectionTitleLabel.setContentCompressionResistancePriority(1000, for: .vertical)
sectionTitleLabel.setContentCompressionResistancePriority(.required, for: .vertical)
mainSectionHeaderTitleLabel = sectionTitleLabel

let descriptionLabel = UILabel()
descriptionLabel.font = MDCTypography.body1Font()

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = descriptionLineHeight - descriptionLabel.font.lineHeight
let attrs = [NSParagraphStyleAttributeName: paragraphStyle]
let attrs = [NSAttributedString.Key.paragraphStyle: paragraphStyle]

descriptionLabel.attributedText =
NSAttributedString(string:componentDescription, attributes:attrs)
descriptionLabel.alpha = MDCTypography.body1FontOpacity()
descriptionLabel.translatesAutoresizingMaskIntoConstraints = false
descriptionLabel.numberOfLines = 0
descriptionLabel.setContentCompressionResistancePriority(1000, for: .vertical)
descriptionLabel.setContentCompressionResistancePriority(.required, for: .vertical)
mainSectionHeaderDescriptionLabel = descriptionLabel

sectionView.addSubview(sectionTitleLabel)
Expand All @@ -445,7 +437,6 @@ extension MDCNodeListViewController {
multiplier: 1.0,
constant: self.padding).isActive = true
}
#if swift(>=3.2)
if #available(iOS 11.0, *) {
// Align to the safe area insets.
sectionTitleLabel.leadingAnchor
Expand All @@ -457,9 +448,6 @@ extension MDCNodeListViewController {
} else {
preiOS11Behavior()
}
#else
preiOS11Behavior()
#endif

NSLayoutConstraint(
item: sectionTitleLabel,
Expand Down Expand Up @@ -555,7 +543,7 @@ extension MDCNodeListViewController {
return true
}

func primaryDemoButtonClicked () {
@objc func primaryDemoButtonClicked () {
let indexPath = IndexPath(row: 0, section: Section.description.rawValue)
self.tableView(self.tableView, didSelectRowAt: indexPath)
}
Expand Down
4 changes: 2 additions & 2 deletions catalog/MDCDragons.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.google.MDCDragons;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.2;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand All @@ -392,7 +392,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.google.MDCDragons;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.2;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
Expand Down
2 changes: 1 addition & 1 deletion catalog/MDCDragons/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import CatalogByConvention
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
UIApplication.shared.statusBarStyle = .lightContent
let tree = CBCCreateNavigationTree()
Expand Down
Loading

0 comments on commit b383721

Please sign in to comment.