|
|
|
|
@ -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() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |