Compare commits

..

No commits in common. "fa1daa682fc94630392e5c4ae48b5d10de9d21e5" and "ed37b16463ee6ad27a5556b15fcefddb7d321adc" have entirely different histories.

6 changed files with 105 additions and 129 deletions

View File

@ -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(_ statusID: String) -> Request<ConversationContext> { public static func getContext(_ status: Status) -> Request<ConversationContext> {
return Request<ConversationContext>(method: .get, path: "/api/v1/statuses/\(statusID)/context") return Request<ConversationContext>(method: .get, path: "/api/v1/statuses/\(status.id)/context")
} }
public static func getCard(_ status: Status) -> Request<Card> { public static func getCard(_ status: Status) -> Request<Card> {

View File

@ -51,13 +51,12 @@ class MastodonCachePersistentStore: NSPersistentContainer {
} }
} }
func addOrUpdate(status: Status, incrementReferenceCount: Bool, completion: (() -> Void)?) { func addOrUpdate(status: Status, incrementReferenceCount: Bool, save: Bool = true) {
backgroundContext.perform { backgroundContext.perform {
self.upsert(status: status, incrementReferenceCount: incrementReferenceCount) self.upsert(status: status, incrementReferenceCount: incrementReferenceCount)
if self.backgroundContext.hasChanges { if save, self.backgroundContext.hasChanges {
try! self.backgroundContext.save() try! self.backgroundContext.save()
} }
completion?()
} }
} }
@ -91,13 +90,12 @@ class MastodonCachePersistentStore: NSPersistentContainer {
} }
} }
func addOrUpdate(account: Account, completion: (() -> Void)?) { func addOrUpdate(account: Account, save: Bool = true) {
backgroundContext.perform { backgroundContext.perform {
self.upsert(account: account) self.upsert(account: account)
if self.backgroundContext.hasChanges { if save, self.backgroundContext.hasChanges {
try! self.backgroundContext.save() try! self.backgroundContext.save()
} }
completion?()
} }
} }

View File

@ -42,13 +42,6 @@ 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()
@ -65,42 +58,32 @@ class ConversationTableViewController: EnhancedTableViewController {
statuses = [(mainStatusID, mainStatusState)] statuses = [(mainStatusID, mainStatusState)]
guard let mainStatus = self.mastodonController.persistentContainer.status(for: self.mainStatusID) else { guard let mainStatus = mastodonController.cache.status(for: mainStatusID) else { fatalError("Missing cached status \(mainStatusID)") }
fatalError("Missing cached status \(self.mainStatusID)")
} let request = Status.getContext(mainStatus)
let mainStatusInReplyToID = mainStatus.inReplyToID
mainStatus.incrementReferenceCount()
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)
let parents = self.getDirectParents(inReplyTo: mainStatusInReplyToID, from: context.ancestors) self.mastodonController.cache.addAll(statuses: parents)
let parentStatuses = context.ancestors.filter { parents.contains($0.id) } self.mastodonController.cache.addAll(statuses: context.descendants)
self.mastodonController.persistentContainer.addAll(statuses: parentStatuses) { self.statuses = parents.map { ($0.id, .unknown) } + self.statuses + context.descendants.map { ($0.id, .unknown) }
self.mastodonController.persistentContainer.addAll(statuses: context.descendants) { let indexPath = IndexPath(row: parents.count, section: 0)
self.statuses = parents.map { ($0, .unknown) } + self.statuses + context.descendants.map { ($0.id, .unknown) } DispatchQueue.main.async {
let indexPath = IndexPath(row: parents.count, section: 0) self.tableView.scrollToRow(at: indexPath, at: .middle, animated: false)
DispatchQueue.main.async {
self.tableView.scrollToRow(at: indexPath, at: .middle, animated: false)
}
}
} }
} }
} }
func getDirectParents(inReplyTo inReplyToID: String?, from statuses: [Status]) -> [String] { func getDirectParents(of status: Status, from statuses: [Status]) -> [Status] {
var statuses = statuses var statuses = statuses
var parents = [String]() var parents: [Status] = []
var currentStatus: Status? = status
var parentID: String? = inReplyToID while currentStatus != nil {
guard let index = statuses.firstIndex(where: { $0.id == currentStatus!.inReplyToID }) else { break }
while parentID != nil, let parentIndex = statuses.firstIndex(where: { $0.id == parentID }) { let parent = statuses.remove(at: index)
let parentStatus = statuses.remove(at: parentIndex) parents.insert(parent, at: 0)
parents.insert(parentStatus.id, at: 0) currentStatus = parent
parentID = parentStatus.inReplyToID
} }
return parents return parents
} }
@ -186,7 +169,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.persistentContainer.status(for: statuses[indexPath.row].id) else { continue } guard let status = mastodonController.cache.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)
@ -196,7 +179,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.persistentContainer.status(for: statuses[indexPath.row].id) else { continue } guard let status = mastodonController.cache.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)

View File

@ -73,13 +73,13 @@ class ProfileTableViewController: EnhancedTableViewController {
tableView.prefetchDataSource = self tableView.prefetchDataSource = self
if let accountID = accountID { if let accountID = accountID {
if mastodonController.persistentContainer.account(for: accountID) != nil { if mastodonController.cache.account(for: accountID) != nil {
updateAccountUI() updateAccountUI()
} else { } else {
loadingVC = LoadingViewController() loadingVC = LoadingViewController()
embedChild(loadingVC!) embedChild(loadingVC!)
mastodonController.cache.account(for: accountID) { (account) in mastodonController.cache.account(for: accountID) { (account) in
guard let account = account else { guard account != nil else {
let alert = UIAlertController(title: "Something Went Wrong", message: "Couldn't load the selected account", preferredStyle: .alert) let alert = UIAlertController(title: "Something Went Wrong", message: "Couldn't load the selected account", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (_) in alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (_) in
self.navigationController!.popViewController(animated: true) self.navigationController!.popViewController(animated: true)
@ -89,11 +89,9 @@ class ProfileTableViewController: EnhancedTableViewController {
} }
return return
} }
self.mastodonController.persistentContainer.addOrUpdate(account: account) { DispatchQueue.main.async {
DispatchQueue.main.async { self.updateAccountUI()
self.updateAccountUI() self.tableView.reloadData()
self.tableView.reloadData()
}
} }
} }
} }
@ -114,20 +112,18 @@ class ProfileTableViewController: EnhancedTableViewController {
getStatuses(onlyPinned: true) { (response) in getStatuses(onlyPinned: true) { (response) in
guard case let .success(statuses, _) = response else { fatalError() } guard case let .success(statuses, _) = response else { fatalError() }
self.mastodonController.persistentContainer.addAll(statuses: statuses) { self.mastodonController.cache.addAll(statuses: statuses)
self.pinnedStatuses = statuses.map { ($0.id, .unknown) } self.pinnedStatuses = statuses.map { ($0.id, .unknown) }
}
} }
getStatuses() { response in getStatuses() { response in
guard case let .success(statuses, pagination) = response else { fatalError() } guard case let .success(statuses, pagination) = response else { fatalError() }
self.mastodonController.persistentContainer.addAll(statuses: statuses) { self.mastodonController.cache.addAll(statuses: statuses)
self.timelineSegments.append(statuses.map { ($0.id, .unknown) }) self.timelineSegments.append(statuses.map { ($0.id, .unknown) })
self.older = pagination?.older self.older = pagination?.older
self.newer = pagination?.newer self.newer = pagination?.newer
}
} }
} }
@ -142,7 +138,7 @@ class ProfileTableViewController: EnhancedTableViewController {
} }
func sendMessageMentioning() { func sendMessageMentioning() {
guard let account = mastodonController.persistentContainer.account(for: accountID) else { fatalError("Missing cached account \(accountID!)") } guard let account = mastodonController.cache.account(for: accountID) else { fatalError("Missing cached account \(accountID!)") }
let vc = UINavigationController(rootViewController: ComposeViewController(mentioningAcct: account.acct, mastodonController: mastodonController)) let vc = UINavigationController(rootViewController: ComposeViewController(mentioningAcct: account.acct, mastodonController: mastodonController))
present(vc, animated: true) present(vc, animated: true)
} }
@ -156,7 +152,7 @@ class ProfileTableViewController: EnhancedTableViewController {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 { if section == 0 {
return accountID == nil || mastodonController.persistentContainer.account(for: accountID) == nil ? 0 : 1 return accountID == nil || mastodonController.cache.account(for: accountID) == nil ? 0 : 1
} else if section == 1 { } else if section == 1 {
return pinnedStatuses.count return pinnedStatuses.count
} else { } else {
@ -191,20 +187,16 @@ class ProfileTableViewController: EnhancedTableViewController {
// MARK: - Table view delegate // MARK: - Table view delegate
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
// todo: if scrolling up, remove statuses at bottom like timeline VC
// load older statuses if at bottom
if timelineSegments.count > 0 && indexPath.section - 1 == timelineSegments.count && indexPath.row == timelineSegments[indexPath.section - 2].count - 1 { if timelineSegments.count > 0 && indexPath.section - 1 == timelineSegments.count && indexPath.row == timelineSegments[indexPath.section - 2].count - 1 {
guard let older = older else { return } guard let older = older else { return }
getStatuses(for: older) { response in getStatuses(for: older) { response in
guard case let .success(newStatuses, pagination) = response else { fatalError() } guard case let .success(newStatuses, pagination) = response else { fatalError() }
self.mastodonController.persistentContainer.addAll(statuses: newStatuses) { self.mastodonController.cache.addAll(statuses: newStatuses)
self.timelineSegments[indexPath.section - 2].append(contentsOf: newStatuses.map { ($0.id, .unknown) }) self.timelineSegments[indexPath.section - 2].append(contentsOf: newStatuses.map { ($0.id, .unknown) })
self.older = pagination?.older self.older = pagination?.older
}
} }
} }
} }
@ -227,35 +219,34 @@ class ProfileTableViewController: EnhancedTableViewController {
getStatuses(for: newer) { response in getStatuses(for: newer) { response in
guard case let .success(newStatuses, pagination) = response else { fatalError() } guard case let .success(newStatuses, pagination) = response else { fatalError() }
self.mastodonController.persistentContainer.addAll(statuses: newStatuses) { self.mastodonController.cache.addAll(statuses: newStatuses)
self.timelineSegments[0].insert(contentsOf: newStatuses.map { ($0.id, .unknown) }, at: 0) self.timelineSegments[0].insert(contentsOf: newStatuses.map { ($0.id, .unknown) }, at: 0)
if let newer = pagination?.newer { if let newer = pagination?.newer {
self.newer = newer self.newer = newer
} }
DispatchQueue.main.async { DispatchQueue.main.async {
self.refreshControl?.endRefreshing() self.refreshControl?.endRefreshing()
}
} }
} }
getStatuses(onlyPinned: true) { (response) in getStatuses(onlyPinned: true) { (response) in
guard case let .success(newPinnedStatuses, _) = response else { fatalError() } guard case let .success(newPinnedStatuses, _) = response else { fatalError() }
self.mastodonController.persistentContainer.addAll(statuses: newPinnedStatuses) { self.mastodonController.cache.addAll(statuses: newPinnedStatuses)
let oldPinnedStatuses = self.pinnedStatuses
var pinnedStatuses = [(id: String, state: StatusState)]() let oldPinnedStatuses = self.pinnedStatuses
for status in newPinnedStatuses { var pinnedStatuses = [(id: String, state: StatusState)]()
let state: StatusState for status in newPinnedStatuses {
if let (_, oldState) = oldPinnedStatuses.first(where: { $0.id == status.id }) { let state: StatusState
state = oldState if let (_, oldState) = oldPinnedStatuses.first(where: { $0.id == status.id }) {
} else { state = oldState
state = .unknown } else {
} state = .unknown
pinnedStatuses.append((status.id, state))
} }
self.pinnedStatuses = pinnedStatuses pinnedStatuses.append((status.id, state))
} }
self.pinnedStatuses = pinnedStatuses
} }
} }
@ -277,7 +268,7 @@ extension ProfileTableViewController: StatusTableViewCellDelegate {
extension ProfileTableViewController: ProfileHeaderTableViewCellDelegate { extension ProfileTableViewController: ProfileHeaderTableViewCellDelegate {
func showMoreOptions(cell: ProfileHeaderTableViewCell) { func showMoreOptions(cell: ProfileHeaderTableViewCell) {
let account = mastodonController.persistentContainer.account(for: accountID)! let account = mastodonController.cache.account(for: accountID)!
mastodonController.cache.relationship(for: account.id) { [weak self] (relationship) in mastodonController.cache.relationship(for: account.id) { [weak self] (relationship) in
guard let self = self else { return } guard let self = self else { return }
@ -302,7 +293,7 @@ extension ProfileTableViewController: UITableViewDataSourcePrefetching {
func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) { func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) {
for indexPath in indexPaths where indexPath.section > 1 { for indexPath in indexPaths where indexPath.section > 1 {
let statusID = timelineSegments[indexPath.section - 2][indexPath.row].id let statusID = timelineSegments[indexPath.section - 2][indexPath.row].id
guard let status = mastodonController.persistentContainer.status(for: statusID) else { continue } guard let status = mastodonController.cache.status(for: statusID) 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)
@ -313,7 +304,7 @@ extension ProfileTableViewController: UITableViewDataSourcePrefetching {
func tableView(_ tableView: UITableView, cancelPrefetchingForRowsAt indexPaths: [IndexPath]) { func tableView(_ tableView: UITableView, cancelPrefetchingForRowsAt indexPaths: [IndexPath]) {
for indexPath in indexPaths where indexPath.section > 1 { for indexPath in indexPaths where indexPath.section > 1 {
let statusID = timelineSegments[indexPath.section - 2][indexPath.row].id let statusID = timelineSegments[indexPath.section - 2][indexPath.row].id
guard let status = mastodonController.persistentContainer.status(for: statusID) else { continue } guard let status = mastodonController.cache.status(for: statusID) 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)

View File

@ -63,7 +63,7 @@ class ProfileHeaderTableViewCell: UITableViewCell {
guard accountID != self.accountID else { return } guard accountID != self.accountID else { return }
self.accountID = accountID self.accountID = accountID
guard let account = mastodonController.persistentContainer.account(for: accountID) else { fatalError("Missing cached account \(accountID)") } guard let account = mastodonController.cache.account(for: accountID) else { fatalError("Missing cached account \(accountID)") }
updateUIForPreferences() updateUIForPreferences()
@ -101,38 +101,42 @@ class ProfileHeaderTableViewCell: UITableViewCell {
} }
} }
fieldsStackView.isHidden = account.fields.isEmpty if let fields = account.fields, !fields.isEmpty {
fieldsStackView.isHidden = false
fieldsStackView.arrangedSubviews.forEach { $0.removeFromSuperview() }
for field in account.fields { fieldsStackView.arrangedSubviews.forEach { $0.removeFromSuperview() }
let nameLabel = UILabel() for field in fields {
nameLabel.text = field.name let nameLabel = UILabel()
nameLabel.font = .boldSystemFont(ofSize: 17) nameLabel.text = field.name
nameLabel.textAlignment = .right nameLabel.font = .boldSystemFont(ofSize: 17)
nameLabel.numberOfLines = 0 nameLabel.textAlignment = .right
fieldNamesStackView.addArrangedSubview(nameLabel) nameLabel.numberOfLines = 0
fieldNamesStackView.addArrangedSubview(nameLabel)
let valueTextView = ContentTextView()
valueTextView.isSelectable = false let valueTextView = ContentTextView()
valueTextView.font = .systemFont(ofSize: 17) valueTextView.isSelectable = false
valueTextView.setTextFromHtml(field.value) valueTextView.font = .systemFont(ofSize: 17)
valueTextView.setEmojis(account.emojis) valueTextView.setTextFromHtml(field.value)
valueTextView.textAlignment = .left valueTextView.setEmojis(account.emojis)
valueTextView.awakeFromNib() valueTextView.textAlignment = .left
valueTextView.navigationDelegate = delegate valueTextView.awakeFromNib()
fieldValuesStack.addArrangedSubview(valueTextView) valueTextView.navigationDelegate = delegate
fieldValuesStack.addArrangedSubview(valueTextView)
}
} else {
fieldsStackView.isHidden = true
}
if accountUpdater == nil {
accountUpdater = mastodonController.cache.accountSubject
.filter { [unowned self] in $0.id == self.accountID }
.receive(on: DispatchQueue.main)
.sink { [unowned self] in self.updateUI(for: $0.id) }
} }
// if accountUpdater == nil {
// accountUpdater = mastodonController.cache.accountSubject
// .filter { [unowned self] in $0.id == self.accountID }
// .receive(on: DispatchQueue.main)
// .sink { [unowned self] in self.updateUI(for: $0.id) }
// }
} }
@objc func updateUIForPreferences() { @objc func updateUIForPreferences() {
guard let account = mastodonController.persistentContainer.account(for: accountID) else { fatalError("Missing cached account \(accountID!)") } guard let account = mastodonController.cache.account(for: accountID) else { fatalError("Missing cached account \(accountID!)") }
avatarContainerView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarContainerView) avatarContainerView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarContainerView)
avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView) avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView)
@ -149,12 +153,12 @@ class ProfileHeaderTableViewCell: UITableViewCell {
} }
@objc func avatarPressed() { @objc func avatarPressed() {
guard let account = mastodonController.persistentContainer.account(for: accountID) else { fatalError("Missing cached account \(accountID!)") } guard let account = mastodonController.cache.account(for: accountID) else { fatalError("Missing cached account \(accountID!)") }
delegate?.showLoadingLargeImage(url: account.avatar, cache: .avatars, description: nil, animatingFrom: avatarImageView) delegate?.showLoadingLargeImage(url: account.avatar, cache: .avatars, description: nil, animatingFrom: avatarImageView)
} }
@objc func headerPressed() { @objc func headerPressed() {
guard let account = mastodonController.persistentContainer.account(for: accountID) else { fatalError("Missing cached account \(accountID!)") } guard let account = mastodonController.cache.account(for: accountID) else { fatalError("Missing cached account \(accountID!)") }
delegate?.showLoadingLargeImage(url: account.header, cache: .headers, description: nil, animatingFrom: headerImageView) delegate?.showLoadingLargeImage(url: account.header, cache: .headers, description: nil, animatingFrom: headerImageView)
} }

View File

@ -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.persistentContainer.status(for: statusID) else { fatalError() } guard let status = mastodonController.cache.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)" timestampAndClientText += "\(application.name)"
} }
timestampAndClientLabel.text = timestampAndClientText timestampAndClientLabel.text = timestampAndClientText
} }