RoomScreenViewModel refactor part 2 (#3169)

This commit is contained in:
Mauro
2024-08-14 18:03:46 +02:00
committed by GitHub
parent e3c4b3781a
commit 0bbdb05220
11 changed files with 144 additions and 99 deletions
@@ -63,8 +63,10 @@ final class RoomScreenCoordinator: CoordinatorProtocol {
init(parameters: RoomScreenCoordinatorParameters) {
roomViewModel = RoomScreenViewModel(roomProxy: parameters.roomProxy,
mediaProvider: parameters.mediaProvider,
appMediator: parameters.appMediator,
appSettings: ServiceLocator.shared.settings)
appSettings: ServiceLocator.shared.settings,
analyticsService: ServiceLocator.shared.analytics)
timelineViewModel = TimelineViewModel(roomProxy: parameters.roomProxy,
focussedEventID: parameters.focussedEventID,
@@ -103,8 +105,6 @@ final class RoomScreenCoordinator: CoordinatorProtocol {
guard let self else { return }
switch action {
case .displayRoomDetails:
actionsSubject.send(.presentRoomDetails)
case .displayEmojiPicker(let itemID, let selectedEmojis):
actionsSubject.send(.presentEmojiPicker(itemID: itemID, selectedEmojis: selectedEmojis))
case .displayReportContent(let itemID, let senderID):
@@ -121,7 +121,7 @@ final class RoomScreenCoordinator: CoordinatorProtocol {
actionsSubject.send(.presentPollForm(mode: mode))
case .displayMediaUploadPreviewScreen(let url):
actionsSubject.send(.presentMediaUploadPreviewScreen(url))
case .displayRoomMemberDetails(userID: let userID):
case .tappedOnSenderDetails(userID: let userID):
actionsSubject.send(.presentRoomMemberDetails(userID: userID))
case .displayMessageForwarding(let forwardingItem):
actionsSubject.send(.presentMessageForwarding(forwardingItem: forwardingItem))
@@ -129,8 +129,6 @@ final class RoomScreenCoordinator: CoordinatorProtocol {
actionsSubject.send(.presentLocationViewer(body: body, geoURI: geoURI, description: description))
case .composer(let action):
composerViewModel.process(timelineAction: action)
case .displayCallScreen:
actionsSubject.send(.presentCallScreen)
case .hasScrolled(direction: let direction):
roomViewModel.timelineHasScrolled(direction: direction)
}
@@ -154,6 +152,10 @@ final class RoomScreenCoordinator: CoordinatorProtocol {
focusOnEvent(eventID: eventID)
case .displayPinnedEventsTimeline:
actionsSubject.send(.presentPinnedEventsTimeline)
case .displayRoomDetails:
actionsSubject.send(.presentRoomDetails)
case .displayCall:
actionsSubject.send(.presentCallScreen)
}
}
.store(in: &cancellables)
@@ -20,14 +20,21 @@ import OrderedCollections
enum RoomScreenViewModelAction {
case focusEvent(eventID: String)
case displayPinnedEventsTimeline
case displayRoomDetails
case displayCall
}
enum RoomScreenViewAction {
case tappedPinnedEventsBanner
case viewAllPins
case displayRoomDetails
case displayCall
}
struct RoomScreenViewState: BindableState {
var roomTitle = ""
var roomAvatar: RoomAvatar
var lastScrollDirection: ScrollDirection?
var isPinningEnabled = false
// This is used to control the banner
@@ -36,6 +43,9 @@ struct RoomScreenViewState: BindableState {
isPinningEnabled && !pinnedEventsBannerState.isEmpty && lastScrollDirection != .top
}
var canJoinCall = false
var hasOngoingCall: Bool
var bindings: RoomScreenViewStateBindings
}
@@ -25,6 +25,7 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
private let roomProxy: RoomProxyProtocol
private let appMediator: AppMediatorProtocol
private let appSettings: AppSettings
private let analyticsService: AnalyticsService
private let pinnedEventStringBuilder: RoomEventStringBuilder
private let actionsSubject: PassthroughSubject<RoomScreenViewModelAction, Never> = .init()
@@ -51,14 +52,21 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
}
init(roomProxy: RoomProxyProtocol,
mediaProvider: MediaProviderProtocol,
appMediator: AppMediatorProtocol,
appSettings: AppSettings) {
appSettings: AppSettings,
analyticsService: AnalyticsService) {
self.roomProxy = roomProxy
self.appMediator = appMediator
self.appSettings = appSettings
self.analyticsService = analyticsService
pinnedEventStringBuilder = .pinnedEventStringBuilder(userID: roomProxy.ownUserID)
super.init(initialViewState: .init(bindings: .init()))
super.init(initialViewState: .init(roomTitle: roomProxy.roomTitle,
roomAvatar: roomProxy.avatar,
hasOngoingCall: roomProxy.hasOngoingCall,
bindings: .init()),
imageProvider: mediaProvider)
setupSubscriptions()
}
@@ -72,6 +80,11 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
state.pinnedEventsBannerState.previousPin()
case .viewAllPins:
actionsSubject.send(.displayPinnedEventsTimeline)
case .displayRoomDetails:
actionsSubject.send(.displayRoomDetails)
case .displayCall:
actionsSubject.send(.displayCall)
analyticsService.trackInteraction(name: .MobileRoomCallButton)
}
}
@@ -84,15 +97,25 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
.actionsPublisher
.filter { $0 == .roomInfoUpdate }
roomInfoSubscription
.throttle(for: .seconds(1), scheduler: DispatchQueue.main, latest: true)
.sink { [weak self] _ in
guard let self else { return }
state.roomTitle = roomProxy.roomTitle
state.roomAvatar = roomProxy.avatar
state.hasOngoingCall = roomProxy.hasOngoingCall
}
.store(in: &cancellables)
Task { [weak self] in
// Don't guard let self here, otherwise the for await will strongify the self reference creating a strong reference cycle.
// If the subscription has sent a value before the Task has started it might be lost, so before entering the loop we always do an update.
await self?.updatePinnedEventIDs()
await self?.handleRoomInfoUpdate()
for await _ in roomInfoSubscription.receive(on: DispatchQueue.main).values {
guard !Task.isCancelled else {
return
}
await self?.updatePinnedEventIDs()
await self?.handleRoomInfoUpdate()
}
}
.store(in: &cancellables)
@@ -128,12 +151,17 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
state.pinnedEventsBannerState.setPinnedEventContents(pinnedEventContents)
}
private func updatePinnedEventIDs() async {
private func handleRoomInfoUpdate() async {
let pinnedEventIDs = await roomProxy.pinnedEventIDs
// Only update the loading state of the banner
if state.pinnedEventsBannerState.isLoading {
state.pinnedEventsBannerState = .loading(numbersOfEvents: pinnedEventIDs.count)
}
let userID = roomProxy.ownUserID
if case let .success(permission) = await roomProxy.canUserJoinCall(userID: userID) {
state.canJoinCall = permission
}
}
private func setupPinnedEventsTimelineProviderIfNeeded() {
@@ -154,10 +182,12 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
}
extension RoomScreenViewModel {
static func mock() -> RoomScreenViewModel {
RoomScreenViewModel(roomProxy: RoomProxyMock(.init()),
static func mock(roomProxyMock: RoomProxyMock) -> RoomScreenViewModel {
RoomScreenViewModel(roomProxy: roomProxyMock,
mediaProvider: MockMediaProvider(),
appMediator: AppMediatorMock.default,
appSettings: ServiceLocator.shared.settings)
appSettings: ServiceLocator.shared.settings,
analyticsService: ServiceLocator.shared.analytics)
}
}
@@ -163,29 +163,29 @@ struct RoomScreen: View {
// .principal + .primaryAction works better than .navigation leading + trailing
// as the latter disables interaction in the action button for rooms with long names
ToolbarItem(placement: .principal) {
RoomHeaderView(roomName: timelineContext.viewState.roomTitle,
roomAvatar: timelineContext.viewState.roomAvatar,
imageProvider: timelineContext.imageProvider)
RoomHeaderView(roomName: roomContext.viewState.roomTitle,
roomAvatar: roomContext.viewState.roomAvatar,
imageProvider: roomContext.imageProvider)
// Using a button stops it from getting truncated in the navigation bar
.contentShape(.rect)
.onTapGesture {
timelineContext.send(viewAction: .displayRoomDetails)
roomContext.send(viewAction: .displayRoomDetails)
}
}
if !ProcessInfo.processInfo.isiOSAppOnMac {
ToolbarItem(placement: .primaryAction) {
callButton
.disabled(timelineContext.viewState.canJoinCall == false)
.disabled(roomContext.viewState.canJoinCall == false)
}
}
}
@ViewBuilder
private var callButton: some View {
if timelineContext.viewState.hasOngoingCall {
if roomContext.viewState.hasOngoingCall {
Button {
timelineContext.send(viewAction: .displayCall)
roomContext.send(viewAction: .displayCall)
} label: {
Label(L10n.actionJoin, icon: \.videoCallSolid)
.labelStyle(.titleAndIcon)
@@ -194,7 +194,7 @@ struct RoomScreen: View {
.accessibilityIdentifier(A11yIdentifiers.roomScreen.joinCall)
} else {
Button {
timelineContext.send(viewAction: .displayCall)
roomContext.send(viewAction: .displayCall)
} label: {
CompoundIcon(\.videoCallSolid)
}
@@ -210,10 +210,11 @@ struct RoomScreen: View {
// MARK: - Previews
struct RoomScreen_Previews: PreviewProvider, TestablePreview {
static let roomViewModel = RoomScreenViewModel.mock()
static let timelineViewModel = TimelineViewModel(roomProxy: RoomProxyMock(.init(id: "stable_id",
name: "Preview room",
hasOngoingCall: true)),
static let roomProxyMock = RoomProxyMock(.init(id: "stable_id",
name: "Preview room",
hasOngoingCall: true))
static let roomViewModel = RoomScreenViewModel.mock(roomProxyMock: roomProxyMock)
static let timelineViewModel = TimelineViewModel(roomProxy: roomProxyMock,
timelineController: MockRoomTimelineController(),
mediaProvider: MockMediaProvider(),
mediaPlayerProvider: MediaPlayerProviderMock(),