Skip to content

Commit

Permalink
TimelineKind refactor (#3193)
Browse files Browse the repository at this point in the history
  • Loading branch information
Velin92 authored Aug 22, 2024
1 parent 8c07397 commit 993fe8c
Show file tree
Hide file tree
Showing 25 changed files with 69 additions and 71 deletions.
8 changes: 4 additions & 4 deletions ElementX/Sources/Mocks/Generated/GeneratedMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11910,11 +11910,11 @@ class RoomTimelineProviderMock: RoomTimelineProviderProtocol {
set(value) { underlyingPaginationState = value }
}
var underlyingPaginationState: PaginationState!
var isLive: Bool {
get { return underlyingIsLive }
set(value) { underlyingIsLive = value }
var kind: TimelineKind {
get { return underlyingKind }
set(value) { underlyingKind = value }
}
var underlyingIsLive: Bool!
var underlyingKind: TimelineKind!
var membershipChangePublisher: AnyPublisher<Void, Never> {
get { return underlyingMembershipChangePublisher }
set(value) { underlyingMembershipChangePublisher = value }
Expand Down
2 changes: 1 addition & 1 deletion ElementX/Sources/Mocks/RoomTimelineProviderMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class AutoUpdatingRoomTimelineProviderMock: RoomTimelineProvider {
}

super.init(timeline: timelineMock,
isLive: true,
kind: .live,
paginationStatePublisher: innerPaginationStatePublisher.eraseToAnyPublisher())

Task.detached {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ final class PinnedEventsTimelineScreenCoordinator: CoordinatorProtocol {
viewModel = PinnedEventsTimelineScreenViewModel()
timelineViewModel = TimelineViewModel(roomProxy: parameters.roomProxy,
timelineController: parameters.timelineController,
isPinnedEventsTimeline: true,
mediaProvider: parameters.mediaProvider,
mediaPlayerProvider: parameters.mediaPlayerProvider,
voiceMessageMediaManager: parameters.voiceMessageMediaManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,10 @@ struct PinnedEventsTimelineScreen: View {
struct PinnedEventsTimelineScreen_Previews: PreviewProvider, TestablePreview {
static let viewModel = PinnedEventsTimelineScreenViewModel()
static let emptyTimelineViewModel: TimelineViewModel = {
let timelineController = MockRoomTimelineController()
let timelineController = MockRoomTimelineController(timelineKind: .pinned)
timelineController.timelineItems = []
return TimelineViewModel(roomProxy: JoinedRoomProxyMock(.init(name: "Preview room")),
timelineController: timelineController,
isPinnedEventsTimeline: true,
mediaProvider: MockMediaProvider(),
mediaPlayerProvider: MediaPlayerProviderMock(),
voiceMessageMediaManager: VoiceMessageMediaManagerMock(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ final class RoomScreenCoordinator: CoordinatorProtocol {
timelineViewModel = TimelineViewModel(roomProxy: parameters.roomProxy,
focussedEventID: parameters.focussedEvent?.eventID,
timelineController: parameters.timelineController,
isPinnedEventsTimeline: false,
mediaProvider: parameters.mediaProvider,
mediaPlayerProvider: parameters.mediaPlayerProvider,
voiceMessageMediaManager: parameters.voiceMessageMediaManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,20 @@ struct PinnedItemsBannerView: View {

@ViewBuilder
private var viewAllButton: some View {
switch state {
case .loaded:
Button { onViewAllButtonTap() } label: {
Text(L10n.screenRoomPinnedBannerViewAllButtonTitle)
.font(.compound.bodyMDSemibold)
.foregroundStyle(Color.compound.textPrimary)
.padding(.horizontal, 16)
.padding(.vertical, 5)
}
case .loading:
ProgressView()
Button { onViewAllButtonTap() } label: {
Text(state.isLoading ? "" : L10n.screenRoomPinnedBannerViewAllButtonTitle)
.font(.compound.bodyMDSemibold)
.foregroundStyle(Color.compound.textPrimary)
.opacity(state.isLoading ? 0 : 1)
// Use overlay instead otherwise the sliding animation would not work
.overlay(alignment: .trailing) {
ProgressView()
.opacity(state.isLoading ? 1 : 0)
}
.padding(.horizontal, 16)
.padding(.vertical, 5)
}
.disabled(state.isLoading)
}

private var content: some View {
Expand Down
1 change: 0 additions & 1 deletion ElementX/Sources/Screens/RoomScreen/View/RoomScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ struct RoomScreen_Previews: PreviewProvider, TestablePreview {
static let roomViewModel = RoomScreenViewModel.mock(roomProxyMock: roomProxyMock)
static let timelineViewModel = TimelineViewModel(roomProxy: roomProxyMock,
timelineController: MockRoomTimelineController(),
isPinnedEventsTimeline: false,
mediaProvider: MockMediaProvider(),
mediaPlayerProvider: MediaPlayerProviderMock(),
voiceMessageMediaManager: VoiceMessageMediaManagerMock(),
Expand Down
7 changes: 2 additions & 5 deletions ElementX/Sources/Screens/Timeline/TimelineViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ class TimelineViewModel: TimelineViewModelType, TimelineViewModelProtocol {
init(roomProxy: JoinedRoomProxyProtocol,
focussedEventID: String? = nil,
timelineController: RoomTimelineControllerProtocol,
isPinnedEventsTimeline: Bool,
mediaProvider: MediaProviderProtocol,
mediaPlayerProvider: MediaPlayerProviderProtocol,
voiceMessageMediaManager: VoiceMessageMediaManagerProtocol,
Expand Down Expand Up @@ -81,7 +80,7 @@ class TimelineViewModel: TimelineViewModelType, TimelineViewModelProtocol {
appSettings: appSettings,
analyticsService: analyticsService)

super.init(initialViewState: TimelineViewState(isPinnedEventsTimeline: isPinnedEventsTimeline,
super.init(initialViewState: TimelineViewState(isPinnedEventsTimeline: timelineController.timelineKind == .pinned,
roomID: roomProxy.id,
isEncryptedOneToOneRoom: roomProxy.isEncryptedOneToOneRoom,
timelineViewState: TimelineState(focussedEvent: focussedEventID.map { .init(eventID: $0, appearance: .immediate) }),
Expand Down Expand Up @@ -827,7 +826,6 @@ extension TimelineViewModel {
static let mock = TimelineViewModel(roomProxy: JoinedRoomProxyMock(.init(name: "Preview room")),
focussedEventID: nil,
timelineController: MockRoomTimelineController(),
isPinnedEventsTimeline: false,
mediaProvider: MockMediaProvider(),
mediaPlayerProvider: MediaPlayerProviderMock(),
voiceMessageMediaManager: VoiceMessageMediaManagerMock(),
Expand All @@ -838,8 +836,7 @@ extension TimelineViewModel {

static let pinnedEventsTimelineMock = TimelineViewModel(roomProxy: JoinedRoomProxyMock(.init(name: "Preview room")),
focussedEventID: nil,
timelineController: MockRoomTimelineController(),
isPinnedEventsTimeline: true,
timelineController: MockRoomTimelineController(timelineKind: .pinned),
mediaProvider: MockMediaProvider(),
mediaPlayerProvider: MediaPlayerProviderMock(),
voiceMessageMediaManager: VoiceMessageMediaManagerMock(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ struct ReadReceiptsSummaryView_Previews: PreviewProvider, TestablePreview {
let roomProxyMock = JoinedRoomProxyMock(.init(name: "Room", members: members))
let mock = TimelineViewModel(roomProxy: roomProxyMock,
timelineController: MockRoomTimelineController(),
isPinnedEventsTimeline: false,
mediaProvider: MockMediaProvider(),
mediaPlayerProvider: MediaPlayerProviderMock(),
voiceMessageMediaManager: VoiceMessageMediaManagerMock(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ struct TimelineReadReceiptsView_Previews: PreviewProvider, TestablePreview {

static let viewModel = TimelineViewModel(roomProxy: JoinedRoomProxyMock(.init(name: "Test", members: members)),
timelineController: MockRoomTimelineController(),
isPinnedEventsTimeline: false,
mediaProvider: MockMediaProvider(),
mediaPlayerProvider: MediaPlayerProviderMock(),
voiceMessageMediaManager: VoiceMessageMediaManagerMock(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ struct HighlightedTimelineItemTimeline_Previews: PreviewProvider {
static let timelineViewModel = TimelineViewModel(roomProxy: roomProxyMock,
focussedEventID: focussedEventID,
timelineController: MockRoomTimelineController(),
isPinnedEventsTimeline: false,
mediaProvider: MockMediaProvider(),
mediaPlayerProvider: MediaPlayerProviderMock(),
voiceMessageMediaManager: VoiceMessageMediaManagerMock(),
Expand Down
1 change: 0 additions & 1 deletion ElementX/Sources/Screens/Timeline/View/TimelineView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ struct TimelineView_Previews: PreviewProvider, TestablePreview {
static let roomViewModel = RoomScreenViewModel.mock(roomProxyMock: roomProxyMock)
static let timelineViewModel = TimelineViewModel(roomProxy: roomProxyMock,
timelineController: MockRoomTimelineController(),
isPinnedEventsTimeline: false,
mediaProvider: MockMediaProvider(),
mediaPlayerProvider: MediaPlayerProviderMock(),
voiceMessageMediaManager: VoiceMessageMediaManagerMock(),
Expand Down
6 changes: 3 additions & 3 deletions ElementX/Sources/Services/Room/JoinedRoomProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class JoinedRoomProxy: JoinedRoomProxyProtocol {
}

do {
let timeline = try await TimelineProxy(timeline: room.pinnedEventsTimeline(internalIdPrefix: nil, maxEventsToLoad: 100), isLive: true)
let timeline = try await TimelineProxy(timeline: room.pinnedEventsTimeline(internalIdPrefix: nil, maxEventsToLoad: 100), kind: .pinned)
await timeline.subscribeForUpdates()
innerPinnedEventsTimeline = timeline
return timeline
Expand Down Expand Up @@ -172,7 +172,7 @@ class JoinedRoomProxy: JoinedRoomProxyProtocol {
self.roomListItem = roomListItem
self.room = room

timeline = try await TimelineProxy(timeline: room.timeline(), isLive: true)
timeline = try await TimelineProxy(timeline: room.timeline(), kind: .live)

Task {
await updateMembers()
Expand Down Expand Up @@ -216,7 +216,7 @@ class JoinedRoomProxy: JoinedRoomProxyProtocol {
func timelineFocusedOnEvent(eventID: String, numberOfEvents: UInt16) async -> Result<TimelineProxyProtocol, RoomProxyError> {
do {
let timeline = try await room.timelineFocusedOnEvent(eventId: eventID, numContextEvents: numberOfEvents, internalIdPrefix: UUID().uuidString)
return .success(TimelineProxy(timeline: timeline, isLive: false))
return .success(TimelineProxy(timeline: timeline, kind: .detached))
} catch let error as FocusEventError {
switch error {
case .InvalidEventId(_, let error):
Expand Down
6 changes: 3 additions & 3 deletions ElementX/Sources/Services/Timeline/RoomTimelineProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class RoomTimelineProvider: RoomTimelineProviderProtocol {
.eraseToAnyPublisher()
}

private(set) var isLive: Bool
let kind: TimelineKind

private let membershipChangeSubject = PassthroughSubject<Void, Never>()
var membershipChangePublisher: AnyPublisher<Void, Never> {
Expand All @@ -54,10 +54,10 @@ class RoomTimelineProvider: RoomTimelineProviderProtocol {
roomTimelineObservationToken?.cancel()
}

init(timeline: Timeline, isLive: Bool, paginationStatePublisher: AnyPublisher<PaginationState, Never>) {
init(timeline: Timeline, kind: TimelineKind, paginationStatePublisher: AnyPublisher<PaginationState, Never>) {
serialDispatchQueue = DispatchQueue(label: "io.element.elementx.roomtimelineprovider", qos: .utility)
itemProxiesSubject = CurrentValueSubject<[TimelineItemProxy], Never>([])
self.isLive = isLive
self.kind = kind

paginationStatePublisher
.sink { [weak self] in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ protocol RoomTimelineProviderProtocol {
var itemProxies: [TimelineItemProxy] { get }
/// Whether the timeline is back/forward paginating or not (or has reached the start/end of the room).
var paginationState: PaginationState { get }
/// Whether or not the provider is for a live timeline.
var isLive: Bool { get }
/// The kind of the timeline
var kind: TimelineKind { get }
/// A publisher that signals when changes to the room's membership have occurred through `/sync`.
///
/// This is temporary and will be replace by a subscription on the room itself.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class MockRoomTimelineController: RoomTimelineControllerProtocol {

var roomProxy: JoinedRoomProxyProtocol?
var roomID: String { roomProxy?.id ?? "MockRoomIdentifier" }
var timelineKind: TimelineKind

let callbacks = PassthroughSubject<RoomTimelineControllerCallback, Never>()

Expand All @@ -36,7 +37,8 @@ class MockRoomTimelineController: RoomTimelineControllerProtocol {

private var client: UITestsSignalling.Client?

init(listenForSignals: Bool = false) {
init(timelineKind: TimelineKind = .live, listenForSignals: Bool = false) {
self.timelineKind = timelineKind
callbacks.send(.paginationState(PaginationState(backward: .idle, forward: .timelineEndReached)))
callbacks.send(.isLive(true))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class RoomTimelineController: RoomTimelineControllerProtocol {
private let timelineItemFactory: RoomTimelineItemFactoryProtocol
private let appSettings: AppSettings
private let serialDispatchQueue: DispatchQueue
private let shouldHideStart: Bool

let callbacks = PassthroughSubject<RoomTimelineControllerCallback, Never>()

Expand All @@ -42,17 +41,19 @@ class RoomTimelineController: RoomTimelineControllerProtocol {
roomProxy.id
}

var timelineKind: TimelineKind {
liveTimelineProvider.kind
}

init(roomProxy: JoinedRoomProxyProtocol,
timelineProxy: TimelineProxyProtocol,
initialFocussedEventID: String?,
shouldHideStart: Bool,
timelineItemFactory: RoomTimelineItemFactoryProtocol,
appSettings: AppSettings) {
self.roomProxy = roomProxy
liveTimelineProvider = timelineProxy.timelineProvider
self.timelineItemFactory = timelineItemFactory
self.appSettings = appSettings
self.shouldHideStart = shouldHideStart
serialDispatchQueue = DispatchQueue(label: "io.element.elementx.roomtimelineprovider", qos: .utility)

activeTimeline = timelineProxy
Expand Down Expand Up @@ -312,7 +313,7 @@ class RoomTimelineController: RoomTimelineControllerProtocol {

// Inform the world that the initial items are loading from the store
callbacks.send(.paginationState(.init(backward: .paginating, forward: .paginating)))
callbacks.send(.isLive(activeTimelineProvider.isLive))
callbacks.send(.isLive(activeTimelineProvider.kind == .live))

updateTimelineItemsCancellable = activeTimelineProvider
.updatePublisher
Expand Down Expand Up @@ -367,7 +368,7 @@ class RoomTimelineController: RoomTimelineControllerProtocol {
// Check if we need to add anything to the top of the timeline.
switch paginationState.backward {
case .timelineEndReached:
if !shouldHideStart, !roomProxy.isEncryptedOneToOneRoom {
if timelineKind != .pinned, !roomProxy.isEncryptedOneToOneRoom {
let timelineStart = TimelineStartRoomTimelineItem(name: roomProxy.name)
newTimelineItems.insert(timelineStart, at: 0)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ struct RoomTimelineControllerFactory: RoomTimelineControllerFactoryProtocol {
RoomTimelineController(roomProxy: roomProxy,
timelineProxy: roomProxy.timeline,
initialFocussedEventID: initialFocussedEventID,
shouldHideStart: false,
timelineItemFactory: timelineItemFactory,
appSettings: ServiceLocator.shared.settings)
}
Expand All @@ -36,7 +35,6 @@ struct RoomTimelineControllerFactory: RoomTimelineControllerFactoryProtocol {
return RoomTimelineController(roomProxy: roomProxy,
timelineProxy: pinnedEventsTimeline,
initialFocussedEventID: nil,
shouldHideStart: true,
timelineItemFactory: timelineItemFactory,
appSettings: ServiceLocator.shared.settings)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ enum RoomTimelineControllerError: Error {
@MainActor
protocol RoomTimelineControllerProtocol {
var roomID: String { get }
var timelineKind: TimelineKind { get }

var timelineItems: [RoomTimelineItemProtocol] { get }
var callbacks: PassthroughSubject<RoomTimelineControllerCallback, Never> { get }
Expand Down
Loading

0 comments on commit 993fe8c

Please sign in to comment.