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

Address review comments for PR 6118, 6169, 6175 #6201

Closed
wants to merge 3 commits into from
Closed
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 @@ -47,9 +47,7 @@ class OfflineMapLayersImporter(
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

viewModel.isLoading.observe(this) { isLoading ->
viewModel.trackableWorker.isWorking.observe(this) { isLoading ->
if (isLoading) {
binding.addLayerButton.isEnabled = false
binding.layers.visibility = View.GONE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ class OfflineMapLayersPicker(
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

sharedViewModel.isLoading.observe(this) { isLoading ->
sharedViewModel.trackableWorker.isWorking.observe(this) { isLoading ->
if (isLoading) {
binding.progressIndicator.visibility = View.VISIBLE
binding.layers.visibility = View.GONE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.net.Uri
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import org.odk.collect.androidshared.async.TrackableWorker
import org.odk.collect.androidshared.system.copyToFile
import org.odk.collect.androidshared.system.getFileExtension
import org.odk.collect.androidshared.system.getFileName
Expand All @@ -16,11 +17,10 @@ import java.io.File

class OfflineMapLayersViewModel(
private val referenceLayerRepository: ReferenceLayerRepository,
private val scheduler: Scheduler,
scheduler: Scheduler,
private val settingsProvider: SettingsProvider
) : ViewModel() {
private val _isLoading = MutableLiveData<Boolean>()
val isLoading: LiveData<Boolean> = _isLoading
val trackableWorker = TrackableWorker(scheduler)

private val _existingLayers = MutableLiveData<List<ReferenceLayer>>()
val existingLayers: LiveData<List<ReferenceLayer>> = _existingLayers
Expand All @@ -35,51 +35,39 @@ class OfflineMapLayersViewModel(
}

private fun loadExistingLayers() {
_isLoading.value = true
scheduler.immediate(
background = {
val layers = referenceLayerRepository.getAll()
_isLoading.postValue(false)
_existingLayers.postValue(layers)
},
foreground = { }
)
trackableWorker.immediate {
val layers = referenceLayerRepository.getAll()
_existingLayers.postValue(layers)
}
}

fun loadLayersToImport(uris: List<Uri>, context: Context) {
_isLoading.value = true
scheduler.immediate(
background = {
tempLayersDir = TempFiles.createTempDir().also {
it.deleteOnExit()
}
val layers = mutableListOf<ReferenceLayer>()
uris.forEach { uri ->
if (uri.getFileExtension(context) == MbtilesFile.FILE_EXTENSION) {
uri.getFileName(context)?.let { fileName ->
val layerFile = File(tempLayersDir, fileName).also { file ->
uri.copyToFile(context, file)
}
layers.add(ReferenceLayer(layerFile.absolutePath, layerFile, MbtilesFile.readName(layerFile) ?: layerFile.name))
trackableWorker.immediate {
tempLayersDir = TempFiles.createTempDir().also {
it.deleteOnExit()
}
val layers = mutableListOf<ReferenceLayer>()
uris.forEach { uri ->
if (uri.getFileExtension(context) == MbtilesFile.FILE_EXTENSION) {
uri.getFileName(context)?.let { fileName ->
val layerFile = File(tempLayersDir, fileName).also { file ->
uri.copyToFile(context, file)
}
layers.add(ReferenceLayer(layerFile.absolutePath, layerFile, MbtilesFile.readName(layerFile) ?: layerFile.name))
}
}
_isLoading.postValue(false)
_layersToImport.postValue(layers)
},
foreground = { }
)
}
_layersToImport.postValue(layers)
}
}

fun importNewLayers(shared: Boolean) {
_isLoading.value = true
scheduler.immediate(
trackableWorker.immediate(
background = {
tempLayersDir.listFiles()?.forEach {
referenceLayerRepository.addLayer(it, shared)
}
tempLayersDir.delete()
_isLoading.postValue(false)
},
foreground = {
loadExistingLayers()
Expand All @@ -92,11 +80,9 @@ class OfflineMapLayersViewModel(
}

fun deleteLayer(layerId: String) {
_isLoading.value = true
scheduler.immediate {
trackableWorker.immediate {
referenceLayerRepository.delete(layerId)
_existingLayers.postValue(_existingLayers.value?.filter { it.id != layerId })
_isLoading.postValue(false)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,7 @@ class OfflineMapLayersPickerTest {
private val settingsProvider = InMemSettingsProvider()
private val externalWebPageHelper = mock<ExternalWebPageHelper>()

private val uris = mutableListOf<Uri>()
private val testRegistry = object : ActivityResultRegistry() {
override fun <I, O> onLaunch(
requestCode: Int,
contract: ActivityResultContract<I, O>,
input: I,
options: ActivityOptionsCompat?
) {
assertThat(contract, instanceOf(ActivityResultContracts.GetMultipleContents()::class.java))
assertThat(input, equalTo("*/*"))
dispatchResult(requestCode, uris)
}
}
private val testRegistry = TestRegistry()

@get:Rule
val fragmentScenarioLauncherRule = FragmentScenarioLauncherRule(
Expand Down Expand Up @@ -311,7 +299,7 @@ class OfflineMapLayersPickerTest {
fun `clicking the 'add layer' and selecting layers displays the confirmation dialog`() {
val scenario = launchFragment()

uris.add(Uri.parse("blah"))
testRegistry.addUris(Uri.parse("blah"))
EspressoHelpers.clickOnText(string.add_layer)

scenario.onFragment {
Expand Down Expand Up @@ -345,8 +333,7 @@ class OfflineMapLayersPickerTest {

scheduler.flush()

uris.add(file1.toUri())
uris.add(file2.toUri())
testRegistry.addUris(file1.toUri(), file2.toUri())

EspressoHelpers.clickOnText(string.add_layer)
scheduler.flush()
Expand All @@ -370,8 +357,7 @@ class OfflineMapLayersPickerTest {

scheduler.flush()

uris.add(file1.toUri())
uris.add(file2.toUri())
testRegistry.addUris(file1.toUri(), file2.toUri())

EspressoHelpers.clickOnText(string.add_layer)
scheduler.flush()
Expand Down Expand Up @@ -579,4 +565,23 @@ class OfflineMapLayersPickerTest {
private fun launchFragment(): FragmentScenario<OfflineMapLayersPicker> {
return fragmentScenarioLauncherRule.launchInContainer(OfflineMapLayersPicker::class.java)
}

private class TestRegistry : ActivityResultRegistry() {
val uris = mutableListOf<Uri>()

override fun <I, O> onLaunch(
requestCode: Int,
contract: ActivityResultContract<I, O>,
input: I,
options: ActivityOptionsCompat?
) {
assertThat(contract, instanceOf(ActivityResultContracts.GetMultipleContents()::class.java))
assertThat(input, equalTo("*/*"))
dispatchResult(requestCode, uris)
}

fun addUris(vararg uris: Uri) {
this.uris.addAll(uris)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ public void handleOnBackPressed() {

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

if (getToolbar() != null) {
getToolbar().setNavigationOnClickListener(v -> {
onCloseClicked();
Expand Down