Skip to content

Commit

Permalink
fixup! Add logic to react on connectivity events with banner
Browse files Browse the repository at this point in the history
  • Loading branch information
dmigach committed Apr 14, 2021
1 parent 9052953 commit bfa4aca
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
17 changes: 9 additions & 8 deletions DemoApp/BannerShowingConnectionDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import UIKit
final class BannerShowingConnectionDelegate {
// MARK: - Private Properties

private let navigationController: UINavigationController
private let view: UIView
private let bannerView = BannerView()
private let bannerAppearanceDuration: TimeInterval = 0.5

// MARK: -

init(navigationController: UINavigationController) {
self.navigationController = navigationController
init(showUnder view: UIView) {
self.view = view
setupViews()
}
}
Expand Down Expand Up @@ -47,15 +47,16 @@ private extension BannerShowingConnectionDelegate {
}

func attachToTopViewIfNeeded() {
guard let topView = navigationController.topViewController?.view,
topView != bannerView.superview else { return }
guard bannerView.superview != view else { return }

topView.addSubview(bannerView)
view.addSubview(bannerView)

let guide = topView.safeAreaLayoutGuide
bannerView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate(
[
bannerView.topAnchor.constraint(equalTo: guide.topAnchor)
bannerView.topAnchor.constraint(equalTo: view.bottomAnchor),
bannerView.widthAnchor.constraint(equalTo: view.widthAnchor),
bannerView.heightAnchor.constraint(equalToConstant: 28)
]
)
}
Expand Down
24 changes: 13 additions & 11 deletions DemoApp/BannerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,30 @@ import UIKit
final class BannerView: UIView {
private let label = UILabel()

override func didMoveToSuperview() {
super.didMoveToSuperview()
setUpLayout()
defaultAppearance()
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}

@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func update(text: String) {
label.text = text
}

private func commonInit() {
setUpLayout()
defaultAppearance()
}

private func setUpLayout() {
guard let superview = superview else { return }

addSubview(label)

translatesAutoresizingMaskIntoConstraints = false
label.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate(
[
widthAnchor.constraint(equalTo: superview.widthAnchor),
heightAnchor.constraint(equalToConstant: 28),
label.trailingAnchor.constraint(equalTo: trailingAnchor),
label.leadingAnchor.constraint(equalTo: leadingAnchor),
label.topAnchor.constraint(equalTo: topAnchor),
Expand Down
2 changes: 1 addition & 1 deletion DemoApp/DemoAppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class DemoAppCoordinator {
init(navigationController: UINavigationController) {
self.navigationController = navigationController
connectionDelegate = BannerShowingConnectionDelegate(
navigationController: navigationController
showUnder: navigationController.navigationBar
)
injectActions()
}
Expand Down

0 comments on commit bfa4aca

Please sign in to comment.