diff --git a/collect_app/src/main/java/org/odk/collect/android/injection/config/AppDependencyComponent.java b/collect_app/src/main/java/org/odk/collect/android/injection/config/AppDependencyComponent.java index 404728e990a..a1512e95923 100644 --- a/collect_app/src/main/java/org/odk/collect/android/injection/config/AppDependencyComponent.java +++ b/collect_app/src/main/java/org/odk/collect/android/injection/config/AppDependencyComponent.java @@ -16,8 +16,8 @@ import org.odk.collect.android.application.initialization.ExistingProjectMigrator; import org.odk.collect.android.audio.AudioRecordingControllerFragment; import org.odk.collect.android.audio.AudioRecordingErrorDialogFragment; -import org.odk.collect.android.backgroundwork.SendFormsTaskSpec; import org.odk.collect.android.backgroundwork.AutoUpdateTaskSpec; +import org.odk.collect.android.backgroundwork.SendFormsTaskSpec; import org.odk.collect.android.backgroundwork.SyncFormsTaskSpec; import org.odk.collect.android.configure.qr.QRCodeScannerFragment; import org.odk.collect.android.configure.qr.QRCodeTabsActivity; @@ -47,7 +47,6 @@ import org.odk.collect.android.preferences.dialogs.ChangeAdminPasswordDialog; import org.odk.collect.android.preferences.dialogs.ResetDialogPreferenceFragmentCompat; import org.odk.collect.android.preferences.dialogs.ServerAuthDialogFragment; -import org.odk.collect.android.preferences.screens.BaseAdminPreferencesFragment; import org.odk.collect.android.preferences.screens.BasePreferencesFragment; import org.odk.collect.android.preferences.screens.BaseProjectPreferencesFragment; import org.odk.collect.android.preferences.screens.ExperimentalPreferencesFragment; @@ -77,8 +76,8 @@ import org.odk.collect.android.utilities.ThemeUtils; import org.odk.collect.android.widgets.QuestionWidget; import org.odk.collect.android.widgets.items.SelectOneFromMapDialogFragment; -import org.odk.collect.async.network.NetworkStateProvider; import org.odk.collect.async.Scheduler; +import org.odk.collect.async.network.NetworkStateProvider; import org.odk.collect.draw.DrawActivity; import org.odk.collect.googlemaps.GoogleMapFragment; import org.odk.collect.location.LocationClient; @@ -230,8 +229,6 @@ interface Builder { void inject(BaseProjectPreferencesFragment baseProjectPreferencesFragment); - void inject(BaseAdminPreferencesFragment baseAdminPreferencesFragment); - void inject(AndroidShortcutsActivity androidShortcutsActivity); void inject(ProjectSettingsDialog projectSettingsDialog); diff --git a/collect_app/src/main/java/org/odk/collect/android/injection/config/AppDependencyModule.java b/collect_app/src/main/java/org/odk/collect/android/injection/config/AppDependencyModule.java index 531f3c2aa08..ff00be36f83 100644 --- a/collect_app/src/main/java/org/odk/collect/android/injection/config/AppDependencyModule.java +++ b/collect_app/src/main/java/org/odk/collect/android/injection/config/AppDependencyModule.java @@ -69,7 +69,6 @@ import org.odk.collect.android.preferences.Defaults; import org.odk.collect.android.preferences.PreferenceVisibilityHandler; import org.odk.collect.android.preferences.ProjectPreferencesViewModel; -import org.odk.collect.android.preferences.source.SettingsStore; import org.odk.collect.android.preferences.source.SharedPreferencesSettingsProvider; import org.odk.collect.android.projects.ProjectCreator; import org.odk.collect.android.projects.ProjectDeleter; @@ -394,18 +393,6 @@ public FormSessionRepository providesFormSessionStore(Application application) { return new AppStateFormSessionRepository(application); } - @Provides - @Named("GENERAL_SETTINGS_STORE") - public SettingsStore providesGeneralSettingsStore(SettingsProvider settingsProvider) { - return new SettingsStore(settingsProvider.getUnprotectedSettings()); - } - - @Provides - @Named("ADMIN_SETTINGS_STORE") - public SettingsStore providesAdminSettingsStore(SettingsProvider settingsProvider) { - return new SettingsStore(settingsProvider.getProtectedSettings()); - } - @Provides public ExternalWebPageHelper providesExternalWebPageHelper() { return new ExternalWebPageHelper(); diff --git a/collect_app/src/main/java/org/odk/collect/android/preferences/screens/BaseAdminPreferencesFragment.java b/collect_app/src/main/java/org/odk/collect/android/preferences/screens/BaseAdminPreferencesFragment.java index 1f98d8b22af..66a3fd832bf 100644 --- a/collect_app/src/main/java/org/odk/collect/android/preferences/screens/BaseAdminPreferencesFragment.java +++ b/collect_app/src/main/java/org/odk/collect/android/preferences/screens/BaseAdminPreferencesFragment.java @@ -6,43 +6,40 @@ import androidx.annotation.NonNull; import org.jetbrains.annotations.NotNull; -import org.odk.collect.android.injection.DaggerUtils; import org.odk.collect.android.preferences.source.SettingsStore; - -import javax.inject.Inject; -import javax.inject.Named; +import org.odk.collect.shared.settings.Settings; public abstract class BaseAdminPreferencesFragment extends BasePreferencesFragment { - @Inject - @Named("ADMIN_SETTINGS_STORE") - SettingsStore adminSettingsStore; + private String projectId; + private Settings adminSettings; @Override public void onAttach(@NonNull Context context) { super.onAttach(context); - DaggerUtils.getComponent(context).inject(this); + projectId = projectsDataService.getCurrentProject().getUuid(); + adminSettings = settingsProvider.getProtectedSettings(projectId); } @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { - getPreferenceManager().setPreferenceDataStore(adminSettingsStore); + getPreferenceManager().setPreferenceDataStore(new SettingsStore(adminSettings)); } @Override public void onResume() { super.onResume(); - settingsProvider.getProtectedSettings().registerOnSettingChangeListener(this); + adminSettings.registerOnSettingChangeListener(this); } @Override public void onPause() { super.onPause(); - settingsProvider.getProtectedSettings().unregisterOnSettingChangeListener(this); + adminSettings.unregisterOnSettingChangeListener(this); } @Override public void onSettingChanged(@NotNull String key) { - settingsChangeHandler.onSettingChanged(projectsDataService.getCurrentProject().getUuid(), settingsProvider.getProtectedSettings().getAll().get(key), key); + settingsChangeHandler.onSettingChanged(projectId, adminSettings.getAll().get(key), key); } } diff --git a/collect_app/src/main/java/org/odk/collect/android/preferences/screens/BaseProjectPreferencesFragment.java b/collect_app/src/main/java/org/odk/collect/android/preferences/screens/BaseProjectPreferencesFragment.java index 7c998c48ee9..46b03df0d2b 100644 --- a/collect_app/src/main/java/org/odk/collect/android/preferences/screens/BaseProjectPreferencesFragment.java +++ b/collect_app/src/main/java/org/odk/collect/android/preferences/screens/BaseProjectPreferencesFragment.java @@ -14,16 +14,12 @@ import org.odk.collect.android.preferences.ProjectPreferencesViewModel; import org.odk.collect.android.preferences.source.SettingsStore; import org.odk.collect.android.utilities.AdminPasswordProvider; +import org.odk.collect.shared.settings.Settings; import javax.inject.Inject; -import javax.inject.Named; public abstract class BaseProjectPreferencesFragment extends BasePreferencesFragment { - @Inject - @Named("GENERAL_SETTINGS_STORE") - SettingsStore generalSettingsStore; - @Inject AdminPasswordProvider adminPasswordProvider; @@ -34,17 +30,22 @@ public abstract class BaseProjectPreferencesFragment extends BasePreferencesFrag PreferenceVisibilityHandler preferenceVisibilityHandler; protected ProjectPreferencesViewModel projectPreferencesViewModel; + private Settings settings; + private String projectId; @Override public void onAttach(@NonNull Context context) { super.onAttach(context); DaggerUtils.getComponent(context).inject(this); projectPreferencesViewModel = new ViewModelProvider(requireActivity(), factory).get(ProjectPreferencesViewModel.class); + + projectId = projectsDataService.getCurrentProject().getUuid(); + settings = settingsProvider.getUnprotectedSettings(projectId); } @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { - getPreferenceManager().setPreferenceDataStore(generalSettingsStore); + getPreferenceManager().setPreferenceDataStore(new SettingsStore(settings)); } @Override @@ -56,17 +57,17 @@ public void onViewCreated(@NotNull View view, @Nullable Bundle savedInstanceStat @Override public void onResume() { super.onResume(); - settingsProvider.getUnprotectedSettings().registerOnSettingChangeListener(this); + settings.registerOnSettingChangeListener(this); } @Override public void onPause() { super.onPause(); - settingsProvider.getUnprotectedSettings().unregisterOnSettingChangeListener(this); + settings.unregisterOnSettingChangeListener(this); } @Override public void onSettingChanged(@NotNull String key) { - settingsChangeHandler.onSettingChanged(projectsDataService.getCurrentProject().getUuid(), settingsProvider.getUnprotectedSettings().getAll().get(key), key); + settingsChangeHandler.onSettingChanged(projectId, settings.getAll().get(key), key); } } diff --git a/collect_app/src/test/java/org/odk/collect/android/preferences/screens/FormMetadataPreferencesFragmentTest.kt b/collect_app/src/test/java/org/odk/collect/android/preferences/screens/FormMetadataPreferencesFragmentTest.kt index 45e9c3bfc3a..8ec2fd3ece3 100644 --- a/collect_app/src/test/java/org/odk/collect/android/preferences/screens/FormMetadataPreferencesFragmentTest.kt +++ b/collect_app/src/test/java/org/odk/collect/android/preferences/screens/FormMetadataPreferencesFragmentTest.kt @@ -1,7 +1,7 @@ package org.odk.collect.android.preferences.screens -import android.content.Context import androidx.preference.Preference +import androidx.test.core.app.ApplicationProvider import androidx.test.ext.junit.runners.AndroidJUnit4 import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo @@ -11,19 +11,19 @@ import org.junit.Test import org.junit.runner.RunWith import org.mockito.kotlin.mock import org.mockito.kotlin.whenever -import org.odk.collect.android.R +import org.odk.collect.android.application.Collect import org.odk.collect.android.injection.config.AppDependencyModule import org.odk.collect.android.support.CollectHelpers import org.odk.collect.fragmentstest.FragmentScenarioLauncherRule import org.odk.collect.metadata.InstallIDProvider -import org.odk.collect.settings.InMemSettingsProvider import org.odk.collect.settings.SettingsProvider import org.odk.collect.settings.keys.ProjectKeys @RunWith(AndroidJUnit4::class) class FormMetadataPreferencesFragmentTest { private val installIDProvider = mock() - private val settingsProvider = InMemSettingsProvider() + private val settingsProvider = + ApplicationProvider.getApplicationContext().component.settingsProvider() @get:Rule var launcherRule = FragmentScenarioLauncherRule() @@ -34,11 +34,9 @@ class FormMetadataPreferencesFragmentTest { override fun providesInstallIDProvider(settingsProvider: SettingsProvider): InstallIDProvider { return installIDProvider } - - override fun providesSettingsProvider(context: Context): SettingsProvider { - return settingsProvider - } }) + + CollectHelpers.setupDemoProject() } @Test @@ -72,7 +70,8 @@ class FormMetadataPreferencesFragmentTest { whenever(installIDProvider.installID).thenReturn("123456789") settingsProvider.getUnprotectedSettings().save(ProjectKeys.KEY_METADATA_USERNAME, "John") settingsProvider.getUnprotectedSettings().save(ProjectKeys.KEY_METADATA_PHONENUMBER, "789") - settingsProvider.getUnprotectedSettings().save(ProjectKeys.KEY_METADATA_EMAIL, "john@gmail.com") + settingsProvider.getUnprotectedSettings() + .save(ProjectKeys.KEY_METADATA_EMAIL, "john@gmail.com") launcherRule .launch(FormMetadataPreferencesFragment::class.java)