|
|
|
|
@ -12,7 +12,9 @@ import net.pokeranalytics.android.model.interfaces.Deletable |
|
|
|
|
import net.pokeranalytics.android.model.interfaces.DeleteValidityStatus |
|
|
|
|
import net.pokeranalytics.android.model.interfaces.Identifiable |
|
|
|
|
import net.pokeranalytics.android.ui.interfaces.FilterableType |
|
|
|
|
import net.pokeranalytics.android.ui.view.ImageDecorator |
|
|
|
|
import net.pokeranalytics.android.ui.view.RowRepresentable |
|
|
|
|
import net.pokeranalytics.android.ui.view.RowViewType |
|
|
|
|
import net.pokeranalytics.android.ui.view.rowrepresentable.FilterCategoryRow |
|
|
|
|
import java.util.* |
|
|
|
|
|
|
|
|
|
@ -21,14 +23,14 @@ import java.util.* |
|
|
|
|
* It contains a list of [FilterCondition] describing the complete query to launch |
|
|
|
|
* The [Filter] is working closely with a [Filterable] interface providing the entity we want the query being launched on |
|
|
|
|
*/ |
|
|
|
|
open class Filter : RealmObject(), RowRepresentable, Identifiable, Deletable { |
|
|
|
|
open class Filter : RealmObject(), RowRepresentable, Identifiable, Deletable, ImageDecorator { |
|
|
|
|
|
|
|
|
|
companion object { |
|
|
|
|
|
|
|
|
|
// Create a new instance |
|
|
|
|
fun newInstance(realm: Realm, filterableType:Int): Filter { |
|
|
|
|
fun newInstance(realm: Realm, filterableType: Int): Filter { |
|
|
|
|
val filter = Filter() |
|
|
|
|
filter.filterableTypeOrdinal = filterableType |
|
|
|
|
filter.filterableTypeOrdinal = filterableType |
|
|
|
|
return filter |
|
|
|
|
//return realm.copyToRealm(filter) |
|
|
|
|
} |
|
|
|
|
@ -38,26 +40,26 @@ open class Filter : RealmObject(), RowRepresentable, Identifiable, Deletable { |
|
|
|
|
return realm.where<Filter>().equalTo("id", filterId).findFirst() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
inline fun <reified T : Filterable> queryOn(realm: Realm, query: Query, sortField: String? = null): RealmResults<T> { |
|
|
|
|
val realmQuery = realm.where<T>() |
|
|
|
|
sortField?.let { |
|
|
|
|
return query.queryWith(realmQuery).sort(it).findAll() |
|
|
|
|
} ?: run { |
|
|
|
|
return query.queryWith(realmQuery).findAll() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
inline fun <reified T : Filterable> queryOn(realm: Realm, query: Query, sortField: String? = null): RealmResults<T> { |
|
|
|
|
val realmQuery = realm.where<T>() |
|
|
|
|
sortField?.let { |
|
|
|
|
return query.queryWith(realmQuery).sort(it).findAll() |
|
|
|
|
} ?: run { |
|
|
|
|
return query.queryWith(realmQuery).findAll() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun sortedByUsage(realm: Realm): RealmResults<Filter> { |
|
|
|
|
return realm.where(Filter::class.java).findAll().sort("usageCount") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@PrimaryKey |
|
|
|
|
override var id = UUID.randomUUID().toString() |
|
|
|
|
override var id = UUID.randomUUID().toString() |
|
|
|
|
|
|
|
|
|
// the queryWith name |
|
|
|
|
var name: String = "" |
|
|
|
|
var name: String = "" |
|
|
|
|
|
|
|
|
|
// the number of use of the queryWith, |
|
|
|
|
// for MutableRealmInteger, see https://realm.io/docs/java/latest/#counters |
|
|
|
|
@ -66,106 +68,115 @@ open class Filter : RealmObject(), RowRepresentable, Identifiable, Deletable { |
|
|
|
|
var filterConditions: RealmList<FilterCondition> = RealmList() |
|
|
|
|
private set |
|
|
|
|
|
|
|
|
|
private var filterableTypeOrdinal: Int? = null |
|
|
|
|
|
|
|
|
|
val filterableType: FilterableType |
|
|
|
|
get() { |
|
|
|
|
this.filterableTypeOrdinal?.let { |
|
|
|
|
return FilterableType.values()[it] |
|
|
|
|
} |
|
|
|
|
return FilterableType.ALL |
|
|
|
|
} |
|
|
|
|
private var filterableTypeOrdinal: Int? = null |
|
|
|
|
|
|
|
|
|
override val viewType: Int |
|
|
|
|
get() = RowViewType.TITLE_VALUE_ACTION.ordinal |
|
|
|
|
override val imageRes: Int? |
|
|
|
|
get() = R.drawable.ic_outline_settings |
|
|
|
|
override val imageTint: Int? |
|
|
|
|
get() = R.color.green |
|
|
|
|
override val imageClickable: Boolean? |
|
|
|
|
get() = true |
|
|
|
|
|
|
|
|
|
val filterableType: FilterableType |
|
|
|
|
get() { |
|
|
|
|
this.filterableTypeOrdinal?.let { |
|
|
|
|
return FilterableType.values()[it] |
|
|
|
|
} |
|
|
|
|
return FilterableType.ALL |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun createOrUpdateFilterConditions(filterConditionRows: ArrayList<QueryCondition>) { |
|
|
|
|
println("list of querys saving: ${filterConditionRows.map { it.id }}") |
|
|
|
|
println("list of querys previous: ${this.filterConditions.map { it.queryCondition.id }}") |
|
|
|
|
filterConditionRows |
|
|
|
|
println("list of querys saving: ${filterConditionRows.map { it.id }}") |
|
|
|
|
println("list of querys previous: ${this.filterConditions.map { it.queryCondition.id }}") |
|
|
|
|
filterConditionRows |
|
|
|
|
.map { |
|
|
|
|
it.groupId |
|
|
|
|
} |
|
|
|
|
.distinct() |
|
|
|
|
.forEach { groupId-> |
|
|
|
|
.forEach { groupId -> |
|
|
|
|
filterConditionRows |
|
|
|
|
.filter { |
|
|
|
|
it.groupId == groupId |
|
|
|
|
} |
|
|
|
|
.apply { |
|
|
|
|
|
|
|
|
|
println("list of querys: ${this.map { it.id }}") |
|
|
|
|
val casted = arrayListOf<QueryCondition>() |
|
|
|
|
casted.addAll(this) |
|
|
|
|
val newFilterCondition = FilterCondition(casted) |
|
|
|
|
val previousCondition = filterConditions.filter { |
|
|
|
|
it.filterName == newFilterCondition.filterName && it.operator == newFilterCondition.operator |
|
|
|
|
} |
|
|
|
|
filterConditions.removeAll(previousCondition) |
|
|
|
|
filterConditions.add(newFilterCondition) |
|
|
|
|
} |
|
|
|
|
println("list of querys: ${this.map { it.id }}") |
|
|
|
|
val casted = arrayListOf<QueryCondition>() |
|
|
|
|
casted.addAll(this) |
|
|
|
|
val newFilterCondition = FilterCondition(casted) |
|
|
|
|
val previousCondition = filterConditions.filter { |
|
|
|
|
it.filterName == newFilterCondition.filterName && it.operator == newFilterCondition.operator |
|
|
|
|
} |
|
|
|
|
filterConditions.removeAll(previousCondition) |
|
|
|
|
filterConditions.add(newFilterCondition) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun remove(filterCategoryRow: FilterCategoryRow) { |
|
|
|
|
val sections = filterCategoryRow.filterSectionRows.map { it.name } |
|
|
|
|
val savedSections = filterConditions.filter { sections.contains(it.sectionName) } |
|
|
|
|
this.filterConditions.removeAll(savedSections) |
|
|
|
|
} |
|
|
|
|
fun remove(filterCategoryRow: FilterCategoryRow) { |
|
|
|
|
val sections = filterCategoryRow.filterSectionRows.map { it.name } |
|
|
|
|
val savedSections = filterConditions.filter { sections.contains(it.sectionName) } |
|
|
|
|
this.filterConditions.removeAll(savedSections) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun countBy(filterCategoryRow: FilterCategoryRow): Int { |
|
|
|
|
val sections = filterCategoryRow.filterSectionRows.map { it.name } |
|
|
|
|
println("list of sections $sections") |
|
|
|
|
val savedSections = filterConditions.filter { sections.contains(it.sectionName) }.flatMap { it.queryCondition.id } |
|
|
|
|
println("list of savedSections $savedSections") |
|
|
|
|
return savedSections.size |
|
|
|
|
println("list of sections $sections") |
|
|
|
|
val savedSections = filterConditions.filter { sections.contains(it.sectionName) }.flatMap { it.queryCondition.id } |
|
|
|
|
println("list of savedSections $savedSections") |
|
|
|
|
return savedSections.size |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun contains(filterElementRow: QueryCondition): Boolean { |
|
|
|
|
println("list of saved queries ${filterConditions.map { it.queryCondition.id }}") |
|
|
|
|
println("list of contains ${filterElementRow.id}") |
|
|
|
|
val contained = filterConditions.flatMap{ it.queryCondition.id }.contains(filterElementRow.id.first()) |
|
|
|
|
println("list of : $contained") |
|
|
|
|
return contained |
|
|
|
|
println("list of saved queries ${filterConditions.map { it.queryCondition.id }}") |
|
|
|
|
println("list of contains ${filterElementRow.id}") |
|
|
|
|
val contained = filterConditions.flatMap { it.queryCondition.id }.contains(filterElementRow.id.first()) |
|
|
|
|
println("list of : $contained") |
|
|
|
|
return contained |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Get the saved value for the given [filterElementRow] |
|
|
|
|
*/ |
|
|
|
|
fun loadValueForElement(filterElementRow: QueryCondition) { |
|
|
|
|
fun loadValueForElement(filterElementRow: QueryCondition) { |
|
|
|
|
val filtered = filterConditions.filter { |
|
|
|
|
it.queryCondition.id == filterElementRow.id |
|
|
|
|
} |
|
|
|
|
if (filtered.isNotEmpty()) { |
|
|
|
|
return filterElementRow.updateValueBy(filtered.first()) |
|
|
|
|
return filterElementRow.updateValueBy(filtered.first()) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
inline fun <reified T : Filterable> results(firstField: String? = null, secondField: String? = null): RealmResults<T> { |
|
|
|
|
|
|
|
|
|
val realmQuery = realm.where<T>() |
|
|
|
|
val realmQuery = realm.where<T>() |
|
|
|
|
|
|
|
|
|
if (firstField != null && secondField != null) { |
|
|
|
|
return this.query.queryWith(realmQuery).distinct(firstField, secondField).findAll() |
|
|
|
|
} |
|
|
|
|
if (firstField != null && secondField != null) { |
|
|
|
|
return this.query.queryWith(realmQuery).distinct(firstField, secondField).findAll() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (firstField != null) { |
|
|
|
|
return this.query.queryWith(realmQuery).distinct(firstField).findAll() |
|
|
|
|
} |
|
|
|
|
if (firstField != null) { |
|
|
|
|
return this.query.queryWith(realmQuery).distinct(firstField).findAll() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return this.query.queryWith(realmQuery).findAll() |
|
|
|
|
} |
|
|
|
|
return this.query.queryWith(realmQuery).findAll() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
val query: Query |
|
|
|
|
get() { |
|
|
|
|
val query = Query() |
|
|
|
|
this.filterConditions.forEach { |
|
|
|
|
query.add(it.queryCondition) |
|
|
|
|
} |
|
|
|
|
return query |
|
|
|
|
} |
|
|
|
|
val query: Query |
|
|
|
|
get() { |
|
|
|
|
val query = Query() |
|
|
|
|
this.filterConditions.forEach { |
|
|
|
|
query.add(it.queryCondition) |
|
|
|
|
} |
|
|
|
|
return query |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun getDisplayName(context: Context): String { |
|
|
|
|
if (name.isNotEmpty()) return name |
|
|
|
|
return this.query.getName(context) |
|
|
|
|
} |
|
|
|
|
override fun getDisplayName(context: Context): String { |
|
|
|
|
if (name.isNotEmpty()) return name |
|
|
|
|
return this.query.getName(context) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun isValidForDelete(realm: Realm): Boolean { |
|
|
|
|
return true |
|
|
|
|
|