[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.

149 lines
5.8 KiB

//
// StatusStackView.swift
// elpha-ios
//
// Created by Dwayne Harris on 10/9/18.
// Copyright © 2018 Elpha. All rights reserved.
//
import AlamofireImage
import UIKit
class StatusView: UIView {
@IBOutlet var contentView: UIView!
@IBOutlet var boostView: UIView!
@IBOutlet var boostAvatarImageView: UIImageView!
@IBOutlet var boostDisplayNameLabel: UILabel!
@IBOutlet var boostUsernameLabel: UILabel!
@IBOutlet var replyView: UIView!
@IBOutlet var replyAvatarImageView: UIImageView!
@IBOutlet var replyDisplayNameLabel: UILabel!
@IBOutlet var replyUsernameLabel: UILabel!
@IBOutlet var avatarImageView: UIImageView!
@IBOutlet var displayNameLabel: UILabel!
@IBOutlet var usernameLabel: UILabel!
@IBOutlet var contentLabel: UILabel!
@IBOutlet var timestampLabel: UILabel!
@IBOutlet var repliesImageView: UIImageView!
@IBOutlet var repliesLabel: UILabel!
@IBOutlet var boostsImageView: UIImageView!
@IBOutlet var boostsLabel: UILabel!
@IBOutlet var favoritesImageView: UIImageView!
@IBOutlet var favoritesLabel: UILabel!
@IBOutlet var topDividerView: UIView!
@IBOutlet var topLoadMoreView: UIView!
@IBOutlet var bottomDividerView: UIView!
@IBOutlet var bottomLoadMoreView: UIView!
@IBOutlet var attachmentsView: UIView!
@IBOutlet var attachmentsHeightConstraint: NSLayoutConstraint!
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
private func setup() {
Bundle.main.loadNibNamed("StatusView", owner: self, options: nil)
addSubview(contentView)
contentView.frame = self.bounds
contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
}
public func update(withStatus status: StatusMO) {
topDividerView.isHidden = false
topLoadMoreView.isHidden = true
boostView.isHidden = true
replyView.isHidden = true
bottomLoadMoreView.isHidden = true
bottomDividerView.isHidden = false
attachmentsView.backgroundColor = UIColor.white
attachmentsHeightConstraint.constant = frame.width
attachmentsView.isHidden = true
let avatarFilter = AspectScaledToFillSizeWithRoundedCornersFilter(
size: CGSize(width: 40.0, height: 40.0),
radius: 20.0,
divideRadiusByImageScale: true
)
func updateStatusContent(_ status: StatusMO) {
if let account = status.account {
avatarImageView.af_setImage(withURL: account.avatarURL!, filter: avatarFilter)
displayNameLabel.text = account.displayName
usernameLabel.text = account.acct
}
if let attachments = status.attachments, attachments.count > 0 {
attachmentsView.isHidden = false
AttachmentsManager.setupAttachmentsView(attachmentsView, withAttachments: attachments)
}
if let content = status.content {
do {
let styledContent = "<style>html * { font-size: 15px; color: #170c49; font-family: -apple-system } p { margin: 0; padding: 0 }</style> \(content)"
let attributedText = try NSAttributedString(
data: styledContent.data(using: String.Encoding.unicode, allowLossyConversion: true)!,
options: [.documentType: NSAttributedString.DocumentType.html],
documentAttributes: nil
)
contentLabel.attributedText = attributedText
} catch {
print("\(error)")
}
}
timestampLabel.text = status.createdAt!.timeAgo()
repliesLabel.text = "0"
boostsLabel.text = NumberFormatter.localizedString(from: NSNumber(value: status.reblogsCount), number: .decimal)
favoritesLabel.text = NumberFormatter.localizedString(from: NSNumber(value: status.favouritesCount), number: .decimal)
if status.reblogged {
boostsImageView.image = UIImage(named: "Boost Bold")
} else {
boostsImageView.image = UIImage(named: "Boost Regular")
}
if status.favourited {
favoritesImageView.image = UIImage(named: "Star Filled")
} else {
favoritesImageView.image = UIImage(named: "Star Regular")
}
}
if let reblog = status.reblog {
boostView.isHidden = false
if let account = status.account {
boostAvatarImageView.af_setImage(withURL: account.avatarURL!, filter: avatarFilter)
boostDisplayNameLabel.text = account.displayName
boostUsernameLabel.text = account.acct
}
updateStatusContent(reblog)
} else {
if let account = status.account {
avatarImageView.af_setImage(withURL: account.avatarURL!, filter: avatarFilter)
displayNameLabel.text = account.displayName
usernameLabel.text = account.acct
}
updateStatusContent(status)
}
if let replyAccountID = status.inReplyToAccountID {
if let replyAccount = MastodonDataManager.accountByID(replyAccountID) {
replyView.isHidden = false
replyAvatarImageView.af_setImage(withURL: replyAccount.avatarURL!, filter: avatarFilter)
replyDisplayNameLabel.text = replyAccount.displayName
replyUsernameLabel.text = replyAccount.acct
}
}
}
}