Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move alt text editor to a separate view controller #864

Draft
wants to merge 6 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions Mastodon.xcworkspace/xcshareddata/swiftpm/Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,6 @@
"version": "2.6.1"
}
},
{
"package": "UIHostingConfigurationBackport",
"repositoryURL": "https://github.com/woxtu/UIHostingConfigurationBackport.git",
"state": {
"branch": null,
"revision": "6091f2d38faa4b24fc2ca0389c651e2f666624a3",
"version": "0.1.0"
}
},
{
"package": "UITextView+Placeholder",
"repositoryURL": "https://github.com/MainasuK/UITextView-Placeholder.git",
Expand Down
18 changes: 0 additions & 18 deletions MastodonSDK/Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,6 @@
"version" : "2.13.0"
}
},
{
"identity" : "thirdpartymailer",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vtourraine/ThirdPartyMailer.git",
"state" : {
"revision" : "44c1cfaa6969963f22691aa67f88a69e3b6d651f",
"version" : "2.1.0"
}
},
{
"identity" : "tocropviewcontroller",
"kind" : "remoteSourceControl",
Expand All @@ -234,15 +225,6 @@
"version" : "2.6.1"
}
},
{
"identity" : "uihostingconfigurationbackport",
"kind" : "remoteSourceControl",
"location" : "https://github.com/woxtu/UIHostingConfigurationBackport.git",
"state" : {
"revision" : "6091f2d38faa4b24fc2ca0389c651e2f666624a3",
"version" : "0.1.0"
}
},
{
"identity" : "uitextview-placeholder",
"kind" : "remoteSourceControl",
Expand Down
8 changes: 3 additions & 5 deletions MastodonSDK/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ let package = Package(
name: "MastodonSDKDynamic",
type: .dynamic,
targets: publicLibraryTargets
)
),
],
dependencies: [
.package(name: "ArkanaKeys", path: "../dependencies/ArkanaKeys"),
Expand All @@ -53,7 +53,6 @@ let package = Package(
.package(url: "https://github.com/TwidereProject/MetaTextKit.git", exact: "2.2.5"),
.package(url: "https://github.com/TwidereProject/TabBarPager.git", from: "0.1.0"),
.package(url: "https://github.com/uias/Tabman", from: "2.13.0"),
.package(url: "https://github.com/woxtu/UIHostingConfigurationBackport.git", from: "0.1.0"),
.package(url: "https://github.com/SDWebImage/SDWebImage.git", from: "5.12.0"),
.package(url: "https://github.com/eneko/Stripes.git", from: "0.2.0"),
.package(url: "https://github.com/onevcat/Kingfisher.git", from: "7.4.1"),
Expand All @@ -68,7 +67,7 @@ let package = Package(
"MastodonCommon",
],
exclude: [
"Template/Stencil"
"Template/Stencil",
]
),
.target(
Expand Down Expand Up @@ -97,7 +96,7 @@ let package = Package(
.product(name: "CommonOSLog", package: "CommonOSLog"),
.product(name: "ArkanaKeys", package: "ArkanaKeys"),
.product(name: "KeychainAccess", package: "KeychainAccess"),
.product(name: "MetaTextKit", package: "MetaTextKit")
.product(name: "MetaTextKit", package: "MetaTextKit"),
]
),
.target(
Expand All @@ -123,7 +122,6 @@ let package = Package(
.product(name: "Nuke", package: "Nuke"),
.product(name: "Introspect", package: "SwiftUI-Introspect"),
.product(name: "UITextView+Placeholder", package: "UITextView-Placeholder"),
.product(name: "UIHostingConfigurationBackport", package: "UIHostingConfigurationBackport"),
.product(name: "TabBarPager", package: "TabBarPager"),
.product(name: "OrderedCollections", package: "swift-collections"),
.product(name: "Tabman", package: "Tabman"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//
// AttachmentDescriptionComposerView.swift
//
//
// Created by Jed Fox on 2023-01-06.
//

import SwiftUI
import VisionKit

struct AttachmentDescriptionComposerView: View {
internal init(prompt: String, thumbnail: UIImage, description: Binding<String>) {
self.prompt = prompt
self.thumbnail = thumbnail
self._savedDescription = description
self._description = State(initialValue: description.wrappedValue)
self._analysis = StateObject(wrappedValue: ImageAnalyzerModel(image: thumbnail))
}

let prompt: String
let thumbnail: UIImage
@Binding var savedDescription: String

@Environment(\.dismiss) private var dismiss
@State private var description: String
@StateObject private var analysis: ImageAnalyzerModel

var body: some View {
NavigationView {
Form {
Section(content: {}, footer: {
HStack {
Spacer(minLength: 0)
Image(uiImage: thumbnail)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(maxHeight: 200)
.cornerRadius(4)
.overlay(alignment: .bottomTrailing) {
if let transcript = analysis.transcript, !description.contains(transcript) {
Button {
if description.isEmpty {
description = transcript
} else {
description += "\n\n" + transcript
}
} label: {
Label("Use Detected Text", systemImage: "text.viewfinder")
.foregroundColor(.black)
}
.buttonStyle(.borderedProminent)
.buttonBorderShape(.capsule)
.tint(.white)
.padding()
.transition(.opacity)
}
}
.animation(.default, value: analysis.transcript)
Spacer(minLength: 0)
}
})
Section(header: Text(prompt)) {
TextEditor(text: $description)
}
}.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") {
dismiss()
}
}
ToolbarItem(placement: .confirmationAction) {
Button("Save") {
savedDescription = description
dismiss()
}
}
}
}
}
}

@MainActor private class ImageAnalyzerModel: ObservableObject {
let image: UIImage

@Published var transcript: String?

private var analysisTask: Task<Void, Error>?

init(image: UIImage) {
self.image = image

guard #available(iOS 16, *), ImageAnalyzer.isSupported else { return }
let analyzer = ImageAnalyzer()
analysisTask = Task.detached(priority: .high) { [weak self] in
let result = try await analyzer.analyze(image, configuration: .init(.text))
await MainActor.run { [self] in
self?.transcript = result.transcript
}
}
}

deinit {
analysisTask?.cancel()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@ import Introspect
public struct AttachmentView: View {

@ObservedObject var viewModel: AttachmentViewModel


@State private var isEditingDescription = false

var blurEffect: UIBlurEffect {
UIBlurEffect(style: .systemUltraThinMaterialDark)
}

public var body: some View {
let thumbnail = viewModel.thumbnail ?? .placeholder(color: .secondarySystemFill)
Color.clear.aspectRatio(358.0/232.0, contentMode: .fill)
.overlay(
ZStack {
let image = viewModel.thumbnail ?? .placeholder(color: .secondarySystemFill)
Image(uiImage: image)
Image(uiImage: thumbnail)
.resizable()
.aspectRatio(contentMode: .fill)
.allowsHitTesting(false)
Expand All @@ -46,7 +48,7 @@ public struct AttachmentView: View {
}
}()
Spacer()
ZStack(alignment: .bottom) {
ZStack(alignment: .bottomLeading) {
Rectangle()
.fill(
LinearGradient(
Expand All @@ -57,20 +59,21 @@ public struct AttachmentView: View {
)
.frame(height: 80)

TextField("", text: $viewModel.caption)
.lineLimit(1)
.textFieldStyle(.plain)
.font(.footnote)
.foregroundColor(Color(UIColor.white.withAlphaComponent(0.8)))
.padding(EdgeInsets(top: 0, leading: 8, bottom: 0, trailing: 4))
.placeholder(when: viewModel.caption.isEmpty) {
Text(placeholder)
.font(.footnote)
.foregroundColor(Color(UIColor.white.withAlphaComponent(0.8)))
.padding(EdgeInsets(top: 0, leading: 8, bottom: 0, trailing: 4))
.lineLimit(1)
}
.padding(EdgeInsets(top: 6, leading: 0, bottom: 10, trailing: 0))
Button {
isEditingDescription = true
} label: {
Label(viewModel.caption.isEmpty ? placeholder : viewModel.caption, systemImage: "pencil")
.lineLimit(1)
.font(.footnote)
.foregroundColor(.white.opacity(0.8))
.padding(EdgeInsets(top: 6, leading: 8, bottom: 10, trailing: 4))
}
}.sheet(isPresented: $isEditingDescription) {
AttachmentDescriptionComposerView(
prompt: placeholder,
thumbnail: thumbnail,
description: $viewModel.caption
)
}
}
)
Expand Down Expand Up @@ -196,29 +199,3 @@ public struct AttachmentView: View {
} // end body

}

// https://stackoverflow.com/a/57715771/3797903
extension View {
fileprivate func placeholder<Content: View>(
when shouldShow: Bool,
alignment: Alignment = .leading,
@ViewBuilder placeholder: () -> Content) -> some View {

ZStack(alignment: alignment) {
placeholder().opacity(shouldShow ? 1 : 0)
self
}
}

fileprivate func placeholder(
_ text: String,
when shouldShow: Bool,
alignment: Alignment = .leading) -> some View {

placeholder(when: shouldShow, alignment: alignment) {
Text(text)
.foregroundColor(.white.opacity(0.7))
.lineLimit(1)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import UIKit
import MastodonCore
import MastodonSDK
import CoreDataStack
import UIHostingConfigurationBackport

extension ComposeContentViewModel {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import os.log
import UIKit
import UIHostingConfigurationBackport

final class ComposeContentTableViewCell: UITableViewCell {

Expand Down
Loading