2019-01-05 17:59:55 +00:00
|
|
|
//
|
2020-07-05 20:17:56 +00:00
|
|
|
// MyProfileViewController.swift
|
2019-01-05 17:59:55 +00:00
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 11/24/18.
|
|
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2020-10-14 23:34:30 +00:00
|
|
|
import Pachyderm
|
2019-01-05 17:59:55 +00:00
|
|
|
|
2020-07-05 20:17:56 +00:00
|
|
|
class MyProfileViewController: ProfileViewController {
|
2019-01-05 17:59:55 +00:00
|
|
|
|
2020-01-05 20:25:07 +00:00
|
|
|
init(mastodonController: MastodonController) {
|
|
|
|
super.init(accountID: nil, mastodonController: mastodonController)
|
2019-01-05 17:59:55 +00:00
|
|
|
|
|
|
|
title = "My Profile"
|
2019-06-11 17:21:22 +00:00
|
|
|
tabBarItem.image = UIImage(systemName: "person.fill")
|
|
|
|
|
2020-09-16 21:52:00 +00:00
|
|
|
mastodonController.getOwnAccount { (result) in
|
|
|
|
guard case let .success(account) = result else { return }
|
|
|
|
|
2020-07-05 20:17:56 +00:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.accountID = account.id
|
2020-10-14 23:34:30 +00:00
|
|
|
self.setAvatarTabBarImage(account: account)
|
2020-07-05 20:17:56 +00:00
|
|
|
}
|
2019-01-05 17:59:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-05 20:17:56 +00:00
|
|
|
required init?(coder: NSCoder) {
|
2019-01-05 17:59:55 +00:00
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
2020-07-03 21:07:57 +00:00
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
2020-07-05 20:17:56 +00:00
|
|
|
|
2020-07-03 21:07:57 +00:00
|
|
|
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Preferences", style: .plain, target: self, action: #selector(preferencesPressed))
|
2020-09-21 22:42:06 +00:00
|
|
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(preferencesChanged), name: .preferencesChanged, object: nil)
|
2020-07-03 21:07:57 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 23:34:30 +00:00
|
|
|
private func setAvatarTabBarImage<Account: AccountProtocol>(account: Account) {
|
2020-11-01 18:59:58 +00:00
|
|
|
let avatarURL = account.avatar
|
|
|
|
_ = ImageCache.avatars.get(avatarURL, completion: { [weak self] (data) in
|
|
|
|
guard let self = self, let data = data else { return }
|
|
|
|
|
|
|
|
let maybeGrayscale: UIImage?
|
|
|
|
if Preferences.shared.grayscaleImages {
|
|
|
|
maybeGrayscale = ImageGrayscalifier.convert(url: avatarURL, data: data)
|
|
|
|
} else {
|
|
|
|
maybeGrayscale = UIImage(data: data)
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let image = maybeGrayscale else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-21 22:42:06 +00:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
let size = CGSize(width: 30, height: 30)
|
|
|
|
let rect = CGRect(origin: .zero, size: size)
|
|
|
|
let tabBarImage = UIGraphicsImageRenderer(size: size).image { (_) in
|
|
|
|
let radius = Preferences.shared.avatarStyle.cornerRadiusFraction * 30
|
|
|
|
UIBezierPath(roundedRect: rect, cornerRadius: radius).addClip()
|
|
|
|
image.draw(in: rect)
|
|
|
|
}
|
|
|
|
let alwaysOriginalImage = tabBarImage.withRenderingMode(.alwaysOriginal)
|
|
|
|
self.tabBarItem.image = alwaysOriginalImage
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func preferencesChanged() {
|
2020-10-14 23:34:30 +00:00
|
|
|
guard let id = mastodonController.account?.id,
|
|
|
|
let account = mastodonController.persistentContainer.account(for: id) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
setAvatarTabBarImage(account: account)
|
2020-09-21 22:42:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Interaction
|
|
|
|
|
2019-01-05 17:59:55 +00:00
|
|
|
@objc func preferencesPressed() {
|
2020-01-08 02:29:15 +00:00
|
|
|
present(PreferencesNavigationController(mastodonController: mastodonController), animated: true)
|
2019-06-14 00:53:17 +00:00
|
|
|
}
|
2019-01-05 17:59:55 +00:00
|
|
|
|
|
|
|
}
|