2022-12-23 09:41:55 +00:00
|
|
|
import SwiftUI
|
2022-12-25 05:55:33 +00:00
|
|
|
import Accounts
|
|
|
|
import Env
|
|
|
|
import DesignSystem
|
|
|
|
import TextView
|
2022-12-25 07:17:16 +00:00
|
|
|
import Models
|
|
|
|
import Network
|
2022-12-25 18:15:35 +00:00
|
|
|
import PhotosUI
|
2022-12-23 09:41:55 +00:00
|
|
|
|
|
|
|
public struct StatusEditorView: View {
|
2022-12-25 07:17:16 +00:00
|
|
|
@EnvironmentObject private var client: Client
|
2022-12-25 05:55:33 +00:00
|
|
|
@EnvironmentObject private var currentAccount: CurrentAccount
|
2022-12-23 09:41:55 +00:00
|
|
|
@Environment(\.dismiss) private var dismiss
|
|
|
|
|
2022-12-25 07:17:16 +00:00
|
|
|
@StateObject private var viewModel: StatusEditorViewModel
|
2022-12-25 05:55:33 +00:00
|
|
|
|
2022-12-26 07:24:55 +00:00
|
|
|
public init(mode: StatusEditorViewModel.Mode) {
|
|
|
|
_viewModel = StateObject(wrappedValue: .init(mode: mode))
|
2022-12-23 09:41:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public var body: some View {
|
|
|
|
NavigationStack {
|
2022-12-25 07:17:16 +00:00
|
|
|
ZStack(alignment: .bottom) {
|
2022-12-25 18:15:35 +00:00
|
|
|
VStack(spacing: 12) {
|
2022-12-25 07:17:16 +00:00
|
|
|
accountHeaderView
|
|
|
|
TextView($viewModel.statusText)
|
|
|
|
.placeholder("What's on your mind")
|
2022-12-25 18:15:35 +00:00
|
|
|
mediasView
|
2022-12-25 07:17:16 +00:00
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
accessoryView
|
|
|
|
.padding(.bottom, 12)
|
|
|
|
}
|
|
|
|
.onAppear {
|
|
|
|
viewModel.client = client
|
2022-12-26 07:24:55 +00:00
|
|
|
viewModel.prepareStatusText()
|
2022-12-23 09:41:55 +00:00
|
|
|
}
|
2022-12-25 05:55:33 +00:00
|
|
|
.padding(.horizontal, DS.Constants.layoutPadding)
|
2022-12-26 07:24:55 +00:00
|
|
|
.navigationTitle(viewModel.mode.title)
|
2022-12-23 09:41:55 +00:00
|
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
|
|
.toolbar {
|
|
|
|
ToolbarItem(placement: .navigationBarTrailing) {
|
|
|
|
Button {
|
2022-12-25 07:17:16 +00:00
|
|
|
Task {
|
2022-12-25 16:46:51 +00:00
|
|
|
let status = await viewModel.postStatus()
|
|
|
|
if status != nil {
|
|
|
|
dismiss()
|
|
|
|
}
|
2022-12-25 07:17:16 +00:00
|
|
|
}
|
2022-12-23 09:41:55 +00:00
|
|
|
} label: {
|
2022-12-25 16:46:51 +00:00
|
|
|
if viewModel.isPosting {
|
|
|
|
ProgressView()
|
|
|
|
} else {
|
|
|
|
Text("Post")
|
|
|
|
}
|
2022-12-23 09:41:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ToolbarItem(placement: .navigationBarLeading) {
|
|
|
|
Button {
|
|
|
|
dismiss()
|
|
|
|
} label: {
|
|
|
|
Text("Cancel")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-25 05:55:33 +00:00
|
|
|
|
2022-12-25 18:15:35 +00:00
|
|
|
@ViewBuilder
|
|
|
|
private var accountHeaderView: some View {
|
2022-12-25 05:55:33 +00:00
|
|
|
if let account = currentAccount.account {
|
|
|
|
HStack {
|
|
|
|
AvatarView(url: account.avatar, size: .status)
|
|
|
|
VStack(alignment: .leading, spacing: 0) {
|
|
|
|
account.displayNameWithEmojis
|
|
|
|
.font(.subheadline)
|
|
|
|
.fontWeight(.semibold)
|
|
|
|
Text("@\(account.acct)")
|
|
|
|
.font(.footnote)
|
|
|
|
.foregroundColor(.gray)
|
|
|
|
}
|
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-25 07:17:16 +00:00
|
|
|
|
2022-12-25 18:15:35 +00:00
|
|
|
private var mediasView: some View {
|
|
|
|
ScrollView(.horizontal) {
|
|
|
|
HStack {
|
|
|
|
ForEach(viewModel.mediasImages) { container in
|
|
|
|
Image(uiImage: container.image)
|
|
|
|
.resizable()
|
|
|
|
.aspectRatio(contentMode: .fill)
|
|
|
|
.frame(width: 150, height: 150)
|
|
|
|
.clipped()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-25 07:17:16 +00:00
|
|
|
private var accessoryView: some View {
|
|
|
|
HStack {
|
2022-12-25 18:15:35 +00:00
|
|
|
PhotosPicker(selection: $viewModel.selectedMedias,
|
|
|
|
matching: .images) {
|
2022-12-25 07:17:16 +00:00
|
|
|
Image(systemName: "photo.fill.on.rectangle.fill")
|
|
|
|
}
|
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
}
|
2022-12-25 05:55:33 +00:00
|
|
|
|
2022-12-23 09:41:55 +00:00
|
|
|
}
|