forked from shadowfacts/Tusker
Convert conversation VC to use CoreData models
This commit is contained in:
parent
ed37b16463
commit
030bee1948
|
@ -38,8 +38,8 @@ public class Status: Decodable {
|
||||||
public let bookmarked: Bool?
|
public let bookmarked: Bool?
|
||||||
public let card: Card?
|
public let card: Card?
|
||||||
|
|
||||||
public static func getContext(_ status: Status) -> Request<ConversationContext> {
|
public static func getContext(_ statusID: String) -> Request<ConversationContext> {
|
||||||
return Request<ConversationContext>(method: .get, path: "/api/v1/statuses/\(status.id)/context")
|
return Request<ConversationContext>(method: .get, path: "/api/v1/statuses/\(statusID)/context")
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func getCard(_ status: Status) -> Request<Card> {
|
public static func getCard(_ status: Status) -> Request<Card> {
|
||||||
|
|
|
@ -42,6 +42,13 @@ class ConversationTableViewController: EnhancedTableViewController {
|
||||||
fatalError("init(coder:) has not been implemented")
|
fatalError("init(coder:) has not been implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
deinit {
|
||||||
|
mastodonController.persistentContainer.status(for: mainStatusID)?.decrementReferenceCount()
|
||||||
|
for (id, _) in statuses {
|
||||||
|
mastodonController.persistentContainer.status(for: id)?.decrementReferenceCount()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override func viewDidLoad() {
|
override func viewDidLoad() {
|
||||||
super.viewDidLoad()
|
super.viewDidLoad()
|
||||||
|
|
||||||
|
@ -58,32 +65,42 @@ class ConversationTableViewController: EnhancedTableViewController {
|
||||||
|
|
||||||
statuses = [(mainStatusID, mainStatusState)]
|
statuses = [(mainStatusID, mainStatusState)]
|
||||||
|
|
||||||
guard let mainStatus = mastodonController.cache.status(for: mainStatusID) else { fatalError("Missing cached status \(mainStatusID)") }
|
guard let mainStatus = self.mastodonController.persistentContainer.status(for: self.mainStatusID) else {
|
||||||
|
fatalError("Missing cached status \(self.mainStatusID)")
|
||||||
|
}
|
||||||
|
let mainStatusInReplyToID = mainStatus.inReplyToID
|
||||||
|
mainStatus.incrementReferenceCount()
|
||||||
|
|
||||||
let request = Status.getContext(mainStatus)
|
let request = Status.getContext(mainStatusID)
|
||||||
mastodonController.run(request) { response in
|
mastodonController.run(request) { response in
|
||||||
guard case let .success(context, _) = response else { fatalError() }
|
guard case let .success(context, _) = response else { fatalError() }
|
||||||
let parents = self.getDirectParents(of: mainStatus, from: context.ancestors)
|
|
||||||
self.mastodonController.cache.addAll(statuses: parents)
|
let parents = self.getDirectParents(inReplyTo: mainStatusInReplyToID, from: context.ancestors)
|
||||||
self.mastodonController.cache.addAll(statuses: context.descendants)
|
let parentStatuses = context.ancestors.filter { parents.contains($0.id) }
|
||||||
self.statuses = parents.map { ($0.id, .unknown) } + self.statuses + context.descendants.map { ($0.id, .unknown) }
|
self.mastodonController.persistentContainer.addAll(statuses: parentStatuses) {
|
||||||
let indexPath = IndexPath(row: parents.count, section: 0)
|
self.mastodonController.persistentContainer.addAll(statuses: context.descendants) {
|
||||||
DispatchQueue.main.async {
|
self.statuses = parents.map { ($0, .unknown) } + self.statuses + context.descendants.map { ($0.id, .unknown) }
|
||||||
self.tableView.scrollToRow(at: indexPath, at: .middle, animated: false)
|
let indexPath = IndexPath(row: parents.count, section: 0)
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
self.tableView.scrollToRow(at: indexPath, at: .middle, animated: false)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDirectParents(of status: Status, from statuses: [Status]) -> [Status] {
|
func getDirectParents(inReplyTo inReplyToID: String?, from statuses: [Status]) -> [String] {
|
||||||
var statuses = statuses
|
var statuses = statuses
|
||||||
var parents: [Status] = []
|
var parents = [String]()
|
||||||
var currentStatus: Status? = status
|
|
||||||
while currentStatus != nil {
|
var parentID: String? = inReplyToID
|
||||||
guard let index = statuses.firstIndex(where: { $0.id == currentStatus!.inReplyToID }) else { break }
|
|
||||||
let parent = statuses.remove(at: index)
|
while parentID != nil, let parentIndex = statuses.firstIndex(where: { $0.id == parentID }) {
|
||||||
parents.insert(parent, at: 0)
|
let parentStatus = statuses.remove(at: parentIndex)
|
||||||
currentStatus = parent
|
parents.insert(parentStatus.id, at: 0)
|
||||||
|
parentID = parentStatus.inReplyToID
|
||||||
}
|
}
|
||||||
|
|
||||||
return parents
|
return parents
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -169,7 +186,7 @@ extension ConversationTableViewController: StatusTableViewCellDelegate {
|
||||||
extension ConversationTableViewController: UITableViewDataSourcePrefetching {
|
extension ConversationTableViewController: UITableViewDataSourcePrefetching {
|
||||||
func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) {
|
func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) {
|
||||||
for indexPath in indexPaths {
|
for indexPath in indexPaths {
|
||||||
guard let status = mastodonController.cache.status(for: statuses[indexPath.row].id) else { continue }
|
guard let status = mastodonController.persistentContainer.status(for: statuses[indexPath.row].id) else { continue }
|
||||||
_ = ImageCache.avatars.get(status.account.avatar, completion: nil)
|
_ = ImageCache.avatars.get(status.account.avatar, completion: nil)
|
||||||
for attachment in status.attachments {
|
for attachment in status.attachments {
|
||||||
_ = ImageCache.attachments.get(attachment.url, completion: nil)
|
_ = ImageCache.attachments.get(attachment.url, completion: nil)
|
||||||
|
@ -179,7 +196,7 @@ extension ConversationTableViewController: UITableViewDataSourcePrefetching {
|
||||||
|
|
||||||
func tableView(_ tableView: UITableView, cancelPrefetchingForRowsAt indexPaths: [IndexPath]) {
|
func tableView(_ tableView: UITableView, cancelPrefetchingForRowsAt indexPaths: [IndexPath]) {
|
||||||
for indexPath in indexPaths {
|
for indexPath in indexPaths {
|
||||||
guard let status = mastodonController.cache.status(for: statuses[indexPath.row].id) else { continue }
|
guard let status = mastodonController.persistentContainer.status(for: statuses[indexPath.row].id) else { continue }
|
||||||
ImageCache.avatars.cancelWithoutCallback(status.account.avatar)
|
ImageCache.avatars.cancelWithoutCallback(status.account.avatar)
|
||||||
for attachment in status.attachments {
|
for attachment in status.attachments {
|
||||||
ImageCache.attachments.cancelWithoutCallback(attachment.url)
|
ImageCache.attachments.cancelWithoutCallback(attachment.url)
|
||||||
|
|
|
@ -40,11 +40,11 @@ class ConversationMainStatusTableViewCell: BaseStatusTableViewCell {
|
||||||
|
|
||||||
override func updateUI(statusID: String, state: StatusState) {
|
override func updateUI(statusID: String, state: StatusState) {
|
||||||
super.updateUI(statusID: statusID, state: state)
|
super.updateUI(statusID: statusID, state: state)
|
||||||
guard let status = mastodonController.cache.status(for: statusID) else { fatalError() }
|
guard let status = mastodonController.persistentContainer.status(for: statusID) else { fatalError() }
|
||||||
|
|
||||||
var timestampAndClientText = ConversationMainStatusTableViewCell.dateFormatter.string(from: status.createdAt)
|
var timestampAndClientText = ConversationMainStatusTableViewCell.dateFormatter.string(from: status.createdAt)
|
||||||
if let application = status.application {
|
if let application = status.application {
|
||||||
timestampAndClientText += " • \(application.name)"
|
timestampAndClientText += " • \(application)"
|
||||||
}
|
}
|
||||||
timestampAndClientLabel.text = timestampAndClientText
|
timestampAndClientLabel.text = timestampAndClientText
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue