Fixes filters

perftest
Laurent 3 years ago
parent 6a1249a78c
commit bd2c17909b
  1. 1
      app/src/main/java/net/pokeranalytics/android/model/Criteria.kt
  2. 2
      app/src/main/java/net/pokeranalytics/android/model/realm/Filter.kt
  3. 2
      app/src/main/java/net/pokeranalytics/android/ui/modules/data/CustomFieldDataFragment.kt
  4. 20
      app/src/main/java/net/pokeranalytics/android/ui/modules/filter/FilterDetailsFragment.kt
  5. 2
      app/src/main/java/net/pokeranalytics/android/ui/modules/filter/FilterDetailsViewModel.kt
  6. 27
      app/src/main/java/net/pokeranalytics/android/ui/modules/filter/FilterViewModel.kt
  7. 23
      app/src/main/java/net/pokeranalytics/android/ui/modules/filter/FiltersFragment.kt

@ -68,7 +68,6 @@ sealed class Criteria(override var uniqueIdentifier: Int) : IntIdentifiable, Row
realm.findById(CustomField::class.java, this.customFieldId)?.entries?.forEach { realm.findById(CustomField::class.java, this.customFieldId)?.entries?.forEach {
objects.add(QueryCondition.CustomFieldListQuery(it)) objects.add(QueryCondition.CustomFieldListQuery(it))
} }
objects.sort()
realm.close() realm.close()
return objects.map { Query(it) } return objects.map { Query(it) }
} }

@ -77,7 +77,7 @@ open class Filter : RealmObject(), RowRepresentable, RowUpdatable, Deletable, Us
var filterConditions: RealmList<FilterCondition> = RealmList() var filterConditions: RealmList<FilterCondition> = RealmList()
private set private set
private var filterableTypeUniqueIdentifier: Int? = null var filterableTypeUniqueIdentifier: Int? = null
val filterableType: FilterableType val filterableType: FilterableType
get() { get() {

@ -133,7 +133,7 @@ class CustomFieldDataFragment : EditableDataFragment(), StaticRowRepresentableDa
tag: Int tag: Int
): CharSequence { ): CharSequence {
return when (row) { return when (row) {
SimpleRow.NAME -> if (customField.name.isNotEmpty()) customField.name else NULL_TEXT SimpleRow.NAME -> customField.name.ifEmpty { NULL_TEXT }
else -> super.charSequenceForRow(row, context, 0) else -> super.charSequenceForRow(row, context, 0)
} }
} }

@ -10,7 +10,6 @@ import net.pokeranalytics.android.R
import net.pokeranalytics.android.databinding.FragmentFilterDetailsBinding import net.pokeranalytics.android.databinding.FragmentFilterDetailsBinding
import net.pokeranalytics.android.exceptions.PAIllegalStateException import net.pokeranalytics.android.exceptions.PAIllegalStateException
import net.pokeranalytics.android.model.filter.QueryCondition import net.pokeranalytics.android.model.filter.QueryCondition
import net.pokeranalytics.android.model.realm.Filter
import net.pokeranalytics.android.ui.adapter.RowRepresentableAdapter import net.pokeranalytics.android.ui.adapter.RowRepresentableAdapter
import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate
import net.pokeranalytics.android.ui.fragment.components.RealmFragment import net.pokeranalytics.android.ui.fragment.components.RealmFragment
@ -18,7 +17,6 @@ import net.pokeranalytics.android.ui.helpers.DateTimePickerManager
import net.pokeranalytics.android.ui.view.RowRepresentable import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.RowViewType import net.pokeranalytics.android.ui.view.RowViewType
import net.pokeranalytics.android.ui.view.rows.* import net.pokeranalytics.android.ui.view.rows.*
import net.pokeranalytics.android.util.extensions.findById
import timber.log.Timber import timber.log.Timber
import java.util.* import java.util.*
@ -92,7 +90,7 @@ open class FilterDetailsFragment : RealmFragment(), RowRepresentableDelegate {
this.arguments?.let { bundle -> this.arguments?.let { bundle ->
val filter = this.activityModel.currentFilter ?: throw PAIllegalStateException("Filter is null") val filter = this.activityModel.currentFilter
val category = bundle.getInt(BundleKey.DATA_TYPE.value) val category = bundle.getInt(BundleKey.DATA_TYPE.value)
val categoryRow = FilterCategoryRow.values()[category] val categoryRow = FilterCategoryRow.values()[category]
Timber.d("Category row = $categoryRow") Timber.d("Category row = $categoryRow")
@ -102,7 +100,7 @@ open class FilterDetailsFragment : RealmFragment(), RowRepresentableDelegate {
} ?: throw PAIllegalStateException("Missing bundle") } ?: throw PAIllegalStateException("Missing bundle")
Timber.d(">> Filter = ${this.activityModel.currentFilter}") // Timber.d(">> Filter = ${this.activityModel.currentFilter}")
Timber.d("selectedRow = ${this.activityModel.selectedCategoryRow}") Timber.d("selectedRow = ${this.activityModel.selectedCategoryRow}")
this.binding.toolbar.title = this.activityModel.selectedCategoryRow?.localizedTitle(requireContext()) this.binding.toolbar.title = this.activityModel.selectedCategoryRow?.localizedTitle(requireContext())
@ -232,22 +230,22 @@ open class FilterDetailsFragment : RealmFragment(), RowRepresentableDelegate {
*/ */
private fun saveData() { private fun saveData() {
// val currentFilter = this.activityModel.currentFilter val filter = this.activityModel.currentFilter
this.activityModel.currentFilter?.id?.let { filterId -> // this.activityModel.currentFilter?.id?.let { filterId ->
this.activityModel.selectedCategoryRow?.let { category -> this.activityModel.selectedCategoryRow?.let { category ->
getRealm().executeTransactionAsync { asyncRealm -> // getRealm().executeTransactionAsync { asyncRealm ->
asyncRealm.findById<Filter>(filterId)?.let { filter -> // asyncRealm.findById<Filter>(filterId)?.let { filter ->
filter.remove(category) filter.remove(category)
val validConditions = this.model.selectedRows.filter { it.queryCondition != null } val validConditions = this.model.selectedRows.filter { it.queryCondition != null }
filter.createOrUpdateFilterConditions(validConditions) filter.createOrUpdateFilterConditions(validConditions)
} }
} // }
} // }
} // }
// //TODO: Save currentFilter details data // //TODO: Save currentFilter details data
// Timber.d("Save data for queryWith: ${currentFilter?.id}") // Timber.d("Save data for queryWith: ${currentFilter?.id}")

@ -31,7 +31,7 @@ class FilterDetailsViewModel(categoryRow: FilterCategoryRow, var filter: Filter)
this.defineSelectedItems() this.defineSelectedItems()
} }
override fun adapterRows(): List<RowRepresentable>? { override fun adapterRows(): List<RowRepresentable> {
return this.rows return this.rows
} }

@ -12,7 +12,7 @@ import net.pokeranalytics.android.util.extensions.findById
class FilterViewModel : ViewModel(), StaticRowRepresentableDataSource { class FilterViewModel : ViewModel(), StaticRowRepresentableDataSource {
var currentFilter: Filter? = null var currentFilter: Filter = Filter()
// Main // Main
var filterableType: FilterableType? = null var filterableType: FilterableType? = null
@ -27,26 +27,27 @@ class FilterViewModel : ViewModel(), StaticRowRepresentableDataSource {
fun init(realm: Realm) { fun init(realm: Realm) {
if (this.currentFilter != null) { // can be called twice and we don't want that // if (this.currentFilter != null) { // can be called twice and we don't want that
return // return
} // }
this.primaryKey?.let { this.primaryKey?.let {
val filter = realm.findById<Filter>(it) ?: throw PAIllegalStateException("Can't find filter with id=$it") val filter = realm.findById<Filter>(it) ?: throw PAIllegalStateException("Can't find filter with id=$it")
this.currentFilter = realm.copyFromRealm(filter) this.currentFilter = realm.copyFromRealm(filter)
this.isUpdating = true this.isUpdating = true
} ?: run { } ?: run {
this.filterableType?.uniqueIdentifier?.let { this.currentFilter.filterableTypeUniqueIdentifier = this.filterableType?.uniqueIdentifier
this.currentFilter = Filter.newInstance(it) //realm.copyFromRealm(Filter.newInstanceForResult(realm, this.filterableType.ordinal)) // this.filterableType?.uniqueIdentifier?.let {
} // this.currentFilter = Filter.newInstance(it) //realm.copyFromRealm(Filter.newInstanceForResult(realm, this.filterableType.ordinal))
// }
} }
// Create a copy if the user cancels the updates // Create a copy if the user cancels the updates
this.currentFilter?.let { // this.currentFilter?.let {
if (it.isValid && it.isManaged) { // if (it.isValid && it.isManaged) {
this.filterCopy = realm.copyFromRealm(it) // this.filterCopy = realm.copyFromRealm(it)
} // }
} // }
this.categoryRows.clear() this.categoryRows.clear()
@ -64,7 +65,7 @@ class FilterViewModel : ViewModel(), StaticRowRepresentableDataSource {
override fun charSequenceForRow(row: RowRepresentable, context: Context, tag: Int): CharSequence { override fun charSequenceForRow(row: RowRepresentable, context: Context, tag: Int): CharSequence {
// Return the number of selected filters for this category // Return the number of selected filters for this category
val count = this.currentFilter?.countBy(row as FilterCategoryRow) ?: 0 val count = this.currentFilter.countBy(row as FilterCategoryRow)
return if (count > 0) count.toString() else "" return if (count > 0) count.toString() else ""
} }

@ -104,7 +104,7 @@ open class FiltersFragment : RealmFragment(), RowRepresentableDelegate {
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) { when (item.itemId) {
R.id.save -> validateUpdates() R.id.save -> save()
} }
return true return true
} }
@ -155,9 +155,7 @@ open class FiltersFragment : RealmFragment(), RowRepresentableDelegate {
val categoryRow = row as FilterCategoryRow val categoryRow = row as FilterCategoryRow
this.model.selectedCategoryRow = categoryRow this.model.selectedCategoryRow = categoryRow
this.model.currentFilter?.let { _ -> (activity as FiltersActivity).showDetailsFragment(categoryRow)
(activity as FiltersActivity).showDetailsFragment(categoryRow)
}
} }
/** /**
@ -216,18 +214,15 @@ open class FiltersFragment : RealmFragment(), RowRepresentableDelegate {
/** /**
* Validate the updates of the queryWith * Validate the updates of the queryWith
*/ */
private fun validateUpdates() { private fun save() {
val currentFilter = this.model.currentFilter val filter = this.model.currentFilter
getRealm().executeTransactionAsync { realm -> getRealm().executeTransactionAsync { asyncRealm ->
currentFilter?.let { filter -> filter.name = filter.query.getName(requireContext())
filter.name = filter.query.getName(requireContext()) asyncRealm.copyToRealmOrUpdate(filter)
realm.copyToRealmOrUpdate(filter) }
}
}
val filterId = currentFilter?.id ?: "" finishActivityWithResult(filter.id)
finishActivityWithResult(filterId)
} }
/** /**

Loading…
Cancel
Save