Dwayne Harris
6 years ago
9 changed files with 463 additions and 55 deletions
-
4elpha-ios.xcodeproj/project.pbxproj
-
12elpha-ios/Assets.xcassets/Help.imageset/Contents.json
-
BINelpha-ios/Assets.xcassets/Help.imageset/help-circle.pdf
-
346elpha-ios/AttachmentsView.swift
-
110elpha-ios/Base.lproj/Main.storyboard
-
2elpha-ios/Elpha.xcdatamodeld/Elpha.xcdatamodel/contents
-
2elpha-ios/MastodonDataManager.swift
-
3elpha-ios/TimelineTableViewCell.swift
-
39elpha-ios/TimelineTableViewController.swift
@ -0,0 +1,12 @@ |
|||
{ |
|||
"images" : [ |
|||
{ |
|||
"idiom" : "universal", |
|||
"filename" : "help-circle.pdf" |
|||
} |
|||
], |
|||
"info" : { |
|||
"version" : 1, |
|||
"author" : "xcode" |
|||
} |
|||
} |
@ -0,0 +1,346 @@ |
|||
// |
|||
// AttachmentsView.swift |
|||
// elpha-ios |
|||
// |
|||
// Created by Dwayne Harris on 10/6/18. |
|||
// Copyright © 2018 Elpha. All rights reserved. |
|||
// |
|||
|
|||
import AlamofireImage |
|||
import UIKit |
|||
|
|||
func getTopConstraint(from: UIView, to: UIView) -> NSLayoutConstraint { |
|||
return NSLayoutConstraint( |
|||
item: from, |
|||
attribute: NSLayoutConstraint.Attribute.top, |
|||
relatedBy: NSLayoutConstraint.Relation.equal, |
|||
toItem: to, |
|||
attribute: NSLayoutConstraint.Attribute.top, |
|||
multiplier: 1, |
|||
constant: 0 |
|||
) |
|||
} |
|||
|
|||
func getBottomConstraint(from: UIView, to: UIView) -> NSLayoutConstraint { |
|||
return NSLayoutConstraint( |
|||
item: from, |
|||
attribute: NSLayoutConstraint.Attribute.bottom, |
|||
relatedBy: NSLayoutConstraint.Relation.equal, |
|||
toItem: to, |
|||
attribute: NSLayoutConstraint.Attribute.bottom, |
|||
multiplier: 1, |
|||
constant: 0 |
|||
) |
|||
} |
|||
|
|||
func getLeadingConstraint(from: UIView, to: UIView) -> NSLayoutConstraint { |
|||
return NSLayoutConstraint( |
|||
item: from, |
|||
attribute: NSLayoutConstraint.Attribute.leading, |
|||
relatedBy: NSLayoutConstraint.Relation.equal, |
|||
toItem: to, |
|||
attribute: NSLayoutConstraint.Attribute.leading, |
|||
multiplier: 1, |
|||
constant: 0 |
|||
) |
|||
} |
|||
|
|||
func getTrailingConstraint(from: UIView, to: UIView) -> NSLayoutConstraint { |
|||
return NSLayoutConstraint( |
|||
item: from, |
|||
attribute: NSLayoutConstraint.Attribute.trailing, |
|||
relatedBy: NSLayoutConstraint.Relation.equal, |
|||
toItem: to, |
|||
attribute: NSLayoutConstraint.Attribute.trailing, |
|||
multiplier: 1, |
|||
constant: 0 |
|||
) |
|||
} |
|||
|
|||
func getWidthConstraint(view: UIView, width: CGFloat) -> NSLayoutConstraint { |
|||
return NSLayoutConstraint( |
|||
item: view, |
|||
attribute: NSLayoutConstraint.Attribute.width, |
|||
relatedBy: NSLayoutConstraint.Relation.equal, |
|||
toItem: nil, |
|||
attribute: NSLayoutConstraint.Attribute.notAnAttribute, |
|||
multiplier: 1, |
|||
constant: width |
|||
) |
|||
} |
|||
|
|||
func getHeightConstraint(view: UIView, height: CGFloat) -> NSLayoutConstraint { |
|||
return NSLayoutConstraint( |
|||
item: view, |
|||
attribute: NSLayoutConstraint.Attribute.height, |
|||
relatedBy: NSLayoutConstraint.Relation.equal, |
|||
toItem: nil, |
|||
attribute: NSLayoutConstraint.Attribute.notAnAttribute, |
|||
multiplier: 1, |
|||
constant: height |
|||
) |
|||
} |
|||
|
|||
class AttachmentsView: UIView { |
|||
public var attachments: NSOrderedSet? = nil { |
|||
didSet { |
|||
if let attachments = attachments { |
|||
let placeholderImage = UIImage(named: "Help") |
|||
|
|||
print("attachments: \(attachments.count)") |
|||
|
|||
switch attachments.count { |
|||
case 0: |
|||
self.isHidden = true |
|||
case 1: |
|||
let filter = AspectScaledToFillSizeFilter(size: CGSize(width: self.frame.width, height: self.frame.width)) |
|||
|
|||
let attachment = attachments.firstObject as! AttachmentMO |
|||
let imageView = UIImageView() |
|||
|
|||
imageView.af_setImage( |
|||
withURL: attachment.url!, |
|||
placeholderImage: placeholderImage, |
|||
filter: filter |
|||
) |
|||
|
|||
self.addSubview(imageView) |
|||
|
|||
imageView.translatesAutoresizingMaskIntoConstraints = false |
|||
|
|||
self.addConstraint(getLeadingConstraint(from: imageView, to: self)) |
|||
self.addConstraint(getTrailingConstraint(from: imageView, to: self)) |
|||
self.addConstraint(getTopConstraint(from: imageView, to: self)) |
|||
self.addConstraint(getBottomConstraint(from: imageView, to: self)) |
|||
self.addConstraint(getWidthConstraint(view: imageView, width: self.frame.width)) |
|||
self.addConstraint(getHeightConstraint(view: imageView, height: self.frame.width)) |
|||
case 2: |
|||
let filter = AspectScaledToFillSizeFilter(size: CGSize(width: (self.frame.width / 2) - 2, height: self.frame.width)) |
|||
|
|||
let firstImageView = UIImageView() |
|||
let secondImageView = UIImageView() |
|||
|
|||
firstImageView.af_setImage( |
|||
withURL: (attachments[0] as! AttachmentMO).url!, |
|||
placeholderImage: placeholderImage, |
|||
filter: filter |
|||
) |
|||
|
|||
secondImageView.af_setImage( |
|||
withURL: (attachments[1] as! AttachmentMO).url!, |
|||
placeholderImage: placeholderImage, |
|||
filter: filter |
|||
) |
|||
|
|||
self.addSubview(firstImageView) |
|||
self.addSubview(secondImageView) |
|||
|
|||
[firstImageView, secondImageView].forEach { imageView in |
|||
self.addConstraint(getLeadingConstraint(from: imageView, to: self)) |
|||
self.addConstraint(getTrailingConstraint(from: imageView, to: self)) |
|||
self.addConstraint(getTopConstraint(from: imageView, to: self)) |
|||
self.addConstraint(getBottomConstraint(from: imageView, to: self)) |
|||
self.addConstraint(getHeightConstraint(view: imageView, height: self.frame.width)) |
|||
self.addConstraint(getWidthConstraint(view: imageView, width: (self.frame.width / 2) - 2)) |
|||
} |
|||
|
|||
let betweenConstraint = NSLayoutConstraint( |
|||
item: firstImageView, |
|||
attribute: NSLayoutConstraint.Attribute.trailing, |
|||
relatedBy: NSLayoutConstraint.Relation.equal, |
|||
toItem: secondImageView, |
|||
attribute: NSLayoutConstraint.Attribute.leading, |
|||
multiplier: 1, |
|||
constant: 4 |
|||
) |
|||
|
|||
self.addConstraint(betweenConstraint) |
|||
case 3: |
|||
let halfWidth = (self.frame.width / 2) - 2 |
|||
|
|||
let primaryFilter = AspectScaledToFillSizeFilter(size: CGSize(width: halfWidth, height: self.frame.width)) |
|||
let secondaryFilter = AspectScaledToFillSizeFilter(size: CGSize(width: halfWidth, height: halfWidth)) |
|||
|
|||
let firstImageView = UIImageView() |
|||
let secondImageView = UIImageView() |
|||
let thirdImageView = UIImageView() |
|||
|
|||
firstImageView.af_setImage( |
|||
withURL: (attachments[0] as! AttachmentMO).url!, |
|||
placeholderImage: placeholderImage, |
|||
filter: primaryFilter |
|||
) |
|||
|
|||
secondImageView.af_setImage( |
|||
withURL: (attachments[1] as! AttachmentMO).url!, |
|||
placeholderImage: placeholderImage, |
|||
filter: secondaryFilter |
|||
) |
|||
|
|||
thirdImageView.af_setImage( |
|||
withURL: (attachments[2] as! AttachmentMO).url!, |
|||
placeholderImage: placeholderImage, |
|||
filter: secondaryFilter |
|||
) |
|||
|
|||
self.addSubview(firstImageView) |
|||
self.addSubview(secondImageView) |
|||
self.addSubview(thirdImageView) |
|||
|
|||
self.addConstraint(getLeadingConstraint(from: firstImageView, to: self)) |
|||
self.addConstraint(getTopConstraint(from: firstImageView, to: self)) |
|||
self.addConstraint(getBottomConstraint(from: firstImageView, to: self)) |
|||
self.addConstraint(getHeightConstraint(view: firstImageView, height: self.frame.width)) |
|||
self.addConstraint(getWidthConstraint(view: firstImageView, width: halfWidth)) |
|||
|
|||
self.addConstraint(getTopConstraint(from: secondImageView, to: self)) |
|||
self.addConstraint(getBottomConstraint(from: thirdImageView, to: self)) |
|||
|
|||
[secondImageView, thirdImageView].forEach { imageView in |
|||
self.addConstraint(getTrailingConstraint(from: imageView, to: self)) |
|||
self.addConstraint(getHeightConstraint(view: imageView, height: halfWidth)) |
|||
self.addConstraint(getWidthConstraint(view: imageView, width: halfWidth)) |
|||
} |
|||
|
|||
let firstSecondConstraint = NSLayoutConstraint( |
|||
item: firstImageView, |
|||
attribute: NSLayoutConstraint.Attribute.trailing, |
|||
relatedBy: NSLayoutConstraint.Relation.equal, |
|||
toItem: secondImageView, |
|||
attribute: NSLayoutConstraint.Attribute.leading, |
|||
multiplier: 1, |
|||
constant: 4 |
|||
) |
|||
|
|||
let firstThirdConstraint = NSLayoutConstraint( |
|||
item: firstImageView, |
|||
attribute: NSLayoutConstraint.Attribute.trailing, |
|||
relatedBy: NSLayoutConstraint.Relation.equal, |
|||
toItem: thirdImageView, |
|||
attribute: NSLayoutConstraint.Attribute.leading, |
|||
multiplier: 1, |
|||
constant: 4 |
|||
) |
|||
|
|||
let secondThirdConstraint = NSLayoutConstraint( |
|||
item: secondImageView, |
|||
attribute: NSLayoutConstraint.Attribute.bottom, |
|||
relatedBy: NSLayoutConstraint.Relation.equal, |
|||
toItem: thirdImageView, |
|||
attribute: NSLayoutConstraint.Attribute.top, |
|||
multiplier: 1, |
|||
constant: 4 |
|||
) |
|||
|
|||
self.addConstraints([firstSecondConstraint, firstThirdConstraint, secondThirdConstraint]) |
|||
default: |
|||
let halfWidth = (self.frame.width / 2) - 2 |
|||
|
|||
let filter = AspectScaledToFillSizeFilter(size: CGSize(width: halfWidth, height: halfWidth)) |
|||
|
|||
let firstImageView = UIImageView() |
|||
let secondImageView = UIImageView() |
|||
let thirdImageView = UIImageView() |
|||
let fourthImageView = UIImageView() |
|||
|
|||
firstImageView.af_setImage( |
|||
withURL: (attachments[0] as! AttachmentMO).url!, |
|||
placeholderImage: placeholderImage, |
|||
filter: filter |
|||
) |
|||
|
|||
secondImageView.af_setImage( |
|||
withURL: (attachments[1] as! AttachmentMO).url!, |
|||
placeholderImage: placeholderImage, |
|||
filter: filter |
|||
) |
|||
|
|||
thirdImageView.af_setImage( |
|||
withURL: (attachments[2] as! AttachmentMO).url!, |
|||
placeholderImage: placeholderImage, |
|||
filter: filter |
|||
) |
|||
|
|||
fourthImageView.af_setImage( |
|||
withURL: (attachments[3] as! AttachmentMO).url!, |
|||
placeholderImage: placeholderImage, |
|||
filter: filter |
|||
) |
|||
|
|||
self.addSubview(firstImageView) |
|||
self.addSubview(secondImageView) |
|||
self.addSubview(thirdImageView) |
|||
self.addSubview(fourthImageView) |
|||
|
|||
[firstImageView, secondImageView, thirdImageView, fourthImageView].forEach { imageView in |
|||
self.addConstraint(getHeightConstraint(view: imageView, height: halfWidth)) |
|||
self.addConstraint(getWidthConstraint(view: imageView, width: halfWidth)) |
|||
} |
|||
|
|||
[firstImageView, secondImageView].forEach { imageView in |
|||
self.addConstraint(getTopConstraint(from: imageView, to: self)) |
|||
} |
|||
|
|||
[thirdImageView, fourthImageView].forEach { imageView in |
|||
self.addConstraint(getBottomConstraint(from: imageView, to: self)) |
|||
} |
|||
|
|||
[firstImageView, thirdImageView].forEach { imageView in |
|||
self.addConstraint(getLeadingConstraint(from: imageView, to: self)) |
|||
} |
|||
|
|||
[secondImageView, fourthImageView].forEach { imageView in |
|||
self.addConstraint(getTrailingConstraint(from: imageView, to: self)) |
|||
} |
|||
|
|||
let firstBetweenConstraint = NSLayoutConstraint( |
|||
item: firstImageView, |
|||
attribute: NSLayoutConstraint.Attribute.trailing, |
|||
relatedBy: NSLayoutConstraint.Relation.equal, |
|||
toItem: secondImageView, |
|||
attribute: NSLayoutConstraint.Attribute.leading, |
|||
multiplier: 1, |
|||
constant: 4 |
|||
) |
|||
|
|||
let secondBetweenConstraint = NSLayoutConstraint( |
|||
item: secondImageView, |
|||
attribute: NSLayoutConstraint.Attribute.bottom, |
|||
relatedBy: NSLayoutConstraint.Relation.equal, |
|||
toItem: fourthImageView, |
|||
attribute: NSLayoutConstraint.Attribute.top, |
|||
multiplier: 1, |
|||
constant: 4 |
|||
) |
|||
|
|||
let thirdBetweenConstraint = NSLayoutConstraint( |
|||
item: fourthImageView, |
|||
attribute: NSLayoutConstraint.Attribute.leading, |
|||
relatedBy: NSLayoutConstraint.Relation.equal, |
|||
toItem: thirdImageView, |
|||
attribute: NSLayoutConstraint.Attribute.trailing, |
|||
multiplier: 1, |
|||
constant: 4 |
|||
) |
|||
|
|||
let fourthBetweenConstraint = NSLayoutConstraint( |
|||
item: thirdImageView, |
|||
attribute: NSLayoutConstraint.Attribute.top, |
|||
relatedBy: NSLayoutConstraint.Relation.equal, |
|||
toItem: firstImageView, |
|||
attribute: NSLayoutConstraint.Attribute.bottom, |
|||
multiplier: 1, |
|||
constant: 4 |
|||
) |
|||
|
|||
self.addConstraints([ |
|||
firstBetweenConstraint, |
|||
secondBetweenConstraint, |
|||
thirdBetweenConstraint, |
|||
fourthBetweenConstraint, |
|||
]) |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue