diff --git a/ServiceLayer/Sources/ServiceLayer/Services/AccountStatusesService.swift b/ServiceLayer/Sources/ServiceLayer/Services/AccountStatusesService.swift index c621c1e..204021b 100644 --- a/ServiceLayer/Sources/ServiceLayer/Services/AccountStatusesService.swift +++ b/ServiceLayer/Sources/ServiceLayer/Services/AccountStatusesService.swift @@ -19,8 +19,11 @@ public struct AccountStatusesService { } public extension AccountStatusesService { - func accountObservation() -> AnyPublisher { + func accountService() -> AnyPublisher { contentDatabase.accountObservation(id: accountID) + .compactMap { $0 } + .map { AccountService(account: $0, mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase) } + .eraseToAnyPublisher() } func statusListService( diff --git a/View Controllers/CollectionViewController.swift b/View Controllers/CollectionViewController.swift index 71ac4c5..830c56b 100644 --- a/View Controllers/CollectionViewController.swift +++ b/View Controllers/CollectionViewController.swift @@ -167,7 +167,10 @@ private extension CollectionViewController { origin: .zero, size: .init(width: 100, height: 100))) accountHeaderView.viewModel = accountsStatusesViewModel - accountsStatusesViewModel.$account.dropFirst().receive(on: DispatchQueue.main).sink { [weak self] _ in + accountsStatusesViewModel.$accountViewModel + .dropFirst() + .receive(on: DispatchQueue.main) + .sink { [weak self] _ in accountHeaderView.viewModel = accountsStatusesViewModel self?.sizeTableHeaderFooterViews() } diff --git a/ViewModels/Sources/ViewModels/AccountStatusesViewModel.swift b/ViewModels/Sources/ViewModels/AccountStatusesViewModel.swift index 21cfa63..876d509 100644 --- a/ViewModels/Sources/ViewModels/AccountStatusesViewModel.swift +++ b/ViewModels/Sources/ViewModels/AccountStatusesViewModel.swift @@ -6,7 +6,7 @@ import Mastodon import ServiceLayer public class AccountStatusesViewModel: StatusListViewModel { - @Published public private(set) var account: Account? + @Published public private(set) var accountViewModel: AccountViewModel? @Published public var collection = AccountStatusCollection.statuses private let accountStatusesService: AccountStatusesService private var cancellables = Set() @@ -22,9 +22,10 @@ public class AccountStatusesViewModel: StatusListViewModel { $collection.sink(receiveValue: collectionSubject.send).store(in: &cancellables) - accountStatusesService.accountObservation() + accountStatusesService.accountService() + .map(AccountViewModel.init(accountService:)) .assignErrorsToAlertItem(to: \.alertItem, on: self) - .assign(to: &$account) + .assign(to: &$accountViewModel) } public override func request(maxID: String? = nil, minID: String? = nil) { @@ -43,12 +44,7 @@ public class AccountStatusesViewModel: StatusListViewModel { } public override var title: AnyPublisher { - $account.map { - guard let acct = $0?.acct else { return nil } - - return "@".appending(acct) - } - .eraseToAnyPublisher() + $accountViewModel.map { $0?.accountName }.eraseToAnyPublisher() } } diff --git a/ViewModels/Sources/ViewModels/AccountViewModel.swift b/ViewModels/Sources/ViewModels/AccountViewModel.swift index 01cf007..0e4d73a 100644 --- a/ViewModels/Sources/ViewModels/AccountViewModel.swift +++ b/ViewModels/Sources/ViewModels/AccountViewModel.swift @@ -20,6 +20,8 @@ public class AccountViewModel: ObservableObject { public extension AccountViewModel { var avatarURL: URL { accountService.account.avatar } + var headerURL: URL { accountService.account.header } + var displayName: String { accountService.account.displayName } var accountName: String { "@".appending(accountService.account.acct) } diff --git a/Views/AccountHeaderView.swift b/Views/AccountHeaderView.swift index eaea7ae..8da5abf 100644 --- a/Views/AccountHeaderView.swift +++ b/Views/AccountHeaderView.swift @@ -11,18 +11,18 @@ class AccountHeaderView: UIView { var viewModel: AccountStatusesViewModel? { didSet { - if let account = viewModel?.account { - headerImageView.kf.setImage(with: account.header) + if let accountViewModel = viewModel?.accountViewModel { + headerImageView.kf.setImage(with: accountViewModel.headerURL) let noteFont = UIFont.preferredFont(forTextStyle: .callout) - let mutableNote = NSMutableAttributedString(attributedString: account.note.attributed) + let mutableNote = NSMutableAttributedString(attributedString: accountViewModel.note) let noteRange = NSRange(location: 0, length: mutableNote.length) mutableNote.removeAttribute(.font, range: noteRange) mutableNote.addAttributes( [.font: noteFont as Any, .foregroundColor: UIColor.label], range: noteRange) - mutableNote.insert(emoji: account.emojis, view: noteTextView) + mutableNote.insert(emoji: accountViewModel.emoji, view: noteTextView) mutableNote.resizeAttachments(toLineHeight: noteFont.lineHeight) noteTextView.attributedText = mutableNote noteTextView.isHidden = false