|
| 1 | +// |
| 2 | +// ChatFlowCoordinator.swift |
| 3 | +// |
| 4 | +// PubNub Real-time Cloud-Hosted Push API and Push Notification Client Frameworks |
| 5 | +// Copyright © 2022 PubNub Inc. |
| 6 | +// https://www.pubnub.com/ |
| 7 | +// https://www.pubnub.com/terms |
| 8 | +// |
| 9 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +// of this software and associated documentation files (the "Software"), to deal |
| 11 | +// in the Software without restriction, including without limitation the rights |
| 12 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +// copies of the Software, and to permit persons to whom the Software is |
| 14 | +// furnished to do so, subject to the following conditions: |
| 15 | +// |
| 16 | +// The above copyright notice and this permission notice shall be included in |
| 17 | +// all copies or substantial portions of the Software. |
| 18 | +// |
| 19 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | +// THE SOFTWARE. |
| 26 | +// |
| 27 | + |
| 28 | +import Foundation |
| 29 | +import UIKit |
| 30 | +import PubNub |
| 31 | +import PubNubChat |
| 32 | +import PubNubChatComponents |
| 33 | + |
| 34 | +enum PatientError: Error { |
| 35 | + case membershipFetch(String) |
| 36 | +} |
| 37 | + |
| 38 | +protocol ChatFlowCoordinator { |
| 39 | + func start() throws |
| 40 | +} |
| 41 | + |
| 42 | +final class DefaultChatFlowCoordinator: ChatFlowCoordinator { |
| 43 | + private let navigationController: UINavigationController |
| 44 | + private let provider: TelehealthChatProvider |
| 45 | + |
| 46 | + init(navigationController: UINavigationController, provider: TelehealthChatProvider) { |
| 47 | + self.navigationController = navigationController |
| 48 | + self.provider = provider |
| 49 | + } |
| 50 | + |
| 51 | + func start() throws { |
| 52 | + navigationController.setNavigationBarHidden(false, animated: false) |
| 53 | + if provider.hasDoctorRole { |
| 54 | + try startWithDoctor() |
| 55 | + } else { |
| 56 | + try startWithPatient() |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +//MARK: - Private |
| 62 | +private extension DefaultChatFlowCoordinator { |
| 63 | + private func startWithDoctor() throws { |
| 64 | + setUpChannelListViewController() |
| 65 | + } |
| 66 | + |
| 67 | + private func startWithPatient() throws { |
| 68 | + let viewModel = try makeMessageViewModel() |
| 69 | + navigationController.pushViewController( |
| 70 | + viewModel.configuredComponentView(), |
| 71 | + animated: true |
| 72 | + ) |
| 73 | + } |
| 74 | + |
| 75 | + private func makeMessageViewModel() throws -> MessageListComponentViewModel<TelehealthCustomData, PubNubManagedChatEntities> { |
| 76 | + let currentUser = try provider.chatProvider.fetchCurrentUser() |
| 77 | + guard let membership = currentUser.memberships.first(where: { $0.managedUser.id == provider.uuid }) else { |
| 78 | + throw PatientError.membershipFetch("Cannot fetch membership for the current user") |
| 79 | + } |
| 80 | + |
| 81 | + let messageListViewModel = try provider.chatProvider.messageListComponentViewModel(pubnubChannelId: membership.channelId) |
| 82 | + messageListViewModel.customNavigationTitleString = nil |
| 83 | + messageListViewModel.leftBarButtonNavigationItems = nil |
| 84 | + messageListViewModel.rightBarButtonNavigationItems = nil |
| 85 | + messageListViewModel.customNavigationTitleView = { viewModel in |
| 86 | + UIView.customUserView( |
| 87 | + with: membership.channel.name, |
| 88 | + secondaryLabelText: membership.channel.details, |
| 89 | + avatarURL: membership.channel.avatarURL, |
| 90 | + backgroundColor: .clear, |
| 91 | + primaryLabelTextColor: .white, |
| 92 | + secondaryLabelTextColor: .white |
| 93 | + ) |
| 94 | + } |
| 95 | + |
| 96 | + return messageListViewModel |
| 97 | + } |
| 98 | + |
| 99 | + private func setUpChannelListViewController() { |
| 100 | + let viewModel = makeChannelViewModel() |
| 101 | + let channelListViewController = viewModel.configuredComponentView() |
| 102 | + channelListViewController.title = "Telehealth" |
| 103 | + navigationController.pushViewController(channelListViewController, animated: true) |
| 104 | + } |
| 105 | + |
| 106 | + private func makeChannelViewModel() -> ChannelListComponentViewModel<TelehealthCustomData, PubNubManagedChatEntities> { |
| 107 | + let channelListViewModel = provider.chatProvider.senderMembershipsChanneListComponentViewModel() |
| 108 | + channelListViewModel.leftBarButtonNavigationItems = nil |
| 109 | + channelListViewModel.layoutShouldContainSupplimentaryViews = false |
| 110 | + channelListViewModel.didSelectChannel = { [weak self] viewController, viewModel, selectedChannel in |
| 111 | + self?.setupMessageListViewController(for: selectedChannel) |
| 112 | + } |
| 113 | + channelListViewModel.customNavigationTitleView = { viewModel in |
| 114 | + UIView.customChannelListNavigationItemTitleView() |
| 115 | + } |
| 116 | + return channelListViewModel |
| 117 | + } |
| 118 | + |
| 119 | + private func setupMessageListViewController<Custom: ChannelCustomData>(for channel: ChatChannel<Custom>) { |
| 120 | + guard let messageListViewModel = try? provider.chatProvider.messageListComponentViewModel(pubnubChannelId: channel.id) else { |
| 121 | + preconditionFailure("Cannot create message list for the channel ID: \(channel.id)") |
| 122 | + } |
| 123 | + |
| 124 | + messageListViewModel.customNavigationTitleString = nil |
| 125 | + messageListViewModel.rightBarButtonNavigationItems = nil |
| 126 | + messageListViewModel.customNavigationTitleView = { [weak self] viewModel in |
| 127 | + guard let name = channel.name, let self = self else { return UIView() } |
| 128 | + return self.customMessageListNavigationTitleView(for: name) |
| 129 | + } |
| 130 | + |
| 131 | + let messageListViewController = messageListViewModel.configuredComponentView() |
| 132 | + navigationController.pushViewController(messageListViewController, animated: true) |
| 133 | + } |
| 134 | + |
| 135 | + private func customMessageListNavigationTitleView(for name: String) -> UIView { |
| 136 | + let label = UILabel() |
| 137 | + label.text = name |
| 138 | + label.font = UIFont(name: "Poppins-Bold", size: 16) |
| 139 | + return label |
| 140 | + } |
| 141 | +} |
0 commit comments