From 797c93d1e4caf0088b3e377eb907f0c5500bb907 Mon Sep 17 00:00:00 2001 From: Guido Marucci Blas Date: Wed, 4 Oct 2017 13:08:22 -0300 Subject: [PATCH] Migrate Portal to use Swift 4. --- .../Views/CustomComponent/CustomView.swift | 2 +- Portal.xcodeproj/project.pbxproj | 23 ++++++++++++------- Portal/Middlewares/StatePersistor.swift | 4 +++- Portal/View/Components/Button.swift | 2 +- Portal/View/Components/Collection.swift | 2 +- Portal/View/Components/Label.swift | 6 ++--- Portal/View/Components/NavigationBar.swift | 2 +- Portal/View/Components/Progress.swift | 2 +- Portal/View/Components/Table.swift | 6 ++--- Portal/View/Components/TextField.swift | 8 +++---- Portal/View/Components/TextView.swift | 6 ++--- Portal/View/LayoutEngine.swift | 10 +++++--- Portal/View/UIKit/MessageDispatcher.swift | 3 +-- .../NavigationBarTitleRenderer.swift | 6 ++--- .../UIKit/Renderers/SegmentedRenderer.swift | 12 +++++----- Portal/View/UIKit/UIKitRenderable.swift | 2 +- PortalTests/SegmentedRendererSpec.swift | 4 ++-- 17 files changed, 56 insertions(+), 44 deletions(-) diff --git a/Example/Views/CustomComponent/CustomView.swift b/Example/Views/CustomComponent/CustomView.swift index b3afdb1..8da1abe 100644 --- a/Example/Views/CustomComponent/CustomView.swift +++ b/Example/Views/CustomComponent/CustomView.swift @@ -21,7 +21,7 @@ final class CustomView: UIView { redView.addGestureRecognizer(gestureRecognizer) } - func tapped() { + @objc func tapped() { onTap?() } diff --git a/Portal.xcodeproj/project.pbxproj b/Portal.xcodeproj/project.pbxproj index 988cab6..bfbd49d 100644 --- a/Portal.xcodeproj/project.pbxproj +++ b/Portal.xcodeproj/project.pbxproj @@ -832,12 +832,13 @@ 9D0F333C1E78F81800C65BED = { CreatedOnToolsVersion = 8.2.1; DevelopmentTeam = 7SMPV9L38V; - LastSwiftMigration = 0820; + LastSwiftMigration = 0900; ProvisioningStyle = Automatic; }; 9D0F33451E78F81800C65BED = { CreatedOnToolsVersion = 8.2.1; DevelopmentTeam = 7SMPV9L38V; + LastSwiftMigration = 0900; ProvisioningStyle = Automatic; TestTargetID = 9D7824071E92B9C7004455B1; }; @@ -849,7 +850,7 @@ 9D7824071E92B9C7004455B1 = { CreatedOnToolsVersion = 8.3; DevelopmentTeam = 7SMPV9L38V; - LastSwiftMigration = 0830; + LastSwiftMigration = 0900; ProvisioningStyle = Automatic; }; }; @@ -1238,7 +1239,8 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -1260,7 +1262,8 @@ PRODUCT_BUNDLE_IDENTIFIER = com.syrmo.Portal; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.0; }; name = Release; }; @@ -1277,7 +1280,8 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.syrmo.PortalTests; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/PortalExample.app/PortalExample"; }; name = Debug; @@ -1295,7 +1299,8 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.syrmo.PortalTests; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/PortalExample.app/PortalExample"; }; name = Release; @@ -1355,7 +1360,8 @@ PRODUCT_BUNDLE_IDENTIFIER = me.guidomb.Portal.Example; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -1373,7 +1379,8 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = me.guidomb.Portal.Example; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Default; + SWIFT_VERSION = 4.0; }; name = Release; }; diff --git a/Portal/Middlewares/StatePersistor.swift b/Portal/Middlewares/StatePersistor.swift index a80c13b..4d9a988 100644 --- a/Portal/Middlewares/StatePersistor.swift +++ b/Portal/Middlewares/StatePersistor.swift @@ -41,7 +41,9 @@ public final class StatePersistor< private let dispatchQueue = DispatchQueue(label: "com.Portal.StatePersistor") - public init(serializer: StatePersistorSerializerType, shouldPersist: @escaping TransitionFilter = { _ in true }) { + public init( + serializer: StatePersistorSerializerType, + shouldPersist: @escaping TransitionFilter = { _, _, _ in true }) { self.serializer = serializer self.shouldPersist = shouldPersist } diff --git a/Portal/View/Components/Button.swift b/Portal/View/Components/Button.swift index 7d1e943..3758292 100644 --- a/Portal/View/Components/Button.swift +++ b/Portal/View/Components/Button.swift @@ -16,7 +16,7 @@ public struct ButtonProperties: AutoPropertyDiffable { // sourcery: skipDiff public var onTap: MessageType? - fileprivate init( + public init( text: String? = .none, isActive: Bool = false, icon: Image? = .none, diff --git a/Portal/View/Components/Collection.swift b/Portal/View/Components/Collection.swift index f73d0f5..f2b469a 100644 --- a/Portal/View/Components/Collection.swift +++ b/Portal/View/Components/Collection.swift @@ -159,7 +159,7 @@ public struct CollectionStyleSheet: AutoPropertyDiffable { } public func collectionStyleSheet( - configure: (inout BaseStyleSheet, inout CollectionStyleSheet) -> Void = { _ in }) + configure: (inout BaseStyleSheet, inout CollectionStyleSheet) -> Void = { _, _ in }) -> StyleSheet { var base = BaseStyleSheet() var component = CollectionStyleSheet() diff --git a/Portal/View/Components/Label.swift b/Portal/View/Components/Label.swift index 28eb3da..05062d2 100644 --- a/Portal/View/Components/Label.swift +++ b/Portal/View/Components/Label.swift @@ -13,7 +13,7 @@ public struct LabelProperties: AutoPropertyDiffable { public let text: String public let textAfterLayout: String? - fileprivate init(text: String, textAfterLayout: String?) { + public init(text: String, textAfterLayout: String?) { self.text = text self.textAfterLayout = textAfterLayout } @@ -44,7 +44,7 @@ public func properties( public struct LabelStyleSheet: AutoPropertyDiffable { - static let `default` = StyleSheet(component: LabelStyleSheet()) + public static let `default` = StyleSheet(component: LabelStyleSheet()) public var textColor: Color public var textFont: Font @@ -74,7 +74,7 @@ public struct LabelStyleSheet: AutoPropertyDiffable { } public func labelStyleSheet( - configure: (inout BaseStyleSheet, inout LabelStyleSheet) -> Void = { _ in }) -> StyleSheet { + configure: (inout BaseStyleSheet, inout LabelStyleSheet) -> Void = { _, _ in }) -> StyleSheet { var base = BaseStyleSheet() var component = LabelStyleSheet() configure(&base, &component) diff --git a/Portal/View/Components/NavigationBar.swift b/Portal/View/Components/NavigationBar.swift index 218546e..49ad30e 100644 --- a/Portal/View/Components/NavigationBar.swift +++ b/Portal/View/Components/NavigationBar.swift @@ -133,7 +133,7 @@ public struct NavigationBarStyleSheet { } public func navigationBarStyleSheet( - configure: (inout BaseStyleSheet, inout NavigationBarStyleSheet) -> Void = { _ in } + configure: (inout BaseStyleSheet, inout NavigationBarStyleSheet) -> Void = { _, _ in } ) -> StyleSheet { var base = BaseStyleSheet() var component = NavigationBarStyleSheet() diff --git a/Portal/View/Components/Progress.swift b/Portal/View/Components/Progress.swift index a921840..0adc673 100644 --- a/Portal/View/Components/Progress.swift +++ b/Portal/View/Components/Progress.swift @@ -8,7 +8,7 @@ public struct ProgressCounter { - internal static let initial = ProgressCounter() + public static let initial = ProgressCounter() // sourcery: ignoreInChangeSet public var partial: UInt diff --git a/Portal/View/Components/Table.swift b/Portal/View/Components/Table.swift index 1ce4a70..45c9c63 100644 --- a/Portal/View/Components/Table.swift +++ b/Portal/View/Components/Table.swift @@ -26,7 +26,7 @@ public struct TableProperties: AutoPropertyDiffable { // sourcery: skipDiff public var refresh: RefreshProperties? - fileprivate init( + public init( items: [TableItemProperties] = [], showsVerticalScrollIndicator: Bool = true, showsHorizontalScrollIndicator: Bool = true, @@ -58,7 +58,7 @@ public struct TableItemProperties { public let selectionStyle: TableItemSelectionStyle public let renderer: Renderer - fileprivate init( + public init( height: UInt, onTap: MessageType?, selectionStyle: TableItemSelectionStyle, @@ -155,7 +155,7 @@ public struct TableStyleSheet: AutoPropertyDiffable { } public func tableStyleSheet( - configure: (inout BaseStyleSheet, inout TableStyleSheet) -> Void = { _ in }) -> StyleSheet { + configure: (inout BaseStyleSheet, inout TableStyleSheet) -> Void = { _, _ in }) -> StyleSheet { var base = BaseStyleSheet() var component = TableStyleSheet() configure(&base, &component) diff --git a/Portal/View/Components/TextField.swift b/Portal/View/Components/TextField.swift index e20679a..49b4b67 100644 --- a/Portal/View/Components/TextField.swift +++ b/Portal/View/Components/TextField.swift @@ -17,7 +17,7 @@ public struct TextFieldProperties: AutoPropertyDiffable { // sourcery: skipDiff public var onEvents: TextFieldEvents = TextFieldEvents() - fileprivate init( + public init( text: String? = .none, placeholder: String? = .none, isSecureTextEntry: Bool = false, @@ -48,7 +48,7 @@ public struct TextFieldEvents { public var onEditingChanged: ((String) -> MessageType)? public var onEditingEnd: ((String) -> MessageType)? - fileprivate init( + public init( onEditingBegin: ((String) -> MessageType)? = .none, onEditingChanged: ((String) -> MessageType)? = .none, onEditingEnd: ((String) -> MessageType)? = .none @@ -95,7 +95,7 @@ public func textFieldEvents( public struct TextFieldStyleSheet: AutoPropertyDiffable { - static let `default` = StyleSheet(component: TextFieldStyleSheet()) + public static let `default` = StyleSheet(component: TextFieldStyleSheet()) public var textColor: Color public var textFont: Font @@ -115,7 +115,7 @@ public struct TextFieldStyleSheet: AutoPropertyDiffable { } public func textFieldStyleSheet( - configure: (inout BaseStyleSheet, inout TextFieldStyleSheet) -> Void = { _ in } + configure: (inout BaseStyleSheet, inout TextFieldStyleSheet) -> Void = { _, _ in } ) -> StyleSheet { var base = BaseStyleSheet() var component = TextFieldStyleSheet() diff --git a/Portal/View/Components/TextView.swift b/Portal/View/Components/TextView.swift index e1aefcb..318d8ed 100644 --- a/Portal/View/Components/TextView.swift +++ b/Portal/View/Components/TextView.swift @@ -28,7 +28,7 @@ public struct TextViewProperties: AutoPropertyDiffable { public var isScrollEnabled: Bool public var isEditable: Bool - fileprivate init(text: Text = .regular(""), isScrollEnabled: Bool = false, isEditable: Bool = false) { + public init(text: Text = .regular(""), isScrollEnabled: Bool = false, isEditable: Bool = false) { self.text = text self.isScrollEnabled = isScrollEnabled self.isEditable = isEditable @@ -47,7 +47,7 @@ public func properties( public struct TextViewStyleSheet: AutoPropertyDiffable { - static let `default` = StyleSheet(component: TextViewStyleSheet()) + public static let `default` = StyleSheet(component: TextViewStyleSheet()) public var textColor: Color public var textFont: Font @@ -68,7 +68,7 @@ public struct TextViewStyleSheet: AutoPropertyDiffable { } public func textViewStyleSheet( - configure: (inout BaseStyleSheet, inout TextViewStyleSheet) -> Void = { _ in } + configure: (inout BaseStyleSheet, inout TextViewStyleSheet) -> Void = { _, _ in } ) -> StyleSheet { var base = BaseStyleSheet() var component = TextViewStyleSheet() diff --git a/Portal/View/LayoutEngine.swift b/Portal/View/LayoutEngine.swift index 1eab5bf..4b24e8f 100644 --- a/Portal/View/LayoutEngine.swift +++ b/Portal/View/LayoutEngine.swift @@ -16,9 +16,13 @@ public protocol LayoutEngine { } -internal struct YogaLayoutEngine: LayoutEngine { +public struct YogaLayoutEngine: LayoutEngine { + + public init() { + + } - func executeLayout(for view: UIView) { + public func executeLayout(for view: UIView) { view.yoga.isEnabled = true view.yoga.width = view.bounds.size.width view.yoga.height = view.bounds.size.height @@ -26,7 +30,7 @@ internal struct YogaLayoutEngine: LayoutEngine { } // swiftlint:disable cyclomatic_complexity - func apply(changeSet: [Layout.Property], to view: UIView) { + public func apply(changeSet: [Layout.Property], to view: UIView) { view.yoga.isEnabled = true for property in changeSet { diff --git a/Portal/View/UIKit/MessageDispatcher.swift b/Portal/View/UIKit/MessageDispatcher.swift index 4541026..b5c2871 100644 --- a/Portal/View/UIKit/MessageDispatcher.swift +++ b/Portal/View/UIKit/MessageDispatcher.swift @@ -23,8 +23,7 @@ internal final class MessageDispatcher: NSObject { self.sender2Message = sender2Message } - @objc - internal func dispatch(sender: Any) { + @objc internal func dispatch(sender: Any) { guard let message = sender2Message(sender) else { return } mailbox.dispatch(message: message) } diff --git a/Portal/View/UIKit/Renderers/NavigationBarTitleRenderer.swift b/Portal/View/UIKit/Renderers/NavigationBarTitleRenderer.swift index 4391a6d..bdf4fcd 100644 --- a/Portal/View/UIKit/Renderers/NavigationBarTitleRenderer.swift +++ b/Portal/View/UIKit/Renderers/NavigationBarTitleRenderer.swift @@ -58,12 +58,12 @@ extension UINavigationBar { self.barTintColor = style.base.backgroundColor.asUIColor self.tintColor = style.component.tintColor.asUIColor self.isTranslucent = style.component.isTranslucent - var titleTextAttributes: [String : Any] = [ - NSForegroundColorAttributeName: style.component.titleTextColor.asUIColor + var titleTextAttributes: [NSAttributedStringKey : Any] = [ + .foregroundColor: style.component.titleTextColor.asUIColor ] let font = style.component.titleTextFont let fontSize = style.component.titleTextSize - font.uiFont(withSize: fontSize) |> { titleTextAttributes[NSFontAttributeName] = $0 } + font.uiFont(withSize: fontSize) |> { titleTextAttributes[.font] = $0 } self.titleTextAttributes = titleTextAttributes } diff --git a/Portal/View/UIKit/Renderers/SegmentedRenderer.swift b/Portal/View/UIKit/Renderers/SegmentedRenderer.swift index c969937..0e80efc 100644 --- a/Portal/View/UIKit/Renderers/SegmentedRenderer.swift +++ b/Portal/View/UIKit/Renderers/SegmentedRenderer.swift @@ -56,9 +56,9 @@ fileprivate extension UISegmentedControl { } fileprivate func apply(changeSet: [SegmentedStyleSheet.Property]) { - let attributes = titleTextAttributes(for: .normal) as? [String : Any] - let font = attributes?[NSFontAttributeName] as? UIFont - let color = attributes?[NSForegroundColorAttributeName] as? UIColor + let attributes = titleTextAttributes(for: .normal) as? [NSAttributedStringKey : Any] + let font = attributes?[.font] as? UIFont + let color = attributes?[.foregroundColor] as? UIColor var fontName: String? = font?.fontName var fontSize: CGFloat? = font?.pointSize @@ -85,11 +85,11 @@ fileprivate extension UISegmentedControl { } fileprivate func setFont(name: String?, size: CGFloat?, color: UIColor?) { - var dictionary = [String: Any]() + var dictionary = [NSAttributedStringKey: Any]() let font = UIFont(name: name ?? UIFont.systemFont(ofSize: 15).fontName, size: size ?? CGFloat(defaultSegmentedFontSize)) - color |> { dictionary[NSForegroundColorAttributeName] = $0 } - font |> { dictionary[NSFontAttributeName] = $0 } + color |> { dictionary[.foregroundColor] = $0 } + font |> { dictionary[.font] = $0 } setTitleTextAttributes(dictionary, for: .normal) } diff --git a/Portal/View/UIKit/UIKitRenderable.swift b/Portal/View/UIKit/UIKitRenderable.swift index cb9c820..fd878e0 100644 --- a/Portal/View/UIKit/UIKitRenderable.swift +++ b/Portal/View/UIKit/UIKitRenderable.swift @@ -58,7 +58,7 @@ extension String { func maximumFontSize(forWidth width: CGFloat, font: UIFont) -> CGFloat { let text = self as NSString - let minimumBoundingRect = text.size(attributes: [NSFontAttributeName: font]) + let minimumBoundingRect = text.size(withAttributes: [.font: font]) return width * font.pointSize / minimumBoundingRect.width } diff --git a/PortalTests/SegmentedRendererSpec.swift b/PortalTests/SegmentedRendererSpec.swift index e19b5eb..e8c25d8 100644 --- a/PortalTests/SegmentedRendererSpec.swift +++ b/PortalTests/SegmentedRendererSpec.swift @@ -131,7 +131,7 @@ class SegmentedRendererSpec: QuickSpec { _ = segmented.apply(changeSet: changeSet, layoutEngine: layoutEngine) let attributes = segmented.titleTextAttributes(for: .normal)! - let color = attributes[NSForegroundColorAttributeName] as! UIColor + let color = attributes[NSAttributedStringKey.foregroundColor] as! UIColor expect(color).to(equal(UIColor.blue)) } @@ -140,7 +140,7 @@ class SegmentedRendererSpec: QuickSpec { _ = segmented.apply(changeSet: changeSet, layoutEngine: layoutEngine) let attributes = segmented.titleTextAttributes(for: .normal)! - let font = attributes[NSFontAttributeName] as! UIFont + let font = attributes[NSAttributedStringKey.font] as! UIFont expect(font).to(equal(UIFont(name: "Helvetica", size: 15))) }