Last active
August 27, 2023 15:43
-
-
Save nirbhayph/a6132b9313f5b7318334bf5c0e3ef103 to your computer and use it in GitHub Desktop.
Kotlin's Flow API - Combining and Merging Flows for Reactive Programming
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Nirbhay Pherwani on 8/14/2023. | |
* Linktree - https://linktree.com/nirbhaypherwani | |
*/ | |
data class Like(val userName: String, val postTitle: String, val reactionName: String) | |
data class Comment(val userName: String, val postTitle: String, val commentText: String) | |
data class Post(val userName: String, val postTitle: String) | |
val likesList = listOf( | |
Like("Alice", "Exploring the Cosmos", "👍"), | |
Like("Rahul", "Starry Night Photography", "❤"), | |
Like("Grace", "Astronomy Basics", "👏"), | |
Like("Charlie", "Starry Night Photography", "👍"), | |
Like("Charlie", "Astronomy Basics", "❤️"), | |
Like("Bob", "Exploring the Cosmos", "👏"), | |
) | |
val commentsList = listOf( | |
Comment("Alice", "Exploring the Cosmos", "This is amazing!"), | |
Comment("Charlie", "Starry Night Photography", "Beautiful shot!"), | |
Comment("Alice", "Astronomy Basics", "Great explanation!"), | |
Comment("Rahul", "Starry Night Photography", "Wow, I love this!"), | |
Comment("Rahul", "Exploring the Cosmos", "Mind-blowing!"), | |
Comment("Grace", "Astronomy 2 Basics", "I finally understand this concept!") | |
) | |
val postsList = listOf( | |
Post("Alice", "Exploring the Cosmos"), | |
Post("Bob", "Starry Night Photography"), | |
Post("Charlie", "Astronomy 2 Basics"), | |
Post("Eve", "Starry Night Photography"), | |
Post("Alice", "Astronomy Basics") | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.nirbhay.compose.easyspannablegridusage | |
import android.util.Log | |
import androidx.lifecycle.ViewModel | |
import androidx.lifecycle.viewModelScope | |
import kotlinx.coroutines.delay | |
import kotlinx.coroutines.flow.* | |
import kotlinx.coroutines.launch | |
/** | |
* Created by Nirbhay Pherwani on 8/14/2023. | |
* Linktree - https://linktree.com/nirbhaypherwani | |
*/ | |
class NotificationsViewModel: ViewModel() { | |
init { | |
quickNotifications() | |
summarizedNotifications() | |
specificNotifications() | |
} | |
private fun quickNotifications(){ | |
viewModelScope.launch { | |
val likesActivity: Flow<Like> = flowOf(*likesList.toTypedArray()).onEach { | |
delay(50) | |
} | |
val commentsActivity: Flow<Comment> = flowOf(*commentsList.toTypedArray()).onEach { | |
delay(200) | |
} | |
val postsActivity: Flow<Post> = flowOf(*postsList.toTypedArray()).onEach { | |
delay(100) | |
} | |
merge(likesActivity, commentsActivity, postsActivity).collect { | |
when(it) { | |
is Comment -> { | |
Log.i("Quick Notification", "${it.userName} commented on your post: ${it.commentText}") | |
} | |
is Like -> { | |
Log.i("Quick Notification", "${it.userName} reacted with ${it.reactionName} to your post: ${it.postTitle}") | |
} | |
is Post -> { | |
Log.i("Quick Notification", "${it.userName} added a new post: ${it.postTitle}") | |
} | |
} | |
} | |
} | |
} | |
private fun summarizedNotifications() { | |
viewModelScope.launch { | |
val likesActivity: Flow<Like> = flowOf(*likesList.toTypedArray()).onEach { | |
delay(50) | |
} | |
val commentsActivity: Flow<Comment> = flowOf(*commentsList.toTypedArray()).onEach { | |
delay(200) | |
} | |
val postsActivity: Flow<Post> = flowOf(*postsList.toTypedArray()).onEach { | |
delay(100) | |
} | |
likesActivity.combine(commentsActivity) { like, comment -> | |
"${like.userName} reacted with ${like.reactionName} on ${like.postTitle}\n" + | |
"and ${comment.userName} commented ${comment.commentText} on ${comment.postTitle}" | |
}.combine(postsActivity) { activity, post -> | |
Log.i("Summary", "$activity .. Also ${post.userName} added a new post '${post.postTitle}'") | |
}.collect() | |
} | |
} | |
private fun specificNotifications() { | |
viewModelScope.launch { | |
val likesActivity: Flow<Like> = flowOf(*likesList.toTypedArray()) | |
val commentsActivity: Flow<Comment> = flowOf(*commentsList.toTypedArray()) | |
likesActivity.zip(commentsActivity) { like, comment -> | |
if(like.userName == comment.userName && like.postTitle == comment.postTitle) { | |
Log.i("Notification","${like.userName} reacted and commented on your post ${like.postTitle}") | |
} | |
}.collect() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment