Added back simple template

This commit is contained in:
Stefan Ceriu
2022-05-10 15:05:56 +03:00
committed by Stefan Ceriu
parent db1114d381
commit d3c8b65181
8 changed files with 483 additions and 18 deletions
@@ -0,0 +1,86 @@
//
// 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 SwiftUI
import CommonKit
struct TemplateSimpleScreenCoordinatorParameters {
let promptType: TemplateSimpleScreenPromptType
}
final class TemplateSimpleScreenCoordinator: Coordinator, Presentable {
// MARK: - Properties
// MARK: Private
private let parameters: TemplateSimpleScreenCoordinatorParameters
private let templateSimpleScreenHostingController: UIViewController
private var templateSimpleScreenViewModel: TemplateSimpleScreenViewModelProtocol
private var indicatorPresenter: UserIndicatorTypePresenterProtocol
private var loadingIndicator: UserIndicator?
// MARK: Public
// Must be used only internally
var childCoordinators: [Coordinator] = []
var completion: ((TemplateSimpleScreenViewModelResult) -> Void)?
// MARK: - Setup
@available(iOS 14.0, *)
init(parameters: TemplateSimpleScreenCoordinatorParameters) {
self.parameters = parameters
let viewModel = TemplateSimpleScreenViewModel(promptType: parameters.promptType)
let view = TemplateSimpleScreen(viewModel: viewModel.context)
templateSimpleScreenViewModel = viewModel
templateSimpleScreenHostingController = VectorHostingController(rootView: view)
indicatorPresenter = UserIndicatorTypePresenter(presentingViewController: templateSimpleScreenHostingController)
}
// MARK: - Public
func start() {
MXLog.debug("[TemplateSimpleScreenCoordinator] did start.")
templateSimpleScreenViewModel.completion = { [weak self] result in
guard let self = self else { return }
MXLog.debug("[TemplateSimpleScreenCoordinator] TemplateSimpleScreenViewModel did complete with result: \(result).")
self.completion?(result)
}
}
func toPresentable() -> UIViewController {
return self.templateSimpleScreenHostingController
}
// 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
}
}
@@ -0,0 +1,67 @@
//
// 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
// MARK: - Coordinator
enum TemplateSimpleScreenPromptType {
case regular
case upgrade
}
extension TemplateSimpleScreenPromptType: Identifiable, CaseIterable {
var id: Self { self }
var title: String {
switch self {
case .regular:
return VectorL10n.roomCreationMakePublicPromptTitle
case .upgrade:
return VectorL10n.roomDetailsHistorySectionPromptTitle
}
}
var image: ImageAsset {
switch self {
case .regular:
return Asset.Images.appSymbol
case .upgrade:
return Asset.Images.keyVerificationSuccessShield
}
}
}
// MARK: View model
enum TemplateSimpleScreenViewModelResult {
case accept
case cancel
}
// MARK: View
struct TemplateSimpleScreenViewState: BindableState {
var promptType: TemplateSimpleScreenPromptType
var count: Int
}
enum TemplateSimpleScreenViewAction {
case incrementCount
case decrementCount
case accept
case cancel
}
@@ -0,0 +1,54 @@
//
// 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 SwiftUI
@available(iOS 14, *)
typealias TemplateSimpleScreenViewModelType = StateStoreViewModel<TemplateSimpleScreenViewState,
Never,
TemplateSimpleScreenViewAction>
@available(iOS 14, *)
class TemplateSimpleScreenViewModel: TemplateSimpleScreenViewModelType, TemplateSimpleScreenViewModelProtocol {
// MARK: - Properties
// MARK: Private
// MARK: Public
var completion: ((TemplateSimpleScreenViewModelResult) -> Void)?
// MARK: - Setup
init(promptType: TemplateSimpleScreenPromptType, initialCount: Int = 0) {
super.init(initialViewState: TemplateSimpleScreenViewState(promptType: promptType, count: 0))
}
// MARK: - Public
override func process(viewAction: TemplateSimpleScreenViewAction) {
switch viewAction {
case .accept:
completion?(.accept)
case .cancel:
completion?(.cancel)
case .incrementCount:
state.count += 1
case .decrementCount:
state.count -= 1
}
}
}
@@ -0,0 +1,24 @@
//
// 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
protocol TemplateSimpleScreenViewModelProtocol {
var completion: ((TemplateSimpleScreenViewModelResult) -> Void)? { get set }
@available(iOS 14, *)
var context: TemplateSimpleScreenViewModelType.Context { get }
}
@@ -0,0 +1,113 @@
//
// 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 SwiftUI
@available(iOS 14.0, *)
struct TemplateSimpleScreen: View {
// MARK: - Properties
// MARK: Private
@Environment(\.theme) private var theme
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
private var horizontalPadding: CGFloat {
horizontalSizeClass == .regular ? 50 : 16
}
// MARK: Public
@ObservedObject var viewModel: TemplateSimpleScreenViewModel.Context
// MARK: Views
var body: some View {
GeometryReader { geometry in
VStack {
ScrollView(showsIndicators: false) {
mainContent
.padding(.top, 50)
.padding(.horizontal, horizontalPadding)
}
buttons
.padding(.horizontal, horizontalPadding)
.padding(.bottom, geometry.safeAreaInsets.bottom > 0 ? 0 : 16)
}
}
.background(theme.colors.background.ignoresSafeArea())
.accentColor(theme.colors.accent)
}
/// The main content of the view to be shown in a scroll view.
var mainContent: some View {
VStack(spacing: 36) {
Text(viewModel.viewState.promptType.title)
.font(theme.fonts.title1B)
.foregroundColor(theme.colors.primaryContent)
.accessibilityIdentifier("title")
Image(viewModel.viewState.promptType.image.name)
.resizable()
.scaledToFit()
.frame(width:100)
.foregroundColor(theme.colors.accent)
HStack{
Text("Counter: \(viewModel.viewState.count)")
.foregroundColor(theme.colors.primaryContent)
Button("-") {
viewModel.send(viewAction: .decrementCount)
}
Button("+") {
viewModel.send(viewAction: .incrementCount)
}
}
.font(theme.fonts.title3)
}
}
/// The action buttons shown at the bottom of the view.
var buttons: some View {
VStack {
Button { viewModel.send(viewAction: .accept) } label: {
Text("Accept")
.font(theme.fonts.bodySB)
}
.buttonStyle(PrimaryActionButtonStyle())
Button { viewModel.send(viewAction: .cancel) } label: {
Text("Cancel")
.font(theme.fonts.body)
.padding(.vertical, 12)
}
}
}
}
// MARK: - Previews
@available(iOS 14.0, *)
struct TemplateSimpleScreen_Previews: PreviewProvider {
static let stateRenderer = MockTemplateSimpleScreenScreenState.stateRenderer
static var previews: some View {
stateRenderer.screenGroup()
}
}
@@ -0,0 +1,45 @@
//
// 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 XCTest
import RiotSwiftUI
@available(iOS 14.0, *)
class TemplateSimpleScreenUITests: MockScreenTest {
override class var screenType: MockScreenState.Type {
return MockTemplateSimpleScreenScreenState.self
}
override class func createTest() -> MockScreenTest {
return TemplateSimpleScreenUITests(selector: #selector(verifyTemplateSimpleScreenScreen))
}
func verifyTemplateSimpleScreenScreen() throws {
guard let screenState = screenState as? MockTemplateSimpleScreenScreenState else { fatalError("no screen") }
switch screenState {
case .promptType(let promptType):
verifyTemplateSimpleScreenPromptType(promptType: promptType)
}
}
func verifyTemplateSimpleScreenPromptType(promptType: TemplateSimpleScreenPromptType) {
let title = app.staticTexts["title"]
XCTAssert(title.exists)
XCTAssertEqual(title.label, promptType.title)
}
}
@@ -0,0 +1,49 @@
//
// 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 XCTest
@testable import RiotSwiftUI
@available(iOS 14.0, *)
class TemplateSimpleScreenViewModelTests: XCTestCase {
private enum Constants {
static let counterInitialValue = 0
}
var viewModel: TemplateSimpleScreenViewModelProtocol!
var context: TemplateSimpleScreenViewModelType.Context!
override func setUpWithError() throws {
viewModel = TemplateSimpleScreenViewModel(promptType: .regular, initialCount: Constants.counterInitialValue)
context = viewModel.context
}
func testInitialState() {
XCTAssertEqual(context.viewState.count, Constants.counterInitialValue)
}
func testCounter() throws {
context.send(viewAction: .incrementCount)
XCTAssertEqual(context.viewState.count, 1)
context.send(viewAction: .incrementCount)
XCTAssertEqual(context.viewState.count, 2)
context.send(viewAction: .decrementCount)
XCTAssertEqual(context.viewState.count, 1)
}
}
+45 -18
View File
@@ -5,27 +5,54 @@ if [ ! $# -eq 2 ]; then
exit 1
fi
MODULE_DIR="../ElementX/Sources/Modules/Screens"
OUTPUT_DIR=$MODULE_DIR/$1
SCREEN_NAME=$2
SCREEN_VAR_NAME=`echo $SCREEN_NAME | awk '{ print tolower(substr($0, 1, 1)) substr($0, 2) }'`
TEMPLATE_DIR=$MODULE_DIR/Templates/SimpleScreenExample/
if [ -e $OUTPUT_DIR ]; then
echo "Error: Folder ${OUTPUT_DIR} already exists"
SCREENS_DIR="../../ElementX/Sources/Screens"/$1
UI_TESTS_DIR="../../UITests/Sources"
UNIT_TESTS_DIR="../../UnitTests/Sources"
if [ -e $SCREENS_DIR ]; then
echo "Error: Folder ${SCREENS_DIR} already exists"
exit 1
fi
echo "Create folder ${OUTPUT_DIR}"
echo "Creating ${SCREENS_DIR}"
mkdir -p $OUTPUT_DIR
cp -R $TEMPLATE_DIR $OUTPUT_DIR/
mkdir -p $SCREENS_DIR
cd $OUTPUT_DIR
for file in $(find * -type f -print)
do
echo "Building ${file/TemplateSimpleScreen/$SCREEN_NAME}..."
perl -p -i -e "s/TemplateSimpleScreen/"$SCREEN_NAME"/g" $file
perl -p -i -e "s/templateSimpleScreen/"$SCREEN_VAR_NAME"/g" $file
echo "Copying screen files"
mv ${file} ${file/TemplateSimpleScreen/$SCREEN_NAME}
done
cp -R "Templates/SimpleScreenExample/ElementX/" $SCREENS_DIR/
echo "Copying tests"
cp -R "Templates/SimpleScreenExample/Tests/UI/" $UI_TESTS_DIR/
cp -R "Templates/SimpleScreenExample/Tests/Unit" $UNIT_TESTS_DIR/
SCREEN_NAME=$2
SCREEN_VAR_NAME=`echo $SCREEN_NAME | awk '{ print tolower(substr($0, 1, 1)) substr($0, 2) }'`
function rename_files {
for file in $(find * -type f -print)
do
perl -p -i -e "s/TemplateSimpleScreen/"$SCREEN_NAME"/g" $file
perl -p -i -e "s/templateSimpleScreen/"$SCREEN_VAR_NAME"/g" $file
mv ${file} ${file/TemplateSimpleScreen/$SCREEN_NAME}
done
}
echo "Renaming files"
cd $SCREENS_DIR
rename_files
cd - > /dev/null
cd $UI_TESTS_DIR
rename_files
cd - > /dev/null
cd $UNIT_TESTS_DIR
rename_files
cd - > /dev/null
echo "Done"