Update models and migration

dev
Aurelien Hubert 7 years ago
parent 3b077e693f
commit 7d17287e65
  1. 1
      app/src/main/java/net/pokeranalytics/android/model/migrations/PokerAnalyticsMigration.kt
  2. 60
      app/src/main/java/net/pokeranalytics/android/model/realm/CustomField.kt
  3. 5
      app/src/main/java/net/pokeranalytics/android/model/realm/CustomFieldEntry.kt

@ -110,6 +110,7 @@ class PokerAnalyticsMigration : RealmMigration {
schema.get("CustomField")?.let { schema.get("CustomField")?.let {
it.addField("type", Integer::class.java).setNullable("type", false) it.addField("type", Integer::class.java).setNullable("type", false)
it.addField("duplicateValue", Boolean::class.java) it.addField("duplicateValue", Boolean::class.java)
it.addField("sortCondition", Integer::class.java)
it.addRealmListField("entries", CustomFieldEntry::class.java) it.addRealmListField("entries", CustomFieldEntry::class.java)
} }

@ -17,6 +17,7 @@ import net.pokeranalytics.android.ui.view.RowViewType
import net.pokeranalytics.android.ui.view.rowrepresentable.CustomFieldRow import net.pokeranalytics.android.ui.view.rowrepresentable.CustomFieldRow
import net.pokeranalytics.android.ui.view.rowrepresentable.CustomizableRowRepresentable import net.pokeranalytics.android.ui.view.rowrepresentable.CustomizableRowRepresentable
import net.pokeranalytics.android.ui.view.rowrepresentable.SimpleRow import net.pokeranalytics.android.ui.view.rowrepresentable.SimpleRow
import net.pokeranalytics.android.util.enumerations.IntIdentifiable
import timber.log.Timber import timber.log.Timber
import java.util.* import java.util.*
import kotlin.collections.ArrayList import kotlin.collections.ArrayList
@ -24,10 +25,16 @@ import kotlin.collections.ArrayList
open class CustomField : RealmObject(), NameManageable, StaticRowRepresentableDataSource, RowRepresentable { open class CustomField : RealmObject(), NameManageable, StaticRowRepresentableDataSource, RowRepresentable {
enum class Type(var resId: Int) { enum class Type(override var uniqueIdentifier: Int, var resId: Int) : IntIdentifiable {
LIST(R.string.enum_custom_field_type), LIST(0, R.string.enum_custom_field_type),
NUMBER(R.string.number), NUMBER(1, R.string.number),
AMOUNT(R.string.amount) AMOUNT(2, R.string.amount)
}
enum class Sort(override var uniqueIdentifier: Int) : IntIdentifiable {
DEFAULT(0),
ASCENDING(1),
DESCENDING(2)
} }
@PrimaryKey @PrimaryKey
@ -37,7 +44,7 @@ open class CustomField : RealmObject(), NameManageable, StaticRowRepresentableDa
override var name: String = "" override var name: String = ""
// Migration // Migration
var type: Int = Type.LIST.ordinal var type: Int = Type.LIST.uniqueIdentifier
set(value) { set(value) {
field = value field = value
this.updateRowRepresentation() this.updateRowRepresentation()
@ -45,10 +52,14 @@ open class CustomField : RealmObject(), NameManageable, StaticRowRepresentableDa
var duplicateValue: Boolean = false var duplicateValue: Boolean = false
var entries: RealmList<CustomFieldEntry> = RealmList() var entries: RealmList<CustomFieldEntry> = RealmList()
var sortCondition: Int = Sort.DEFAULT.uniqueIdentifier
set(value) {
field = value
sortEntries()
updateRowRepresentation()
}
// @todo
override fun getDisplayName(context: Context): String { override fun getDisplayName(context: Context): String {
return this.name return this.name
} }
@ -106,6 +117,9 @@ open class CustomField : RealmObject(), NameManageable, StaticRowRepresentableDa
} }
} }
/**
* Update the row representation
*/
private fun updatedRowRepresentationForCurrentState(): List<RowRepresentable> { private fun updatedRowRepresentationForCurrentState(): List<RowRepresentable> {
val rows = ArrayList<RowRepresentable>() val rows = ArrayList<RowRepresentable>()
rows.add(SimpleRow.NAME) rows.add(SimpleRow.NAME)
@ -114,8 +128,10 @@ open class CustomField : RealmObject(), NameManageable, StaticRowRepresentableDa
if (type == Type.LIST.ordinal && entries.size >= 0) { if (type == Type.LIST.ordinal && entries.size >= 0) {
rows.add(CustomizableRowRepresentable(RowViewType.HEADER_TITLE, R.string.items_list)) rows.add(CustomizableRowRepresentable(RowViewType.HEADER_TITLE, R.string.items_list))
if (entries.isNotEmpty()) { if (entries.isNotEmpty()) {
Timber.d("entries: ${entries.size}") sortEntries()
entries.sortBy { it.order } entries.forEach { customFieldEntry ->
customFieldEntry.isMovable = sortCondition == Sort.DEFAULT.uniqueIdentifier
}
rows.addAll(entries) rows.addAll(entries)
} }
} }
@ -124,25 +140,41 @@ open class CustomField : RealmObject(), NameManageable, StaticRowRepresentableDa
return rows return rows
} }
/**
* Sort the entries element
*/
private fun sortEntries() {
when (sortCondition) {
Sort.ASCENDING.uniqueIdentifier -> entries.sortBy { it.value }
Sort.DESCENDING.uniqueIdentifier -> entries.sortByDescending { it.value }
}
entries.forEachIndexed { index, customFieldEntry ->
customFieldEntry.order = index
}
}
fun updateRowRepresentation() { fun updateRowRepresentation() {
this.rowRepresentation = this.updatedRowRepresentationForCurrentState() this.rowRepresentation = this.updatedRowRepresentationForCurrentState()
} }
/** /**
* Add entry * Add an entry
*/ */
fun addEntry() { fun addEntry() {
val entry = CustomFieldEntry() val entry = CustomFieldEntry()
entry.order = (entries.lastOrNull()?.order ?: 0) + 1
entries.add(entry) entries.add(entry)
sortEntries()
updateRowRepresentation() updateRowRepresentation()
} }
/** /**
* * Delete an entry
*/ */
fun deleteEntry() { fun deleteEntry(entry: CustomFieldEntry) {
entries.remove(entry)
sortEntries()
updateRowRepresentation()
} }
} }

@ -21,12 +21,15 @@ open class CustomFieldEntry : RealmObject(), RowRepresentable {
var value: String = "" var value: String = ""
var order: Int = 0 var order: Int = 0
@Ignore
var isMovable: Boolean = false
@Ignore @Ignore
override val viewType: Int = RowViewType.TITLE_VALUE_ACTION.ordinal override val viewType: Int = RowViewType.TITLE_VALUE_ACTION.ordinal
override val imageRes: Int? override val imageRes: Int?
get() { get() {
return R.drawable.ic_reorder return if (isMovable) R.drawable.ic_reorder else null
} }
override val imageTint: Int? override val imageTint: Int?

Loading…
Cancel
Save