LNRelationManager.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // LNRelationManager.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/12/12.
  6. //
  7. import Foundation
  8. protocol LNRelationManagerNotify {
  9. func onUserRelationChanged(uid: String, relation: LNUserRelationShip)
  10. func onMyRelationInfoChanged()
  11. func onUserBlackListChanged()
  12. }
  13. extension LNRelationManagerNotify {
  14. func onUserRelationChanged(uid: String, relation: LNUserRelationShip) {}
  15. func onMyRelationInfoChanged() {}
  16. func onUserBlackListChanged() {}
  17. }
  18. var myRelationInfo: LNUserRelationStatVO {
  19. LNRelationManager.shared.myRelationInfo
  20. }
  21. extension String {
  22. var isInMyBlackList: Bool {
  23. LNRelationManager.shared.myBlackList.contains(self)
  24. }
  25. }
  26. struct LNUserRelationShip: OptionSet, Decodable {
  27. let rawValue: UInt8
  28. static let followed = LNUserRelationShip(rawValue: 1<<0)
  29. static let fans = LNUserRelationShip(rawValue: 1<<1)
  30. }
  31. class LNRelationManager {
  32. static var shared = LNRelationManager()
  33. private(set) var myRelationInfo: LNUserRelationStatVO = LNUserRelationStatVO()
  34. private var relationIsChanged = false
  35. fileprivate var myBlackList = Set<String>()
  36. private init() {
  37. LNEventDeliver.addObserver(self)
  38. }
  39. func updateMyRelationInfo(_ info: LNUserRelationStatVO) {
  40. myRelationInfo = info
  41. notifyUserRelationInfoChanged()
  42. }
  43. func reloadRelationInfoIfNeed() {
  44. guard relationIsChanged else { return }
  45. LNProfileManager.shared.reloadMyProfileDetail()
  46. relationIsChanged = false
  47. }
  48. func operateFollow(uid: String, follow: Bool,
  49. queue: DispatchQueue = .main,
  50. handler: ((Bool) -> Void)?) {
  51. LNHttpManager.shared.operateFollow(uid: uid, follow: follow) { [weak self] err in
  52. guard let self else { return }
  53. if err == nil {
  54. getRelationWithUser(uid: uid, handler: nil)
  55. if follow {
  56. showToast(.init(key: "A00030"))
  57. }
  58. }
  59. queue.asyncIfNotGlobal {
  60. handler?(err == nil)
  61. }
  62. relationIsChanged = true
  63. }
  64. }
  65. func getUserFollowList(next: String, queue: DispatchQueue = .main,
  66. handler: @escaping ([LNRelationUserVO]?, String?, Int?) -> Void) {
  67. LNHttpManager.shared.getUserFollowList(size: 30, next: next) { res, err in
  68. queue.asyncIfNotGlobal {
  69. handler(res?.list, res?.next, res?.total)
  70. }
  71. }
  72. }
  73. func getUserFansList(next: String, queue: DispatchQueue = .main,
  74. handler: @escaping ([LNRelationUserVO]?, String?, Int?) -> Void) {
  75. LNHttpManager.shared.getUserFansList(size: 30, next: next) { res, err in
  76. queue.asyncIfNotGlobal {
  77. handler(res?.list, res?.next, res?.total)
  78. }
  79. }
  80. }
  81. func getRelationWithUser(uid: String, queue: DispatchQueue = .main,
  82. handler: ((LNUserRelationShip?) -> Void)?) {
  83. LNHttpManager.shared.getRelationWithUser(uid: uid) { [weak self] res, err in
  84. guard let self else { return }
  85. queue.asyncIfNotGlobal { [weak self] in
  86. guard let self else { return }
  87. handler?(res?.list.first?.relateion)
  88. if let relation = res?.list.first?.relateion {
  89. notifyRelationChanged(uid: uid, relation: relation)
  90. }
  91. }
  92. }
  93. }
  94. }
  95. extension LNRelationManager {
  96. func blackListUser(uid: String, black: Bool, queue: DispatchQueue = .main, handler: ((Bool) -> Void)?) {
  97. LNHttpManager.shared.blackListUser(uid: uid, black: black) { [weak self] err in
  98. guard let self else { return }
  99. queue.asyncIfNotGlobal {
  100. handler?(err == nil)
  101. }
  102. if black {
  103. myBlackList.insert(uid)
  104. } else {
  105. myBlackList.remove(uid)
  106. }
  107. notifyUserBlackListChanged()
  108. }
  109. }
  110. private func reloadMyBlackList() {
  111. var next = ""
  112. var list: [LNUserBlackVO] = []
  113. func _loadList() {
  114. LNHttpManager.shared.getBlackList(size: 30, next: next) { [weak self] res, err in
  115. guard let self else { return }
  116. guard let res, err == nil else { return }
  117. list.append(contentsOf: res.list)
  118. next = res.next
  119. if next.isEmpty {
  120. myBlackList.formUnion(list.map({ $0.userNo }))
  121. notifyUserBlackListChanged()
  122. } else {
  123. _loadList()
  124. }
  125. }
  126. }
  127. _loadList()
  128. }
  129. }
  130. extension LNRelationManager: LNAccountManagerNotify {
  131. func onUserLogin() {
  132. myRelationInfo = LNUserRelationStatVO()
  133. myBlackList.removeAll()
  134. reloadMyBlackList()
  135. }
  136. }
  137. extension LNRelationManager {
  138. private func notifyRelationChanged(uid: String, relation: LNUserRelationShip) {
  139. LNEventDeliver.notifyEvent {
  140. ($0 as? LNRelationManagerNotify)?.onUserRelationChanged(uid: uid, relation: relation)
  141. }
  142. }
  143. private func notifyUserRelationInfoChanged() {
  144. LNEventDeliver.notifyEvent {
  145. ($0 as? LNRelationManagerNotify)?.onMyRelationInfoChanged()
  146. }
  147. }
  148. private func notifyUserBlackListChanged() {
  149. LNEventDeliver.notifyEvent {
  150. ($0 as? LNRelationManagerNotify)?.onUserBlackListChanged()
  151. }
  152. }
  153. }