mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-04-21 17:12:45 +02:00
code restyle for poll detail
This commit is contained in:
@@ -56,41 +56,44 @@ final class PollHistoryCoordinator: NSObject, Coordinator, Presentable {
|
||||
}
|
||||
|
||||
func showPollDetail(_ poll: TimelinePollDetails) {
|
||||
|
||||
guard let event = parameters.room.mxSession.store.event(withEventId: poll.id, inRoom: parameters.room.roomId),
|
||||
let detailCoordinator: PollHistoryDetailCoordinator = try? .init(parameters: .init(event: event, room: self.parameters.room)) else {
|
||||
let detailCoordinator: PollHistoryDetailCoordinator = try? .init(parameters: .init(event: event, room: parameters.room)) else {
|
||||
pollHistoryViewModel.context.alertInfo = .init(id: true, title: VectorL10n.settingsDiscoveryErrorMessage)
|
||||
return
|
||||
}
|
||||
detailCoordinator.toPresentable().presentationController?.delegate = self
|
||||
detailCoordinator.completion = { [weak self, weak detailCoordinator] result in
|
||||
guard let self = self, let coordinator = detailCoordinator else { return }
|
||||
switch result {
|
||||
case .dismiss:
|
||||
self.toPresentable().dismiss(animated: true)
|
||||
self.remove(childCoordinator: coordinator)
|
||||
case .viewInTimeline:
|
||||
self.toPresentable().dismiss(animated: false)
|
||||
self.remove(childCoordinator: coordinator)
|
||||
var event = event
|
||||
if poll.closed {
|
||||
let room = self.parameters.room
|
||||
let relatedEvents = room.mxSession.store.relations(forEvent: event.eventId, inRoom: room.roomId, relationType: MXEventRelationTypeReference)
|
||||
let pollEndedEvent = relatedEvents.first(where: { $0.eventType == .pollEnd })
|
||||
event = pollEndedEvent ?? event
|
||||
}
|
||||
self.completion?(event)
|
||||
}
|
||||
detailCoordinator.completion = { [weak self, weak detailCoordinator, weak event] result in
|
||||
guard let self, let coordinator = detailCoordinator, let event = event else { return }
|
||||
self.handlePollDetailResult(result, coordinator: coordinator, event: event, poll: poll)
|
||||
}
|
||||
|
||||
self.add(childCoordinator: detailCoordinator)
|
||||
add(childCoordinator: detailCoordinator)
|
||||
detailCoordinator.start()
|
||||
self.toPresentable().present(detailCoordinator.toPresentable(), animated: true)
|
||||
toPresentable().present(detailCoordinator.toPresentable(), animated: true)
|
||||
}
|
||||
|
||||
func toPresentable() -> UIViewController {
|
||||
pollHistoryHostingController
|
||||
}
|
||||
|
||||
private func handlePollDetailResult(_ result: PollHistoryDetailViewModelResult, coordinator: Coordinator, event: MXEvent, poll: TimelinePollDetails) {
|
||||
switch result {
|
||||
case .dismiss:
|
||||
toPresentable().dismiss(animated: true)
|
||||
remove(childCoordinator: coordinator)
|
||||
case .viewInTimeline:
|
||||
toPresentable().dismiss(animated: false)
|
||||
remove(childCoordinator: coordinator)
|
||||
var event = event
|
||||
if poll.closed {
|
||||
let room = parameters.room
|
||||
let relatedEvents = room.mxSession.store.relations(forEvent: event.eventId, inRoom: room.roomId, relationType: MXEventRelationTypeReference)
|
||||
let pollEndedEvent = relatedEvents.first(where: { $0.eventType == .pollEnd })
|
||||
event = pollEndedEvent ?? event
|
||||
}
|
||||
completion?(event)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: UIAdaptivePresentationControllerDelegate
|
||||
|
||||
+10
-31
@@ -14,10 +14,10 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import CommonKit
|
||||
import SwiftUI
|
||||
import Combine
|
||||
import CommonKit
|
||||
import MatrixSDK
|
||||
import SwiftUI
|
||||
|
||||
struct PollHistoryDetailCoordinatorParameters {
|
||||
let event: MXEvent
|
||||
@@ -28,8 +28,6 @@ final class PollHistoryDetailCoordinator: Coordinator, Presentable {
|
||||
private let parameters: PollHistoryDetailCoordinatorParameters
|
||||
private let pollHistoryDetailHostingController: UIViewController
|
||||
private var pollHistoryDetailViewModel: PollHistoryDetailViewModelProtocol
|
||||
private var indicatorPresenter: UserIndicatorTypePresenterProtocol
|
||||
private var loadingIndicator: UserIndicator?
|
||||
|
||||
// Must be used only internally
|
||||
var childCoordinators: [Coordinator] = []
|
||||
@@ -42,12 +40,15 @@ final class PollHistoryDetailCoordinator: Coordinator, Presentable {
|
||||
let viewModel = PollHistoryDetailViewModel(timelineViewModel: timelinePollCoordinator.viewModel)
|
||||
let view = PollHistoryDetail(viewModel: viewModel.context)
|
||||
pollHistoryDetailViewModel = viewModel
|
||||
|
||||
pollHistoryDetailHostingController = VectorHostingController(rootView: view)
|
||||
|
||||
indicatorPresenter = UserIndicatorTypePresenter(presentingViewController: pollHistoryDetailHostingController)
|
||||
self.add(childCoordinator: timelinePollCoordinator)
|
||||
viewModel.completion = { [weak self] result in
|
||||
add(childCoordinator: timelinePollCoordinator)
|
||||
}
|
||||
|
||||
// MARK: - Public
|
||||
|
||||
func start() {
|
||||
MXLog.debug("[PollHistoryDetailCoordinator] did start.")
|
||||
pollHistoryDetailViewModel.completion = { [weak self] result in
|
||||
guard let self = self else { return }
|
||||
switch result {
|
||||
case .dismiss:
|
||||
@@ -58,29 +59,7 @@ final class PollHistoryDetailCoordinator: Coordinator, Presentable {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Public
|
||||
|
||||
func start() {
|
||||
MXLog.debug("[PollHistoryDetailCoordinator] did start.")
|
||||
|
||||
}
|
||||
|
||||
func toPresentable() -> UIViewController {
|
||||
pollHistoryDetailHostingController
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
/// Show an activity indicator whilst loading.
|
||||
/// - Parameters:
|
||||
/// - label: The label to show on the indicator.
|
||||
/// - isInteractionBlocking: Whether the indicator should block any user interaction.
|
||||
private func startLoading(label: String = VectorL10n.loading, isInteractionBlocking: Bool = true) {
|
||||
loadingIndicator = indicatorPresenter.present(.loading(label: label, isInteractionBlocking: isInteractionBlocking))
|
||||
}
|
||||
|
||||
/// Hide the currently displayed activity indicator.
|
||||
private func stopLoading() {
|
||||
loadingIndicator = nil
|
||||
}
|
||||
}
|
||||
|
||||
-1
@@ -48,7 +48,6 @@ enum MockPollHistoryDetailScreenState: MockScreenState, CaseIterable {
|
||||
}
|
||||
|
||||
var screenView: ([Any], AnyView) {
|
||||
|
||||
let viewModel = PollHistoryDetailViewModel(timelineViewModel: TimelinePollViewModel(timelinePollDetails: poll))
|
||||
|
||||
return ([viewModel], AnyView(PollHistoryDetail(viewModel: viewModel.context)))
|
||||
|
||||
+5
-7
@@ -25,19 +25,17 @@ enum PollHistoryDetailViewModelResult {
|
||||
case viewInTimeline
|
||||
}
|
||||
|
||||
// MARK: View model
|
||||
|
||||
|
||||
|
||||
// MARK: View
|
||||
|
||||
struct PollHistoryDetailViewState: BindableState {
|
||||
var timelineViewModel: TimelinePollViewModelProtocol
|
||||
var timelineViewModel: TimelinePollViewModelType.Context
|
||||
|
||||
var pollStartDate: Date {
|
||||
timelineViewModel.context.viewState.poll.startDate
|
||||
timelineViewModel.viewState.poll.startDate
|
||||
}
|
||||
|
||||
var isPollClosed: Bool {
|
||||
timelineViewModel.context.viewState.poll.closed
|
||||
timelineViewModel.viewState.poll.closed
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-9
@@ -20,17 +20,14 @@ import SwiftUI
|
||||
typealias PollHistoryDetailViewModelType = StateStoreViewModel<PollHistoryDetailViewState, PollHistoryDetailViewAction>
|
||||
|
||||
class PollHistoryDetailViewModel: PollHistoryDetailViewModelType, PollHistoryDetailViewModelProtocol {
|
||||
// MARK: - Properties
|
||||
|
||||
// MARK: Private
|
||||
|
||||
// MARK: Public
|
||||
|
||||
var completion: PollHistoryDetailViewModelCallback?
|
||||
|
||||
// MARK: - Setup
|
||||
|
||||
init(timelineViewModel: TimelinePollViewModelProtocol) {
|
||||
super.init(initialViewState: PollHistoryDetailViewState(timelineViewModel: timelineViewModel))
|
||||
super.init(initialViewState: PollHistoryDetailViewState(timelineViewModel: timelineViewModel.context))
|
||||
}
|
||||
|
||||
// MARK: - Public
|
||||
@@ -43,8 +40,4 @@ class PollHistoryDetailViewModel: PollHistoryDetailViewModelType, PollHistoryDet
|
||||
completion?(.viewInTimeline)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - TimelinePollViewModelProtocol
|
||||
|
||||
}
|
||||
|
||||
+1
@@ -17,5 +17,6 @@
|
||||
import Foundation
|
||||
|
||||
protocol PollHistoryDetailViewModelProtocol {
|
||||
var completion: PollHistoryDetailViewModelCallback? { get set }
|
||||
var context: PollHistoryDetailViewModelType.Context { get }
|
||||
}
|
||||
|
||||
+15
-3
@@ -23,11 +23,10 @@ class PollHistoryDetailViewModelTests: XCTestCase {
|
||||
static let counterInitialValue = 0
|
||||
}
|
||||
|
||||
var viewModel: PollHistoryDetailViewModelProtocol!
|
||||
var viewModel: PollHistoryDetailViewModel!
|
||||
var context: PollHistoryDetailViewModelType.Context!
|
||||
|
||||
override func setUpWithError() throws {
|
||||
|
||||
let answerOptions = [TimelinePollAnswerOption(id: "1", text: "1", count: 1, winner: false, selected: false),
|
||||
TimelinePollAnswerOption(id: "2", text: "2", count: 1, winner: false, selected: false),
|
||||
TimelinePollAnswerOption(id: "3", text: "3", count: 1, winner: false, selected: false)]
|
||||
@@ -51,5 +50,18 @@ class PollHistoryDetailViewModelTests: XCTestCase {
|
||||
func testInitialState() {
|
||||
XCTAssertFalse(context.viewState.isPollClosed)
|
||||
}
|
||||
|
||||
|
||||
func testProcessAction() {
|
||||
viewModel.completion = { result in
|
||||
XCTAssertEqual(result, .viewInTimeline)
|
||||
}
|
||||
viewModel.process(viewAction: .viewInTimeline)
|
||||
}
|
||||
|
||||
func testProcessDismiss() {
|
||||
viewModel.completion = { result in
|
||||
XCTAssertEqual(result, .dismiss)
|
||||
}
|
||||
viewModel.process(viewAction: .dismiss)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,8 +17,6 @@
|
||||
import SwiftUI
|
||||
|
||||
struct PollHistoryDetail: View {
|
||||
// MARK: - Properties
|
||||
|
||||
// MARK: Private
|
||||
|
||||
@Environment(\.theme) private var theme: ThemeSwiftUI
|
||||
@@ -29,9 +27,6 @@ struct PollHistoryDetail: View {
|
||||
|
||||
var body: some View {
|
||||
navigation
|
||||
.padding([.horizontal], 16)
|
||||
.padding([.top, .bottom])
|
||||
.background(theme.colors.background.ignoresSafeArea())
|
||||
}
|
||||
|
||||
private var navigation: some View {
|
||||
@@ -45,6 +40,7 @@ struct PollHistoryDetail: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var content: some View {
|
||||
let timelineViewModel = viewModel.viewState.timelineViewModel
|
||||
return ScrollView {
|
||||
@@ -54,17 +50,20 @@ struct PollHistoryDetail: View {
|
||||
.font(theme.fonts.caption1)
|
||||
.padding([.top])
|
||||
.accessibilityIdentifier("PollHistoryDetail.date")
|
||||
TimelinePollView(viewModel: timelineViewModel.context)
|
||||
TimelinePollView(viewModel: timelineViewModel)
|
||||
.navigationTitle(navigationTitle)
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.navigationBarBackButtonHidden(true)
|
||||
.navigationBarItems(leading: btnBack, trailing: btnDone)
|
||||
.navigationBarItems(leading: backButton, trailing: doneButton)
|
||||
viewInTimeline
|
||||
}
|
||||
}
|
||||
.padding([.horizontal], 16)
|
||||
.padding([.top, .bottom])
|
||||
.background(theme.colors.background.ignoresSafeArea())
|
||||
}
|
||||
|
||||
private var btnBack: some View {
|
||||
private var backButton: some View {
|
||||
Button(action: {
|
||||
viewModel.send(viewAction: .dismiss)
|
||||
}) {
|
||||
@@ -73,7 +72,8 @@ struct PollHistoryDetail: View {
|
||||
.foregroundColor(theme.colors.accent)
|
||||
}
|
||||
}
|
||||
private var btnDone: some View {
|
||||
|
||||
private var doneButton: some View {
|
||||
Button {
|
||||
viewModel.send(viewAction: .dismiss)
|
||||
} label: {
|
||||
|
||||
Reference in New Issue
Block a user