-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP Implement connetion status banner
- Loading branch information
Showing
7 changed files
with
190 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// | ||
// Copyright © 2021 Stream.io Inc. All rights reserved. | ||
// | ||
|
||
import StreamChat | ||
import UIKit | ||
|
||
final class BannerShowingConnectionDelegate { | ||
// MARK: - Private Properties | ||
|
||
private let navigationController: UINavigationController | ||
private var connectionEstablishmentTime: Date? | ||
|
||
// MARK: - | ||
|
||
init(navigationController: UINavigationController) { | ||
self.navigationController = navigationController | ||
} | ||
} | ||
|
||
// MARK: - ChatConnectionControllerDelegate | ||
|
||
extension BannerShowingConnectionDelegate: ChatConnectionControllerDelegate { | ||
public func connectionController(_ controller: ChatConnectionController, didUpdateConnectionStatus status: ConnectionStatus) { | ||
switch status { | ||
case .connected: | ||
connectionEstablishmentTime = Date() | ||
case .disconnecting: | ||
break | ||
case .connecting: | ||
if let time = connectionEstablishmentTime { | ||
let elapsedTime = time.distance(to: Date()) | ||
if elapsedTime > 5 { | ||
// TODO: We need to reset the property we make desicion on whether to show the banner or not | ||
showBanner() | ||
} | ||
} | ||
case .disconnected, .initialized: | ||
break | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Private Methods | ||
|
||
private extension BannerShowingConnectionDelegate { | ||
func showBanner() { | ||
guard let view = navigationController.topViewController?.view else { return } | ||
let bannerView = BannerView() | ||
view.addSubview(bannerView) | ||
bannerView.alpha = 0 | ||
bannerView.update(text: "Reconnecting...") | ||
|
||
UIView.animate(withDuration: 0.5) { | ||
bannerView.alpha = 1 | ||
} | ||
|
||
DispatchQueue.main.asyncAfter(deadline: .now() + 3) { | ||
UIView.animate(withDuration: 1) { | ||
bannerView.alpha = 0 | ||
} completion: { _ in | ||
bannerView.removeFromSuperview() | ||
} | ||
} | ||
|
||
let guide = view.safeAreaLayoutGuide | ||
NSLayoutConstraint.activate( | ||
[ | ||
bannerView.topAnchor.constraint(equalTo: guide.topAnchor) | ||
] | ||
) | ||
} | ||
} |
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,47 @@ | ||
// | ||
// Copyright © 2021 Stream.io Inc. All rights reserved. | ||
// | ||
|
||
import StreamChatUI | ||
import UIKit | ||
|
||
final class BannerView: UIView { | ||
private let label = UILabel() | ||
|
||
override func didMoveToSuperview() { | ||
super.didMoveToSuperview() | ||
setUpLayout() | ||
defaultAppearance() | ||
} | ||
|
||
func update(text: String) { | ||
label.text = text | ||
} | ||
|
||
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), | ||
label.bottomAnchor.constraint(equalTo: bottomAnchor) | ||
] | ||
) | ||
} | ||
|
||
private func defaultAppearance() { | ||
backgroundColor = UIConfig.default.colorPalette.border2.withAlphaComponent(0.9) | ||
label.textAlignment = .center | ||
label.font = UIConfig.default.font.body | ||
label.textColor = UIConfig.default.colorPalette.popoverBackground | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// | ||
// Copyright © 2021 Stream.io Inc. All rights reserved. | ||
// | ||
|
||
import StreamChat | ||
import StreamChatUI | ||
import UIKit | ||
|
||
final class DemoAppCoordinator { | ||
static var shared: DemoAppCoordinator! | ||
|
||
private var connectionController: ChatConnectionController? | ||
private let navigationController: UINavigationController | ||
private let connectionDelegate: BannerShowingConnectionDelegate | ||
|
||
init(navigationController: UINavigationController) { | ||
self.navigationController = navigationController | ||
connectionDelegate = BannerShowingConnectionDelegate( | ||
navigationController: navigationController | ||
) | ||
} | ||
|
||
func presentChat(userCredentials: UserCredentials) { | ||
LogConfig.level = .error | ||
|
||
// Create a token | ||
let token = try! Token(rawValue: userCredentials.token) | ||
|
||
// Create client | ||
let config = ChatClientConfig(apiKey: .init(userCredentials.apiKey)) | ||
let client = ChatClient(config: config, tokenProvider: .static(token)) | ||
|
||
// Config | ||
UIConfig.default.navigation.channelListRouter = DemoChatChannelListRouter.self | ||
|
||
// Channels with the current user | ||
let controller = client.channelListController(query: .init(filter: .containMembers(userIds: [userCredentials.id]))) | ||
let chatList = ChatChannelListVC() | ||
chatList.controller = controller | ||
|
||
connectionController = client.connectionController() | ||
connectionController?.delegate = connectionDelegate | ||
|
||
navigationController.viewControllers = [chatList] | ||
navigationController.isNavigationBarHidden = false | ||
|
||
let window = navigationController.view.window! | ||
|
||
UIView.transition(with: window, duration: 0.3, options: .transitionFlipFromRight, animations: { | ||
window.rootViewController = self.navigationController | ||
}) | ||
} | ||
} |
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