element-ios/RiotSwiftUI/Modules/Spaces/SpaceCreation/SpaceCreationSettings/View/SpaceCreationSettings.swift
Gil Eluard 30c3c3da3b [iOS] Create public space #143
- Update after design review
2021-12-01 23:56:59 +01:00

180 lines
7.4 KiB
Swift

// File created from TemplateAdvancedRoomsExample
// $ createSwiftUITwoScreen.sh Spaces/SpaceCreation SpaceCreation SpaceCreationMenu SpaceCreationSettings
//
// 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 Combine
@available(iOS 14.0, *)
struct SpaceCreationSettings: View {
// MARK: - Properties
@ObservedObject var viewModel: SpaceCreationSettingsViewModel.Context
// MARK: Private
@Environment(\.theme) private var theme: ThemeSwiftUI
// MARK: Public
@ViewBuilder
var body: some View {
VStack {
ThemableNavigationBar(title: nil, showBackButton: true) {
viewModel.send(viewAction: .back)
} closeAction: {
viewModel.send(viewAction: .cancel)
}
mainView
}
.background(theme.colors.background)
.navigationBarHidden(true)
}
// MARK: - Private
@ViewBuilder
private var mainView: some View {
VStack(alignment: .center) {
formView
footerView
}
.padding(EdgeInsets(top: 24, leading: 16, bottom: 24, trailing: 16))
}
@ViewBuilder
private var headerView: some View {
VStack(alignment: .center, spacing: nil) {
Text(VectorL10n.spacesCreationSettingsMessage).multilineTextAlignment(.center)
Spacer().frame(height: 22)
}
}
@ViewBuilder
private var avatarView: some View {
ZStack(alignment: .bottomTrailing) {
GeometryReader { reader in
ZStack {
SpaceAvatarImage(mxContentUri: viewModel.viewState.avatar.mxContentUri, matrixItemId: viewModel.viewState.avatar.matrixItemId, displayName: viewModel.viewState.avatar.displayName, size: .xxLarge)
.padding(6)
if let image = viewModel.viewState.avatarImage {
Image(uiImage: image)
.resizable()
.scaledToFill()
.frame(width: 80, height: 80, alignment: .center)
.clipShape(RoundedRectangle(cornerRadius: 8))
}
}.padding(10)
.gesture(TapGesture().onEnded { _ in
viewModel.send(viewAction: .pickImage(reader.frame(in: .global)))
})
}
Image(uiImage: Asset.Images.spaceCreationCamera.image)
.renderingMode(.template)
.foregroundColor(theme.colors.secondaryContent)
.frame(width: 32, height: 32, alignment: .center)
.background(theme.colors.background)
.clipShape(Circle())
}.frame(width: 104, height: 104)
}
@ViewBuilder
private var formView: some View {
GeometryReader { geometryReader in
ScrollView {
ScrollViewReader { scrollViewReader in
VStack {
headerView
Spacer()
avatarView
Spacer().frame(height:40)
RoundedBorderTextField(title: VectorL10n.createRoomPlaceholderName, placeHolder: "", text: $viewModel.roomName, footerText: .constant(viewModel.viewState.roomNameError), isError: .constant(true), isFirstResponder: true, configuration: UIKitTextInputConfiguration( returnKeyType: .next), onTextChanged: { newText in
viewModel.send(viewAction: .nameChanged(newText))
}, onEditingChanged: { editing in
// if editing {
// scrollDown(reader: scrollViewReader)
// }
})
.id("nameTextField")
.padding(.horizontal, 2)
.padding(.bottom, 20)
RoundedBorderTextEditor(title: nil, placeHolder: VectorL10n.spaceTopic, text: $viewModel.topic, textMaxHeight: 72, error: .constant(nil), onTextChanged: {
newText in
viewModel.send(viewAction: .topicChanged(newText))
}, onEditingChanged: { editing in
if editing {
scrollDown(reader: scrollViewReader)
}
})
.id("topicTextEditor")
.padding(.horizontal, 2)
.padding(.bottom, viewModel.viewState.showRoomAddress ? 20 : 3)
if viewModel.viewState.showRoomAddress {
RoundedBorderTextField(title: VectorL10n.spacesCreationAddress, placeHolder: "# \(viewModel.viewState.defaultAddress)", text: $viewModel.address, footerText: .constant(viewModel.viewState.addressMessage), isError: .constant(!viewModel.viewState.isAddressValid), configuration: UIKitTextInputConfiguration(keyboardType: .URL, returnKeyType: .done, autocapitalizationType: .none), onTextChanged: {
newText in
viewModel.send(viewAction: .addressChanged(newText))
}, onEditingChanged: { editing in
// if editing {
// scrollDown(reader: scrollViewReader)
// }
})
.id("addressTextField")
.accessibility(identifier: "addressTextField")
.padding(.horizontal, 2)
.padding(.bottom, 3)
}
}
.animation(.easeOut(duration: 0.2))
.frame(minHeight: geometryReader.size.height - 2)
}
}
}
}
@ViewBuilder
private var footerView: some View {
ThemableButton(icon: nil, title: VectorL10n.next) {
ResponderManager.resignFirstResponder()
viewModel.send(viewAction: .done)
}
}
private func scrollDown(reader: ScrollViewProxy) {
let identifier = viewModel.viewState.showRoomAddress ? "addressTextField" : "topicTextEditor"
DispatchQueue.main.async {
withAnimation {
reader.scrollTo(identifier, anchor: .bottom)
}
}
}
}
// MARK: - Previews
@available(iOS 14.0, *)
struct SpaceCreationSettings_Previews: PreviewProvider {
static let stateRenderer = MockSpaceCreationSettingsScreenState.stateRenderer
static var previews: some View {
Group {
stateRenderer.screenGroup(addNavigation: true)
.theme(.light).preferredColorScheme(.light)
stateRenderer.screenGroup(addNavigation: true)
.theme(.dark).preferredColorScheme(.dark)
}
}
}