Move userMentioned outside of Status

This commit is contained in:
Thomas Ricouard 2023-02-26 09:38:26 +01:00
parent 47bd92cd75
commit 347335f770
3 changed files with 11 additions and 28 deletions

View file

@ -66,13 +66,7 @@ public extension AnyStatus {
}
}
protocol StatusUI {
var userMentioned: Bool? { get set }
}
public final class Status: AnyStatus, Codable, Identifiable, Equatable, Hashable, StatusUI {
public var userMentioned: Bool?
public final class Status: AnyStatus, Codable, Identifiable, Equatable, Hashable {
public static func == (lhs: Status, rhs: Status) -> Bool {
lhs.id == rhs.id && lhs.viewId == rhs.viewId
}
@ -109,8 +103,7 @@ public final class Status: AnyStatus, Codable, Identifiable, Equatable, Hashable
public let sensitive: Bool
public let language: String?
public init(userMentioned: Bool? = nil, id: String, content: HTMLString, account: Account, createdAt: ServerDate, editedAt: ServerDate?, reblog: ReblogStatus?, mediaAttachments: [MediaAttachment], mentions: [Mention], repliesCount: Int, reblogsCount: Int, favouritesCount: Int, card: Card?, favourited: Bool?, reblogged: Bool?, pinned: Bool?, bookmarked: Bool?, emojis: [Emoji], url: String?, application: Application?, inReplyToId: String?, inReplyToAccountId: String?, visibility: Visibility, poll: Poll?, spoilerText: HTMLString, filtered: [Filtered]?, sensitive: Bool, language: String?) {
self.userMentioned = userMentioned
public init(id: String, content: HTMLString, account: Account, createdAt: ServerDate, editedAt: ServerDate?, reblog: ReblogStatus?, mediaAttachments: [MediaAttachment], mentions: [Mention], repliesCount: Int, reblogsCount: Int, favouritesCount: Int, card: Card?, favourited: Bool?, reblogged: Bool?, pinned: Bool?, bookmarked: Bool?, emojis: [Emoji], url: String?, application: Application?, inReplyToId: String?, inReplyToAccountId: String?, visibility: Visibility, poll: Poll?, spoilerText: HTMLString, filtered: [Filtered]?, sensitive: Bool, language: String?) {
self.id = id
self.content = content
self.account = account

View file

@ -38,6 +38,7 @@ public class StatusRowViewModel: ObservableObject {
@Published var localStatus: Status?
private let theme = Theme.shared
private let userMentionned: Bool
private var seen = false
@ -53,7 +54,7 @@ public class StatusRowViewModel: ObservableObject {
var highlightRowColor: Color {
if status.visibility == .direct {
return theme.tintColor.opacity(0.15)
} else if status.userMentioned != nil {
} else if userMentionned {
return theme.secondaryBackgroundColor
} else {
return theme.primaryBackgroundColor
@ -96,6 +97,13 @@ public class StatusRowViewModel: ObservableObject {
displaySpoiler = !(status.reblog?.spoilerText.asRawText ?? status.spoilerText.asRawText).isEmpty
}
if status.mentions.first(where: { $0.id == CurrentAccount.shared.account?.id }) != nil {
userMentionned = true
} else {
userMentionned = false
}
isFiltered = filter != nil
if let url = embededStatusURL(),

View file

@ -103,11 +103,6 @@ class TimelineViewModel: ObservableObject {
{
pendingStatusesObserver.pendingStatuses.insert(event.status.id, at: 0)
let newStatus = event.status
if let accountId {
if newStatus.mentions.first(where: { $0.id == accountId }) != nil {
newStatus.userMentioned = true
}
}
await datasource.insert(newStatus, at: 0)
await cacheHome()
let statuses = await datasource.get()
@ -212,7 +207,6 @@ extension TimelineViewModel: StatusesFetcher {
minId: nil,
offset: 0))
updateMentionsToBeHighlighted(&statuses)
ReblogCache.shared.removeDuplicateReblogs(&statuses)
await datasource.set(statuses)
@ -320,7 +314,6 @@ extension TimelineViewModel: StatusesFetcher {
{
pagesLoaded += 1
updateMentionsToBeHighlighted(&newStatuses)
ReblogCache.shared.removeDuplicateReblogs(&newStatuses)
allStatuses.insert(contentsOf: newStatuses, at: 0)
@ -342,7 +335,6 @@ extension TimelineViewModel: StatusesFetcher {
minId: nil,
offset: datasource.get().count))
updateMentionsToBeHighlighted(&newStatuses)
ReblogCache.shared.removeDuplicateReblogs(&newStatuses)
await datasource.append(contentOf: newStatuses)
@ -354,16 +346,6 @@ extension TimelineViewModel: StatusesFetcher {
}
}
private func updateMentionsToBeHighlighted(_ statuses: inout [Status]) {
if !statuses.isEmpty, let accountId {
for i in statuses.indices {
if statuses[i].mentions.first(where: { $0.id == accountId }) != nil {
statuses[i].userMentioned = true
}
}
}
}
func statusDidAppear(status: Status) {
pendingStatusesObserver.removeStatus(status: status)
visibileStatusesIds.insert(status.id)