Simplify some things on status-stuff

This commit is contained in:
Nathan Mattes 2023-10-13 13:04:15 +02:00
parent 2f672f0a20
commit f5d680c7b4
3 changed files with 44 additions and 63 deletions

View File

@ -11,16 +11,6 @@ import Foundation
public final class Status: NSManagedObject {
public typealias ID = String
public class TranslatedContent: NSObject {
public let content: String
public let provider: String?
public init(content: String, provider: String?) {
self.content = content
self.provider = provider
}
}
// sourcery: autoGenerateProperty
@NSManaged public private(set) var identifier: ID
// sourcery: autoGenerateProperty
@ -120,7 +110,6 @@ public final class Status: NSManagedObject {
@NSManaged public private(set) var revealedAt: Date?
// sourcery: autoUpdatableObject
@NSManaged public private(set) var translatedContent: TranslatedContent?
}
extension Status {
@ -535,11 +524,6 @@ extension Status: AutoUpdatableObject {
self.revealedAt = revealedAt
}
}
public func update(translatedContent: TranslatedContent?) {
if self.translatedContent != translatedContent {
self.translatedContent = translatedContent
}
}
public func update(attachments: [MastodonAttachment]) {
if self.attachments != attachments {
self.attachments = attachments

View File

@ -42,7 +42,7 @@ extension Mastodon.Entity {
// Informational
public let reblogsCount: Int
public let favouritesCount: Int
public let repliesCount: Int?
public let repliesCount: Int
public let url: String?
public let inReplyToID: Status.ID?
@ -54,15 +54,23 @@ extension Mastodon.Entity {
public let text: String?
// Authorized user
public let favourited: Bool?
public let reblogged: Bool?
public let muted: Bool?
public let bookmarked: Bool?
public let favourited: Bool
public let reblogged: Bool
public let muted: Bool
public let bookmarked: Bool
public let pinned: Bool?
// Computed Properties
public var translatedContent: String? = nil
public var domain: String? {
// get from URL
guard let url else { return nil }
let components = URLComponents(string: url)
return components?.host
}
enum CodingKeys: String, CodingKey {
case id
case uri
@ -134,3 +142,15 @@ extension Mastodon.Entity.Status {
}
}
}
extension Mastodon.Entity.Status {
public class TranslatedContent: NSObject {
public let content: String
public let provider: String?
public init(content: String, provider: String?) {
self.content = content
self.provider = provider
}
}
}

View File

@ -206,7 +206,7 @@ extension StatusView {
)
.map { _, emojis in
do {
let content = MastodonContent(content: author.displayNameWithFallback, emojis: emojis.asDictionary)
let content = MastodonContent(content: author.displayNameWithFallback, emojis: author.emojis?.asDictionary ?? [:])
let metaContent = try MastodonMetaContent.convert(document: content)
return metaContent
} catch {
@ -302,7 +302,7 @@ extension StatusView {
}
func configureTranslated(status: Mastodon.Entity.Status) {
let translatedContent: Status.TranslatedContent? = {
let translatedContent: Mastodon.Entity.Status.TranslatedContent? = {
if let translatedContent = status.reblog?.translatedContent {
return translatedContent
}
@ -340,7 +340,7 @@ extension StatusView {
viewModel.language = (status.reblog ?? status).language
// content
do {
let content = MastodonContent(content: statusEdit.content, emojis: statusEdit.emojis.asDictionary)
let content = MastodonContent(content: statusEdit.content, emojis: statusEdit.emojis?.asDictionary ?? [:])
let metaContent = try MastodonMetaContent.convert(document: content)
viewModel.content = metaContent
viewModel.translatedFromLanguage = nil
@ -361,7 +361,7 @@ extension StatusView {
// spoilerText
if let spoilerText = status.spoilerText, !spoilerText.isEmpty {
do {
let content = MastodonContent(content: spoilerText, emojis: status.emojis.asDictionary)
let content = MastodonContent(content: spoilerText, emojis: status.emojis?.asDictionary ?? [:])
let metaContent = try MastodonMetaContent.convert(document: content)
viewModel.spoilerContent = metaContent
} catch {
@ -375,7 +375,7 @@ extension StatusView {
viewModel.language = (status.reblog ?? status).language
// content
do {
let content = MastodonContent(content: status.content, emojis: status.emojis.asDictionary)
let content = MastodonContent(content: status.content, emojis: status.emojis?.asDictionary ?? [:])
let metaContent = try MastodonMetaContent.convert(document: content)
viewModel.content = metaContent
viewModel.translatedFromLanguage = nil
@ -385,8 +385,8 @@ extension StatusView {
viewModel.content = PlaintextMetaContent(string: "")
}
// visibility
status.publisher(for: \.visibilityRaw)
.compactMap { MastodonVisibility(rawValue: $0) }
status.publisher(for: \.visibility)
.map { $0 ?? .public }
.assign(to: \.visibility, on: viewModel)
.store(in: &disposeBag)
// sensitive
@ -504,59 +504,36 @@ extension StatusView {
let status = status.reblog ?? status
status.publisher(for: \.repliesCount)
.map(Int.init)
.assign(to: \.replyCount, on: viewModel)
.store(in: &disposeBag)
status.publisher(for: \.reblogsCount)
.map(Int.init)
.assign(to: \.reblogCount, on: viewModel)
emojis?.asDictionary ?? [:] .assign(to: \.reblogCount, on: viewModel)
.store(in: &disposeBag)
status.publisher(for: \.favouritesCount)
.map(Int.init)
.assign(to: \.favoriteCount, on: viewModel)
.store(in: &disposeBag)
status.publisher(for: \.editedAt)
.assign(to: \.editedAt, on: viewModel)
.store(in: &disposeBag)
status.publisher(for: \.editHistory)
.compactMap({ guard let edits = $0 else { return nil }
return Array(edits)
})
.assign(to: \.statusEdits, on: viewModel)
.store(in: &disposeBag)
// status.publisher(for: \.editHistory)
// .compactMap({ guard let edits = $0 else { return nil }
// //TODO: @zeitschlag get edits here
// return Array(edits)
// })
// .assign(to: \.statusEdits, on: viewModel)
// .store(in: &disposeBag)
// relationship
status.publisher(for: \.rebloggedBy)
.map { [weak viewModel] rebloggedBy in
guard let viewModel = viewModel else { return false }
guard let authContext = viewModel.authContext else { return false }
return rebloggedBy.contains(where: {
$0.id == authContext.mastodonAuthenticationBox.userID && $0.domain == authContext.mastodonAuthenticationBox.domain
})
}
status.publisher(for: \.reblogged)
.assign(to: \.isReblog, on: viewModel)
.store(in: &disposeBag)
status.publisher(for: \.favouritedBy)
.map { [weak viewModel]favouritedBy in
guard let viewModel = viewModel else { return false }
guard let authContext = viewModel.authContext else { return false }
return favouritedBy.contains(where: {
$0.id == authContext.mastodonAuthenticationBox.userID && $0.domain == authContext.mastodonAuthenticationBox.domain
})
}
status.publisher(for: \.favourited)
.assign(to: \.isFavorite, on: viewModel)
.store(in: &disposeBag)
status.publisher(for: \.bookmarkedBy)
.map { [weak viewModel] bookmarkedBy in
guard let viewModel = viewModel else { return false }
guard let authContext = viewModel.authContext else { return false }
return bookmarkedBy.contains(where: {
$0.id == authContext.mastodonAuthenticationBox.userID && $0.domain == authContext.mastodonAuthenticationBox.domain
})
}
status.publisher(for: \.bookmarked)
.assign(to: \.isBookmark, on: viewModel)
.store(in: &disposeBag)
}