Skip to content
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

Handle non-existing layers #6184

Merged
merged 3 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import org.odk.collect.async.Scheduler
import org.odk.collect.maps.MapConfigurator
import org.odk.collect.maps.layers.OfflineMapLayersPicker
import org.odk.collect.maps.layers.ReferenceLayerRepository
import org.odk.collect.settings.keys.ProjectKeys
import org.odk.collect.settings.keys.ProjectKeys.CATEGORY_BASEMAP
import org.odk.collect.settings.keys.ProjectKeys.KEY_BASEMAP_SOURCE
import org.odk.collect.strings.localization.getLocalizedString
Expand Down Expand Up @@ -65,8 +66,8 @@ class MapsPreferencesFragment : BaseProjectPreferencesFragment(), Preference.OnP

override fun onSettingChanged(key: String) {
super.onSettingChanged(key)
if (key == REFERENCE_LAYER_KEY) {
findPreference<Preference>(REFERENCE_LAYER_KEY)!!.summary = getLayerName()
if (key == ProjectKeys.KEY_REFERENCE_LAYER) {
findPreference<Preference>(ProjectKeys.KEY_REFERENCE_LAYER)!!.summary = getLayerName()
}
}

Expand All @@ -80,7 +81,7 @@ class MapsPreferencesFragment : BaseProjectPreferencesFragment(), Preference.OnP
override fun onPreferenceClick(preference: Preference): Boolean {
if (allowClick(javaClass.name)) {
when (preference.key) {
REFERENCE_LAYER_KEY -> {
ProjectKeys.KEY_REFERENCE_LAYER -> {
DialogFragmentUtils.showIfNotShowing(
OfflineMapLayersPicker::class.java,
childFragmentManager
Expand Down Expand Up @@ -121,14 +122,14 @@ class MapsPreferencesFragment : BaseProjectPreferencesFragment(), Preference.OnP
}

private fun initLayersPref() {
findPreference<Preference>(REFERENCE_LAYER_KEY)?.apply {
findPreference<Preference>(ProjectKeys.KEY_REFERENCE_LAYER)?.apply {
onPreferenceClickListener = this@MapsPreferencesFragment
summary = getLayerName()
}
}

private fun getLayerName(): String {
val layerId = settingsProvider.getUnprotectedSettings().getString(REFERENCE_LAYER_KEY)
val layerId = settingsProvider.getUnprotectedSettings().getString(ProjectKeys.KEY_REFERENCE_LAYER)
return if (layerId == null) {
requireContext().getLocalizedString(org.odk.collect.strings.R.string.none)
} else {
Expand All @@ -147,14 +148,15 @@ class MapsPreferencesFragment : BaseProjectPreferencesFragment(), Preference.OnP
baseCategory.addPreference(pref)
}

// Clear the reference layer if it isn't supported by the new basemap.
val layerId = settingsProvider.getUnprotectedSettings().getString(REFERENCE_LAYER_KEY)
if (layerId != null && !cftor.supportsLayer(referenceLayerRepository.get(layerId)!!.file)) {
settingsProvider.getUnprotectedSettings().save(REFERENCE_LAYER_KEY, null)
}
}
// Clear the reference layer if it does not exist or it isn't supported by the new basemap.
val layerId = settingsProvider.getUnprotectedSettings().getString(ProjectKeys.KEY_REFERENCE_LAYER)
if (layerId != null) {
val layer = referenceLayerRepository.get(layerId)
if (layer == null || !cftor.supportsLayer(layer.file)) {
settingsProvider.getUnprotectedSettings().save(ProjectKeys.KEY_REFERENCE_LAYER, null)
}

companion object {
const val REFERENCE_LAYER_KEY = "reference_layer"
settingsProvider.getUnprotectedSettings().save(ProjectKeys.KEY_REFERENCE_LAYER, null)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.odk.collect.android.preferences.screens

import android.content.Context
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.gson.Gson
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
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
import org.odk.collect.android.application.initialization.AnalyticsInitializer
import org.odk.collect.android.application.initialization.MapsInitializer
import org.odk.collect.android.injection.config.AppDependencyModule
import org.odk.collect.android.projects.ProjectsDataService
import org.odk.collect.android.support.CollectHelpers
import org.odk.collect.fragmentstest.FragmentScenarioLauncherRule
import org.odk.collect.projects.Project
import org.odk.collect.projects.ProjectsRepository
import org.odk.collect.settings.InMemSettingsProvider
import org.odk.collect.settings.SettingsProvider
import org.odk.collect.settings.keys.ProjectKeys
import org.odk.collect.shared.strings.UUIDGenerator

@RunWith(AndroidJUnit4::class)
class MapsPreferencesFragmentTest {

@get:Rule
val launcherRule = FragmentScenarioLauncherRule()

private val project = Project.DEMO_PROJECT
private val projectsDataService = mock<ProjectsDataService>().apply {
whenever(getCurrentProject()).thenReturn(project)
}
private val projectsRepository = mock<ProjectsRepository>().apply {
whenever(get(project.uuid)).thenReturn(project)
}
private val settingsProvider = InMemSettingsProvider()

@Before
fun setup() {
CollectHelpers.overrideAppDependencyModule(object : AppDependencyModule() {
override fun providesCurrentProjectProvider(
settingsProvider: SettingsProvider,
projectsRepository: ProjectsRepository,
analyticsInitializer: AnalyticsInitializer,
context: Context,
mapsInitializer: MapsInitializer
): ProjectsDataService {
return projectsDataService
}

override fun providesProjectsRepository(
uuidGenerator: UUIDGenerator,
gson: Gson,
settingsProvider: SettingsProvider
): ProjectsRepository {
return projectsRepository
}

override fun providesSettingsProvider(context: Context): SettingsProvider {
return settingsProvider
}
})
}

@Test
fun `if saved layer does not exist it is set to 'none'`() {
val settings = settingsProvider.getUnprotectedSettings()
settings.save(ProjectKeys.KEY_REFERENCE_LAYER, "blah")

launcherRule.launch(MapsPreferencesFragment::class.java)

assertThat(settings.getString(ProjectKeys.KEY_REFERENCE_LAYER), equalTo(null))
}
}