parent
daf567e716
commit
ea99385835
@ -0,0 +1,251 @@ |
|||||||
|
package net.pokeranalytics.android.ui.viewmodel |
||||||
|
|
||||||
|
import androidx.lifecycle.ViewModel |
||||||
|
import io.realm.RealmList |
||||||
|
import io.realm.RealmResults |
||||||
|
import net.pokeranalytics.android.exceptions.PAIllegalStateException |
||||||
|
import net.pokeranalytics.android.exceptions.RowRepresentableEditDescriptorException |
||||||
|
import net.pokeranalytics.android.ui.adapter.RowRepresentableAdapter |
||||||
|
import net.pokeranalytics.android.ui.adapter.RowRepresentableDelegate |
||||||
|
import net.pokeranalytics.android.ui.fragment.components.bottomsheet.BottomSheetType |
||||||
|
import net.pokeranalytics.android.ui.view.RowRepresentable |
||||||
|
import net.pokeranalytics.android.ui.view.RowRepresentableEditDescriptor |
||||||
|
import net.pokeranalytics.android.ui.view.rowrepresentable.SessionRow |
||||||
|
import java.util.* |
||||||
|
|
||||||
|
class BottomSheetViewModel : ViewModel() { |
||||||
|
|
||||||
|
lateinit var row: RowRepresentable |
||||||
|
lateinit var delegate: RowRepresentableDelegate |
||||||
|
|
||||||
|
var currentCurrency: Currency? = null |
||||||
|
var valueAsPlaceholder: Boolean = false |
||||||
|
var isClearable: Boolean = true |
||||||
|
var isDeletable: Boolean = false |
||||||
|
var rowRepresentableEditDescriptors: ArrayList<RowRepresentableEditDescriptor>? = null |
||||||
|
|
||||||
|
/** |
||||||
|
* Storage for a data that has been newly created |
||||||
|
*/ |
||||||
|
var addedData: Any? = null |
||||||
|
|
||||||
|
/** |
||||||
|
* String storage |
||||||
|
*/ |
||||||
|
var stringValue: String? = null |
||||||
|
var secondStringValue: String? = null |
||||||
|
|
||||||
|
/** |
||||||
|
Double Edit Text |
||||||
|
*/ |
||||||
|
val values = ArrayList<String>() |
||||||
|
var isEditingBlinds: Boolean = false |
||||||
|
|
||||||
|
/** |
||||||
|
* Lists, dynamic or static |
||||||
|
*/ |
||||||
|
lateinit var dataAdapter: RowRepresentableAdapter |
||||||
|
var realmData: RealmResults<RowRepresentable>? = null |
||||||
|
var staticRows: List<RowRepresentable> = emptyList() |
||||||
|
|
||||||
|
/** |
||||||
|
* Sum |
||||||
|
*/ |
||||||
|
var doubleValue: Double? = 0.0 |
||||||
|
var currentDefaultValue = 0.0 |
||||||
|
|
||||||
|
/** |
||||||
|
* Table Size |
||||||
|
*/ |
||||||
|
var defaultSize: Int? = null |
||||||
|
|
||||||
|
/** |
||||||
|
* Multiselection |
||||||
|
*/ |
||||||
|
val selectedRows: ArrayList<RowRepresentable> = ArrayList() |
||||||
|
|
||||||
|
/** |
||||||
|
* Limit and Game |
||||||
|
*/ |
||||||
|
var limit: Int? = 0 |
||||||
|
val someValues = ArrayList<Any?>() |
||||||
|
|
||||||
|
fun load() { |
||||||
|
|
||||||
|
when(this.row.bottomSheetType) { |
||||||
|
BottomSheetType.DOUBLE_EDIT_TEXT -> { |
||||||
|
val descriptors = this.rowRepresentableEditDescriptors ?: throw RowRepresentableEditDescriptorException("RowRepresentableEditDescriptor not found") |
||||||
|
if (descriptors.size != 2) { |
||||||
|
throw RowRepresentableEditDescriptorException("RowRepresentableEditDescriptor inconsistency") |
||||||
|
} |
||||||
|
|
||||||
|
this.isEditingBlinds = this.row == SessionRow.BLINDS |
||||||
|
|
||||||
|
this.stringValue = descriptors[0].defaultValue as? String |
||||||
|
this.secondStringValue = descriptors[1].defaultValue as? String |
||||||
|
} |
||||||
|
BottomSheetType.SUM -> { |
||||||
|
val descriptors = this.rowRepresentableEditDescriptors ?: throw RowRepresentableEditDescriptorException("RowRepresentableEditDescriptor not found") |
||||||
|
if (descriptors.size != 5) { |
||||||
|
throw RowRepresentableEditDescriptorException("RowRepresentableEditDescriptor inconsistency") |
||||||
|
} |
||||||
|
|
||||||
|
this.currentDefaultValue = try { |
||||||
|
descriptors[2].defaultValue as Double |
||||||
|
} catch (e: Exception) { |
||||||
|
0.0 |
||||||
|
} |
||||||
|
} |
||||||
|
BottomSheetType.LIST -> { |
||||||
|
|
||||||
|
val bottomSheetData = this.rowRepresentableEditDescriptors ?: throw RowRepresentableEditDescriptorException("RowRepresentableEditDescriptor not found") |
||||||
|
if (bottomSheetData.size != 1) { |
||||||
|
throw RowRepresentableEditDescriptorException("RowRepresentableEditDescriptor inconsistency") |
||||||
|
} |
||||||
|
if (bottomSheetData.first().data == null) { |
||||||
|
throw RowRepresentableEditDescriptorException("RowRepresentableEditDescriptor inconsistency") |
||||||
|
} |
||||||
|
this.realmData = bottomSheetData.first().data as RealmResults<RowRepresentable> |
||||||
|
|
||||||
|
} |
||||||
|
BottomSheetType.DOUBLE_LIST, BottomSheetType.LIST_GAME -> { |
||||||
|
val bottomSheetData = this.rowRepresentableEditDescriptors ?: throw RowRepresentableEditDescriptorException("RowRepresentableEditDescriptor not found") |
||||||
|
if (bottomSheetData.size != 2) { |
||||||
|
throw RowRepresentableEditDescriptorException("RowRepresentableEditDescriptor inconsistency") |
||||||
|
} |
||||||
|
if (bottomSheetData[1].data == null) { |
||||||
|
throw RowRepresentableEditDescriptorException("RowRepresentableEditDescriptor inconsistency") |
||||||
|
} |
||||||
|
this.limit = bottomSheetData[0].defaultValue as Int? |
||||||
|
this.realmData = bottomSheetData[1].data |
||||||
|
} |
||||||
|
BottomSheetType.MULTI_SELECTION -> { |
||||||
|
|
||||||
|
val bottomSheetData = |
||||||
|
this.rowRepresentableEditDescriptors ?: throw RowRepresentableEditDescriptorException("RowRepresentableEditDescriptor not found") |
||||||
|
if (bottomSheetData.size != 1) { |
||||||
|
throw RowRepresentableEditDescriptorException("RowRepresentableEditDescriptor inconsistency") |
||||||
|
} |
||||||
|
|
||||||
|
this.realmData = bottomSheetData.first().data as RealmResults<RowRepresentable> |
||||||
|
bottomSheetData.first().defaultValue?.let { |
||||||
|
(it as RealmList<*>).forEach { row -> |
||||||
|
this.selectedRows.add(row as RowRepresentable) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
BottomSheetType.NUMERIC_TEXT -> { |
||||||
|
val bottomSheetData = |
||||||
|
this.rowRepresentableEditDescriptors |
||||||
|
?: throw RowRepresentableEditDescriptorException("RowRepresentableEditDescriptor not found") |
||||||
|
bottomSheetData[0].defaultValue?.let { |
||||||
|
this.doubleValue = it.toString().toDoubleOrNull() |
||||||
|
} |
||||||
|
} |
||||||
|
BottomSheetType.LIST_STATIC -> { |
||||||
|
val bottomSheetData = this.rowRepresentableEditDescriptors ?:throw RowRepresentableEditDescriptorException("RowRepresentableEditDescriptor not found") |
||||||
|
if (bottomSheetData.size != 1) { |
||||||
|
throw RowRepresentableEditDescriptorException("RowRepresentableEditDescriptor inconsistency") |
||||||
|
} |
||||||
|
if (bottomSheetData.first().staticData == null) { |
||||||
|
throw RowRepresentableEditDescriptorException("RowRepresentableEditDescriptor inconsistency") |
||||||
|
} |
||||||
|
this.staticRows = bottomSheetData.first().staticData as List<RowRepresentable> |
||||||
|
|
||||||
|
} |
||||||
|
BottomSheetType.GRID -> { |
||||||
|
val bottomSheetData = this.rowRepresentableEditDescriptors ?:throw RowRepresentableEditDescriptorException("RowRepresentableEditDescriptor not found") |
||||||
|
if (bottomSheetData.size != 1) { |
||||||
|
throw RowRepresentableEditDescriptorException("RowRepresentableEditDescriptor inconsistency") |
||||||
|
} |
||||||
|
this.defaultSize = bottomSheetData.first().defaultValue as Int? |
||||||
|
|
||||||
|
} |
||||||
|
else -> {} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
fun getValue() : Any? { |
||||||
|
return when(this.row.bottomSheetType) { |
||||||
|
BottomSheetType.DOUBLE_EDIT_TEXT -> { |
||||||
|
if (this.values.isEmpty()) { return null } |
||||||
|
if (this.values.all { it.isEmpty() }) { return null } |
||||||
|
return this.values |
||||||
|
} |
||||||
|
BottomSheetType.EDIT_TEXT, BottomSheetType.EDIT_TEXT_MULTI_LINES -> { |
||||||
|
this.stringValue?.trim()?.let { |
||||||
|
if (it.isNotEmpty()) { |
||||||
|
return it |
||||||
|
} |
||||||
|
} |
||||||
|
return null |
||||||
|
} |
||||||
|
BottomSheetType.MULTI_SELECTION -> this.selectedRows |
||||||
|
BottomSheetType.NUMERIC_TEXT -> this.doubleValue |
||||||
|
BottomSheetType.GRID -> this.defaultSize |
||||||
|
BottomSheetType.DOUBLE_LIST, BottomSheetType.LIST_GAME -> this.someValues |
||||||
|
BottomSheetType.LIST_STATIC -> this.selectedRows.firstOrNull() |
||||||
|
BottomSheetType.SUM -> this.doubleValue |
||||||
|
else -> null |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
fun onClear() { |
||||||
|
this.delegate.onRowValueChanged(null, this.row) |
||||||
|
} |
||||||
|
|
||||||
|
fun onRowValueChanged() { |
||||||
|
|
||||||
|
// if some data has been added |
||||||
|
this.addedData?.let { |
||||||
|
this.delegate.onRowValueChanged(it, this.row) |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
// otherwise, default behavior |
||||||
|
val value = when(this.row.bottomSheetType) { |
||||||
|
BottomSheetType.DOUBLE_EDIT_TEXT -> arrayListOf(this.stringValue, this.secondStringValue) |
||||||
|
BottomSheetType.DOUBLE_LIST, BottomSheetType.LIST_GAME -> arrayListOf(this.someValues[0], this.someValues[1]) |
||||||
|
else -> getValue() |
||||||
|
} |
||||||
|
|
||||||
|
this.delegate.onRowValueChanged(value, this.row) |
||||||
|
} |
||||||
|
|
||||||
|
fun onRowDeleted() { |
||||||
|
this.delegate.onRowDeleted(this.row) |
||||||
|
} |
||||||
|
|
||||||
|
fun onRowSelected(row: RowRepresentable) { |
||||||
|
if (this.selectedRows.contains(row)) { |
||||||
|
this.selectedRows.remove(row) |
||||||
|
} else { |
||||||
|
this.selectedRows.add(row) |
||||||
|
} |
||||||
|
this.refreshRow(row) |
||||||
|
} |
||||||
|
|
||||||
|
fun onRowSelected(position: Int) { |
||||||
|
val value = when(this.row.bottomSheetType) { |
||||||
|
BottomSheetType.LIST -> this.realmData?.get(position) |
||||||
|
BottomSheetType.LIST_STATIC -> this.staticRows[position] |
||||||
|
else -> throw PAIllegalStateException("row selected for unmanaged bottom sheet type") |
||||||
|
} |
||||||
|
this.delegate.onRowValueChanged(value, this.row) |
||||||
|
} |
||||||
|
|
||||||
|
fun isSelected(row: RowRepresentable): Boolean { |
||||||
|
return this.selectedRows.contains(row) |
||||||
|
} |
||||||
|
|
||||||
|
fun notifyDataSetChanged() { |
||||||
|
this.dataAdapter.notifyDataSetChanged() |
||||||
|
} |
||||||
|
|
||||||
|
fun refreshRow(row: RowRepresentable) { |
||||||
|
this.dataAdapter.refreshRow(row) |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue