Tusker/Tusker/Screens/Profile/MyProfileViewController.swift

88 lines
3.0 KiB
Swift
Raw Normal View History

2019-01-05 17:59:55 +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
import Pachyderm
2019-01-05 17:59:55 +00:00
class MyProfileViewController: ProfileViewController {
2019-01-05 17:59:55 +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")
mastodonController.getOwnAccount { (result) in
guard case let .success(account) = result else { return }
DispatchQueue.main.async {
self.accountID = account.id
self.setAvatarTabBarImage(account: account)
}
2019-01-05 17:59:55 +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-03 21:07:57 +00:00
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Preferences", style: .plain, target: self, action: #selector(preferencesPressed))
NotificationCenter.default.addObserver(self, selector: #selector(preferencesChanged), name: .preferencesChanged, object: nil)
2020-07-03 21:07:57 +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
}
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() {
guard let id = mastodonController.account?.id,
let account = mastodonController.persistentContainer.account(for: id) else {
return
}
setAvatarTabBarImage(account: account)
}
// MARK: - Interaction
2019-01-05 17:59:55 +00:00
@objc func preferencesPressed() {
present(PreferencesNavigationController(mastodonController: mastodonController), animated: true)
2019-06-14 00:53:17 +00:00
}
2019-01-05 17:59:55 +00:00
}