feat: display avatar bar button item for authenticated user

This commit is contained in:
CMK 2021-02-04 19:37:38 +08:00
parent 0abd8ab863
commit 1c3facf456
7 changed files with 114 additions and 30 deletions

View File

@ -101,7 +101,7 @@
DB98338725C945ED00AD9700 /* Strings.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB98338525C945ED00AD9700 /* Strings.swift */; };
DB98338825C945ED00AD9700 /* Assets.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB98338625C945ED00AD9700 /* Assets.swift */; };
DB98339C25C96DE600AD9700 /* APIService+Account.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB98339B25C96DE600AD9700 /* APIService+Account.swift */; };
DBD4ED1125CC0FEB0041B741 /* HomeTimelineViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBD4ED1025CC0FEB0041B741 /* HomeTimelineViewModel.swift */; };
DBD4ED1125CC0FEB0041B741 /* HomeViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBD4ED1025CC0FEB0041B741 /* HomeViewModel.swift */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@ -260,7 +260,7 @@
DB98338525C945ED00AD9700 /* Strings.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Strings.swift; sourceTree = "<group>"; };
DB98338625C945ED00AD9700 /* Assets.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Assets.swift; sourceTree = "<group>"; };
DB98339B25C96DE600AD9700 /* APIService+Account.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "APIService+Account.swift"; sourceTree = "<group>"; };
DBD4ED1025CC0FEB0041B741 /* HomeTimelineViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeTimelineViewModel.swift; sourceTree = "<group>"; };
DBD4ED1025CC0FEB0041B741 /* HomeViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeViewModel.swift; sourceTree = "<group>"; };
DBF53F5F25C14E88008AAC7B /* Mastodon.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; name = Mastodon.xctestplan; path = Mastodon/Mastodon.xctestplan; sourceTree = "<group>"; };
DBF53F6025C14E9D008AAC7B /* MastodonSDK.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; path = MastodonSDK.xctestplan; sourceTree = "<group>"; };
EC6E707B68A67DB08EC288FA /* Pods-MastodonTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-MastodonTests.debug.xcconfig"; path = "Target Support Files/Pods-MastodonTests/Pods-MastodonTests.debug.xcconfig"; sourceTree = "<group>"; };
@ -745,7 +745,7 @@
isa = PBXGroup;
children = (
DB8AF55625C137A8002E6C99 /* HomeViewController.swift */,
DBD4ED1025CC0FEB0041B741 /* HomeTimelineViewModel.swift */,
DBD4ED1025CC0FEB0041B741 /* HomeViewModel.swift */,
);
path = HomeTimeline;
sourceTree = "<group>";
@ -1130,7 +1130,7 @@
DB0140CF25C42AEE00F9F3CF /* OSLog.swift in Sources */,
2D76319F25C1521200929FB9 /* TimelineSection.swift in Sources */,
DB084B5725CBC56C00F898ED /* Toot.swift in Sources */,
DBD4ED1125CC0FEB0041B741 /* HomeTimelineViewModel.swift in Sources */,
DBD4ED1125CC0FEB0041B741 /* HomeViewModel.swift in Sources */,
DB0140A825C40C1500F9F3CF /* MastodonPinBasedAuthenticationViewModelNavigationDelegateShim.swift in Sources */,
DB427DD625BAA00100D1B89D /* AppDelegate.swift in Sources */,
DB98338725C945ED00AD9700 /* Strings.swift in Sources */,

View File

@ -24,3 +24,9 @@ extension MastodonUser.Property {
)
}
}
extension MastodonUser {
public func avatarImageURL() -> URL? {
return URL(string: avatar)
}
}

View File

@ -0,0 +1,12 @@
//
// SplashPreference.swift
// Mastodon
//
// Created by Cirno MainasuK on 2020-2-4.
//
import UIKit
extension UserDefaults {
// TODO: splash scene
}

View File

@ -0,0 +1,67 @@
//
// HomeViewController.swift
// Mastodon
//
// Created by MainasuK Cirno on 2021/1/27.
//
import os.log
import UIKit
import Combine
final class HomeViewController: UIViewController, NeedsDependency {
var disposeBag = Set<AnyCancellable>()
weak var context: AppContext! { willSet { precondition(!isViewLoaded) } }
weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
var viewModel: HomeViewModel!
let avatarBarButtonItem = AvatarBarButtonItem()
}
extension HomeViewController {
override func viewDidLoad() {
super.viewDidLoad()
title = "Home"
view.backgroundColor = .systemBackground
navigationItem.leftBarButtonItem = avatarBarButtonItem
avatarBarButtonItem.avatarButton.addTarget(self, action: #selector(HomeViewController.avatarBarButtonItemDidPressed(_:)), for: .touchUpInside)
Publishers.CombineLatest(
context.authenticationService.activeMastodonAuthentication.eraseToAnyPublisher(),
viewModel.viewDidAppear.eraseToAnyPublisher()
)
.receive(on: DispatchQueue.main)
.sink { [weak self] activeMastodonAuthentication, _ in
guard let self = self else { return }
guard let user = activeMastodonAuthentication?.user,
let avatarImageURL = user.avatarImageURL() else {
let input = AvatarConfigurableViewConfiguration.Input(avatarImageURL: nil)
self.avatarBarButtonItem.configure(withConfigurationInput: input)
return
}
let input = AvatarConfigurableViewConfiguration.Input(avatarImageURL: avatarImageURL)
self.avatarBarButtonItem.configure(withConfigurationInput: input)
}
.store(in: &disposeBag)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
viewModel.viewDidAppear.send()
}
}
extension HomeViewController {
@objc private func avatarBarButtonItemDidPressed(_ sender: UIBarButtonItem) {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
}
}

View File

@ -0,0 +1,24 @@
//
// HomeViewModel.swift
// Mastodon
//
// Created by MainasuK Cirno on 2021-2-4.
//
import UIKit
import Combine
final class HomeViewModel {
// input
let context: AppContext
let viewDidAppear = PassthroughSubject<Void, Never>()
// output
init(context: AppContext) {
self.context = context
}
}

View File

@ -1,26 +0,0 @@
//
// HomeViewController.swift
// Mastodon
//
// Created by MainasuK Cirno on 2021/1/27.
//
import UIKit
final class HomeViewController: UIViewController, NeedsDependency {
weak var context: AppContext! { willSet { precondition(!isViewLoaded) } }
weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
}
extension HomeViewController {
override func viewDidLoad() {
super.viewDidLoad()
title = "Home"
view.backgroundColor = .systemBackground
}
}

View File

@ -40,6 +40,7 @@ class MainTabBarController: UITabBarController {
switch self {
case .home:
let _viewController = HomeViewController()
_viewController.viewModel = HomeViewModel(context: context)
_viewController.context = context
_viewController.coordinator = coordinator
viewController = _viewController