IceCubesApp/Packages/Status/Sources/Status/Editor/StatusEditorView.swift

112 lines
2.7 KiB
Swift
Raw Normal View History

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
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
@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))
}
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-25 05:55:33 +00:00
.padding(.horizontal, DS.Constants.layoutPadding)
2022-12-26 07:24:55 +00:00
.navigationTitle(viewModel.mode.title)
.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
}
} label: {
2022-12-25 16:46:51 +00:00
if viewModel.isPosting {
ProgressView()
} else {
Text("Post")
}
}
}
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
}