2019-09-06 02:30:58 +00:00
|
|
|
//
|
|
|
|
// AccountTableViewCell.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 9/5/19.
|
|
|
|
// Copyright © 2019 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2020-03-15 15:56:41 +00:00
|
|
|
import SwiftSoup
|
2019-09-06 02:30:58 +00:00
|
|
|
|
|
|
|
class AccountTableViewCell: UITableViewCell {
|
|
|
|
|
2022-05-02 03:04:56 +00:00
|
|
|
weak var delegate: (TuskerNavigationDelegate & MenuActionProvider)?
|
2020-01-06 00:39:37 +00:00
|
|
|
var mastodonController: MastodonController! { delegate?.apiController }
|
2019-09-06 02:30:58 +00:00
|
|
|
|
|
|
|
@IBOutlet weak var avatarImageView: UIImageView!
|
2020-03-02 00:40:32 +00:00
|
|
|
@IBOutlet weak var displayNameLabel: EmojiLabel!
|
2019-09-06 02:30:58 +00:00
|
|
|
@IBOutlet weak var usernameLabel: UILabel!
|
2020-03-15 15:56:41 +00:00
|
|
|
@IBOutlet weak var noteLabel: EmojiLabel!
|
2019-09-06 02:30:58 +00:00
|
|
|
|
|
|
|
var accountID: String!
|
|
|
|
|
2020-11-01 18:59:58 +00:00
|
|
|
private var avatarRequest: ImageCache.Request?
|
|
|
|
private var isGrayscale = false
|
2019-09-06 02:30:58 +00:00
|
|
|
|
|
|
|
override func awakeFromNib() {
|
|
|
|
super.awakeFromNib()
|
|
|
|
|
|
|
|
avatarImageView.layer.masksToBounds = true
|
2020-03-15 15:56:41 +00:00
|
|
|
|
2019-09-06 02:30:58 +00:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(updateUIForPrefrences), name: .preferencesChanged, object: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func updateUIForPrefrences() {
|
|
|
|
avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView)
|
|
|
|
|
2020-04-27 23:33:36 +00:00
|
|
|
guard let account = mastodonController.persistentContainer.account(for: accountID) else {
|
2022-03-29 16:34:54 +00:00
|
|
|
// this table view cell could be cached in a table view (e.g., SearchResultsViewController) for an account that's since been purged
|
|
|
|
return
|
2019-09-06 02:30:58 +00:00
|
|
|
}
|
2020-03-02 00:40:32 +00:00
|
|
|
displayNameLabel.updateForAccountDisplayName(account: account)
|
2020-11-01 18:59:58 +00:00
|
|
|
|
|
|
|
if isGrayscale != Preferences.shared.grayscaleImages {
|
|
|
|
updateGrayscaleableUI(account: account)
|
|
|
|
}
|
2019-09-06 02:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func updateUI(accountID: String) {
|
|
|
|
self.accountID = accountID
|
2020-04-27 23:33:36 +00:00
|
|
|
guard let account = mastodonController.persistentContainer.account(for: accountID) else {
|
2019-09-06 02:30:58 +00:00
|
|
|
fatalError("Missing cached account \(accountID)")
|
|
|
|
}
|
|
|
|
|
2020-11-01 18:59:58 +00:00
|
|
|
usernameLabel.text = "@\(account.acct)"
|
|
|
|
|
|
|
|
updateGrayscaleableUI(account: account)
|
|
|
|
updateUIForPrefrences()
|
|
|
|
}
|
|
|
|
|
|
|
|
private func updateGrayscaleableUI(account: AccountMO) {
|
|
|
|
isGrayscale = Preferences.shared.grayscaleImages
|
|
|
|
|
|
|
|
let accountID = self.accountID
|
|
|
|
|
2022-02-17 03:12:47 +00:00
|
|
|
if let avatarURL = account.avatar {
|
|
|
|
avatarRequest = ImageCache.avatars.get(avatarURL) { [weak self] (_, image) in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.avatarRequest = nil
|
|
|
|
|
|
|
|
guard let image = image,
|
|
|
|
self.accountID == accountID,
|
|
|
|
let transformedImage = ImageGrayscalifier.convertIfNecessary(url: avatarURL, image: image) else { return }
|
|
|
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.avatarImageView.image = transformedImage
|
|
|
|
}
|
2019-09-06 02:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-15 15:56:41 +00:00
|
|
|
let doc = try! SwiftSoup.parse(account.note)
|
|
|
|
noteLabel.text = try! doc.text()
|
|
|
|
noteLabel.setEmojis(account.emojis, identifier: account.id)
|
2019-09-06 02:30:58 +00:00
|
|
|
}
|
2020-01-25 15:06:27 +00:00
|
|
|
|
|
|
|
override func prepareForReuse() {
|
|
|
|
super.prepareForReuse()
|
|
|
|
|
|
|
|
avatarRequest?.cancel()
|
|
|
|
}
|
2019-09-06 02:30:58 +00:00
|
|
|
|
2019-12-14 16:59:31 +00:00
|
|
|
}
|
2019-09-06 02:30:58 +00:00
|
|
|
|
2019-12-14 16:59:31 +00:00
|
|
|
extension AccountTableViewCell: SelectableTableViewCell {
|
|
|
|
func didSelectCell() {
|
|
|
|
delegate?.selected(account: accountID)
|
2019-09-06 02:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-06 21:50:27 +00:00
|
|
|
|
|
|
|
extension AccountTableViewCell: MenuPreviewProvider {
|
|
|
|
func getPreviewProviders(for location: CGPoint, sourceViewController: UIViewController) -> PreviewProviders? {
|
2020-01-05 20:25:07 +00:00
|
|
|
guard let mastodonController = mastodonController else { return nil }
|
2020-01-18 02:29:53 +00:00
|
|
|
return (
|
2020-07-05 20:17:56 +00:00
|
|
|
content: { ProfileViewController(accountID: self.accountID, mastodonController: mastodonController) },
|
2022-05-02 03:04:56 +00:00
|
|
|
actions: { self.delegate?.actionsForProfile(accountID: self.accountID, sourceView: self.avatarImageView) ?? [] }
|
2020-01-18 23:56:36 +00:00
|
|
|
)
|
2019-09-06 21:50:27 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-14 23:44:41 +00:00
|
|
|
|
|
|
|
extension AccountTableViewCell: DraggableTableViewCell {
|
|
|
|
func dragItemsForBeginning(session: UIDragSession) -> [UIDragItem] {
|
|
|
|
guard let account = mastodonController.persistentContainer.account(for: accountID),
|
|
|
|
let currentAccountID = mastodonController.accountInfo?.id else {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
let provider = NSItemProvider(object: account.url as NSURL)
|
|
|
|
let activity = UserActivityManager.showProfileActivity(id: account.id, accountID: currentAccountID)
|
|
|
|
provider.registerObject(activity, visibility: .all)
|
|
|
|
return [UIDragItem(itemProvider: provider)]
|
|
|
|
}
|
|
|
|
}
|