Skip to content

Commit

Permalink
Add clear action for entities
Browse files Browse the repository at this point in the history
  • Loading branch information
seadowg committed Mar 13, 2024
1 parent ef34701 commit ea69e99
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.odk.collect.android.feature.entitymanagement

import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import org.odk.collect.android.support.TestDependencies
import org.odk.collect.android.support.pages.FormEntryPage
import org.odk.collect.android.support.rules.CollectTestRule
import org.odk.collect.android.support.rules.TestRuleChain
import org.odk.collect.strings.R.string

class DeleteEntitiesTest {

private val rule = CollectTestRule(useDemoProject = false)
private val testDependencies = TestDependencies()

@get:Rule
val ruleChain: RuleChain = TestRuleChain.chain(testDependencies)
.around(rule)

@Test
fun canClearAllEntities() {
testDependencies.server.addForm("one-question-entity-registration.xml")

rule.withMatchExactlyProject(testDependencies.server.url)
.startBlankForm("One Question Entity Registration")
.fillOutAndFinalize(FormEntryPage.QuestionAndAnswer("Name", "Logan Roy"))
.openEntityBrowser()
.clickOptionsIcon(string.clear_entities)
.clickOnString(string.clear_entities)
.assertTextDoesNotExist("people")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class JsonFileEntitiesRepository(directory: File) : EntitiesRepository {
writeEntities(entities)
}

override fun clear() {
StrictMode.noteSlowCall("Writing to JSON file")
entitiesFile.delete()
}

private fun writeEntities(entities: MutableList<Entity>) {
StrictMode.noteSlowCall("Writing to JSON file")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,19 @@ abstract class EntitiesRepositoryTest {
assertThat(wines.size, equalTo(1))
assertThat(wines[0].label, equalTo("Léoville Barton 2008"))
}

@Test
fun `clear() deletes all entities`() {
val repository = buildSubject()

val wine = Entity("wines", "1", "Léoville Barton 2008")
val whisky = Entity("whiskys", "2", "Lagavulin 16")
repository.save(wine)
repository.save(whisky)

repository.clear()
assertThat(repository.getDatasets().size, equalTo(0))
assertThat(repository.getEntities("wines").size, equalTo(0))
assertThat(repository.getEntities("whiskys").size, equalTo(0))
}
}
3 changes: 2 additions & 1 deletion entities/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

<application>
<activity
android:name=".EntityBrowserActivity" />
android:name=".EntityBrowserActivity"
android:theme="@style/Theme.Material3.DayNight"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ package org.odk.collect.entities

import android.os.Bundle
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.core.view.MenuHost
import androidx.core.view.MenuProvider
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.ViewModelProvider
Expand All @@ -15,7 +20,7 @@ import androidx.recyclerview.widget.RecyclerView.ViewHolder
import org.odk.collect.entities.databinding.DatasetItemLayoutBinding
import org.odk.collect.entities.databinding.ListLayoutBinding

class DatasetsFragment(private val viewModelFactory: ViewModelProvider.Factory) : Fragment() {
class DatasetsFragment(private val viewModelFactory: ViewModelProvider.Factory, private val menuHost: MenuHost) : Fragment() {

private val entitiesViewModel by viewModels<EntitiesViewModel> { viewModelFactory }

Expand All @@ -34,6 +39,19 @@ class DatasetsFragment(private val viewModelFactory: ViewModelProvider.Factory)
entitiesViewModel.datasets.observe(viewLifecycleOwner) {
binding.list.adapter = DatasetsAdapter(it, findNavController())
}

menuHost.addMenuProvider(DatasetsMenuProvider(entitiesViewModel), viewLifecycleOwner)
}
}

private class DatasetsMenuProvider(private val entitiesViewModel: EntitiesViewModel) : MenuProvider {
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
menuInflater.inflate(R.menu.datasets, menu)
}

override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
entitiesViewModel.clearAll()
return true
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ interface EntitiesRepository {
fun save(entity: Entity)
fun getDatasets(): Set<String>
fun getEntities(dataset: String): List<Entity>
fun clear()
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,11 @@ class EntitiesViewModel(

return result
}

fun clearAll() {
scheduler.immediate(background = true) {
entitiesRepository.clear()
_datasets.postValue(entitiesRepository.getDatasets().toList())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class EntityBrowserActivity : LocalizedActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
supportFragmentManager.fragmentFactory = FragmentFactoryBuilder()
.forClass(DatasetsFragment::class) { DatasetsFragment(viewModelFactory) }
.forClass(DatasetsFragment::class) { DatasetsFragment(viewModelFactory, this) }
.forClass(EntitiesFragment::class) { EntitiesFragment(viewModelFactory) }
.build()

Expand All @@ -44,5 +44,6 @@ class EntityBrowserActivity : LocalizedActivity() {
val appBarConfiguration = AppBarConfiguration(navController.graph)
val toolbar = findViewById<Toolbar>(org.odk.collect.androidshared.R.id.toolbar)
toolbar.setupWithNavController(navController, appBarConfiguration)
setSupportActionBar(toolbar)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class InMemEntitiesRepository : EntitiesRepository {
return entities.filter { it.dataset == dataset }
}

override fun clear() {
entities.clear()
}

override fun save(entity: Entity) {
val existing = entities.find { it.id == entity.id && it.dataset == entity.dataset }

Expand Down
9 changes: 9 additions & 0 deletions entities/src/main/res/menu/datasets.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/clear_entities"
android:title="@string/clear_entities"
app:showAsAction="never" />
</menu>
3 changes: 3 additions & 0 deletions strings/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,9 @@
<!-- Text for button that leads to browser for the user to view and manage local entites -->
<string name="entity_browser_button">View local entities</string>

<!-- Text for button that deletes all entities for the project -->
<string name="clear_entities">Clear all</string>

<!--
##############################################
# Crash handling
Expand Down

0 comments on commit ea69e99

Please sign in to comment.