You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
poker-analytics/app/src/main/java/net/pokeranalytics/android/model/LiveData.kt

189 lines
5.1 KiB

package net.pokeranalytics.android.model
import android.content.Context
import androidx.fragment.app.Fragment
import io.realm.Realm
import io.realm.Sort
import net.pokeranalytics.android.R
import net.pokeranalytics.android.model.interfaces.Deletable
import net.pokeranalytics.android.model.realm.*
import net.pokeranalytics.android.model.realm.handhistory.HandHistory
import net.pokeranalytics.android.ui.modules.data.EditableDataActivity
import net.pokeranalytics.android.ui.modules.handhistory.HandHistoryActivity
import net.pokeranalytics.android.ui.view.Localizable
import net.pokeranalytics.android.util.extensions.findById
/**
* An enum managing the business objects related to a realm results
*/
enum class LiveData : Localizable {
BANKROLL,
GAME,
LOCATION,
TOURNAMENT_NAME,
TOURNAMENT_FEATURE,
TRANSACTION,
TRANSACTION_TYPE,
FILTER,
CUSTOM_FIELD,
REPORT_SETUP,
PLAYER,
HAND_HISTORY;
var subType: Int? = null
val relatedEntity: Class<out Deletable>
get() {
return when (this) {
BANKROLL -> Bankroll::class.java
GAME -> Game::class.java
LOCATION -> Location::class.java
TOURNAMENT_NAME -> TournamentName::class.java
TOURNAMENT_FEATURE -> TournamentFeature::class.java
TRANSACTION -> Transaction::class.java
TRANSACTION_TYPE -> TransactionType::class.java
FILTER -> Filter::class.java
CUSTOM_FIELD -> CustomField::class.java
REPORT_SETUP -> ReportSetup::class.java
PLAYER -> Player::class.java
HAND_HISTORY -> HandHistory::class.java
}
}
fun updateOrCreate(realm: Realm, primaryKey: String?): Deletable {
val proxyItem: Deletable? = this.getData(realm, primaryKey)
return proxyItem?.let {
realm.copyFromRealm(it)
} ?: run {
this.newEntity()
}
}
private fun newEntity(): Deletable {
return this.relatedEntity.newInstance()
}
fun getData(realm: Realm, primaryKey: String?): Deletable? {
var proxyItem: Deletable? = null
primaryKey?.let {
val t = realm.findById(this.relatedEntity, it)
t?.let {
proxyItem = t
}
}
return proxyItem
}
override val resId: Int?
get() {
return when (this) {
BANKROLL -> R.string.bankroll
GAME -> R.string.game
LOCATION -> R.string.location
TOURNAMENT_NAME -> R.string.tournament_name
TOURNAMENT_FEATURE -> R.string.tournament_feature
TRANSACTION -> R.string.operation
TRANSACTION_TYPE -> R.string.operation_type
FILTER -> R.string.filter
CUSTOM_FIELD -> R.string.custom_field
REPORT_SETUP -> R.string.custom
PLAYER -> R.string.player
HAND_HISTORY -> R.string.hand_history
}
}
val pluralResId: Int
get() {
return when (this) {
BANKROLL -> R.string.bankrolls
GAME -> R.string.games
LOCATION -> R.string.locations
TOURNAMENT_NAME -> R.string.tournament_names
TOURNAMENT_FEATURE -> R.string.tournament_features
TRANSACTION -> R.string.operations
TRANSACTION_TYPE -> R.string.operation_types
FILTER -> R.string.filters
CUSTOM_FIELD -> R.string.custom_fields
REPORT_SETUP -> R.string.custom
PLAYER -> R.string.players
HAND_HISTORY -> R.string.hands_history
}
}
private val newResId: Int
get() {
return when (this) {
BANKROLL -> R.string.new_bankroll
GAME -> R.string.new_variant
LOCATION -> R.string.new_location
TOURNAMENT_NAME -> R.string.new_tournament_name
TOURNAMENT_FEATURE -> R.string.new_tournament_feature
TRANSACTION -> R.string.new_operation
TRANSACTION_TYPE -> R.string.new_operation_type
FILTER -> R.string.new_filter
CUSTOM_FIELD -> R.string.new_custom_field
REPORT_SETUP -> R.string.new_report
PLAYER -> R.string.new_friend
HAND_HISTORY -> R.string.new_hand
}
}
val isSearchable: Boolean
get() {
return when (this) {
PLAYER, LOCATION -> true
else -> false
}
}
/**
* Return the new entity titleResId
*/
fun newEntityLocalizedTitle(context: Context): String {
return context.getString(this.newResId)
}
/**
* Return the update entity titleResId
*/
fun updateEntityLocalizedTitle(context: Context): String {
return context.getString(R.string.update_entity, this.localizedTitle(context).toLowerCase())
}
/**
* Return the update entity titleResId
*/
fun pluralLocalizedTitle(context: Context): String {
return context.getString(this.pluralResId, context)
}
fun openEditActivity(fragment: Fragment, primaryKey: String? = null, requestCode: Int) {
when (this) {
HAND_HISTORY -> {
HandHistoryActivity.newInstance(fragment, primaryKey)
}
else -> {
EditableDataActivity.newInstanceForResult(fragment, this, primaryKey, requestCode)
}
}
}
val sortFields: Array<String>
get() {
return when (this) {
TRANSACTION, HAND_HISTORY -> arrayOf("date")
FILTER -> arrayOf("useCount", "name")
else -> arrayOf("name")
}
}
val sortOrders: Array<Sort>
get() {
return when (this) {
TRANSACTION, HAND_HISTORY -> arrayOf(Sort.DESCENDING)
FILTER -> arrayOf(Sort.DESCENDING, Sort.ASCENDING)
else -> arrayOf(Sort.ASCENDING)
}
}
}