Merge branch 'langleyd/4781_swiftui_template_examples' of https://github.com/vector-im/element-ios into langleyd/4781_swiftui_template_example2

This commit is contained in:
David Langley
2021-09-15 17:32:19 +01:00
91 changed files with 3348 additions and 354 deletions
@@ -37,6 +37,6 @@ class MockTemplateUserProfileService: TemplateUserProfileServiceProtocol {
}
func simulateUpdate(presence: TemplateUserProfilePresence) {
self.presenceSubject.send(presence)
self.presenceSubject.value = presence
}
}
@@ -1,4 +1,4 @@
//
//
// Copyright 2021 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -19,7 +19,7 @@ import RiotSwiftUI
@available(iOS 14.0, *)
class TemplateUserProfileUITests: MockScreenTest {
override class var screenType: MockScreenState.Type {
return MockTemplateUserProfileScreenState.self
}
@@ -27,7 +27,7 @@ class TemplateUserProfileUITests: MockScreenTest {
override class func createTest() -> MockScreenTest {
return TemplateUserProfileUITests(selector: #selector(verifyTemplateUserProfileScreen))
}
func verifyTemplateUserProfileScreen() throws {
guard let screenState = screenState as? MockTemplateUserProfileScreenState else { fatalError("no screen") }
switch screenState {
@@ -37,13 +37,13 @@ class TemplateUserProfileUITests: MockScreenTest {
verifyTemplateUserProfileLongName(name: name)
}
}
func verifyTemplateUserProfilePresence(presence: TemplateUserProfilePresence) {
let presenceText = app.staticTexts["presenceText"]
XCTAssert(presenceText.exists)
XCTAssert(presenceText.label == presence.title)
}
func verifyTemplateUserProfileLongName(name: String) {
let displayNameText = app.staticTexts["displayNameText"]
XCTAssert(displayNameText.exists)
@@ -26,29 +26,32 @@ class TemplateUserProfileViewModelTests: XCTestCase {
static let displayName = "Alice"
}
var service: MockTemplateUserProfileService!
var viewModel: TemplateUserProfileViewModel!
var viewModel: TemplateUserProfileViewModelProtocol!
var context: TemplateUserProfileViewModelType.Context!
var cancellables = Set<AnyCancellable>()
override func setUpWithError() throws {
service = MockTemplateUserProfileService(displayName: Constants.displayName, presence: Constants.presenceInitialValue)
viewModel = TemplateUserProfileViewModel(templateUserProfileService: service)
viewModel = TemplateUserProfileViewModel.makeTemplateUserProfileViewModel(templateUserProfileService: service)
context = viewModel.context
}
func testInitialState() {
XCTAssertEqual(viewModel.viewState.displayName, Constants.displayName)
XCTAssertEqual(viewModel.viewState.presence, Constants.presenceInitialValue)
XCTAssertEqual(context.viewState.displayName, Constants.displayName)
XCTAssertEqual(context.viewState.presence, Constants.presenceInitialValue)
}
func testFirstPresenceReceived() throws {
let presencePublisher = viewModel.$viewState.map(\.presence).removeDuplicates().collect(1).first()
let presencePublisher = context.$viewState.map(\.presence).removeDuplicates().collect(1).first()
XCTAssertEqual(try xcAwait(presencePublisher), [Constants.presenceInitialValue])
}
func testPresenceUpdatesReceived() throws {
let presencePublisher = viewModel.$viewState.map(\.presence).removeDuplicates().collect(3).first()
let presencePublisher = context.$viewState.map(\.presence).removeDuplicates().collect(3).first()
let awaitDeferred = xcAwaitDeferred(presencePublisher)
let newPresenceValue1: TemplateUserProfilePresence = .online
let newPresenceValue2: TemplateUserProfilePresence = .idle
service.simulateUpdate(presence: newPresenceValue1)
service.simulateUpdate(presence: newPresenceValue2)
XCTAssertEqual(try xcAwait(presencePublisher), [Constants.presenceInitialValue, newPresenceValue1, newPresenceValue2])
XCTAssertEqual(try awaitDeferred(), [Constants.presenceInitialValue, newPresenceValue1, newPresenceValue2])
}
}
@@ -25,29 +25,29 @@ typealias TemplateUserProfileViewModelType = StateStoreViewModel<TemplateUserPro
TemplateUserProfileViewAction>
@available(iOS 14, *)
class TemplateUserProfileViewModel: TemplateUserProfileViewModelType, TemplateUserProfileViewModelProtocol {
// MARK: - Properties
// MARK: Private
private let templateUserProfileService: TemplateUserProfileServiceProtocol
// MARK: Public
var completion: ((TemplateUserProfileViewModelResult) -> Void)?
// MARK: - Setup
static func makeTemplateUserProfileViewModel(templateUserProfileService: TemplateUserProfileServiceProtocol) -> TemplateUserProfileViewModelProtocol {
return TemplateUserProfileViewModel(templateUserProfileService: templateUserProfileService)
}
fileprivate init(templateUserProfileService: TemplateUserProfileServiceProtocol) {
self.templateUserProfileService = templateUserProfileService
super.init(initialViewState: Self.defaultState(templateUserProfileService: templateUserProfileService))
setupPresenceObserving()
}
private static func defaultState(templateUserProfileService: TemplateUserProfileServiceProtocol) -> TemplateUserProfileViewState {
return TemplateUserProfileViewState(
avatar: templateUserProfileService.avatarData,
@@ -55,15 +55,16 @@ class TemplateUserProfileViewModel: TemplateUserProfileViewModelType, TemplateUs
presence: templateUserProfileService.presenceSubject.value
)
}
private func setupPresenceObserving() {
templateUserProfileService.presenceSubject
let presenceUpdatePublisher = templateUserProfileService.presenceSubject
.map(TemplateUserProfileStateAction.updatePresence)
.sinkDispatchTo(self)
.eraseToAnyPublisher()
dispatch(actionPublisher: presenceUpdatePublisher)
}
// MARK: - Public
override func process(viewAction: TemplateUserProfileViewAction) {
switch viewAction {
case .cancel:
@@ -72,13 +73,7 @@ class TemplateUserProfileViewModel: TemplateUserProfileViewModelType, TemplateUs
done()
}
}
/// A redux style reducer
///
/// All modifications to state happen here.
/// - Parameters:
/// - state: The `inout` state to be modified,
/// - action: The action that defines which state modification should take place.
override class func reducer(state: inout TemplateUserProfileViewState, action: TemplateUserProfileStateAction) {
switch action {
case .updatePresence(let presence):
@@ -86,11 +81,11 @@ class TemplateUserProfileViewModel: TemplateUserProfileViewModelType, TemplateUs
}
UILog.debug("[TemplateUserProfileViewModel] reducer with action \(action) produced state: \(state)")
}
private func done() {
completion?(.done)
}
private func cancel() {
completion?(.cancel)
}