Remove MastodonCache usgae from XCBActions

This commit is contained in:
Shadowfacts 2020-05-06 23:02:12 -04:00
parent ebbfc7a132
commit 1e41c8fa17
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
4 changed files with 48 additions and 47 deletions

View File

@ -41,27 +41,30 @@ class MastodonCachePersistentStore: NSPersistentContainer {
}
}
private func upsert(status: Status, incrementReferenceCount: Bool) {
@discardableResult
private func upsert(status: Status, incrementReferenceCount: Bool) -> StatusMO {
if let statusMO = self.status(for: status.id, in: self.backgroundContext) {
statusMO.updateFrom(apiStatus: status, container: self)
if incrementReferenceCount {
statusMO.incrementReferenceCount()
}
return statusMO
} else {
let statusMO = StatusMO(apiStatus: status, container: self, context: self.backgroundContext)
if incrementReferenceCount {
statusMO.incrementReferenceCount()
}
return statusMO
}
}
func addOrUpdate(status: Status, incrementReferenceCount: Bool, completion: (() -> Void)? = nil) {
func addOrUpdate(status: Status, incrementReferenceCount: Bool, completion: ((StatusMO) -> Void)? = nil) {
backgroundContext.perform {
self.upsert(status: status, incrementReferenceCount: incrementReferenceCount)
let statusMO = self.upsert(status: status, incrementReferenceCount: incrementReferenceCount)
if self.backgroundContext.hasChanges {
try! self.backgroundContext.save()
}
completion?()
completion?(statusMO)
self.statusSubject.send(status.id)
}
}
@ -89,28 +92,30 @@ class MastodonCachePersistentStore: NSPersistentContainer {
}
}
private func upsert(account: Account) {
@discardableResult
private func upsert(account: Account) -> AccountMO {
if let accountMO = self.account(for: account.id, in: self.backgroundContext) {
accountMO.updateFrom(apiAccount: account, container: self)
return accountMO
} else {
_ = AccountMO(apiAccount: account, container: self, context: self.backgroundContext)
return AccountMO(apiAccount: account, container: self, context: self.backgroundContext)
}
}
func addOrUpdate(account: Account, completion: (() -> Void)? = nil) {
func addOrUpdate(account: Account, completion: ((AccountMO) -> Void)? = nil) {
backgroundContext.perform {
self.upsert(account: account)
let accountMO = self.upsert(account: account)
if self.backgroundContext.hasChanges {
try! self.backgroundContext.save()
}
completion?()
completion?(accountMO)
self.accountSubject.send(account.id)
}
}
func addAll(accounts: [Account], completion: (() -> Void)? = nil) {
backgroundContext.perform {
accounts.forEach(self.upsert(account:))
accounts.forEach { self.upsert(account: $0) }
if self.backgroundContext.hasChanges {
try! self.backgroundContext.save()
}

View File

@ -173,8 +173,7 @@ class ComposeViewController: UIViewController {
let request = Client.getStatus(id: inReplyToID)
mastodonController.run(request) { (response) in
guard case let .success(status, _) = response else { return }
self.mastodonController.persistentContainer.addOrUpdate(status: status, incrementReferenceCount: true) {
guard let status = self.mastodonController.persistentContainer.status(for: inReplyToID) else { return }
self.mastodonController.persistentContainer.addOrUpdate(status: status, incrementReferenceCount: true) { (status) in
DispatchQueue.main.async {
self.updateInReplyTo(inReplyTo: status)
loadingVC.removeViewAndController()

View File

@ -90,7 +90,7 @@ class ProfileTableViewController: EnhancedTableViewController {
}
return
}
self.mastodonController.persistentContainer.addOrUpdate(account: account) {
self.mastodonController.persistentContainer.addOrUpdate(account: account) { (_) in
DispatchQueue.main.async {
self.updateAccountUI()
self.tableView.reloadData()

View File

@ -38,21 +38,21 @@ struct XCBActions {
private static func getStatus(from request: XCBRequest, session: XCBSession, completion: @escaping (Status) -> Void) {
if let id = request.arguments["statusID"] {
mastodonController.cache.status(for: id) { (status) in
if let status = status {
completion(status)
} else {
let request = Client.getStatus(id: id)
mastodonController.run(request) { (response) in
guard case let .success(status, _) = response else {
session.complete(with: .error, additionalData: [
"error": "Could not get status with ID \(id)"
])
return
}
completion(status)
}
} else if let searchQuery = request.arguments["statusURL"] {
let request = Client.search(query: searchQuery)
mastodonController.run(request) { (response) in
if case let .success(results, _) = response,
let status = results.statuses.first {
mastodonController.cache.add(status: status)
completion(status)
} else {
session.complete(with: .error, additionalData: [
@ -69,21 +69,21 @@ struct XCBActions {
private static func getAccount(from request: XCBRequest, session: XCBSession, completion: @escaping (Account) -> Void) {
if let id = request.arguments["accountID"] {
mastodonController.cache.account(for: id) { (account) in
if let account = account {
completion(account)
} else {
let request = Client.getAccount(id: id)
mastodonController.run(request) { (response) in
guard case let .success(account, _) = response else {
session.complete(with: .error, additionalData: [
"error": "Could not get account with ID \(id)"
])
return
}
completion(account)
}
} else if let searchQuery = request.arguments["accountURL"] {
let request = Client.search(query: searchQuery)
mastodonController.run(request) { (response) in
if case let .success(results, _) = response {
if let account = results.accounts.first {
mastodonController.cache.add(account: account)
completion(account)
} else {
session.complete(with: .error, additionalData: [
@ -101,7 +101,6 @@ struct XCBActions {
mastodonController.run(request) { (response) in
if case let .success(accounts, _) = response {
if let account = accounts.first {
mastodonController.cache.add(account: account)
completion(account)
} else {
session.complete(with: .error, additionalData: [
@ -208,7 +207,6 @@ struct XCBActions {
func performAction(status: Status, completion: ((Status) -> Void)?) {
mastodonController.run(request(status.id)) { (response) in
if case let .success(status, _) = response {
mastodonController.cache.add(status: status)
completion?(status)
session.complete(with: .success, additionalData: [
"statusURL": status.url?.absoluteString,
@ -293,8 +291,7 @@ struct XCBActions {
func performAction(_ account: Account) {
let request = Account.follow(account.id)
mastodonController.run(request) { (response) in
if case let .success(relationship, _) = response {
mastodonController.cache.add(relationship: relationship)
if case .success(_, _) = response {
session.complete(with: .success, additionalData: [
"url": account.url.absoluteString
])