[ABANDONED] Mastodon iOS client.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
9.2 KiB

//
// AttachmentManager.swift
// elpha-ios
//
// Created by Dwayne Harris on 10/6/18.
// Copyright © 2018 Elpha. All rights reserved.
//
import AlamofireImage
import Kingfisher
import UIKit
protocol AttachmentManagerDelegate {
func attachmentTapped(index: Int)
}
class AttachmentManager: NSObject {
var delegate: AttachmentManagerDelegate? = nil
@objc func attachmentTapped(_ gestureRecognizer: UITapGestureRecognizer) {
if let delegate = delegate, let name = gestureRecognizer.name {
delegate.attachmentTapped(index: Int(name)!)
}
}
func setupAttachmentView(_ view: UIView, withAttachments attachments: NSOrderedSet?) {
guard let attachments = attachments, attachments.count > 0 else {
return
}
let placeholder = UIImage(named: "Help")
let halfWidth = (view.frame.width / 2) - 1
let fullSize = CGSize(width: view.frame.width, height: view.frame.width)
let tallSize = CGSize(width: halfWidth, height: view.frame.width)
let smallSize = CGSize(width: halfWidth, height: halfWidth)
let fullResizingProcessor = ResizingImageProcessor(referenceSize: fullSize, mode: .aspectFill) >> CroppingImageProcessor(size: fullSize)
let tallResizingProcessor = ResizingImageProcessor(referenceSize: tallSize, mode: .aspectFill) >> CroppingImageProcessor(size: tallSize)
let smallResizingProcessor = ResizingImageProcessor(referenceSize: smallSize, mode: .aspectFill) >> CroppingImageProcessor(size: smallSize)
for subview in view.subviews as [UIView] {
subview.removeFromSuperview()
}
switch attachments.count {
case 0:
return
case 1:
let attachment = attachments.firstObject as! AttachmentMO
let imageView = UIImageView()
imageView.contentMode = UIImageView.ContentMode.scaleAspectFill
imageView.isUserInteractionEnabled = true
imageView.kf.setImage(with: attachment.url!, placeholder: placeholder, options: [.processor(fullResizingProcessor)])
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(attachmentTapped))
tapGestureRecognizer.delegate = self
tapGestureRecognizer.numberOfTapsRequired = 1
tapGestureRecognizer.numberOfTouchesRequired = 1
tapGestureRecognizer.name = "0"
imageView.addGestureRecognizer(tapGestureRecognizer)
view.addSubview(imageView)
imageView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
imageView.topAnchor.constraint(equalTo: view.topAnchor),
imageView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
imageView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
imageView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
])
case 2:
let imageViews = [
UIImageView(),
UIImageView(),
]
for (index, imageView) in imageViews.enumerated() {
let attachment = attachments[index] as! AttachmentMO
imageView.contentMode = UIImageView.ContentMode.scaleAspectFill
imageView.isUserInteractionEnabled = true
imageView.kf.setImage(with: attachment.url!, placeholder: placeholder, options: [.processor(tallResizingProcessor)])
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(attachmentTapped))
tapGestureRecognizer.delegate = self
tapGestureRecognizer.numberOfTapsRequired = 1
tapGestureRecognizer.numberOfTouchesRequired = 1
tapGestureRecognizer.name = String(index)
imageView.addGestureRecognizer(tapGestureRecognizer)
view.addSubview(imageView)
imageView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
imageView.topAnchor.constraint(equalTo: view.topAnchor),
imageView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
imageView.widthAnchor.constraint(equalToConstant: halfWidth),
])
}
NSLayoutConstraint.activate([
imageViews[0].leadingAnchor.constraint(equalTo: view.leadingAnchor),
imageViews[1].trailingAnchor.constraint(equalTo: view.trailingAnchor),
])
case 3:
let imageViews = [
UIImageView(),
UIImageView(),
UIImageView(),
]
for (index, imageView) in imageViews.enumerated() {
let attachment = attachments[index] as! AttachmentMO
var processor: ImageProcessor
if index == 0 {
processor = tallResizingProcessor
} else {
processor = smallResizingProcessor
}
imageView.contentMode = UIImageView.ContentMode.scaleAspectFill
imageView.isUserInteractionEnabled = true
imageView.kf.setImage(with: attachment.url!, placeholder: placeholder, options: [.processor(processor)])
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(attachmentTapped))
tapGestureRecognizer.delegate = self
tapGestureRecognizer.numberOfTapsRequired = 1
tapGestureRecognizer.numberOfTouchesRequired = 1
tapGestureRecognizer.name = String(index)
imageView.addGestureRecognizer(tapGestureRecognizer)
view.addSubview(imageView)
imageView.translatesAutoresizingMaskIntoConstraints = false
if index == 0 {
NSLayoutConstraint.activate([
imageView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
imageView.topAnchor.constraint(equalTo: view.topAnchor),
imageView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
imageView.widthAnchor.constraint(equalToConstant: halfWidth),
])
} else {
NSLayoutConstraint.activate([
imageView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
imageView.widthAnchor.constraint(equalToConstant: halfWidth),
imageView.heightAnchor.constraint(equalToConstant: halfWidth),
])
}
}
NSLayoutConstraint.activate([
imageViews[1].topAnchor.constraint(equalTo: view.topAnchor),
imageViews[2].bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
default:
let imageViews = [
UIImageView(),
UIImageView(),
UIImageView(),
UIImageView(),
]
for (index, imageView) in imageViews.enumerated() {
let attachment = attachments[index] as! AttachmentMO
imageView.contentMode = UIImageView.ContentMode.scaleAspectFill
imageView.isUserInteractionEnabled = true
imageView.kf.setImage(with: attachment.url!, placeholder: placeholder, options: [.processor(smallResizingProcessor)])
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(attachmentTapped))
tapGestureRecognizer.delegate = self
tapGestureRecognizer.numberOfTapsRequired = 1
tapGestureRecognizer.numberOfTouchesRequired = 1
tapGestureRecognizer.name = String(index)
imageView.addGestureRecognizer(tapGestureRecognizer)
view.addSubview(imageView)
imageView.translatesAutoresizingMaskIntoConstraints = false
}
NSLayoutConstraint.activate([
imageViews[0].topAnchor.constraint(equalTo: view.topAnchor),
imageViews[0].leadingAnchor.constraint(equalTo: view.leadingAnchor),
imageViews[1].topAnchor.constraint(equalTo: view.topAnchor),
imageViews[1].trailingAnchor.constraint(equalTo: view.trailingAnchor),
imageViews[2].leadingAnchor.constraint(equalTo: view.leadingAnchor),
imageViews[2].bottomAnchor.constraint(equalTo: view.bottomAnchor),
imageViews[3].bottomAnchor.constraint(equalTo: view.bottomAnchor),
imageViews[3].trailingAnchor.constraint(equalTo: view.trailingAnchor),
])
}
}
}
extension AttachmentManager: UIGestureRecognizerDelegate {
}