-
Notifications
You must be signed in to change notification settings - Fork 998
Refresh duck.ai when subscription state changes #6395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cmonfortep
wants to merge
1
commit into
feature/cristian/subscriptions/show_duck_ai_pro_settings
Choose a base branch
from
feature/cristian/duckai/refresh_on_subscription_changes
base: feature/cristian/subscriptions/show_duck_ai_pro_settings
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+450
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
57 changes: 57 additions & 0 deletions
57
...at-impl/src/main/java/com/duckduckgo/duckchat/impl/ui/DuckChatWebViewActivityViewModel.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright (c) 2025 DuckDuckGo | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.duckduckgo.duckchat.impl.ui | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.duckduckgo.anvil.annotations.ContributesViewModel | ||
import com.duckduckgo.di.scopes.ActivityScope | ||
import com.duckduckgo.subscriptions.api.Subscriptions | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.channels.BufferOverflow.DROP_OLDEST | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.flow.distinctUntilChanged | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.onEach | ||
import kotlinx.coroutines.flow.receiveAsFlow | ||
import logcat.logcat | ||
|
||
@ContributesViewModel(ActivityScope::class) | ||
class DuckChatWebViewActivityViewModel @Inject constructor( | ||
private val subscriptions: Subscriptions, | ||
) : ViewModel() { | ||
|
||
private val commandChannel = Channel<Command>(capacity = 1, onBufferOverflow = DROP_OLDEST) | ||
val commands = commandChannel.receiveAsFlow() | ||
|
||
sealed class Command { | ||
data object SendSubscriptionAuthUpdateEvent : Command() | ||
} | ||
|
||
init { | ||
observeSubscriptionChanges() | ||
} | ||
|
||
private fun observeSubscriptionChanges() { | ||
subscriptions.getSubscriptionStatusFlow() | ||
.distinctUntilChanged() | ||
.onEach { _ -> | ||
logcat { "CRIS: SUBSCRIPTION STATUS CHANGED" } | ||
commandChannel.trySend(Command.SendSubscriptionAuthUpdateEvent) | ||
}.launchIn(viewModelScope) | ||
} | ||
} |
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
57 changes: 57 additions & 0 deletions
57
...t/duckchat-impl/src/main/java/com/duckduckgo/duckchat/impl/ui/DuckChatWebViewViewModel.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright (c) 2025 DuckDuckGo | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.duckduckgo.duckchat.impl.ui | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.duckduckgo.anvil.annotations.ContributesViewModel | ||
import com.duckduckgo.di.scopes.FragmentScope | ||
import com.duckduckgo.subscriptions.api.Subscriptions | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.channels.BufferOverflow.DROP_OLDEST | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.flow.distinctUntilChanged | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.onEach | ||
import kotlinx.coroutines.flow.receiveAsFlow | ||
import logcat.logcat | ||
|
||
@ContributesViewModel(FragmentScope::class) | ||
class DuckChatWebViewViewModel @Inject constructor( | ||
private val subscriptions: Subscriptions, | ||
) : ViewModel() { | ||
|
||
private val commandChannel = Channel<Command>(capacity = 1, onBufferOverflow = DROP_OLDEST) | ||
val commands = commandChannel.receiveAsFlow() | ||
|
||
sealed class Command { | ||
data object SendSubscriptionAuthUpdateEvent : Command() | ||
} | ||
|
||
init { | ||
observeSubscriptionChanges() | ||
} | ||
|
||
private fun observeSubscriptionChanges() { | ||
subscriptions.getSubscriptionStatusFlow() | ||
.distinctUntilChanged() | ||
.onEach { _ -> | ||
logcat { "CRIS: SUBSCRIPTION STATUS CHANGED" } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like a debugging leftover. |
||
commandChannel.trySend(Command.SendSubscriptionAuthUpdateEvent) | ||
}.launchIn(viewModelScope) | ||
} | ||
} |
138 changes: 138 additions & 0 deletions
138
...mpl/src/test/java/com/duckduckgo/duckchat/impl/ui/DuckChatWebViewActivityViewModelTest.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
* Copyright (c) 2025 DuckDuckGo | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.duckduckgo.duckchat.impl.ui | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import app.cash.turbine.test | ||
import com.duckduckgo.common.test.CoroutineTestRule | ||
import com.duckduckgo.duckchat.impl.ui.DuckChatWebViewActivityViewModel.Command | ||
import com.duckduckgo.subscriptions.api.SubscriptionStatus | ||
import com.duckduckgo.subscriptions.api.SubscriptionStatus.AUTO_RENEWABLE | ||
import com.duckduckgo.subscriptions.api.SubscriptionStatus.EXPIRED | ||
import com.duckduckgo.subscriptions.api.SubscriptionStatus.INACTIVE | ||
import com.duckduckgo.subscriptions.api.SubscriptionStatus.UNKNOWN | ||
import com.duckduckgo.subscriptions.api.Subscriptions | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.whenever | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class DuckChatWebViewActivityViewModelTest { | ||
|
||
@get:Rule | ||
val coroutineTestRule: CoroutineTestRule = CoroutineTestRule() | ||
|
||
private val subscriptions: Subscriptions = mock() | ||
private val subscriptionStatusFlow = MutableSharedFlow<SubscriptionStatus>() | ||
|
||
private lateinit var viewModel: DuckChatWebViewActivityViewModel | ||
|
||
@Before | ||
fun setup() { | ||
whenever(subscriptions.getSubscriptionStatusFlow()).thenReturn(subscriptionStatusFlow) | ||
viewModel = DuckChatWebViewActivityViewModel(subscriptions) | ||
} | ||
|
||
@Test | ||
fun whenSubscriptionStatusChangesToActiveThenSendSubscriptionAuthUpdateEventCommand() = runTest { | ||
viewModel.commands.test { | ||
subscriptionStatusFlow.emit(AUTO_RENEWABLE) | ||
|
||
val command = awaitItem() | ||
assertTrue(command is Command.SendSubscriptionAuthUpdateEvent) | ||
} | ||
} | ||
|
||
@Test | ||
fun whenSubscriptionStatusChangesToInactiveThenSendSubscriptionAuthUpdateEventCommand() = runTest { | ||
viewModel.commands.test { | ||
subscriptionStatusFlow.emit(INACTIVE) | ||
|
||
val command = awaitItem() | ||
assertTrue(command is Command.SendSubscriptionAuthUpdateEvent) | ||
} | ||
} | ||
|
||
@Test | ||
fun whenSubscriptionStatusChangesToExpiredThenSendSubscriptionAuthUpdateEventCommand() = runTest { | ||
viewModel.commands.test { | ||
subscriptionStatusFlow.emit(EXPIRED) | ||
|
||
val command = awaitItem() | ||
assertTrue(command is Command.SendSubscriptionAuthUpdateEvent) | ||
} | ||
} | ||
|
||
@Test | ||
fun whenSubscriptionStatusChangesToUnknownThenSendSubscriptionAuthUpdateEventCommand() = runTest { | ||
viewModel.commands.test { | ||
subscriptionStatusFlow.emit(UNKNOWN) | ||
|
||
val command = awaitItem() | ||
assertTrue(command is Command.SendSubscriptionAuthUpdateEvent) | ||
} | ||
} | ||
|
||
@Test | ||
fun whenSubscriptionStatusChangesTwiceToSameValueThenOnlyOneCommandSent() = runTest { | ||
viewModel.commands.test { | ||
// Emit the same status twice | ||
subscriptionStatusFlow.emit(AUTO_RENEWABLE) | ||
subscriptionStatusFlow.emit(AUTO_RENEWABLE) | ||
|
||
// Should only receive one command due to distinctUntilChanged | ||
val command = awaitItem() | ||
assertTrue(command is Command.SendSubscriptionAuthUpdateEvent) | ||
expectNoEvents() | ||
} | ||
} | ||
|
||
@Test | ||
fun whenSubscriptionStatusChangesTwiceToDifferentValuesThenTwoCommandsSent() = runTest { | ||
viewModel.commands.test { | ||
subscriptionStatusFlow.emit(AUTO_RENEWABLE) | ||
subscriptionStatusFlow.emit(EXPIRED) | ||
|
||
val firstCommand = awaitItem() | ||
assertTrue(firstCommand is Command.SendSubscriptionAuthUpdateEvent) | ||
|
||
val secondCommand = awaitItem() | ||
assertTrue(secondCommand is Command.SendSubscriptionAuthUpdateEvent) | ||
} | ||
} | ||
|
||
@Test | ||
fun whenMultipleSubscriptionStatusChangesOccurThenCorrespondingCommandsSent() = runTest { | ||
viewModel.commands.test { | ||
subscriptionStatusFlow.emit(UNKNOWN) | ||
subscriptionStatusFlow.emit(INACTIVE) | ||
subscriptionStatusFlow.emit(AUTO_RENEWABLE) | ||
subscriptionStatusFlow.emit(EXPIRED) | ||
|
||
repeat(4) { | ||
val command = awaitItem() | ||
assertTrue(command is Command.SendSubscriptionAuthUpdateEvent) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like a debugging leftover.