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

View File

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