RemoteThreadViewModel.swift 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //
  2. // RemoteThreadViewModel.swift
  3. // Mastodon
  4. //
  5. // Created by MainasuK Cirno on 2021-4-12.
  6. //
  7. import os.log
  8. import UIKit
  9. import CoreDataStack
  10. import MastodonCore
  11. import MastodonSDK
  12. final class RemoteThreadViewModel: ThreadViewModel {
  13. init(
  14. context: AppContext,
  15. authContext: AuthContext,
  16. statusID: Mastodon.Entity.Status.ID
  17. ) {
  18. super.init(
  19. context: context,
  20. authContext: authContext,
  21. optionalRoot: nil
  22. )
  23. Task { @MainActor in
  24. let domain = authContext.mastodonAuthenticationBox.domain
  25. let response = try await context.apiService.status(
  26. statusID: statusID,
  27. authenticationBox: authContext.mastodonAuthenticationBox
  28. )
  29. let managedObjectContext = context.managedObjectContext
  30. let request = Status.sortedFetchRequest
  31. request.fetchLimit = 1
  32. request.predicate = Status.predicate(domain: domain, id: response.value.id)
  33. guard let status = managedObjectContext.safeFetch(request).first else {
  34. assertionFailure()
  35. return
  36. }
  37. let threadContext = StatusItem.Thread.Context(status: .init(objectID: status.objectID))
  38. self.root = .root(context: threadContext)
  39. } // end Task
  40. }
  41. init(
  42. context: AppContext,
  43. authContext: AuthContext,
  44. notificationID: Mastodon.Entity.Notification.ID
  45. ) {
  46. super.init(
  47. context: context,
  48. authContext: authContext,
  49. optionalRoot: nil
  50. )
  51. Task { @MainActor in
  52. let domain = authContext.mastodonAuthenticationBox.domain
  53. let response = try await context.apiService.notification(
  54. notificationID: notificationID,
  55. authenticationBox: authContext.mastodonAuthenticationBox
  56. )
  57. guard let statusID = response.value.status?.id else { return }
  58. let managedObjectContext = context.managedObjectContext
  59. let request = Status.sortedFetchRequest
  60. request.fetchLimit = 1
  61. request.predicate = Status.predicate(domain: domain, id: statusID)
  62. guard let status = managedObjectContext.safeFetch(request).first else {
  63. assertionFailure()
  64. return
  65. }
  66. let threadContext = StatusItem.Thread.Context(status: .init(objectID: status.objectID))
  67. self.root = .root(context: threadContext)
  68. } // end Task
  69. }
  70. }