Fix threads not working (IOS-176)

This commit is contained in:
Marcus Kida 2023-11-22 17:01:59 +01:00
parent e3f3e2c662
commit 36d4f6ea15
No known key found for this signature in database
GPG Key ID: 19FF64E08013CA40
2 changed files with 41 additions and 86 deletions

View File

@ -80,34 +80,11 @@ extension MastodonStatusThreadViewModel {
domain: String,
nodes: [Node]
) {
let ids = nodes.map { $0.statusID }
var dictionary: [Status.ID: Status] = [:]
do {
let request = Status.sortedFetchRequest
request.predicate = Status.predicate(domain: domain, ids: ids)
let statuses = try self.context.managedObjectContext.fetch(request)
for status in statuses {
dictionary[status.id] = status
}
} catch {
return
}
var newItems: [StatusItem] = []
#warning("Potentially this can be removed and replaced by native threading logic")
// for (i, node) in nodes.enumerated() {
// guard let status = dictionary[node.statusID] else { continue }
// let isLast = i == nodes.count - 1
//
// let record = ManagedObjectRecord<Status>(objectID: status.objectID)
// let context = StatusItem.Thread.Context(
// status: record,
// displayUpperConversationLink: !isLast,
// displayBottomConversationLink: true
// )
// let item = StatusItem.thread(.leaf(context: context))
// newItems.append(item)
// }
for node in nodes {
let item = StatusItem.thread(.leaf(context: .init(status: node.status)))
newItems.append(item)
}
let items = self.__ancestors + newItems
self.__ancestors = items
@ -117,48 +94,28 @@ extension MastodonStatusThreadViewModel {
domain: String,
nodes: [Node]
) {
let childrenIDs = nodes
.map { node in [node.statusID, node.children.first?.statusID].compactMap { $0 } }
.flatMap { $0 }
var dictionary: [Status.ID: Status] = [:]
do {
let request = Status.sortedFetchRequest
request.predicate = Status.predicate(domain: domain, ids: childrenIDs)
let statuses = try self.context.managedObjectContext.fetch(request)
for status in statuses {
dictionary[status.id] = status
}
} catch {
return
}
var newItems: [StatusItem] = []
#warning("Potentially this can be removed and replaced by native threading logic")
// for node in nodes {
// guard let status = dictionary[node.statusID] else { continue }
// // first tier
// let record = ManagedObjectRecord<Status>(objectID: status.objectID)
// let context = StatusItem.Thread.Context(
// status: record
// )
// let item = StatusItem.thread(.leaf(context: context))
// newItems.append(item)
//
// // second tier
// if let child = node.children.first {
// guard let secondaryStatus = dictionary[child.statusID] else { continue }
// let secondaryRecord = ManagedObjectRecord<Status>(objectID: secondaryStatus.objectID)
// let secondaryContext = StatusItem.Thread.Context(
// status: secondaryRecord,
// displayUpperConversationLink: true
// )
// let secondaryItem = StatusItem.thread(.leaf(context: secondaryContext))
// newItems.append(secondaryItem)
//
// // update first tier context
// context.displayBottomConversationLink = true
// }
// }
for node in nodes {
let context = StatusItem.Thread.Context(status: node.status)
let item = StatusItem.thread(.leaf(context: context))
newItems.append(item)
// second tier
if let child = node.children.first {
guard let secondaryStatus = node.children.first(where: { $0.status.id == child.status.id}) else { continue }
let secondaryContext = StatusItem.Thread.Context(
status: secondaryStatus.status,
displayUpperConversationLink: true
)
let secondaryItem = StatusItem.thread(.leaf(context: secondaryContext))
newItems.append(secondaryItem)
// update first tier context
context.displayBottomConversationLink = true
}
}
var items = self.__descendants
for item in newItems {
@ -172,16 +129,14 @@ extension MastodonStatusThreadViewModel {
extension MastodonStatusThreadViewModel {
class Node {
typealias ID = String
let statusID: ID
let status: MastodonStatus
let children: [Node]
init(
statusID: ID,
status: MastodonStatus,
children: [MastodonStatusThreadViewModel.Node]
) {
self.statusID = statusID
self.status = status
self.children = children
}
}
@ -206,7 +161,7 @@ extension MastodonStatusThreadViewModel.Node {
while let _nextID = nextID {
guard let status = dict[_nextID] else { break }
nodes.append(MastodonStatusThreadViewModel.Node(
statusID: _nextID,
status: .fromEntity(status),
children: []
))
nextID = status.inReplyToID
@ -218,11 +173,11 @@ extension MastodonStatusThreadViewModel.Node {
extension MastodonStatusThreadViewModel.Node {
static func children(
of statusID: ID,
of status: MastodonStatus,
from statuses: [Mastodon.Entity.Status]
) -> [MastodonStatusThreadViewModel.Node] {
var dictionary: [ID: Mastodon.Entity.Status] = [:]
var mapping: [ID: Set<ID>] = [:]
var dictionary: [Mastodon.Entity.Status.ID: Mastodon.Entity.Status] = [:]
var mapping: [Mastodon.Entity.Status.ID: Set<Mastodon.Entity.Status.ID>] = [:]
for status in statuses {
dictionary[status.id] = status
@ -236,28 +191,28 @@ extension MastodonStatusThreadViewModel.Node {
}
var children: [MastodonStatusThreadViewModel.Node] = []
let replies = Array(mapping[statusID] ?? Set())
let replies = Array(mapping[status.id] ?? Set())
.compactMap { dictionary[$0] }
.sorted(by: { $0.createdAt > $1.createdAt })
for reply in replies {
let child = child(of: reply.id, dictionary: dictionary, mapping: mapping)
let child = child(of: reply, dictionary: dictionary, mapping: mapping)
children.append(child)
}
return children
}
static func child(
of statusID: ID,
dictionary: [ID: Mastodon.Entity.Status],
mapping: [ID: Set<ID>]
of status: Mastodon.Entity.Status,
dictionary: [Mastodon.Entity.Status.ID: Mastodon.Entity.Status],
mapping: [Mastodon.Entity.Status.ID: Set<Mastodon.Entity.Status.ID>]
) -> MastodonStatusThreadViewModel.Node {
let childrenIDs = mapping[statusID] ?? []
let childrenIDs = mapping[status.id] ?? []
let children = Array(childrenIDs)
.compactMap { dictionary[$0] }
.sorted(by: { $0.createdAt > $1.createdAt })
.map { status in child(of: status.id, dictionary: dictionary, mapping: mapping) }
.map { status in child(of: status, dictionary: dictionary, mapping: mapping) }
return MastodonStatusThreadViewModel.Node(
statusID: statusID,
status: .fromEntity(status),
children: children
)
}

View File

@ -84,7 +84,7 @@ extension ThreadViewModel.LoadThreadState {
viewModel.mastodonStatusThreadViewModel.appendDescendant(
domain: threadContext.domain,
nodes: response.value.descendants.map { status in
return .init(statusID: status.id, children: [])
return .init(status: .fromEntity(status), children: [])
}
)
} catch {