Skip to content

Commit

Permalink
Communication: Fix Favorites not being reflected correctly in all p…
Browse files Browse the repository at this point in the history
…laces (#174)

* Reload sidebar if favorites change

* Fix ConversationInfoSheet not having correct favorite status
  • Loading branch information
anian03 authored Sep 19, 2024
1 parent 328a96d commit adbfb2b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ extension ConversationInfoSheetViewModel {
convo.isFavorite?.toggle()
conversation = .oneToOneChat(conversation: convo)
}
NotificationCenter.default.post(name: .favoriteConversationChanged,
object: nil,
userInfo: [
conversation.id: conversation.baseConversation.isFavorite as Any
])
}

func sendMessageToUser(with login: String, navigationController: NavigationController, completion: @escaping () -> Void) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ class ConversationViewModel: BaseViewModel {

subscribeToConversationTopic()
fetchOfflineMessages()

NotificationCenter.default.addObserver(self,
selector: #selector(updateFavorites(notification:)),
name: .favoriteConversationChanged,
object: nil)
}

deinit {
Expand Down Expand Up @@ -414,6 +419,22 @@ private extension ConversationViewModel {
diff -= 1
}
}

// Change favorites
@objc
private func updateFavorites(notification: Foundation.Notification) {
let isFavorite = notification.userInfo?[conversation.id] as? Bool ?? conversation.baseConversation.isFavorite
if var convo = conversation.baseConversation as? Channel {
convo.isFavorite = isFavorite
conversation = .channel(conversation: convo)
} else if var convo = conversation.baseConversation as? GroupChat {
convo.isFavorite = isFavorite
conversation = .groupChat(conversation: convo)
} else if var convo = conversation.baseConversation as? OneToOneChat {
convo.isFavorite = isFavorite
conversation = .oneToOneChat(conversation: convo)
}
}
}

// MARK: - ConversationViewModel+SendMessageViewModelDelegate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ class MessagesAvailableViewModel: BaseViewModel {
self.userSession = userSession

super.init()

NotificationCenter.default.addObserver(self,
selector: #selector(updateFavorites(notification:)),
name: .favoriteConversationChanged,
object: nil)
}

func subscribeToConversationMembershipTopic() async {
Expand All @@ -90,14 +95,46 @@ class MessagesAvailableViewModel: BaseViewModel {
allConversations = result
}

@objc
private func updateFavorites(notification: Foundation.Notification) {
// User Info contains:
// - Key: Conversation ID
// - Value: New Value for isFavorite
notification.userInfo?.forEach { id, isFavorite in
guard let id = id as? Int64,
let isFavorite = isFavorite as? Bool else { return }

// Find and update the corresponding conversation
let updatedConversations = allConversations.value?.map { conversation in
var newConversation = conversation
if conversation.id == id {
if var convo = conversation.baseConversation as? Channel {
convo.isFavorite = isFavorite
newConversation = .channel(conversation: convo)
} else if var convo = conversation.baseConversation as? GroupChat {
convo.isFavorite = isFavorite
newConversation = .groupChat(conversation: convo)
} else if var convo = conversation.baseConversation as? OneToOneChat {
convo.isFavorite = isFavorite
newConversation = .oneToOneChat(conversation: convo)
}
}
return newConversation
}
allConversations = .done(response: updatedConversations ?? [])
}
}

func setIsConversationFavorite(conversationId: Int64, isFavorite: Bool) async {
isLoading = true
let result = await messagesService.updateIsConversationFavorite(for: courseId, and: conversationId, isFavorite: isFavorite)
switch result {
case .notStarted, .loading:
isLoading = false
case .success:
await loadConversations()
NotificationCenter.default.post(name: .favoriteConversationChanged,
object: nil,
userInfo: [conversationId: isFavorite])
isLoading = false
case .failure(let error):
isLoading = false
Expand Down Expand Up @@ -352,3 +389,11 @@ enum ConversationFilter: FilterPicker {
}
}
}

// MARK: Reload Notification

extension Foundation.Notification.Name {
// Sending a notification of this type causes the Notification List to be reloaded,
// when favorites are changed from elsewhere.
static let favoriteConversationChanged = Foundation.Notification.Name("FavoriteConversationChanged")
}

0 comments on commit adbfb2b

Please sign in to comment.