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: 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) { if let statusMO = self.status(for: status.id, in: self.backgroundContext) {
statusMO.updateFrom(apiStatus: status, container: self) statusMO.updateFrom(apiStatus: status, container: self)
if incrementReferenceCount { if incrementReferenceCount {
statusMO.incrementReferenceCount() statusMO.incrementReferenceCount()
} }
return statusMO
} else { } else {
let statusMO = StatusMO(apiStatus: status, container: self, context: self.backgroundContext) let statusMO = StatusMO(apiStatus: status, container: self, context: self.backgroundContext)
if incrementReferenceCount { if incrementReferenceCount {
statusMO.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 { backgroundContext.perform {
self.upsert(status: status, incrementReferenceCount: incrementReferenceCount) let statusMO = self.upsert(status: status, incrementReferenceCount: incrementReferenceCount)
if self.backgroundContext.hasChanges { if self.backgroundContext.hasChanges {
try! self.backgroundContext.save() try! self.backgroundContext.save()
} }
completion?() completion?(statusMO)
self.statusSubject.send(status.id) 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) { if let accountMO = self.account(for: account.id, in: self.backgroundContext) {
accountMO.updateFrom(apiAccount: account, container: self) accountMO.updateFrom(apiAccount: account, container: self)
return accountMO
} else { } 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 { backgroundContext.perform {
self.upsert(account: account) let accountMO = self.upsert(account: account)
if self.backgroundContext.hasChanges { if self.backgroundContext.hasChanges {
try! self.backgroundContext.save() try! self.backgroundContext.save()
} }
completion?() completion?(accountMO)
self.accountSubject.send(account.id) self.accountSubject.send(account.id)
} }
} }
func addAll(accounts: [Account], completion: (() -> Void)? = nil) { func addAll(accounts: [Account], completion: (() -> Void)? = nil) {
backgroundContext.perform { backgroundContext.perform {
accounts.forEach(self.upsert(account:)) accounts.forEach { self.upsert(account: $0) }
if self.backgroundContext.hasChanges { if self.backgroundContext.hasChanges {
try! self.backgroundContext.save() try! self.backgroundContext.save()
} }

View File

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

View File

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

View File

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