From 97ef9847b6515b90d8efc2e60ad211dc5be02fd7 Mon Sep 17 00:00:00 2001 From: Melad Raouf Date: Mon, 3 Nov 2025 12:15:57 +0000 Subject: [PATCH] [MS-1216] Call viewModel.start() only on first creation of SetupFragment Added a check for savedInstanceState == null to ensure start() is called only once and not re-triggered after configuration changes (e.g. rotation). --- .../feature/setup/screen/SetupFragment.kt | 4 ++- .../setup/screen/SetupViewModelTest.kt | 30 +++++++++++++++---- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/feature/setup/src/main/java/com/simprints/feature/setup/screen/SetupFragment.kt b/feature/setup/src/main/java/com/simprints/feature/setup/screen/SetupFragment.kt index 10f8d32d83..d323fcd071 100644 --- a/feature/setup/src/main/java/com/simprints/feature/setup/screen/SetupFragment.kt +++ b/feature/setup/src/main/java/com/simprints/feature/setup/screen/SetupFragment.kt @@ -94,7 +94,9 @@ internal class SetupFragment : Fragment(R.layout.fragment_setup) { // Overall setup result observeOverallSetupResult() // Start the setup process - viewModel.start() + if (savedInstanceState == null) { + viewModel.start() + } } private fun observeOverallSetupResult() = viewModel.overallSetupResult.observe(viewLifecycleOwner) { diff --git a/feature/setup/src/test/java/com/simprints/feature/setup/screen/SetupViewModelTest.kt b/feature/setup/src/test/java/com/simprints/feature/setup/screen/SetupViewModelTest.kt index 12b03961ce..ea9328b776 100644 --- a/feature/setup/src/test/java/com/simprints/feature/setup/screen/SetupViewModelTest.kt +++ b/feature/setup/src/test/java/com/simprints/feature/setup/screen/SetupViewModelTest.kt @@ -81,7 +81,11 @@ class SetupViewModelTest { fun `should not request location permission if collectLocation is disabled`() = runTest { // Given coEvery { configManager.getProjectConfiguration().general.collectLocation } returns false - coEvery { configManager.getProjectConfiguration().synchronization.down.commCare } returns mockk() + coEvery { + configManager + .getProjectConfiguration() + .synchronization.down.commCare + } returns mockk() // when viewModel.start() @@ -95,7 +99,11 @@ class SetupViewModelTest { // Given justRun { locationStore.collectLocationInBackground() } coEvery { configManager.getProjectConfiguration() } returns mockk() - coEvery { configManager.getProjectConfiguration().synchronization.down.commCare } returns mockk() + coEvery { + configManager + .getProjectConfiguration() + .synchronization.down.commCare + } returns mockk() // when viewModel.locationPermissionCheckDone(granted = true) @@ -109,7 +117,11 @@ class SetupViewModelTest { // Given justRun { locationStore.collectLocationInBackground() } coEvery { configManager.getProjectConfiguration() } returns mockk() - coEvery { configManager.getProjectConfiguration().synchronization.down.commCare } returns mockk() + coEvery { + configManager + .getProjectConfiguration() + .synchronization.down.commCare + } returns mockk() // when viewModel.locationPermissionCheckDone(granted = false) @@ -122,7 +134,11 @@ class SetupViewModelTest { fun `should request CommCare permission if needed when location permission is granted`() = runTest { // Given justRun { locationStore.collectLocationInBackground() } - coEvery { configManager.getProjectConfiguration().synchronization.down.commCare } returns mockk() + coEvery { + configManager + .getProjectConfiguration() + .synchronization.down.commCare + } returns mockk() // When viewModel.locationPermissionCheckDone(true) @@ -135,7 +151,11 @@ class SetupViewModelTest { fun `should request CommCare permission if needed when location permission is not granted`() = runTest { // Given justRun { locationStore.collectLocationInBackground() } - coEvery { configManager.getProjectConfiguration().synchronization.down.commCare } returns mockk() + coEvery { + configManager + .getProjectConfiguration() + .synchronization.down.commCare + } returns mockk() // When viewModel.locationPermissionCheckDone(false)