A WIP iOS app for Mastodon and Pleroma.
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.

129 lines
4.5 KiB

3 years ago
//
// ProfileHeaderTableViewCell.swift
// Tusker
//
// Created by Shadowfacts on 8/27/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import UIKit
import Pachyderm
3 years ago
protocol ProfileHeaderTableViewCellDelegate: TuskerNavigationDelegate {
func showMoreOptions()
3 years ago
}
class ProfileHeaderTableViewCell: UITableViewCell, PreferencesAdaptive {
3 years ago
var delegate: ProfileHeaderTableViewCellDelegate? {
didSet {
noteLabel.navigationDelegate = delegate
}
}
@IBOutlet weak var headerImageView: UIImageView!
@IBOutlet weak var avatarContainerView: UIView!
@IBOutlet weak var avatarImageView: UIImageView!
3 years ago
@IBOutlet weak var displayNameLabel: UILabel!
@IBOutlet weak var usernameLabel: UILabel!
@IBOutlet weak var followsYouLabel: UILabel!
@IBOutlet weak var noteLabel: StatusContentLabel!
3 years ago
3 years ago
var accountID: String!
3 years ago
var avatarURL: URL?
var headerURL: URL?
3 years ago
override func awakeFromNib() {
avatarContainerView.layer.masksToBounds = true
avatarImageView.layer.masksToBounds = true
avatarImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(avatarPressed)))
avatarImageView.isUserInteractionEnabled = true
headerImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(headerPressed)))
headerImageView.isUserInteractionEnabled = true
3 years ago
}
func updateUIForPreferences() {
3 years ago
guard let account = MastodonCache.account(for: accountID) else { fatalError("Missing cached account \(accountID!)") }
avatarContainerView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarContainerView)
avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView)
displayNameLabel.text = account.realDisplayName
}
3 years ago
func updateUI(for accountID: String) {
self.accountID = accountID
guard let account = MastodonCache.account(for: accountID) else { fatalError("Missing cached account \(accountID)") }
3 years ago
updateUIForPreferences()
3 years ago
usernameLabel.text = "@\(account.acct)"
avatarImageView.image = nil
avatarURL = account.avatar
ImageCache.avatars.get(account.avatar) { (data) in
guard let data = data else { return }
DispatchQueue.main.async {
self.avatarImageView.image = UIImage(data: data)
self.avatarURL = nil
3 years ago
}
}
ImageCache.headers.get(account.header) { (data) in
guard let data = data else { return }
DispatchQueue.main.async {
self.headerImageView.image = UIImage(data: data)
self.headerURL = nil
3 years ago
}
}
noteLabel.setTextFromHtml(account.note)
if accountID != MastodonController.account.id {
// don't show relationship label for the user's own account
if let relationship = MastodonCache.relationship(for: accountID) {
followsYouLabel.isHidden = !relationship.followedBy
} else {
MastodonCache.relationship(for: accountID) { relationship in
DispatchQueue.main.async {
self.followsYouLabel.isHidden = !(relationship?.followedBy ?? false)
}
}
}
}
3 years ago
}
override func prepareForReuse() {
if let url = avatarURL {
ImageCache.avatars.cancel(url)
3 years ago
}
if let url = headerURL {
ImageCache.headers.cancel(url)
3 years ago
}
}
@IBAction func morePressed(_ sender: Any) {
delegate?.showMoreOptions()
}
@objc func avatarPressed() {
delegate?.showLargeImage(avatarImageView.image!, description: nil, animatingFrom: avatarImageView)
}
@objc func headerPressed() {
delegate?.showLargeImage(headerImageView.image!, description: nil, animatingFrom: headerImageView)
}
3 years ago
}
extension ProfileHeaderTableViewCell: PreviewViewControllerProvider {
func getPreviewViewController(forLocation location: CGPoint, sourceViewController: UIViewController) -> UIViewController? {
let noteLabelPoint = noteLabel.convert(location, from: self)
if noteLabel.bounds.contains(noteLabelPoint),
let vc = noteLabel.getViewController(forLinkAt: noteLabelPoint) {
return vc
}
// TODO: should this also have peek/pop for avatar/header images?
return nil
}
}