Skip to content

Commit

Permalink
Observing Login State by Combining Flows
Browse files Browse the repository at this point in the history
  • Loading branch information
Rahkeen committed Jul 26, 2024
1 parent 15c5c0f commit 98a6258
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class HackerNewsWebClient(
return PostInfo(
id = itemId,
upvoted = upvoteElement.hasClass("nosee"),
upvoteUrl = upvoteElement.attr("href")
upvoteUrl = BASE_WEB_URL + upvoteElement.attr("href")
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.emergetools.hackernews.features.comments
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import com.emergetools.hackernews.data.CommentFormData
import com.emergetools.hackernews.data.CommentInfo
import com.emergetools.hackernews.data.HackerNewsSearchClient
import com.emergetools.hackernews.data.HackerNewsWebClient
Expand All @@ -12,8 +13,12 @@ import com.emergetools.hackernews.data.relativeTimeStamp
import com.emergetools.hackernews.features.login.LoginDestinations
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
Expand All @@ -40,7 +45,8 @@ sealed interface CommentsState {
val loggedIn: Boolean,
val upvoted: Boolean,
val upvoteUrl: String,
val postComment: PostCommentState?,
val commentText: String,
val formData: CommentFormData?,
override val comments: List<CommentState>,
) : CommentsState {
override val headerState = HeaderState.Content(
Expand All @@ -52,6 +58,15 @@ sealed interface CommentsState {
upvoted = upvoted,
upvoteUrl = upvoteUrl
)
val postComment = formData?.let { data ->
PostCommentState(
parentId = data.parentId,
goToUrl = data.gotoUrl,
hmac = data.hmac,
loggedIn = loggedIn,
text = commentText
)
}
}
}

Expand Down Expand Up @@ -130,7 +145,24 @@ class CommentsViewModel(
private val userStorage: UserStorage,
) : ViewModel() {
private val internalState = MutableStateFlow<CommentsState>(CommentsState.Loading)
val state = internalState.asStateFlow()
private val loggedInFlow = userStorage.getCookie().map { !it.isNullOrEmpty() }

val state = combine(
loggedInFlow,
internalState.filterIsInstance<CommentsState.Content>()
) { loggedIn, state ->
if (loggedIn != state.loggedIn) {
state.copy(
loggedIn = loggedIn
)
} else {
state
}
}.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(),
initialValue = CommentsState.Loading
)

init {
viewModelScope.launch {
Expand All @@ -151,15 +183,8 @@ class CommentsViewModel(
loggedIn = loggedIn,
upvoted = pageInfo.postInfo.upvoted,
upvoteUrl = pageInfo.postInfo.upvoteUrl,
postComment = pageInfo.commentFormData?.let { data ->
PostCommentState(
parentId = data.parentId,
loggedIn = loggedIn,
goToUrl = data.gotoUrl,
hmac = data.hmac,
text = ""
)
},
commentText = "",
formData = pageInfo.commentFormData,
comments = comments,
)
}
Expand Down Expand Up @@ -195,9 +220,7 @@ class CommentsViewModel(
val current = internalState.value
if (current is CommentsState.Content) {
internalState.value = current.copy(
postComment = current.postComment?.copy(
text = action.text
)
commentText = action.text
)
}
}
Expand All @@ -207,9 +230,7 @@ class CommentsViewModel(
val current = internalState.value
if (current is CommentsState.Content && current.postComment != null) {
internalState.value = current.copy(
postComment = current.postComment.copy(
text = ""
)
commentText = ""
)
val (parentId, gotoUrl, hmac, _, text) = current.postComment
webClient.postComment(parentId, gotoUrl, hmac, text)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ private fun CommentsScreenPreview() {
loggedIn = false,
upvoted = false,
upvoteUrl = "",
postComment = null,
commentText = "",
formData = null,
comments = listOf(
CommentState.Content(
id = 1,
Expand Down

0 comments on commit 98a6258

Please sign in to comment.