Skip to content

Commit 790079a

Browse files
nbradburyclaude
andcommitted
Refactor ReaderBlogSubscriptionUseCase to use Channel instead of Continuation
- Replace Continuation with Channel for async result handling - Remove unused updateEmailPostsFrequency method and its imports - Change switch listeners from setOnClickListener to setOnCheckedChangeListener Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent d133fd6 commit 790079a

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

WordPress/src/main/java/org/wordpress/android/ui/reader/subscription/ReaderBlogSubscriptionUseCase.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package org.wordpress.android.ui.reader.subscription
22

33
import kotlinx.coroutines.CoroutineDispatcher
4-
import kotlinx.coroutines.flow.MutableSharedFlow
5-
import kotlinx.coroutines.flow.first
4+
import kotlinx.coroutines.channels.Channel
65
import kotlinx.coroutines.withContext
76
import org.greenrobot.eventbus.Subscribe
87
import org.greenrobot.eventbus.ThreadMode
@@ -24,7 +23,7 @@ class ReaderBlogSubscriptionUseCase @Inject constructor(
2423
private val networkUtilsWrapper: NetworkUtilsWrapper,
2524
@Named(BG_THREAD) private val bgDispatcher: CoroutineDispatcher
2625
) {
27-
private val updateResultFlow = MutableSharedFlow<UpdateResult>(extraBufferCapacity = 1)
26+
private val updateResultChannel = Channel<UpdateResult>(Channel.BUFFERED)
2827

2928
init {
3029
dispatcher.register(this)
@@ -45,7 +44,7 @@ class ReaderBlogSubscriptionUseCase @Inject constructor(
4544
val action = if (enable) SubscriptionAction.NEW else SubscriptionAction.DELETE
4645
val payload = AddOrDeleteSubscriptionPayload(blogId.toString(), action)
4746
dispatcher.dispatch(AccountActionBuilder.newUpdateSubscriptionNotificationPostAction(payload))
48-
return updateResultFlow.first()
47+
return updateResultChannel.receive()
4948
}
5049

5150
suspend fun updateEmailPosts(blogId: Long, enable: Boolean): UpdateResult {
@@ -55,7 +54,7 @@ class ReaderBlogSubscriptionUseCase @Inject constructor(
5554
val action = if (enable) SubscriptionAction.NEW else SubscriptionAction.DELETE
5655
val payload = AddOrDeleteSubscriptionPayload(blogId.toString(), action)
5756
dispatcher.dispatch(AccountActionBuilder.newUpdateSubscriptionEmailPostAction(payload))
58-
return updateResultFlow.first()
57+
return updateResultChannel.receive()
5958
}
6059

6160
suspend fun updateEmailComments(blogId: Long, enable: Boolean): UpdateResult {
@@ -65,7 +64,7 @@ class ReaderBlogSubscriptionUseCase @Inject constructor(
6564
val action = if (enable) SubscriptionAction.NEW else SubscriptionAction.DELETE
6665
val payload = AddOrDeleteSubscriptionPayload(blogId.toString(), action)
6766
dispatcher.dispatch(AccountActionBuilder.newUpdateSubscriptionEmailCommentAction(payload))
68-
return updateResultFlow.first()
67+
return updateResultChannel.receive()
6968
}
7069

7170
fun refreshSubscriptions() {
@@ -80,7 +79,7 @@ class ReaderBlogSubscriptionUseCase @Inject constructor(
8079
} else {
8180
UpdateResult.Success
8281
}
83-
updateResultFlow.tryEmit(result)
82+
updateResultChannel.trySend(result)
8483

8584
// Refresh subscriptions after successful update
8685
if (!event.isError) {

WordPress/src/main/java/org/wordpress/android/ui/reader/subscription/ReaderSubscriptionSettingsBottomSheetFragment.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,16 @@ class ReaderSubscriptionSettingsBottomSheetFragment : BottomSheetDialogFragment(
6868

6969
private fun setupClickListeners() {
7070
with(binding) {
71-
switchNotifyPosts.setOnClickListener {
72-
viewModel.onNotifyPostsToggled((it as SwitchCompat).isChecked)
71+
switchNotifyPosts.setOnCheckedChangeListener { _, isChecked ->
72+
viewModel.onNotifyPostsToggled(isChecked)
7373
}
7474

75-
switchEmailPosts.setOnClickListener {
76-
viewModel.onEmailPostsToggled((it as SwitchCompat).isChecked)
75+
switchEmailPosts.setOnCheckedChangeListener { _, isChecked ->
76+
viewModel.onEmailPostsToggled(isChecked)
7777
}
7878

79-
switchEmailComments.setOnClickListener {
80-
viewModel.onEmailCommentsToggled((it as SwitchCompat).isChecked)
79+
switchEmailComments.setOnCheckedChangeListener { _, isChecked ->
80+
viewModel.onEmailCommentsToggled(isChecked)
8181
}
8282
}
8383
}
@@ -125,7 +125,7 @@ class ReaderSubscriptionSettingsBottomSheetFragment : BottomSheetDialogFragment(
125125

126126
private fun SwitchCompat.setCheckedSilently(checked: Boolean) {
127127
if (isChecked != checked) {
128-
setOnClickListener(null)
128+
setOnCheckedChangeListener(null)
129129
isChecked = checked
130130
setupClickListeners()
131131
}

0 commit comments

Comments
 (0)