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.
164 lines
7.1 KiB
164 lines
7.1 KiB
//
|
|
// AttachmentManager.swift
|
|
// elpha-ios
|
|
//
|
|
// Created by Dwayne Harris on 10/6/18.
|
|
// Copyright © 2018 Elpha. All rights reserved.
|
|
//
|
|
|
|
import AlamofireImage
|
|
import UIKit
|
|
|
|
class AttachmentsManager {
|
|
static func setupAttachmentsView(_ view: UIView, withAttachments attachments: NSOrderedSet?) {
|
|
guard let attachments = attachments, attachments.count > 0 else {
|
|
return
|
|
}
|
|
|
|
let placeholderImage = UIImage(named: "Help")
|
|
let halfWidth = (view.frame.width / 2) - 1
|
|
|
|
for subview in view.subviews as [UIView] {
|
|
subview.removeFromSuperview()
|
|
}
|
|
|
|
switch attachments.count {
|
|
case 0:
|
|
return
|
|
case 1:
|
|
let filter = AspectScaledToFillSizeFilter(size: CGSize(width: view.frame.width, height: view.frame.width))
|
|
|
|
let attachment = attachments.firstObject as! AttachmentMO
|
|
let imageView = UIImageView()
|
|
|
|
imageView.contentMode = UIImageView.ContentMode.scaleAspectFill
|
|
imageView.af_setImage(
|
|
withURL: attachment.url!,
|
|
placeholderImage: placeholderImage,
|
|
filter: filter
|
|
)
|
|
|
|
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),
|
|
imageView.widthAnchor.constraint(equalToConstant: view.frame.width),
|
|
imageView.heightAnchor.constraint(equalToConstant: view.frame.width),
|
|
])
|
|
case 2:
|
|
let filter = AspectScaledToFillSizeFilter(size: CGSize(width: halfWidth, height: view.frame.width))
|
|
|
|
let imageViews = [
|
|
UIImageView(),
|
|
UIImageView(),
|
|
]
|
|
|
|
for (index, imageView) in imageViews.enumerated() {
|
|
imageView.contentMode = UIImageView.ContentMode.scaleAspectFill
|
|
imageView.af_setImage(
|
|
withURL: (attachments[index] as! AttachmentMO).url!,
|
|
placeholderImage: placeholderImage,
|
|
filter: filter
|
|
)
|
|
|
|
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),
|
|
imageView.heightAnchor.constraint(equalToConstant: view.frame.width),
|
|
])
|
|
}
|
|
|
|
NSLayoutConstraint.activate([
|
|
imageViews[0].leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
imageViews[1].trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
|
])
|
|
case 3:
|
|
let primaryFilter = AspectScaledToFillSizeFilter(size: CGSize(width: halfWidth, height: view.frame.width))
|
|
let secondaryFilter = AspectScaledToFillSizeFilter(size: CGSize(width: halfWidth, height: halfWidth))
|
|
|
|
let imageViews = [
|
|
UIImageView(),
|
|
UIImageView(),
|
|
UIImageView(),
|
|
]
|
|
|
|
for (index, imageView) in imageViews.enumerated() {
|
|
imageView.contentMode = UIImageView.ContentMode.scaleAspectFill
|
|
imageView.af_setImage(
|
|
withURL: (attachments[index] as! AttachmentMO).url!,
|
|
placeholderImage: placeholderImage,
|
|
filter: index == 0 ? primaryFilter : secondaryFilter
|
|
)
|
|
|
|
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),
|
|
imageView.heightAnchor.constraint(equalToConstant: view.frame.width),
|
|
])
|
|
} else {
|
|
NSLayoutConstraint.activate([
|
|
imageView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
|
imageView.widthAnchor.constraint(equalToConstant: halfWidth),
|
|
imageView.heightAnchor.constraint(equalToConstant: halfWidth),
|
|
])
|
|
}
|
|
}
|
|
|
|
NSLayoutConstraint.activate([
|
|
imageViews[2].topAnchor.constraint(equalTo: view.topAnchor),
|
|
imageViews[3].bottomAnchor.constraint(equalTo: view.bottomAnchor),
|
|
])
|
|
default:
|
|
let filter = AspectScaledToFillSizeFilter(size: CGSize(width: halfWidth, height: halfWidth))
|
|
|
|
let imageViews = [
|
|
UIImageView(),
|
|
UIImageView(),
|
|
UIImageView(),
|
|
UIImageView(),
|
|
]
|
|
|
|
for (index, imageView) in imageViews.enumerated() {
|
|
imageView.contentMode = UIImageView.ContentMode.scaleAspectFill
|
|
imageView.af_setImage(
|
|
withURL: (attachments[index] as! AttachmentMO).url!,
|
|
placeholderImage: placeholderImage,
|
|
filter: filter
|
|
)
|
|
|
|
view.addSubview(imageView)
|
|
imageView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
NSLayoutConstraint.activate([
|
|
imageView.widthAnchor.constraint(equalToConstant: halfWidth),
|
|
imageView.heightAnchor.constraint(equalToConstant: halfWidth),
|
|
])
|
|
}
|
|
|
|
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[3].leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
imageViews[3].bottomAnchor.constraint(equalTo: view.bottomAnchor),
|
|
imageViews[4].bottomAnchor.constraint(equalTo: view.bottomAnchor),
|
|
imageViews[4].trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
|
])
|
|
}
|
|
}
|
|
}
|