From fd0054addf44a3a65e7b21a1e208ca5e03dfac0b Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 21 Sep 2020 18:42:06 -0400 Subject: [PATCH] Correctly round corners of My Profile tab icon --- .../Profile/MyProfileViewController.swift | 43 +++++++++++++------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/Tusker/Screens/Profile/MyProfileViewController.swift b/Tusker/Screens/Profile/MyProfileViewController.swift index 1417cd1b..bf1945c5 100644 --- a/Tusker/Screens/Profile/MyProfileViewController.swift +++ b/Tusker/Screens/Profile/MyProfileViewController.swift @@ -21,19 +21,8 @@ class MyProfileViewController: ProfileViewController { DispatchQueue.main.async { self.accountID = account.id + self.setAvatarTabBarImage() } - - _ = ImageCache.avatars.get(account.avatar, completion: { [weak self] (data) in - guard let self = self, let data = data, let image = UIImage(data: data) else { return } - DispatchQueue.main.async { - let size = CGSize(width: 30, height: 30) - let tabBarImage = UIGraphicsImageRenderer(size: size).image { (_) in - image.draw(in: CGRect(origin: .zero, size: size)) - } - let alwaysOriginalImage = tabBarImage.withRenderingMode(.alwaysOriginal) - self.tabBarItem.image = alwaysOriginalImage - } - }) } } @@ -45,8 +34,38 @@ class MyProfileViewController: ProfileViewController { super.viewDidLoad() navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Preferences", style: .plain, target: self, action: #selector(preferencesPressed)) + + NotificationCenter.default.addObserver(self, selector: #selector(preferencesChanged), name: .preferencesChanged, object: nil) } + private func setAvatarTabBarImage() { + guard let id = mastodonController.account?.id, + let account = mastodonController.persistentContainer.account(for: id) else { + return + } + + _ = ImageCache.avatars.get(account.avatar, completion: { [weak self] (data) in + guard let self = self, let data = data, let image = UIImage(data: data) 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() { + setAvatarTabBarImage() + } + + // MARK: - Interaction + @objc func preferencesPressed() { present(PreferencesNavigationController(mastodonController: mastodonController), animated: true) }