-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'trunk' into test-docc-2
- Loading branch information
Showing
22 changed files
with
528 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...UI/ProfileView/Placeholder/PlaceholderDisplayers/AccountButtonsPlaceholderDisplayer.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import UIKit | ||
|
||
/// This ``PlaceholderDisplaying`` implementation is tailored for account buttons. It shows 4 shadow account buttons in the given color. | ||
@MainActor | ||
class AccountButtonsPlaceholderDisplayer: PlaceholderDisplaying { | ||
var placeholderColor: UIColor | ||
private let containerStackView: UIStackView | ||
let isTemporary: Bool | ||
init(containerStackView: UIStackView, color: UIColor, isTemporary: Bool = false) { | ||
self.placeholderColor = color | ||
self.isTemporary = isTemporary | ||
self.containerStackView = containerStackView | ||
} | ||
|
||
func showPlaceholder() { | ||
removeAllArrangedSubviews() | ||
[placeholderView(), placeholderView(), placeholderView(), placeholderView()].forEach(containerStackView.addArrangedSubview) | ||
if isTemporary { | ||
containerStackView.isHidden = false | ||
} | ||
} | ||
|
||
func hidePlaceholder() { | ||
removeAllArrangedSubviews() | ||
if isTemporary { | ||
containerStackView.isHidden = true | ||
} | ||
} | ||
|
||
private func removeAllArrangedSubviews() { | ||
for view in containerStackView.arrangedSubviews { | ||
containerStackView.removeArrangedSubview(view) | ||
view.removeFromSuperview() | ||
} | ||
} | ||
|
||
private func placeholderView() -> UIView { | ||
let view = UIView() | ||
view.translatesAutoresizingMaskIntoConstraints = false | ||
view.backgroundColor = placeholderColor | ||
NSLayoutConstraint.activate([ | ||
view.heightAnchor.constraint(equalToConstant: BaseProfileView.Constants.accountIconLength), | ||
view.widthAnchor.constraint(equalToConstant: BaseProfileView.Constants.accountIconLength), | ||
]) | ||
view.layer.cornerRadius = BaseProfileView.Constants.accountIconLength / 2 | ||
view.clipsToBounds = true | ||
return view | ||
} | ||
|
||
func set(viewColor newColor: UIColor?) { | ||
for arrangedSubview in containerStackView.arrangedSubviews { | ||
arrangedSubview.backgroundColor = newColor | ||
} | ||
} | ||
|
||
func set(layerColor newColor: UIColor?) { | ||
for arrangedSubview in containerStackView.arrangedSubviews { | ||
arrangedSubview.layer.backgroundColor = newColor?.cgColor | ||
} | ||
} | ||
|
||
func prepareForAnimation() {} | ||
} |
45 changes: 45 additions & 0 deletions
45
...I/ProfileView/Placeholder/PlaceholderDisplayers/BackgroundColorPlaceholderDisplayer.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import UIKit | ||
|
||
/// This ``PlaceholderDisplaying`` implementation updates the background color when `showPlaceholder()` is called. | ||
@MainActor | ||
class BackgroundColorPlaceholderDisplayer<T: UIView>: PlaceholderDisplaying { | ||
var placeholderColor: UIColor | ||
let baseView: T | ||
let isTemporary: Bool | ||
let originalBackgroundColor: UIColor | ||
|
||
init(baseView: T, color: UIColor, originalBackgroundColor: UIColor, isTemporary: Bool = false) { | ||
self.placeholderColor = color | ||
self.baseView = baseView | ||
self.isTemporary = isTemporary | ||
self.originalBackgroundColor = originalBackgroundColor | ||
} | ||
|
||
func showPlaceholder() { | ||
if isTemporary { | ||
baseView.isHidden = false | ||
} | ||
set(viewColor: placeholderColor) | ||
} | ||
|
||
func hidePlaceholder() { | ||
set(layerColor: .clear) | ||
set(viewColor: originalBackgroundColor) | ||
if isTemporary { | ||
baseView.isHidden = true | ||
} | ||
} | ||
|
||
func set(viewColor newColor: UIColor?) { | ||
// UIColor can automatically adjust according to `UIUserInterfaceStyle`, but CGColor can't. | ||
// That's why we can't just rely on `layer.backgroundColor`. We need to set this. | ||
baseView.backgroundColor = newColor | ||
} | ||
|
||
func set(layerColor newColor: UIColor?) { | ||
// backgroundColor is not animatable for some UIView subclasses. For example: UILabel. So we need to animate over `layer.backgroundColor`. | ||
baseView.layer.backgroundColor = newColor?.cgColor | ||
} | ||
|
||
func prepareForAnimation() {} | ||
} |
10 changes: 10 additions & 0 deletions
10
.../GravatarUI/ProfileView/Placeholder/PlaceholderDisplayers/LabelPlaceholderDisplayer.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import UIKit | ||
|
||
/// A ``PlaceholderDisplaying`` implementation for a UILabel. | ||
@MainActor | ||
class LabelPlaceholderDisplayer: RectangularPlaceholderDisplayer<UILabel> { | ||
override func prepareForAnimation() { | ||
// If UILabel's backgroundColor is set, the animation won't be visible. So we need to clear it. This is only needed for UILabel so far. | ||
set(viewColor: .clear) | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...rUI/ProfileView/Placeholder/PlaceholderDisplayers/ProfileButtonPlaceholderDisplayer.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import UIKit | ||
|
||
/// A ``PlaceholderDisplaying`` implementation for the "Edit/View profile" button. | ||
@MainActor | ||
class ProfileButtonPlaceholderDisplayer: RectangularPlaceholderDisplayer<UIButton> { | ||
override func showPlaceholder() { | ||
super.showPlaceholder() | ||
baseView.imageView?.isHidden = true | ||
baseView.titleLabel?.isHidden = true | ||
} | ||
|
||
override func hidePlaceholder() { | ||
super.hidePlaceholder() | ||
baseView.imageView?.isHidden = false | ||
baseView.titleLabel?.isHidden = false | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...tarUI/ProfileView/Placeholder/PlaceholderDisplayers/RectangularPlaceholderDisplayer.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import UIKit | ||
|
||
/// A ``PlaceholderDisplaying`` implementation that Inherits ``BackgroundColorPlaceholderDisplayer`. | ||
/// In addition to ``BackgroundColorPlaceholderDisplayer``, this gives a size to the ui element and rounds its corners a bit when `showPlaceholder()` is | ||
/// called. | ||
@MainActor | ||
class RectangularPlaceholderDisplayer<T: UIView>: BackgroundColorPlaceholderDisplayer<T> { | ||
private let cornerRadius: CGFloat | ||
private let height: CGFloat | ||
private let widthRatioToParent: CGFloat | ||
private var layoutConstraints: [NSLayoutConstraint] = [] | ||
private var isShowing: Bool = false | ||
private var originalCornerRadius: CGFloat | ||
|
||
init( | ||
baseView: T, | ||
color: UIColor, | ||
originalBackgroundColor: UIColor = .clear, | ||
cornerRadius: CGFloat, | ||
height: CGFloat, | ||
widthRatioToParent: CGFloat, | ||
isTemporary: Bool = false | ||
) { | ||
self.cornerRadius = cornerRadius | ||
self.height = height | ||
self.widthRatioToParent = widthRatioToParent | ||
self.originalCornerRadius = baseView.layer.cornerRadius | ||
super.init(baseView: baseView, color: color, originalBackgroundColor: originalBackgroundColor, isTemporary: isTemporary) | ||
} | ||
|
||
override func showPlaceholder() { | ||
super.showPlaceholder() | ||
guard !isShowing else { return } | ||
// Deactivate existing ones | ||
NSLayoutConstraint.deactivate(layoutConstraints) | ||
layoutConstraints = baseView.turnIntoPlaceholder(cornerRadius: cornerRadius, height: height, widthRatioToParent: widthRatioToParent) | ||
NSLayoutConstraint.activate(layoutConstraints) | ||
isShowing = true | ||
} | ||
|
||
override func hidePlaceholder() { | ||
super.hidePlaceholder() | ||
baseView.resetPlaceholder(cornerRadius: originalCornerRadius) | ||
NSLayoutConstraint.deactivate(layoutConstraints) | ||
layoutConstraints = [] | ||
isShowing = false | ||
} | ||
} |
Oops, something went wrong.