Tusker/Tusker/Screens/Compose/ComposeView.swift

258 lines
8.6 KiB
Swift
Raw Normal View History

2020-08-31 23:28:50 +00:00
//
// ComposeView.swift
// Tusker
//
// Created by Shadowfacts on 8/18/20.
// Copyright © 2020 Shadowfacts. All rights reserved.
//
import SwiftUI
import Pachyderm
import Combine
@propertyWrapper struct OptionalStateObject<T: ObservableObject>: DynamicProperty {
private class Republisher: ObservableObject {
var cancellable: AnyCancellable?
var wrapped: T? {
didSet {
cancellable?.cancel()
cancellable = wrapped?.objectWillChange
.receive(on: RunLoop.main)
.sink { [unowned self] _ in
self.objectWillChange.send()
}
}
}
}
@StateObject private var republisher = Republisher()
@State private var object: T?
var wrappedValue: T? {
get {
object
}
nonmutating set {
object = newValue
}
}
func update() {
republisher.wrapped = wrappedValue
}
}
2020-08-31 23:28:50 +00:00
struct ComposeView: View {
@ObservedObject var draft: Draft
@EnvironmentObject var mastodonController: MastodonController
@EnvironmentObject var uiState: ComposeUIState
2020-09-09 22:33:59 +00:00
@OptionalStateObject private var poster: PostService?
2020-09-09 22:33:59 +00:00
@State private var isShowingPostErrorAlert = false
@State private var postError: PostService.Error?
private var isPosting: Bool {
poster != nil
}
2020-08-31 23:28:50 +00:00
private let stackPadding: CGFloat = 8
init(draft: Draft) {
2020-08-31 23:28:50 +00:00
self.draft = draft
}
var charactersRemaining: Int {
let limit = mastodonController.instanceFeatures.maxStatusChars
2020-08-31 23:28:50 +00:00
let cwCount = draft.contentWarningEnabled ? draft.contentWarning.count : 0
return limit - (cwCount + CharacterCounter.count(text: draft.text, for: mastodonController.instance))
2020-08-31 23:28:50 +00:00
}
var requiresAttachmentDescriptions: Bool {
guard Preferences.shared.requireAttachmentDescriptions else { return false }
let attachmentIds = draft.attachments.map(\.id)
return attachmentIds.contains { uiState.attachmentsMissingDescriptions.contains($0) }
}
var postButtonEnabled: Bool {
2021-05-04 03:12:59 +00:00
draft.hasContent && charactersRemaining >= 0 && !isPosting && !requiresAttachmentDescriptions && (draft.poll == nil || draft.poll!.options.allSatisfy { !$0.text.isEmpty })
2020-08-31 23:28:50 +00:00
}
var body: some View {
2021-02-06 18:47:45 +00:00
mostOfTheBody.toolbar {
ToolbarItem(placement: .cancellationAction) { cancelButton }
ToolbarItem(placement: .confirmationAction) { postButton }
2020-08-31 23:28:50 +00:00
}
}
var mostOfTheBody: some View {
ZStack(alignment: .top) {
GeometryReader { (outer) in
ScrollView(.vertical) {
mainStack(outerMinY: outer.frame(in: .global).minY)
}
2020-08-31 23:28:50 +00:00
}
if let poster = poster {
// can't use SwiftUI.ProgressView because there's no UIProgressView.Style.bar equivalent, see FB8587149
WrappedProgressView(value: poster.currentStep, total: poster.totalSteps)
}
autocompleteSuggestions
2020-08-31 23:28:50 +00:00
}
.navigationBarTitle("Compose")
.actionSheet(isPresented: $uiState.isShowingSaveDraftSheet, content: self.saveAndCloseSheet)
.alert(isPresented: $isShowingPostErrorAlert) {
Alert(
title: Text("Error Posting Status"),
message: Text(postError?.localizedDescription ?? ""),
dismissButton: .default(Text("OK"))
)
}
}
@ViewBuilder
var autocompleteSuggestions: some View {
2021-02-06 18:47:45 +00:00
VStack(spacing: 0) {
Spacer()
if let state = uiState.autocompleteState {
ComposeAutocompleteView(autocompleteState: state)
}
}
2021-02-06 18:47:45 +00:00
.transition(.move(edge: .bottom))
.animation(.default)
}
func mainStack(outerMinY: CGFloat) -> some View {
2020-08-31 23:28:50 +00:00
VStack(alignment: .leading, spacing: 8) {
if let id = draft.inReplyToID,
let status = mastodonController.persistentContainer.status(for: id) {
ComposeReplyView(
status: status,
stackPadding: stackPadding,
outerMinY: outerMinY
2020-08-31 23:28:50 +00:00
)
}
header
if draft.contentWarningEnabled {
2021-05-04 03:12:59 +00:00
ComposeEmojiTextField(text: $draft.contentWarning, placeholder: "Write your warning here")
2020-08-31 23:28:50 +00:00
}
MainComposeTextView(
draft: draft,
placeholder: Text("What's on your mind?")
)
2021-05-04 03:12:59 +00:00
if let poll = draft.poll {
ComposePollView(draft: draft, poll: poll)
.transition(.opacity.combined(with: .asymmetric(insertion: .scale(scale: 0.5, anchor: .leading), removal: .scale(scale: 0.5, anchor: .trailing))))
.animation(.default)
}
2020-08-31 23:28:50 +00:00
ComposeAttachmentsList(
draft: draft
)
// the list rows provide their own padding, so we cancel out the extra spacing from the VStack
.padding([.top, .bottom], -8)
}
.disabled(isPosting)
2020-08-31 23:28:50 +00:00
.padding(stackPadding)
.padding(.bottom, uiState.autocompleteState != nil ? 46 : nil)
2020-08-31 23:28:50 +00:00
}
private var header: some View {
HStack(alignment: .top) {
ComposeCurrentAccount()
Spacer()
Text(verbatim: charactersRemaining.description)
.foregroundColor(charactersRemaining < 0 ? .red : .secondary)
.font(Font.body.monospacedDigit())
2020-09-13 17:19:56 +00:00
.accessibility(label: Text(charactersRemaining < 0 ? "\(-charactersRemaining) characters too many" : "\(charactersRemaining) characters remaining"))
2020-08-31 23:28:50 +00:00
}.frame(height: 50)
}
private var cancelButton: some View {
Button(action: self.cancel) {
Text("Cancel")
// otherwise all Buttons in the nav bar are made semibold
.font(.system(size: 17, weight: .regular))
}
}
private var postButton: some View {
Button {
Task {
await self.postStatus()
}
} label: {
2020-08-31 23:28:50 +00:00
Text("Post")
}
.disabled(!postButtonEnabled)
}
private func cancel() {
if Preferences.shared.automaticallySaveDrafts {
// draft is already stored in drafts manager, drafts manager is saved by ComposeHostingController.viewWillDisappear
uiState.delegate?.dismissCompose(mode: .cancel)
2020-08-31 23:28:50 +00:00
} else {
// if the draft doesn't have content, it doesn't need to be saved
if draft.hasContent {
uiState.isShowingSaveDraftSheet = true
} else {
DraftsManager.shared.remove(draft)
uiState.delegate?.dismissCompose(mode: .cancel)
}
2020-08-31 23:28:50 +00:00
}
}
private func saveAndCloseSheet() -> ActionSheet {
ActionSheet(title: Text("Do you want to save the current post as a draft?"), buttons: [
.default(Text("Save Draft"), action: {
// draft is already stored in drafts manager, drafts manager is saved by ComposeHostingController.viewWillDisappear
uiState.isShowingSaveDraftSheet = false
uiState.delegate?.dismissCompose(mode: .cancel)
2020-08-31 23:28:50 +00:00
}),
.destructive(Text("Delete Draft"), action: {
DraftsManager.shared.remove(draft)
uiState.isShowingSaveDraftSheet = false
uiState.delegate?.dismissCompose(mode: .cancel)
2020-08-31 23:28:50 +00:00
}),
.cancel(),
])
}
private func postStatus() async {
guard !isPosting,
draft.hasContent else {
return
}
let poster = PostService(mastodonController: mastodonController, draft: draft)
self.poster = poster
do {
try await poster.post()
// wait .25 seconds so the user can see the progress bar has completed
try? await Task.sleep(nanoseconds: 250_000_000)
uiState.delegate?.dismissCompose(mode: .post)
} catch let error as PostService.Error {
self.isShowingPostErrorAlert = true
self.postError = error
} catch {
fatalError("Unreachable")
2020-09-09 22:33:59 +00:00
}
self.poster = nil
2020-08-31 23:28:50 +00:00
}
}
//struct ComposeView_Previews: PreviewProvider {
// static var previews: some View {
// ComposeView()
// }
//}