Compare commits

..

5 Commits

10 changed files with 108 additions and 50 deletions

View File

@ -56,6 +56,7 @@ private let imageType = UTType.image.identifier
private let mp4Type = UTType.mpeg4Movie.identifier
private let quickTimeType = UTType.quickTimeMovie.identifier
private let dataType = UTType.data.identifier
private let gifType = UTType.gif.identifier
extension CompositionAttachment: NSItemProviderWriting {
static var writableTypeIdentifiersForItemProvider: [String] {
@ -95,20 +96,22 @@ extension CompositionAttachment: NSItemProviderReading {
[typeIdentifier] + UIImage.readableTypeIdentifiersForItemProvider + [mp4Type, quickTimeType] + NSURL.readableTypeIdentifiersForItemProvider
}
static func object(withItemProviderData data: Data, typeIdentifier: String) throws -> Self {
static func object(withItemProviderData data: Data, typeIdentifier: String) throws -> CompositionAttachment {
if typeIdentifier == CompositionAttachment.typeIdentifier {
return try PropertyListDecoder().decode(Self.self, from: data)
return try PropertyListDecoder().decode(CompositionAttachment.self, from: data)
} else if typeIdentifier == gifType {
return CompositionAttachment(data: .gif(data))
} else if UIImage.readableTypeIdentifiersForItemProvider.contains(typeIdentifier), let image = try? UIImage.object(withItemProviderData: data, typeIdentifier: typeIdentifier) {
return CompositionAttachment(data: .image(image)) as! Self
return CompositionAttachment(data: .image(image))
} else if let type = UTType(typeIdentifier), type == .mpeg4Movie || type == .quickTimeMovie {
let temporaryDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
let temporaryFileName = ProcessInfo().globallyUniqueString
let fileExt = type.preferredFilenameExtension!
let temporaryFileURL = temporaryDirectoryURL.appendingPathComponent(temporaryFileName).appendingPathExtension(fileExt)
try data.write(to: temporaryFileURL)
return CompositionAttachment(data: .video(temporaryFileURL)) as! Self
return CompositionAttachment(data: .video(temporaryFileURL))
} else if NSURL.readableTypeIdentifiersForItemProvider.contains(typeIdentifier), let url = try? NSURL.object(withItemProviderData: data, typeIdentifier: typeIdentifier) as URL {
return CompositionAttachment(data: .video(url)) as! Self
return CompositionAttachment(data: .video(url))
} else {
throw ItemProviderError.incompatibleTypeIdentifier
}

View File

@ -16,6 +16,7 @@ enum CompositionAttachmentData {
case image(UIImage)
case video(URL)
case drawing(PKDrawing)
case gif(Data)
var type: AttachmentType {
switch self {
@ -27,6 +28,8 @@ enum CompositionAttachmentData {
return .video
case .drawing(_):
return .image
case .gif(_):
return .image
}
}
@ -119,6 +122,8 @@ enum CompositionAttachmentData {
case let .drawing(drawing):
let image = drawing.imageInLightMode(from: drawing.bounds, scale: 1)
completion(.success((image.pngData()!, .png)))
case let .gif(data):
completion(.success((data, .gif)))
}
}
@ -191,6 +196,8 @@ extension CompositionAttachmentData: Codable {
try container.encode("drawing", forKey: .type)
let drawingData = drawing.dataRepresentation()
try container.encode(drawingData, forKey: .drawing)
case .gif(_):
throw EncodingError.invalidValue(self, EncodingError.Context(codingPath: [], debugDescription: "gif CompositionAttachments cannot be encoded"))
}
}
@ -214,7 +221,7 @@ extension CompositionAttachmentData: Codable {
let drawing = try PKDrawing(data: drawingData)
self = .drawing(drawing)
default:
throw DecodingError.dataCorruptedError(forKey: .type, in: container, debugDescription: "CompositionAttachment type must be one of 'image' or 'asset'")
throw DecodingError.dataCorruptedError(forKey: .type, in: container, debugDescription: "CompositionAttachment type must be one of image, asset, or drawing")
}
}

View File

@ -13,18 +13,14 @@ import AVKit
class AssetPreviewViewController: UIViewController {
let attachment: CompositionAttachmentData
let asset: PHAsset
init(attachment: CompositionAttachmentData) {
self.attachment = attachment
init(asset: PHAsset) {
self.asset = asset
super.init(nibName: nil, bundle: nil)
}
convenience init(asset: PHAsset) {
self.init(attachment: .asset(asset))
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@ -34,12 +30,6 @@ class AssetPreviewViewController: UIViewController {
view.backgroundColor = .black
switch attachment {
case let .image(image):
showImage(image)
case let .video(url):
showVideo(asset: AVURLAsset(url: url))
case let .asset(asset):
switch asset.mediaType {
case .image:
if asset.mediaSubtypes.contains(.photoLive) {
@ -52,10 +42,6 @@ class AssetPreviewViewController: UIViewController {
default:
fatalError("asset mediaType must be image or video")
}
case let .drawing(drawing):
let image = drawing.imageInLightMode(from: drawing.bounds)
showImage(image)
}
}
func showImage(_ image: UIImage) {

View File

@ -13,6 +13,7 @@ struct ComposeAttachmentImage: View {
let attachment: CompositionAttachment
let fullSize: Bool
@State private var gifData: Data? = nil
@State private var image: UIImage? = nil
@State private var imageContentMode: ContentMode = .fill
@State private var imageBackgroundColor: Color = .black
@ -20,7 +21,9 @@ struct ComposeAttachmentImage: View {
@Environment(\.colorScheme) private var colorScheme: ColorScheme
var body: some View {
if let image = image {
if let gifData {
GIFViewWrapper(gifData: gifData)
} else if let image {
Image(uiImage: image)
.resizable()
.aspectRatio(contentMode: imageContentMode)
@ -54,11 +57,25 @@ struct ComposeAttachmentImage: View {
// currently only used as thumbnail in ComposeAttachmentRow
size = CGSize(width: 80, height: 80)
}
let isGIF = PHAssetResource.assetResources(for: asset).contains(where: { $0.uniformTypeIdentifier == UTType.gif.identifier })
if isGIF {
PHImageManager.default().requestImageDataAndOrientation(for: asset, options: nil) { data, typeIdentifier, orientation, info in
if typeIdentifier == UTType.gif.identifier {
self.gifData = data
} else if let data {
let image = UIImage(data: data)
DispatchQueue.main.async {
self.image = image
}
}
}
} else {
PHImageManager.default().requestImage(for: asset, targetSize: size, contentMode: .aspectFill, options: nil) { (image, _) in
DispatchQueue.main.async {
self.image = image
}
}
}
case let .video(url):
let asset = AVURLAsset(url: url)
let imageGenerator = AVAssetImageGenerator(asset: asset)
@ -69,10 +86,35 @@ struct ComposeAttachmentImage: View {
image = drawing.imageInLightMode(from: drawing.bounds)
imageContentMode = .fit
imageBackgroundColor = .white
case let .gif(data):
self.gifData = data
}
}
}
private struct GIFViewWrapper: UIViewRepresentable {
typealias UIViewType = GIFImageView
@State private var controller: GIFController
init(gifData: Data) {
self._controller = State(wrappedValue: GIFController(gifData: gifData))
}
func makeUIView(context: Context) -> GIFImageView {
let view = GIFImageView()
controller.attach(to: view)
controller.startAnimating()
view.contentMode = .scaleAspectFit
view.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
view.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
return view
}
func updateUIView(_ uiView: GIFImageView, context: Context) {
}
}
struct ComposeAttachmentImage_Previews: PreviewProvider {
static var previews: some View {
ComposeAttachmentImage(attachment: CompositionAttachment(data: .image(UIImage())), fullSize: false)

View File

@ -120,6 +120,7 @@ struct ComposeEmojiTextField: UIViewRepresentable {
}
func textFieldDidEndEditing(_ textField: UITextField) {
uiState.currentInput = nil
updateAutocompleteState(textField: textField)
didEndEditing?()
}

View File

@ -26,22 +26,24 @@ struct ComposeToolbar: View {
var body: some View {
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 8) {
HStack(spacing: 0) {
Button("CW") {
draft.contentWarningEnabled.toggle()
}
.accessibilityLabel(draft.contentWarningEnabled ? "Remove content warning" : "Add content warning")
.padding(5)
.hoverEffect()
MenuPicker(selection: $draft.visibility, options: Self.visibilityOptions, buttonStyle: .iconOnly)
// the button has a bunch of extra space by default, but combined with what we add it's too much
.padding(.horizontal, -8)
// // the button has a bunch of extra space by default, but combined with what we add it's too much
// .padding(.horizontal, -8)
if mastodonController.instanceFeatures.localOnlyPosts {
MenuPicker(selection: $draft.localOnly, options: [
.init(value: true, title: "Local-only", subtitle: "Only \(mastodonController.accountInfo!.instanceURL.host!)", image: UIImage(named: "link.broken")),
.init(value: false, title: "Federated", image: UIImage(systemName: "link"))
], buttonStyle: .iconOnly)
.padding(.horizontal, -8)
// .padding(.horizontal, -8)
}
if let currentInput = uiState.currentInput, currentInput.toolbarElements.contains(.emojiPicker) {
@ -50,6 +52,8 @@ struct ComposeToolbar: View {
}
.labelStyle(.iconOnly)
.font(.system(size: imageSize))
.padding(5)
.hoverEffect()
}
if let currentInput = uiState.currentInput,
@ -68,6 +72,8 @@ struct ComposeToolbar: View {
}
}
.accessibilityLabel(format.accessibilityLabel)
.padding(5)
.hoverEffect()
}
}
@ -76,6 +82,8 @@ struct ComposeToolbar: View {
Button(action: self.draftsButtonPressed) {
Text("Drafts")
}
.padding(5)
.hoverEffect()
}
.padding(.horizontal, 16)
.frame(minWidth: minWidth)

View File

@ -31,6 +31,13 @@ struct DraftsView: View {
} label: {
DraftView(draft: draft)
}
.contextMenu {
Button(role: .destructive) {
draftsManager.remove(draft)
} label: {
Label("Delete Draft", systemImage: "trash")
}
}
.onDrag {
let activity = UserActivityManager.editDraftActivity(id: draft.id, accountID: mastodonController.accountInfo!.id)
activity.displaysAuxiliaryScene = true

View File

@ -106,13 +106,6 @@ class ProfileStatusesViewController: UIViewController, TimelineLikeCollectionVie
}
}
.store(in: &cancellables)
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
snapshot.appendSections([.header, .pinned, .statuses])
snapshot.appendItems([.header(accountID)], toSection: .header)
dataSource.apply(snapshot, animatingDifferences: false)
state = .setupInitialSnapshot
}
private func createDataSource() -> UICollectionViewDiffableDataSource<Section, Item> {
@ -181,11 +174,18 @@ class ProfileStatusesViewController: UIViewController, TimelineLikeCollectionVie
private func load() async {
guard isViewLoaded,
let accountID,
state == .setupInitialSnapshot,
state == .unloaded,
mastodonController.persistentContainer.account(for: accountID) != nil else {
return
}
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
snapshot.appendSections([.header, .pinned, .statuses])
snapshot.appendItems([.header(accountID)], toSection: .header)
await apply(snapshot, animatingDifferences: false)
state = .setupInitialSnapshot
await controller.loadInitial()
await tryLoadPinned()

View File

@ -152,6 +152,7 @@ class ProfileViewController: UIViewController {
assert(!animated)
// if old doesn't exist, we're selecting the initial view controller, so moving the header around isn't necessary
new.initialHeaderMode = .createView
new.view.translatesAutoresizingMaskIntoConstraints = false
embedChild(new)
self.currentIndex = index
state = .idle
@ -221,6 +222,7 @@ class ProfileViewController: UIViewController {
new.collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: additionalHeightNeededToMatchContentOffset, right: 0)
}
new.view.translatesAutoresizingMaskIntoConstraints = false
embedChild(new)
new.view.transform = CGAffineTransform(translationX: direction * view.bounds.width, y: yTranslationToMatchOldContentOffset)
@ -256,6 +258,7 @@ class ProfileViewController: UIViewController {
animator.startAnimation()
} else {
old.removeViewAndController()
new.view.translatesAutoresizingMaskIntoConstraints = false
embedChild(new)
completion?(true)
}

View File

@ -44,6 +44,7 @@ struct MenuPicker<Value: Hashable>: UIViewRepresentable {
}
})
button.accessibilityLabel = selectedOption.accessibilityLabel ?? selectedOption.title
button.isPointerInteractionEnabled = buttonStyle == .iconOnly
}
struct Option {