From eee1ba0436c4aaff2427b53cedbcb441e379bf2d Mon Sep 17 00:00:00 2001 From: Liam Date: Tue, 12 Mar 2024 22:04:09 +0800 Subject: [PATCH] refactor: Code structure optimization. --- .../ApplicationDetailViewController.swift | 18 +++++++++--------- Example/Colors/Color.swift | 2 +- Example/Colors/SearchViewController.swift | 2 +- Sources/Internal/Impl.swift | 16 ++++++++-------- Sources/Internal/View.swift | 4 ++-- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/Example/Applications/ApplicationDetailViewController.swift b/Example/Applications/ApplicationDetailViewController.swift index fb6ad09..eef2c91 100644 --- a/Example/Applications/ApplicationDetailViewController.swift +++ b/Example/Applications/ApplicationDetailViewController.swift @@ -163,7 +163,7 @@ class ApplicationDetailViewController: UITableViewController { imageName = "header_podcasts" } - if let imageName = imageName { + if let imageName { let image = UIImage(named: imageName) let imageView = UIImageView(image: image) imageView.isUserInteractionEnabled = true @@ -172,7 +172,7 @@ class ApplicationDetailViewController: UITableViewController { imageView.addGestureRecognizer(tapGesture) var frame = view.bounds - if let image = image { + if let image { frame.size.height = image.size.height } let headerView = UIView(frame: frame) @@ -293,8 +293,8 @@ extension ApplicationDetailViewController: BlankSlateDataSource, BlankSlateDeleg return nil } - if let font = font { attributes[.font] = font } - if let textColor = textColor { attributes[.foregroundColor] = textColor } + if let font { attributes[.font] = font } + if let textColor { attributes[.foregroundColor] = textColor } return NSAttributedString(string: text, attributes: attributes) } @@ -407,10 +407,10 @@ extension ApplicationDetailViewController: BlankSlateDataSource, BlankSlateDeleg return nil } - if let font = font { + if let font { attributes[.font] = font } - if let textColor = textColor { + if let textColor { attributes[.foregroundColor] = textColor } attributes[.paragraphStyle] = paragraph @@ -485,8 +485,8 @@ extension ApplicationDetailViewController: BlankSlateDataSource, BlankSlateDeleg } var attributes: [NSAttributedString.Key: Any] = [:] - if let font = font { attributes[.font] = font } - if let textColor = textColor { attributes[.foregroundColor] = textColor } + if let font { attributes[.font] = font } + if let textColor { attributes[.foregroundColor] = textColor } return NSAttributedString(string: text, attributes: attributes) } @@ -546,7 +546,7 @@ extension ApplicationDetailViewController: BlankSlateDataSource, BlankSlateDeleg func offset(forBlankSlate scrollView: UIScrollView) -> CGPoint { if application.type == .kickstarter { var offset = UIApplication.shared.statusBarFrame.height - if let navigationController = navigationController { + if let navigationController { offset += navigationController.navigationBar.frame.height } return CGPoint(x: 0, y: -offset) diff --git a/Example/Colors/Color.swift b/Example/Colors/Color.swift index 999d850..b0361b8 100644 --- a/Example/Colors/Color.swift +++ b/Example/Colors/Color.swift @@ -24,7 +24,7 @@ struct Color: Equatable { } static func roundImage(for size: CGSize, with color: UIColor?) -> UIImage? { - guard let color = color else { return nil } + guard let color else { return nil } let bounds = CGRect(origin: .zero, size: size) diff --git a/Example/Colors/SearchViewController.swift b/Example/Colors/SearchViewController.swift index 63e0a33..c7a7de2 100644 --- a/Example/Colors/SearchViewController.swift +++ b/Example/Colors/SearchViewController.swift @@ -42,7 +42,7 @@ class SearchViewController: UIViewController { self.searchController = searchController searchResultsController.searchController = searchController searchResultsController.searchResult = { [weak self](selectedColor) in - guard let `self` = self else { return } + guard let self else { return } self.selectedColor = selectedColor self.updateContent() } diff --git a/Sources/Internal/Impl.swift b/Sources/Internal/Impl.swift index 412ae1e..d201d1e 100644 --- a/Sources/Internal/Impl.swift +++ b/Sources/Internal/Impl.swift @@ -49,7 +49,7 @@ extension UIScrollView { set { objc_setAssociatedObject(self, &kBlankSlateStatusKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) - guard let newValue = newValue, newValue != .loading else { + guard let newValue, newValue != .loading else { return reloadBlankSlateIfNeeded() } @@ -70,7 +70,7 @@ extension UIScrollView { } func reloadBlankSlateIfNeeded() { - guard let blankSlateDataSource = blankSlateDataSource else { + guard let blankSlateDataSource else { dismissBlankSlateIfNeeded() return } @@ -167,7 +167,7 @@ extension UIScrollView { func dismissBlankSlateIfNeeded() { var isBlankSlateVisible = false - if let blankSlateView = blankSlateView { + if let blankSlateView { isBlankSlateVisible = true blankSlateDelegate?.blankSlateWillDisappear(self) // Notifies that the empty dataset view will disappear @@ -213,11 +213,11 @@ extension UIScrollView { view.autoresizingMask = [.flexibleWidth, .flexibleHeight] view.isHidden = true view.isTouchAllowed = { [weak self] in - guard let `self` = self, let blankSlateDelegate = blankSlateDelegate else { return true } + guard let self, let blankSlateDelegate = blankSlateDelegate else { return true } return blankSlateDelegate.blankSlateShouldAllowTouch(self) } view.shouldRecognizeSimultaneously = { [weak self](other, of) in - guard let `self` = self, let blankSlateDelegate = blankSlateDelegate else { return false } + guard let self, let blankSlateDelegate = blankSlateDelegate else { return false } if let scrollView = blankSlateDelegate as? UIScrollView, scrollView == self { return false } @@ -228,13 +228,13 @@ extension UIScrollView { return false } view.didTap = { [weak self] in - guard let `self` = self, let blankSlateDelegate = blankSlateDelegate else { return } + guard let self, let blankSlateDelegate = blankSlateDelegate else { return } if let button = $0 as? UIButton { return blankSlateDelegate.blankSlate(self, didTapButton: button) } blankSlateDelegate.blankSlate(self, didTapView: $0) } - self.blankSlateView = view + blankSlateView = view return view } @@ -281,7 +281,7 @@ private class WeakObject { private(set) weak var value: AnyObject? init?(_ value: AnyObject?) { - guard let value = value else { return nil } + guard let value else { return nil } self.value = value } diff --git a/Sources/Internal/View.swift b/Sources/Internal/View.swift index 84a34a1..9bab1c0 100644 --- a/Sources/Internal/View.swift +++ b/Sources/Internal/View.swift @@ -208,7 +208,7 @@ extension BlankSlate { let view = element.0 let layout = element.1 - if let previous = previous { // Previous view + if let previous { // Previous view constraints.append(view.topAnchor.constraint(equalTo: previous.0.bottomAnchor, constant: layout.edgeInsets.top)) } else { // First view constraints.append(view.topAnchor.constraint(equalTo: contentView.topAnchor, constant: layout.edgeInsets.top)) @@ -245,7 +245,7 @@ extension BlankSlate { // MARK: - UIGestureRecognizerDelegate override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool { - if let isTouchAllowed = isTouchAllowed, isEqual(gestureRecognizer.view) { + if let isTouchAllowed, isEqual(gestureRecognizer.view) { return isTouchAllowed() } return super.gestureRecognizerShouldBegin(gestureRecognizer)