Skip to content

Commit

Permalink
WIP Implement connetion status banner
Browse files Browse the repository at this point in the history
  • Loading branch information
dmigach committed Apr 9, 2021
1 parent ea0155e commit 4a0b0a3
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 30 deletions.
73 changes: 73 additions & 0 deletions DemoApp/BannerShowingConnectionDelegate.swift
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)
]
)
}
}
47 changes: 47 additions & 0 deletions DemoApp/BannerView.swift
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
}
}
28 changes: 0 additions & 28 deletions DemoApp/ChatPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,6 @@ import StreamChat
import StreamChatUI
import UIKit

extension UIViewController {
// TODO: Where to put this???
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

let chatNavigationController = UINavigationController(rootViewController: chatList)

UIView.transition(with: view.window!, duration: 0.3, options: .transitionFlipFromRight, animations: {
self.view.window!.rootViewController = chatNavigationController
})
}
}

class DemoChatChannelListRouter: _ChatChannelListRouter<NoExtraData> {
override func openCreateNewChannel() {
let storyboard = UIStoryboard(name: "Main", bundle: .main)
Expand Down
53 changes: 53 additions & 0 deletions DemoApp/DemoAppCoordinator.swift
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
})
}
}
2 changes: 1 addition & 1 deletion DemoApp/LoginViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,6 @@ extension LoginViewController: UITableViewDelegate, UITableViewDataSource {
return
}

presentChat(userCredentials: builtInUsers[indexPath.row])
DemoAppCoordinator.shared.presentChat(userCredentials: builtInUsers[indexPath.row])
}
}
5 changes: 4 additions & 1 deletion DemoApp/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ extension UIColor {

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let scene = scene as? UIWindowScene else { return }
scene.windows.forEach { $0.tintColor = .streamBlue }
DemoAppCoordinator.shared = (scene.windows.first?.rootViewController as? UINavigationController).map {
.init(navigationController: $0)
}
}

func sceneDidDisconnect(_ scene: UIScene) {}
Expand Down
12 changes: 12 additions & 0 deletions StreamChat.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
22C2359A259CA87B00DC805A /* Animation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22C23599259CA87B00DC805A /* Animation.swift */; };
22CAFA7625CAE278005935D9 /* RawJSON_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22CAFA7525CAE278005935D9 /* RawJSON_Tests.swift */; };
22FF4365256E943F00133910 /* ChatMessageComposerSuggestionsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22FF4364256E943F00133910 /* ChatMessageComposerSuggestionsViewController.swift */; };
6428DD5526201DCC0065DA1D /* BannerShowingConnectionDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6428DD5426201DCC0065DA1D /* BannerShowingConnectionDelegate.swift */; };
647F66D5261E22C200111B19 /* BannerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 647F66D4261E22C200111B19 /* BannerView.swift */; };
648EC576261EF9D400B8F05F /* DemoAppCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 648EC575261EF9D400B8F05F /* DemoAppCoordinator.swift */; };
6971257E260CA503003C7B47 /* NSManagedObjectContext+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6971256C260CA4CB003C7B47 /* NSManagedObjectContext+Extensions.swift */; };
697C6F90260CFA37000E9023 /* Deprecations.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69712522260BC9B4003C7B47 /* Deprecations.swift */; };
780057FF25F6353600D21095 /* ChatChannel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 780057F625F634FC00D21095 /* ChatChannel.swift */; };
Expand Down Expand Up @@ -1167,6 +1170,9 @@
22C23599259CA87B00DC805A /* Animation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Animation.swift; sourceTree = "<group>"; };
22CAFA7525CAE278005935D9 /* RawJSON_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RawJSON_Tests.swift; sourceTree = "<group>"; };
22FF4364256E943F00133910 /* ChatMessageComposerSuggestionsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatMessageComposerSuggestionsViewController.swift; sourceTree = "<group>"; };
6428DD5426201DCC0065DA1D /* BannerShowingConnectionDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BannerShowingConnectionDelegate.swift; sourceTree = "<group>"; };
647F66D4261E22C200111B19 /* BannerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BannerView.swift; sourceTree = "<group>"; };
648EC575261EF9D400B8F05F /* DemoAppCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DemoAppCoordinator.swift; sourceTree = "<group>"; };
69712522260BC9B4003C7B47 /* Deprecations.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Deprecations.swift; sourceTree = "<group>"; };
6971256C260CA4CB003C7B47 /* NSManagedObjectContext+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSManagedObjectContext+Extensions.swift"; sourceTree = "<group>"; };
698E2A3425F7C8AF00ED9CCC /* APIPathConvertible.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIPathConvertible.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2540,6 +2546,8 @@
792DDA5D256FB69E001DB91B /* LoginViewController.swift */,
7933064825712C8B00FBB586 /* DemoUsers.swift */,
7933060A256FF94800FBB586 /* ChatPresenter.swift */,
648EC575261EF9D400B8F05F /* DemoAppCoordinator.swift */,
6428DD5426201DCC0065DA1D /* BannerShowingConnectionDelegate.swift */,
79330603256FEBE600FBB586 /* AdvancedOptionsViewController.swift */,
792DDA5F256FB69E001DB91B /* Main.storyboard */,
792DDA62256FB69F001DB91B /* Assets.xcassets */,
Expand All @@ -2548,6 +2556,7 @@
792DDAA025711AF2001DB91B /* CreateChatViewController.swift */,
792DDAA725753BEA001DB91B /* CreateGroupViewController.swift */,
794E20F42577DF4D00790DAB /* NameGroupViewController.swift */,
647F66D4261E22C200111B19 /* BannerView.swift */,
);
path = DemoApp;
sourceTree = "<group>";
Expand Down Expand Up @@ -4981,6 +4990,9 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
6428DD5526201DCC0065DA1D /* BannerShowingConnectionDelegate.swift in Sources */,
647F66D5261E22C200111B19 /* BannerView.swift in Sources */,
648EC576261EF9D400B8F05F /* DemoAppCoordinator.swift in Sources */,
792DDA5E256FB69E001DB91B /* LoginViewController.swift in Sources */,
792DDAA825753BEA001DB91B /* CreateGroupViewController.swift in Sources */,
7933064925712C8B00FBB586 /* DemoUsers.swift in Sources */,
Expand Down

0 comments on commit 4a0b0a3

Please sign in to comment.