element-ios/RiotSwiftUI/Modules/Room/Composer/View/Composer.swift

172 lines
6.9 KiB
Swift
Raw Normal View History

//
// Copyright 2022 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 DSBottomSheet
import SwiftUI
import WysiwygComposer
struct Composer: View {
@Environment(\.theme) private var theme: ThemeSwiftUI
2022-10-11 15:25:58 +00:00
2022-10-11 15:54:27 +00:00
@ObservedObject var viewModel: ComposerViewModelType.Context
2022-10-11 15:25:58 +00:00
@ObservedObject var wysiwygViewModel: WysiwygComposerViewModel
let sendMessageAction: (WysiwygComposerContent) -> Void
let showSendMediaActions: () -> Void
@State private var showSendButton = false
private let horizontalPadding: CGFloat = 12
private let borderHeight: CGFloat = 44
private let minTextViewHeight: CGFloat = 20
private var verticalPadding: CGFloat {
(borderHeight - minTextViewHeight) / 2
}
2022-10-12 11:58:44 +00:00
private var cornerRadius: CGFloat {
viewModel.viewState.shouldDisplayContext ? 14 : borderHeight / 2
}
private var formatItems: [FormatItem] {
FormatType.allCases.map { type in
FormatItem(
type: type,
2022-10-11 15:25:58 +00:00
active: wysiwygViewModel.reversedActions.contains(type.composerAction),
disabled: wysiwygViewModel.disabledActions.contains(type.composerAction)
)
}
}
var body: some View {
VStack {
2022-10-12 11:58:44 +00:00
let rect = RoundedRectangle(cornerRadius: cornerRadius)
// TODO: Fix maximise animation bugs before re-enabling
// ZStack(alignment: .topTrailing) {
VStack {
if viewModel.viewState.shouldDisplayContext {
HStack {
if let imageName = viewModel.viewState.contextImageName {
Image(imageName)
2022-10-12 11:58:44 +00:00
.foregroundColor(theme.colors.tertiaryContent)
}
if let contextDescription = viewModel.viewState.contextDescription {
Text(contextDescription)
2022-10-12 09:06:01 +00:00
.font(.system(size: 12.0, weight: .medium))
.foregroundColor(theme.colors.secondaryContent)
}
Spacer()
Button {
viewModel.send(viewAction: .cancel)
} label: {
Image(Asset.Images.inputCloseIcon.name)
2022-10-12 11:58:44 +00:00
.foregroundColor(theme.colors.tertiaryContent)
}
2022-10-12 09:06:01 +00:00
.frame(height: 30)
}
.padding(.horizontal, horizontalPadding)
2022-10-12 09:06:01 +00:00
.padding(.bottom, -verticalPadding)
}
WysiwygComposerView(
content: wysiwygViewModel.content,
replaceText: wysiwygViewModel.replaceText,
select: wysiwygViewModel.select,
didUpdateText: wysiwygViewModel.didUpdateText
)
.textColor(theme.colors.primaryContent)
.frame(height: wysiwygViewModel.idealHeight)
.padding(.horizontal, horizontalPadding)
.onAppear {
wysiwygViewModel.setup()
}
// Button {
// withAnimation(.easeInOut(duration: 0.25)) {
// viewModel.maximised.toggle()
// }
// } label: {
// Image(viewModel.maximised ? Asset.Images.minimiseComposer.name : Asset.Images.maximiseComposer.name)
// .foregroundColor(theme.colors.tertiaryContent)
// }
// .padding(.top, 4)
// .padding(.trailing, 12)
// }
.padding(.vertical, verticalPadding)
}
.clipShape(rect)
.overlay(rect.stroke(theme.colors.quinaryContent, lineWidth: 2))
.padding(.horizontal, 12)
.padding(.top, 8)
.padding(.bottom, 4)
HStack {
Button {
showSendMediaActions()
} label: {
Image(Asset.Images.startComposeModule.name)
.foregroundColor(theme.colors.tertiaryContent)
.padding(11)
.background(Circle().fill(theme.colors.system))
}
FormattingToolbar(formatItems: formatItems) { type in
2022-10-11 15:25:58 +00:00
wysiwygViewModel.apply(type.action)
}
Spacer()
ZStack {
// TODO: Add support for voice messages
// Button {
//
// } label: {
// Image(Asset.Images.voiceMessageRecordButtonDefault.name)
// .foregroundColor(theme.colors.tertiaryContent)
// }
// .isHidden(showSendButton)
// .isHidden(true)
Button {
2022-10-11 15:25:58 +00:00
sendMessageAction(wysiwygViewModel.content)
wysiwygViewModel.clearContent()
} label: {
2022-10-11 15:25:58 +00:00
if viewModel.viewState.sendMode == .edit {
2022-10-11 15:54:27 +00:00
Image(Asset.Images.saveIcon.name)
2022-10-11 15:25:58 +00:00
.foregroundColor(theme.colors.tertiaryContent)
} else {
2022-10-11 15:54:27 +00:00
Image(Asset.Images.sendIcon.name)
2022-10-11 15:25:58 +00:00
.foregroundColor(theme.colors.tertiaryContent)
}
}
.isHidden(!showSendButton)
}
2022-10-11 15:25:58 +00:00
.onChange(of: wysiwygViewModel.isContentEmpty) { empty in
withAnimation(.easeInOut(duration: 0.25)) {
showSendButton = !empty
}
}
}
.padding(.horizontal, 16)
.padding(.bottom, 4)
.animation(.none)
}
}
}
struct Composer_Previews: PreviewProvider {
static let stateRenderer = MockComposerScreenState.stateRenderer
static var previews: some View {
stateRenderer.screenGroup()
}
}
enum ComposerCreateActionListViewAction {
case selectAction(ComposerCreateAction)
}