element-ios/RiotSwiftUI/Modules/Template/TemplateAdvancedRoomsExample/TemplateRoomChat/MockTemplateRoomChatScreenState.swift

64 lines
2.3 KiB
Swift
Raw Normal View History

2021-09-12 13:57:45 +00:00
//
// Copyright 2021 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import SwiftUI
/// Using an enum for the screen allows you define the different state cases with
/// the relevant associated data for each case.
@available(iOS 14.0, *)
enum MockTemplateRoomChatScreenState: MockScreenState, CaseIterable {
// A case for each state you want to represent
// with specific, minimal associated data that will allow you
// mock that screen.
2021-09-20 07:47:28 +00:00
case initializingRoom
case failedToInitializeRoom
case noMessages
case messages
2021-09-12 13:57:45 +00:00
/// The associated screen
var screenType: Any.Type {
TemplateRoomChat.self
}
/// Generate the view struct for the screen state.
var screenView: ([Any], AnyView) {
2021-09-12 13:57:45 +00:00
let service: MockTemplateRoomChatService
switch self {
2021-09-20 07:47:28 +00:00
case .noMessages:
2021-09-12 13:57:45 +00:00
service = MockTemplateRoomChatService(messages: [])
2021-09-20 07:47:28 +00:00
service.simulateUpdate(initializationStatus: .initialized)
case .messages:
service = MockTemplateRoomChatService()
service.simulateUpdate(initializationStatus: .initialized)
2021-09-20 07:47:28 +00:00
case .initializingRoom:
service = MockTemplateRoomChatService()
2021-09-20 07:47:28 +00:00
case .failedToInitializeRoom:
2021-09-12 13:57:45 +00:00
service = MockTemplateRoomChatService()
2021-09-20 07:47:28 +00:00
service.simulateUpdate(initializationStatus: .failedToInitialize)
2021-09-12 13:57:45 +00:00
}
let viewModel = TemplateRoomChatViewModel(templateRoomChatService: service)
// can simulate service and viewModel actions here if needs be.
return (
[service, viewModel],
AnyView(TemplateRoomChat(viewModel: viewModel.context)
.addDependency(MockAvatarService.example))
)
2021-09-12 13:57:45 +00:00
}
}