Set properties based on Entity.Account (IOS-194)

This commit is contained in:
Nathan Mattes 2023-11-20 12:27:10 +01:00
parent bbebfac99d
commit c45252b294
1 changed files with 17 additions and 69 deletions

View File

@ -16,11 +16,16 @@ import MastodonSDK
extension ProfileCardView {
public func configure(account: Mastodon.Entity.Account) {
//TODO: Implement
viewModel.authorBannerImageURL = URL(string: account.header)
viewModel.statusesCount = account.statusesCount
viewModel.followingCount = account.followingCount
viewModel.followersCount = account.followersCount
viewModel.authorAvatarImageURL = account.avatarImageURL()
let emojis = account.emojis?.asDictionary ?? [:]
do {
let content = MastodonContent(content: account.displayNameWithFallback, emojis: account.emojis?.asDictionary ?? [:])
let content = MastodonContent(content: account.displayNameWithFallback, emojis: emojis)
let metaContent = try MastodonMetaContent.convert(document: content)
viewModel.authorName = metaContent
} catch {
@ -29,73 +34,16 @@ extension ProfileCardView {
viewModel.authorName = metaContent
}
}
viewModel.authorUsername = account.acct
public func configure(user: MastodonUser) {
// author avatar
Publishers.CombineLatest3(
user.publisher(for: \.avatar),
user.publisher(for: \.avatarStatic),
UserDefaults.shared.publisher(for: \.preferredStaticAvatar)
)
.map { _ in user.avatarImageURL() }
.assign(to: \.authorAvatarImageURL, on: viewModel)
.store(in: &disposeBag)
// name
Publishers.CombineLatest(
user.publisher(for: \.displayName),
user.publisher(for: \.emojis)
)
.map { _, emojis in
do {
let content = MastodonContent(content: user.displayNameWithFallback, emojis: emojis.asDictionary)
let metaContent = try MastodonMetaContent.convert(document: content)
return metaContent
} catch {
assertionFailure(error.localizedDescription)
return PlaintextMetaContent(string: user.displayNameWithFallback)
}
do {
let content = MastodonContent(content: account.note, emojis: emojis)
let metaContent = try MastodonMetaContent.convert(document: content)
viewModel.bioContent = metaContent
} catch {
assertionFailure(error.localizedDescription)
let metaContent = PlaintextMetaContent(string: account.note)
viewModel.bioContent = metaContent
}
.assign(to: \.authorName, on: viewModel)
.store(in: &disposeBag)
// username
user.publisher(for: \.acct)
.map { $0 as String? }
.assign(to: \.authorUsername, on: viewModel)
.store(in: &disposeBag)
// bio
Publishers.CombineLatest(
user.publisher(for: \.note),
user.publisher(for: \.emojis)
)
.map { note, emojis in
guard let note = note else { return nil }
do {
let content = MastodonContent(content: note, emojis: emojis.asDictionary)
let metaContent = try MastodonMetaContent.convert(document: content)
return metaContent
} catch {
assertionFailure(error.localizedDescription)
return nil
}
}
.assign(to: \.bioContent, on: viewModel)
.store(in: &disposeBag)
// relationship
viewModel.relationshipViewModel.user = user
// dashboard
user.publisher(for: \.statusesCount)
.map { Int($0) }
.assign(to: \.statusesCount, on: viewModel)
.store(in: &disposeBag)
user.publisher(for: \.followingCount)
.map { Int($0) }
.assign(to: \.followingCount, on: viewModel)
.store(in: &disposeBag)
user.publisher(for: \.followersCount)
.map { Int($0) }
.assign(to: \.followersCount, on: viewModel)
.store(in: &disposeBag)
}
}