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

Loading…
Cancel
Save