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 unsupported files when importing .mbtiles #6230

Merged
merged 3 commits into from
Jun 27, 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 @@ -8,10 +8,14 @@ import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import org.odk.collect.async.Scheduler
import org.odk.collect.maps.databinding.OfflineMapLayersImporterBinding
import org.odk.collect.material.MaterialFullScreenDialogFragment
import org.odk.collect.settings.SettingsProvider
import org.odk.collect.strings.R
import org.odk.collect.strings.localization.getLocalizedQuantityString
import org.odk.collect.strings.localization.getLocalizedString

class OfflineMapLayersImporter(
private val referenceLayerRepository: ReferenceLayerRepository,
Expand Down Expand Up @@ -65,8 +69,19 @@ class OfflineMapLayersImporter(
}

viewModel.layersToImport.observe(this) { layersToImport ->
val adapter = OfflineMapLayersImporterAdapter(layersToImport)
val adapter = OfflineMapLayersImporterAdapter(layersToImport.value.layers)
binding.layers.setAdapter(adapter)

if (!layersToImport.isConsumed()) {
layersToImport.consume()

if (layersToImport.value.numberOfSelectedLayers == layersToImport.value.numberOfUnsupportedLayers) {
dismiss()
showNoSupportedLayersWarning(layersToImport.value.numberOfUnsupportedLayers)
} else if (layersToImport.value.numberOfUnsupportedLayers > 0) {
showSomeUnsupportedLayersWarning(layersToImport.value.numberOfSelectedLayers - layersToImport.value.numberOfUnsupportedLayers)
}
}
}
}

Expand All @@ -79,4 +94,34 @@ class OfflineMapLayersImporter(
override fun getToolbar(): Toolbar {
return OfflineMapLayersImporterBinding.bind(requireView()).toolbar
}

private fun showNoSupportedLayersWarning(numberOfLayers: Int) {
MaterialAlertDialogBuilder(requireActivity())
.setTitle(
requireActivity().getLocalizedQuantityString(
R.plurals.non_mbtiles_files_selected_title,
numberOfLayers,
numberOfLayers
)
)
.setMessage(requireActivity().getLocalizedString(R.string.all_non_mbtiles_files_selected_message))
.setPositiveButton(R.string.ok, null)
.create()
.show()
}

private fun showSomeUnsupportedLayersWarning(numberOfLayers: Int) {
MaterialAlertDialogBuilder(requireActivity())
.setTitle(
requireActivity().getLocalizedQuantityString(
R.plurals.non_mbtiles_files_selected_title,
numberOfLayers,
numberOfLayers
)
)
.setMessage(requireActivity().getLocalizedString(R.string.some_non_mbtiles_files_selected_message))
.setPositiveButton(R.string.ok, null)
.create()
.show()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import org.odk.collect.analytics.Analytics
import org.odk.collect.androidshared.data.Consumable
import org.odk.collect.androidshared.system.copyToFile
import org.odk.collect.androidshared.system.getFileExtension
import org.odk.collect.androidshared.system.getFileName
Expand All @@ -27,8 +28,8 @@ class OfflineMapLayersViewModel(
private val _existingLayers = MutableLiveData<List<ReferenceLayer>>()
val existingLayers: LiveData<List<ReferenceLayer>> = _existingLayers

private val _layersToImport = MutableLiveData<List<ReferenceLayer>>()
val layersToImport: LiveData<List<ReferenceLayer>> = _layersToImport
private val _layersToImport = MutableLiveData<Consumable<LayersToImport>>()
val layersToImport: LiveData<Consumable<LayersToImport>> = _layersToImport

private lateinit var tempLayersDir: File

Expand Down Expand Up @@ -67,7 +68,15 @@ class OfflineMapLayersViewModel(
}
}
_isLoading.postValue(false)
_layersToImport.postValue(layers.sortedBy { it.name })
_layersToImport.postValue(
Consumable(
LayersToImport(
uris.size,
uris.size - layers.size,
layers.sortedBy { it.name }
)
)
)
},
foreground = { }
)
Expand Down Expand Up @@ -119,4 +128,10 @@ class OfflineMapLayersViewModel(

Analytics.log(event)
}

data class LayersToImport(
val numberOfSelectedLayers: Int,
val numberOfUnsupportedLayers: Int,
val layers: List<ReferenceLayer>
)
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.odk.collect.maps.layers

import android.app.Application
import androidx.core.net.toUri
import androidx.fragment.app.testing.FragmentScenario
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.RootMatchers.isDialog
import androidx.test.espresso.matcher.ViewMatchers.isChecked
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.isEnabled
Expand All @@ -25,6 +28,7 @@ import org.odk.collect.fragmentstest.FragmentScenarioLauncherRule
import org.odk.collect.settings.InMemSettingsProvider
import org.odk.collect.shared.TempFiles
import org.odk.collect.strings.R
import org.odk.collect.strings.localization.getLocalizedQuantityString
import org.odk.collect.testshared.FakeScheduler
import org.odk.collect.testshared.Interactions
import org.odk.collect.testshared.RecyclerViewMatcher
Expand Down Expand Up @@ -240,6 +244,43 @@ class OfflineMapLayersImporterTest {
assertThat(booleanCaptor.secondValue, equalTo(false))
}

@Test
fun `the warning dialog is displayed if some selected files are not supported and the importer dialog is kept displayed`() {
val file1 = TempFiles.createTempFile("layerA", ".txt")
val file2 = TempFiles.createTempFile("layerB", MbtilesFile.FILE_EXTENSION)

launchFragment().onFragment {
it.viewModel.loadLayersToImport(listOf(file1.toUri(), file2.toUri()), it.requireContext())

scheduler.flush()

val context = ApplicationProvider.getApplicationContext<Application>()
onView(withText(context.getLocalizedQuantityString(R.plurals.non_mbtiles_files_selected_title, 1, 1))).inRoot(isDialog()).check(matches(isDisplayed()))
onView(withText(R.string.some_non_mbtiles_files_selected_message)).inRoot(isDialog()).check(matches(isDisplayed()))
onView(withText(R.string.ok)).inRoot(isDialog()).check(matches(isDisplayed()))

assertThat(it.isVisible, equalTo(true))
}
}

@Test
fun `the warning dialog is displayed if all selected files are not supported and the importer dialog is dismissed`() {
val file = TempFiles.createTempFile("layerA", ".txt")

launchFragment().onFragment {
it.viewModel.loadLayersToImport(listOf(file.toUri()), it.requireContext())

scheduler.flush()

val context = ApplicationProvider.getApplicationContext<Application>()
onView(withText(context.getLocalizedQuantityString(R.plurals.non_mbtiles_files_selected_title, 1, 1))).inRoot(isDialog()).check(matches(isDisplayed()))
onView(withText(R.string.all_non_mbtiles_files_selected_message)).inRoot(isDialog()).check(matches(isDisplayed()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be cleaner for these tests to use the helpers available in Assertions/Interactions, but I'm pretty sure that's something you were going to deal with in #6201 anyway.

onView(withText(R.string.ok)).inRoot(isDialog()).check(matches(isDisplayed()))

assertThat(it.isVisible, equalTo(false))
}
}

private fun launchFragment(): FragmentScenario<OfflineMapLayersImporter> {
return fragmentScenarioLauncherRule.launchInContainer(OfflineMapLayersImporter::class.java)
}
Expand Down
12 changes: 12 additions & 0 deletions strings/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,18 @@
<!-- Text for button that lets the user delete a offline layer -->
<string name="delete_layer">Delete layer</string>

<!-- Text for title displayed in a dialog to inform that some/all the selected files are not MBTiles and they can't be imported -->
<plurals name="non_mbtiles_files_selected_title">
<item quantity="one">%d layer can\'t be added</item>
<item quantity="other">%d layers can\'t be added</item>
</plurals>

<!-- Text for message displayed in a dialog to inform that all the selected files are not MBTiles and they can't be imported -->
<string name="all_non_mbtiles_files_selected_message">The files you selected are not MBTiles. You can only add MBTiles files.</string>

<!-- Text for message displayed in a dialog to inform that some of the selected files are not MBTiles and they can't be imported -->
<string name="some_non_mbtiles_files_selected_message">Some of the files you selected are not MBTiles. You can only add MBTiles files.</string>

<!-- Text for title above list of layers -->
<string name="layers_list_title">Layers</string>

Expand Down