Use account view model

This commit is contained in:
Justin Mazzocchi 2020-09-26 00:13:50 -07:00
parent eab12976cd
commit 07521837a9
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C
5 changed files with 19 additions and 15 deletions

View file

@ -19,8 +19,11 @@ public struct AccountStatusesService {
}
public extension AccountStatusesService {
func accountObservation() -> AnyPublisher<Account?, Error> {
func accountService() -> AnyPublisher<AccountService, Error> {
contentDatabase.accountObservation(id: accountID)
.compactMap { $0 }
.map { AccountService(account: $0, mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase) }
.eraseToAnyPublisher()
}
func statusListService(

View file

@ -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()
}

View file

@ -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<AnyCancellable>()
@ -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<String?, Never> {
$account.map {
guard let acct = $0?.acct else { return nil }
return "@".appending(acct)
}
.eraseToAnyPublisher()
$accountViewModel.map { $0?.accountName }.eraseToAnyPublisher()
}
}

View file

@ -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) }

View file

@ -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