LNFeedManager.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // LNFeedManager.swift
  3. // Gami
  4. //
  5. // Created by OneeChan on 2026/3/4.
  6. //
  7. import Foundation
  8. protocol LNFeedManagerNotify {
  9. func onFeedCommentCountChanged(id: String, count: Int)
  10. func onFeedLikedChanged(id: String, liked: Bool)
  11. func onFeedDelete(id: String)
  12. func onPostFeedSuccess()
  13. }
  14. extension LNFeedManagerNotify {
  15. func onFeedDelete(id: String) { }
  16. func onFeedCommentCountChanged(id: String, count: Int) { }
  17. func onFeedLikedChanged(id: String, liked: Bool) { }
  18. func onPostFeedSuccess() { }
  19. }
  20. class LNFeedManager {
  21. static let shared = LNFeedManager()
  22. static let feedMaxIntput = 500
  23. static let feedCommentMaxInput = 200
  24. static var videoMute = true
  25. func likeFeed(id: String, queue: DispatchQueue = .main, handler: ((LNFeedLikeResponse?) -> Void)? = nil) {
  26. LNHttpManager.shared.likeFeed(id: id) { [weak self] res, err in
  27. queue.asyncIfNotGlobal {
  28. handler?(res)
  29. }
  30. if let self, let res, err == nil {
  31. notifyFeedLikeChanged(id: id, liked: res.like)
  32. }
  33. }
  34. }
  35. func getFeedList(uid: String, next: String?, queue: DispatchQueue = .main, handler: @escaping (LNFeedListResponse?) -> Void) {
  36. LNHttpManager.shared.getFeedList(uid: uid, size: 30, next: next ?? "") { list, err in
  37. queue.asyncIfNotGlobal {
  38. handler(list)
  39. }
  40. }
  41. }
  42. func getFeedDetail(id: String, queue: DispatchQueue = .main, handler: @escaping (LNFeedDetailVO?) -> Void) {
  43. LNHttpManager.shared.getFeedDetail(id: id) { detail, err in
  44. queue.asyncIfNotGlobal {
  45. handler(detail)
  46. }
  47. }
  48. }
  49. func postFeed(item: LNPostFeedItem, queue: DispatchQueue = .main, handler: @escaping (Bool) -> Void) {
  50. LNHttpManager.shared.postFeed(item: item) { [weak self] err in
  51. queue.asyncIfNotGlobal {
  52. handler(err == nil)
  53. }
  54. if let self, err == nil {
  55. notifyPostFeedSuccess()
  56. }
  57. }
  58. }
  59. func sendFeedComment(id: String, content: String, queue: DispatchQueue = .main, handler: @escaping (Bool) -> Void) {
  60. LNHttpManager.shared.sendFeedComment(id: id, content: content) { err in
  61. queue.asyncIfNotGlobal {
  62. handler(err == nil)
  63. }
  64. }
  65. }
  66. func getFeedCommentList(id: String, next: String?, queue: DispatchQueue = .main, handler: @escaping (LNFeedCommentListResponse?) -> Void) {
  67. LNHttpManager.shared.getFeedCommentList(id: id, size: 30, next: next ?? "") { list, err in
  68. queue.asyncIfNotGlobal {
  69. handler(list)
  70. }
  71. }
  72. }
  73. func listFeedComment(id: String, queue: DispatchQueue = .main, handler: @escaping (Bool) -> Void) {
  74. LNHttpManager.shared.listFeedComment(id: id) { err in
  75. queue.asyncIfNotGlobal {
  76. handler(err == nil)
  77. }
  78. }
  79. }
  80. func deleteFeed(id: String, queue: DispatchQueue = .main, handler: @escaping (Bool) -> Void) {
  81. LNHttpManager.shared.deleteFeed(id: id) { [weak self] err in
  82. queue.asyncIfNotGlobal {
  83. handler(err == nil)
  84. }
  85. if let self, err == nil {
  86. notifyFeedDelete(id: id)
  87. }
  88. }
  89. }
  90. }
  91. extension LNFeedManager {
  92. func notifyFeedCommentChanged(id: String, count: Int) {
  93. LNEventDeliver.notifyEvent { ($0 as? LNFeedManagerNotify)?.onFeedCommentCountChanged(id: id, count: count) }
  94. }
  95. func notifyPostFeedSuccess() {
  96. LNEventDeliver.notifyEvent { ($0 as? LNFeedManagerNotify)?.onPostFeedSuccess() }
  97. }
  98. func notifyFeedDelete(id: String) {
  99. LNEventDeliver.notifyEvent { ($0 as? LNFeedManagerNotify)?.onFeedDelete(id: id) }
  100. }
  101. func notifyFeedLikeChanged(id: String, liked: Bool) {
  102. LNEventDeliver.notifyEvent { ($0 as? LNFeedManagerNotify)?.onFeedLikedChanged(id: id, liked: liked) }
  103. }
  104. }