2021-07-19 11:12:45 +02:00
|
|
|
//
|
|
|
|
// ComposeView.swift
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-7-16.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
public struct ComposeView: View {
|
|
|
|
|
|
|
|
@EnvironmentObject var viewModel: ComposeViewModel
|
|
|
|
@State var statusEditorViewWidth: CGFloat = .zero
|
|
|
|
|
|
|
|
let horizontalMargin: CGFloat = 20
|
|
|
|
|
|
|
|
public init() { }
|
|
|
|
|
|
|
|
public var body: some View {
|
|
|
|
GeometryReader { proxy in
|
|
|
|
List {
|
|
|
|
// Content Warning
|
|
|
|
if viewModel.isContentWarningComposing {
|
|
|
|
ContentWarningEditorView(
|
|
|
|
contentWarningContent: $viewModel.contentWarningContent,
|
|
|
|
placeholder: viewModel.contentWarningPlaceholder
|
|
|
|
)
|
|
|
|
.padding(EdgeInsets(top: 6, leading: horizontalMargin, bottom: 6, trailing: horizontalMargin))
|
|
|
|
.background(viewModel.contentWarningBackgroundColor)
|
|
|
|
.transition(.opacity)
|
2021-07-20 13:24:24 +02:00
|
|
|
.listRow(backgroundColor: Color(viewModel.backgroundColor))
|
2021-07-19 11:12:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Author
|
|
|
|
StatusAuthorView(
|
|
|
|
avatarImageURL: viewModel.avatarImageURL,
|
|
|
|
name: viewModel.authorName,
|
|
|
|
username: viewModel.authorUsername
|
|
|
|
)
|
|
|
|
.padding(EdgeInsets(top: 20, leading: horizontalMargin, bottom: 16, trailing: horizontalMargin))
|
2021-07-20 13:24:24 +02:00
|
|
|
.listRow(backgroundColor: Color(viewModel.backgroundColor))
|
2021-07-19 11:12:45 +02:00
|
|
|
|
|
|
|
// Editor
|
|
|
|
StatusEditorView(
|
|
|
|
string: $viewModel.statusContent,
|
|
|
|
placeholder: viewModel.statusPlaceholder,
|
|
|
|
width: statusEditorViewWidth,
|
|
|
|
attributedString: viewModel.statusContentAttributedString,
|
2021-07-20 10:40:04 +02:00
|
|
|
keyboardType: .twitter,
|
|
|
|
viewDidAppear: $viewModel.viewDidAppear
|
2021-07-19 11:12:45 +02:00
|
|
|
)
|
|
|
|
.frame(width: statusEditorViewWidth)
|
|
|
|
.frame(minHeight: 100)
|
|
|
|
.padding(EdgeInsets(top: 0, leading: horizontalMargin, bottom: 0, trailing: horizontalMargin))
|
2021-07-20 13:24:24 +02:00
|
|
|
.listRow(backgroundColor: Color(viewModel.backgroundColor))
|
2021-07-19 11:12:45 +02:00
|
|
|
|
|
|
|
// Attachments
|
2021-07-20 10:40:04 +02:00
|
|
|
ForEach(viewModel.attachmentViewModels) { attachmentViewModel in
|
|
|
|
let descriptionBinding = Binding {
|
|
|
|
return attachmentViewModel.descriptionContent
|
|
|
|
} set: { newValue in
|
|
|
|
attachmentViewModel.descriptionContent = newValue
|
|
|
|
}
|
|
|
|
|
2021-07-19 11:12:45 +02:00
|
|
|
StatusAttachmentView(
|
2021-07-20 10:40:04 +02:00
|
|
|
image: attachmentViewModel.thumbnailImage,
|
|
|
|
descriptionPlaceholder: attachmentViewModel.descriptionPlaceholder,
|
|
|
|
description: descriptionBinding,
|
|
|
|
errorPrompt: attachmentViewModel.errorPrompt,
|
|
|
|
errorPromptImage: attachmentViewModel.errorPromptImage,
|
|
|
|
isUploading: attachmentViewModel.isUploading,
|
|
|
|
progressViewTintColor: attachmentViewModel.progressViewTintColor,
|
2021-07-19 11:12:45 +02:00
|
|
|
removeButtonAction: {
|
2021-07-20 10:40:04 +02:00
|
|
|
self.viewModel.removeAttachmentViewModel(attachmentViewModel)
|
2021-07-19 11:12:45 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
.padding(EdgeInsets(top: 16, leading: horizontalMargin, bottom: 0, trailing: horizontalMargin))
|
|
|
|
.fixedSize(horizontal: false, vertical: true)
|
2021-07-20 13:24:24 +02:00
|
|
|
.listRow(backgroundColor: Color(viewModel.backgroundColor))
|
2021-07-19 11:12:45 +02:00
|
|
|
|
|
|
|
// bottom padding
|
|
|
|
Color.clear
|
|
|
|
.frame(height: viewModel.toolbarHeight + 20)
|
2021-07-20 13:24:24 +02:00
|
|
|
.listRow(backgroundColor: Color(viewModel.backgroundColor))
|
2021-07-19 11:12:45 +02:00
|
|
|
} // end List
|
|
|
|
.introspectTableView(customize: { tableView in
|
2021-07-20 10:40:04 +02:00
|
|
|
// tableView.keyboardDismissMode = .onDrag
|
2021-07-19 11:12:45 +02:00
|
|
|
tableView.verticalScrollIndicatorInsets.bottom = viewModel.toolbarHeight
|
|
|
|
})
|
|
|
|
.preference(
|
|
|
|
key: ComposeListViewFramePreferenceKey.self,
|
|
|
|
value: proxy.frame(in: .local)
|
|
|
|
)
|
|
|
|
.onPreferenceChange(ComposeListViewFramePreferenceKey.self) { frame in
|
|
|
|
var frame = frame
|
|
|
|
frame.size.width = frame.width - 2 * horizontalMargin
|
|
|
|
statusEditorViewWidth = frame.width
|
2021-07-20 13:24:24 +02:00
|
|
|
} // end List
|
|
|
|
.introspectTableView(customize: { tableView in
|
|
|
|
tableView.backgroundColor = .clear
|
|
|
|
})
|
|
|
|
.background(Color(viewModel.backgroundColor).ignoresSafeArea())
|
|
|
|
} // end GeometryReader
|
|
|
|
} // end body
|
2021-07-19 11:12:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ComposeListViewFramePreferenceKey: PreferenceKey {
|
|
|
|
static var defaultValue: CGRect = .zero
|
|
|
|
static func reduce(value: inout CGRect, nextValue: () -> CGRect) { }
|
|
|
|
}
|
|
|
|
|
|
|
|
extension View {
|
2021-07-20 13:24:24 +02:00
|
|
|
func listRow(backgroundColor: Color) -> some View {
|
2021-07-19 11:12:45 +02:00
|
|
|
self.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
|
|
|
|
.listRowInsets(EdgeInsets(top: -1, leading: -1, bottom: -1, trailing: -1))
|
2021-07-20 13:24:24 +02:00
|
|
|
.background(backgroundColor)
|
2021-07-19 11:12:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct ComposeView_Previews: PreviewProvider {
|
|
|
|
|
|
|
|
static let viewModel: ComposeViewModel = {
|
|
|
|
let viewModel = ComposeViewModel()
|
|
|
|
return viewModel
|
|
|
|
}()
|
|
|
|
|
|
|
|
static var previews: some View {
|
|
|
|
ComposeView().environmentObject(viewModel)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|