-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[Multiple-Profile]feat: define trigger app restart guard #20652
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
Merged
Merged
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
81 changes: 81 additions & 0 deletions
81
AnkiDroid/src/main/java/com/ichi2/anki/multiprofile/ProfileSwitchGuard.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,81 @@ | ||
| /* | ||
| * Copyright (c) 2026 Ashish Yadav <mailtoashish693@gmail.com> | ||
| * | ||
| * This program is free software; you can redistribute it and/or modify it under | ||
| * the terms of the GNU General Public License as published by the Free Software | ||
| * Foundation; either version 3 of the License, or (at your option) any later | ||
| * version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| * details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License along with | ||
| * this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package com.ichi2.anki.multiprofile | ||
|
|
||
| import com.ichi2.anki.multiprofile.ProfileManager.ProfileSwitchContext | ||
|
|
||
| /** | ||
| * Guards profile switching by running a set of safety checks | ||
| * before delegating to [ProfileManager]. | ||
| * | ||
| * @param profileManager The manager that persists the switch. | ||
| * @param checks Ordered list of safety checks to run | ||
| * before allowing the switch. | ||
| */ | ||
| class ProfileSwitchGuard( | ||
| private val profileManager: ProfileManager, | ||
| private val checks: List<SafetyCheck>, | ||
| ) { | ||
| sealed class Result { | ||
| data object Success : Result() | ||
|
|
||
| /** | ||
| * Indicates the switch was blocked. | ||
| * @param reasons All currently active blocks that are NOT being ignored. | ||
| */ | ||
| data class Blocked( | ||
| val reasons: Set<BlockReason>, | ||
| ) : Result() | ||
| } | ||
|
|
||
| enum class BlockReason { | ||
| BACKUP_IN_PROGRESS, | ||
| MEDIA_SYNC_IN_PROGRESS, | ||
| COLLECTION_BUSY, | ||
| } | ||
|
|
||
| fun interface SafetyCheck { | ||
| suspend fun verify(): BlockReason? | ||
| } | ||
|
|
||
| /** | ||
| * Runs all checks. | ||
| * @param newProfileId The target profile. | ||
| * @param skipReasons A set of reasons the user has explicitly chosen to ignore. | ||
| */ | ||
| suspend operator fun invoke( | ||
| newProfileId: ProfileId, | ||
| skipReasons: Set<BlockReason> = emptySet(), | ||
| ): Result { | ||
| val activeBlockedReasons = mutableSetOf<BlockReason>() | ||
|
|
||
| for (check in checks) { | ||
| val reason = check.verify() | ||
| if (reason != null && !skipReasons.contains(reason)) { | ||
| activeBlockedReasons.add(reason) | ||
| } | ||
| } | ||
|
|
||
| return if (activeBlockedReasons.isEmpty()) { | ||
| with(ProfileSwitchContext) { profileManager.switchActiveProfile(newProfileId) } | ||
| Result.Success | ||
| } else { | ||
| Result.Blocked(activeBlockedReasons) | ||
| } | ||
| } | ||
| } | ||
138 changes: 138 additions & 0 deletions
138
AnkiDroid/src/test/java/com/ichi2/anki/multiprofile/ProfileSwitchGuardTest.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) 2026 Ashish Yadav <mailtoashish693@gmail.com> | ||
| * | ||
| * This program is free software; you can redistribute it and/or modify it under | ||
| * the terms of the GNU General Public License as published by the Free Software | ||
| * Foundation; either version 3 of the License, or (at your option) any later | ||
| * version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| * details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License along with | ||
| * this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package com.ichi2.anki.multiprofile | ||
|
|
||
| import com.ichi2.anki.multiprofile.ProfileManager.ProfileSwitchContext | ||
| import com.ichi2.anki.multiprofile.ProfileSwitchGuard.BlockReason | ||
| import com.ichi2.anki.multiprofile.ProfileSwitchGuard.Result | ||
| import com.ichi2.anki.multiprofile.ProfileSwitchGuard.SafetyCheck | ||
| import io.mockk.coEvery | ||
| import io.mockk.mockk | ||
| import io.mockk.verify | ||
| import kotlinx.coroutines.test.runTest | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Test | ||
|
|
||
| class ProfileSwitchGuardTest { | ||
| private val profileManager: ProfileManager = mockk(relaxed = true) | ||
| private val targetId = ProfileId("p_12345678") | ||
|
|
||
| @Test | ||
| fun `invoke returns Success and switches when all checks pass`() = | ||
| runTest { | ||
| val check1 = | ||
| mockk<SafetyCheck> { | ||
| coEvery { verify() } returns null | ||
| } | ||
| val check2 = | ||
| mockk<SafetyCheck> { | ||
| coEvery { verify() } returns null | ||
| } | ||
|
|
||
| val guard = ProfileSwitchGuard(profileManager, listOf(check1, check2)) | ||
|
|
||
| val result = guard(targetId) | ||
|
|
||
| assertEquals(Result.Success, result) | ||
| with(ProfileSwitchContext) { | ||
| verify(exactly = 1) { profileManager.switchActiveProfile(targetId) } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `invoke returns Blocked and does NOT switch when a check fails`() = | ||
| runTest { | ||
| val check1 = | ||
| mockk<SafetyCheck> { | ||
| coEvery { verify() } returns BlockReason.BACKUP_IN_PROGRESS | ||
| } | ||
|
|
||
| val guard = ProfileSwitchGuard(profileManager, listOf(check1)) | ||
|
|
||
| val result = guard(targetId) | ||
|
|
||
| assertTrue(result is Result.Blocked) | ||
| assertEquals(setOf(BlockReason.BACKUP_IN_PROGRESS), (result as Result.Blocked).reasons) | ||
|
|
||
| with(ProfileSwitchContext) { | ||
| verify(exactly = 0) { profileManager.switchActiveProfile(any()) } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `invoke returns Success if the blocked reason is in skipReasons`() = | ||
| runTest { | ||
| val check1 = | ||
| mockk<SafetyCheck> { | ||
| coEvery { verify() } returns BlockReason.MEDIA_SYNC_IN_PROGRESS | ||
| } | ||
|
|
||
| val guard = ProfileSwitchGuard(profileManager, listOf(check1)) | ||
|
|
||
| val result = guard(targetId, skipReasons = setOf(BlockReason.MEDIA_SYNC_IN_PROGRESS)) | ||
|
|
||
| assertEquals(Result.Success, result) | ||
| with(ProfileSwitchContext) { | ||
| verify(exactly = 1) { profileManager.switchActiveProfile(targetId) } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `invoke returns Blocked if multiple checks fail and only one is skipped`() = | ||
| runTest { | ||
| val syncCheck = | ||
| mockk<SafetyCheck> { | ||
| coEvery { verify() } returns BlockReason.MEDIA_SYNC_IN_PROGRESS | ||
| } | ||
| val backupCheck = | ||
| mockk<SafetyCheck> { | ||
| coEvery { verify() } returns BlockReason.BACKUP_IN_PROGRESS | ||
| } | ||
|
|
||
| val guard = ProfileSwitchGuard(profileManager, listOf(syncCheck, backupCheck)) | ||
|
|
||
| val result = guard(targetId, skipReasons = setOf(BlockReason.MEDIA_SYNC_IN_PROGRESS)) | ||
|
|
||
| assertTrue(result is Result.Blocked) | ||
| val blockedReasons = (result as Result.Blocked).reasons | ||
| assertEquals(1, blockedReasons.size) | ||
| assertTrue(blockedReasons.contains(BlockReason.BACKUP_IN_PROGRESS)) | ||
|
|
||
| with(ProfileSwitchContext) { | ||
| verify(exactly = 0) { profileManager.switchActiveProfile(any()) } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `invoke collects all active blocked reasons if multiple fail`() = | ||
| runTest { | ||
| val check1 = mockk<SafetyCheck> { coEvery { verify() } returns BlockReason.BACKUP_IN_PROGRESS } | ||
| val check2 = mockk<SafetyCheck> { coEvery { verify() } returns BlockReason.COLLECTION_BUSY } | ||
|
|
||
| val guard = ProfileSwitchGuard(profileManager, listOf(check1, check2)) | ||
|
|
||
| val result = guard(targetId) | ||
|
|
||
| assertTrue(result is Result.Blocked) | ||
| assertEquals( | ||
| setOf(BlockReason.BACKUP_IN_PROGRESS, BlockReason.COLLECTION_BUSY), | ||
| (result as Result.Blocked).reasons, | ||
| ) | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.