Impressia/Vernissage/Views/ComposeView.swift

172 lines
6.9 KiB
Swift
Raw Normal View History

2023-01-06 18:16:08 +01:00
//
// https://mczachurski.dev
// Copyright © 2023 Marcin Czachurski and the repository contributors.
// Licensed under the MIT License.
//
import SwiftUI
2023-02-14 18:40:08 +01:00
import PhotosUI
2023-01-10 11:30:30 +01:00
import MastodonKit
2023-01-06 18:16:08 +01:00
struct ComposeView: View {
2023-01-10 20:38:02 +01:00
enum FocusField: Hashable {
2023-02-14 18:40:08 +01:00
case unknown
2023-01-10 20:38:02 +01:00
case content
}
2023-01-10 11:30:30 +01:00
@EnvironmentObject var applicationState: ApplicationState
2023-02-03 15:16:30 +01:00
@EnvironmentObject var client: Client
2023-01-06 18:16:08 +01:00
@Environment(\.dismiss) private var dismiss
2023-01-10 11:30:30 +01:00
@State var statusViewModel: StatusModel?
2023-01-14 08:52:51 +01:00
@State private var text = String.empty()
2023-02-14 18:40:08 +01:00
@State private var photosPickerVisible = false
@State private var selectedItems: [PhotosPickerItem] = []
@State private var photosData: [Data] = []
2023-01-10 20:38:02 +01:00
@FocusState private var focusedField: FocusField?
2023-01-10 11:30:30 +01:00
private let contentWidth = Int(UIScreen.main.bounds.width) - 50
2023-01-06 18:16:08 +01:00
var body: some View {
NavigationView {
2023-01-10 11:30:30 +01:00
ScrollView {
VStack (alignment: .leading){
if let accountData = applicationState.account {
2023-01-10 11:30:30 +01:00
HStack {
UsernameRow(
2023-01-18 18:41:42 +01:00
accountId: accountData.id,
2023-01-10 11:30:30 +01:00
accountAvatar: accountData.avatar,
accountDisplayName: accountData.displayName,
2023-01-18 18:41:42 +01:00
accountUsername: accountData.username)
2023-01-10 11:30:30 +01:00
Spacer()
}
.padding(8)
}
2023-01-10 20:38:02 +01:00
2023-01-10 11:30:30 +01:00
TextField("Type what's on your mind", text: $text)
.padding(8)
2023-01-10 20:38:02 +01:00
.focused($focusedField, equals: .content)
2023-02-17 08:44:46 +01:00
.keyboardType(.twitter)
2023-01-10 20:38:02 +01:00
.task {
self.focusedField = .content
}
2023-02-14 18:40:08 +01:00
.toolbar {
ToolbarItemGroup(placement: .keyboard) {
HStack(alignment: .center) {
Button {
hideKeyboard()
self.focusedField = .unknown
self.photosPickerVisible = true
} label: {
Image(systemName: "photo.on.rectangle.angled")
}
Spacer()
}
}
}
HStack(alignment: .center) {
ForEach(self.photosData, id: \.self) { photoData in
if let uiImage = UIImage(data: photoData) {
Image(uiImage: uiImage)
.resizable()
.frame(width: 80, height: 80)
.clipShape(RoundedRectangle(cornerRadius: 10))
}
}
}
2023-02-17 08:44:46 +01:00
.padding(8)
2023-02-14 18:40:08 +01:00
2023-01-10 20:38:02 +01:00
if let status = self.statusViewModel {
2023-01-18 18:41:42 +01:00
HStack (alignment: .top) {
2023-01-24 20:38:21 +01:00
UserAvatar(accountAvatar: status.account.avatar, size: .comment)
2023-01-10 11:30:30 +01:00
VStack (alignment: .leading, spacing: 0) {
HStack (alignment: .top) {
2023-01-18 18:41:42 +01:00
Text(statusViewModel?.account.displayNameWithoutEmojis ?? "")
2023-01-10 11:30:30 +01:00
.foregroundColor(.mainTextColor)
.font(.footnote)
.fontWeight(.bold)
2023-01-10 20:38:02 +01:00
2023-01-10 11:30:30 +01:00
Spacer()
}
2023-01-10 20:38:02 +01:00
2023-01-23 18:01:27 +01:00
MarkdownFormattedText(status.content.asMarkdown, withFontSize: 14, andWidth: contentWidth)
.environment(\.openURL, OpenURLAction { url in .handled })
2023-01-10 11:30:30 +01:00
}
}
.padding(8)
.background(Color.selectedRowColor)
}
2023-01-10 20:38:02 +01:00
2023-01-10 11:30:30 +01:00
Spacer()
}
2023-01-06 18:16:08 +01:00
}
2023-01-10 11:30:30 +01:00
.frame(alignment: .topLeading)
2023-01-06 18:16:08 +01:00
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button {
2023-01-10 20:38:02 +01:00
Task {
await self.publishStatus()
dismiss()
2023-01-15 12:41:55 +01:00
ToastrService.shared.showSuccess("Status published", imageSystemName: "message.fill")
2023-01-10 20:38:02 +01:00
}
2023-01-06 18:16:08 +01:00
} label: {
Text("Publish")
.foregroundColor(.white)
}
2023-01-10 20:38:02 +01:00
.disabled(self.text.isEmpty)
2023-01-06 18:16:08 +01:00
.buttonStyle(.borderedProminent)
.tint(.accentColor)
}
ToolbarItem(placement: .cancellationAction) {
Button("Cancel", role: .cancel) {
dismiss()
}
}
}
2023-02-14 18:40:08 +01:00
.onChange(of: self.selectedItems) { selectedItem in
self.photosData = []
for item in self.selectedItems {
item.loadTransferable(type: Data.self) { result in
switch result {
case .success(let data):
if let data {
self.photosData.append(data)
} else {
ToastrService.shared.showError(subtitle: "Cannot show image preview.")
}
case .failure(let error):
ErrorService.shared.handle(error, message: "Cannot retreive image from library.", showToastr: true)
}
}
self.focusedField = .content
}
}
.photosPicker(isPresented: $photosPickerVisible, selection: $selectedItems, maxSelectionCount: 4, matching: .images)
2023-01-06 18:16:08 +01:00
.navigationBarTitle(Text("Compose"), displayMode: .inline)
}
}
2023-01-10 11:30:30 +01:00
2023-01-10 20:38:02 +01:00
private func publishStatus() async {
do {
if let newStatus = try await self.client.statuses?.new(status:Mastodon.Statuses.Components(inReplyToId: self.statusViewModel?.id, text: self.text)) {
let statusModel = StatusModel(status: newStatus)
let commentModel = CommentModel(status: statusModel, showDivider: false)
self.applicationState.newComment = commentModel
}
2023-01-10 20:38:02 +01:00
} catch {
2023-01-15 12:41:55 +01:00
ErrorService.shared.handle(error, message: "Error during post status.", showToastr: true)
2023-01-10 20:38:02 +01:00
}
}
2023-01-06 18:16:08 +01:00
}