updated RowRepresentable

feature/top10
Razmig Sarkissian 7 years ago
parent 0aa5d50f02
commit 33dc9437bc
  1. 2
      app/src/main/java/net/pokeranalytics/android/model/LiveData.kt
  2. 15
      app/src/main/java/net/pokeranalytics/android/model/realm/Bankroll.kt
  3. 16
      app/src/main/java/net/pokeranalytics/android/model/realm/Game.kt
  4. 9
      app/src/main/java/net/pokeranalytics/android/model/realm/Location.kt
  5. 9
      app/src/main/java/net/pokeranalytics/android/model/realm/TournamentFeature.kt
  6. 9
      app/src/main/java/net/pokeranalytics/android/model/realm/TransactionType.kt
  7. 77
      app/src/main/java/net/pokeranalytics/android/ui/adapter/components/RowRepresentableAdapter.kt
  8. 32
      app/src/main/java/net/pokeranalytics/android/ui/fragment/DataListFragment.kt
  9. 9
      app/src/main/java/net/pokeranalytics/android/ui/fragment/EditableDataFragment.kt
  10. 8
      app/src/main/java/net/pokeranalytics/android/ui/view/RowRepresentable.kt
  11. 35
      app/src/main/java/net/pokeranalytics/android/ui/view/RowViewType.kt

@ -16,6 +16,8 @@ interface ObjectSavable {
fun isValidForSave(): Boolean {
return true
}
fun uniqueIdentifier(): String
}
/**

@ -8,15 +8,12 @@ import net.pokeranalytics.android.model.ObjectSavable
import net.pokeranalytics.android.ui.adapter.components.LiveDataDataSource
import net.pokeranalytics.android.ui.adapter.components.RowRepresentableDataSource
import net.pokeranalytics.android.ui.fragment.components.bottomsheet.BottomSheetData
import net.pokeranalytics.android.ui.view.BankrollRow
import net.pokeranalytics.android.ui.view.RowEditable
import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.SimpleRow
import net.pokeranalytics.android.ui.view.*
import java.util.*
import kotlin.collections.ArrayList
open class Bankroll(name: String = "") : RealmObject(), RowRepresentableDataSource, LiveDataDataSource,
RowEditable, ObjectSavable {
RowEditable, ObjectSavable, RowRepresentable {
companion object {
fun newInstance() : Bankroll {
@ -42,6 +39,14 @@ open class Bankroll(name: String = "") : RealmObject(), RowRepresentableDataSour
// @todo rate management
override fun getDisplayName(): String {
return this.name
}
override fun uniqueIdentifier(): String {
return this.id
}
override val title: String get() = this.name
override val primaryKey: String get() = this.id

@ -4,16 +4,15 @@ import android.text.InputType
import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
import net.pokeranalytics.android.model.ObjectSavable
import net.pokeranalytics.android.ui.adapter.components.DisplayableDelegate
import net.pokeranalytics.android.ui.adapter.components.LiveDataDataSource
import net.pokeranalytics.android.ui.adapter.components.RowRepresentableDataSource
import net.pokeranalytics.android.ui.fragment.components.bottomsheet.BottomSheetData
import net.pokeranalytics.android.ui.view.GameRow
import net.pokeranalytics.android.ui.view.RowEditable
import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.SimpleRow
import net.pokeranalytics.android.ui.view.*
import java.util.*
open class Game : RealmObject(), RowRepresentableDataSource, LiveDataDataSource, RowEditable, ObjectSavable {
open class Game : RealmObject(), RowRepresentableDataSource, LiveDataDataSource, RowEditable, ObjectSavable,
RowRepresentable {
@PrimaryKey
var id = UUID.randomUUID().toString()
@ -26,6 +25,13 @@ open class Game : RealmObject(), RowRepresentableDataSource, LiveDataDataSource,
override val title: String get() = this.name
override val primaryKey: String get() = this.id
override fun getDisplayName(): String {
return this.name
}
override fun uniqueIdentifier(): String {
return this.id
}
override fun adapterRows(): ArrayList<RowRepresentable> {
val rows = ArrayList<RowRepresentable>()

@ -13,7 +13,7 @@ import net.pokeranalytics.android.ui.view.SimpleRow
import java.util.*
open class Location : RealmObject(), RowRepresentableDataSource, LiveDataDataSource, RowEditable, ObjectSavable {
open class Location : RealmObject(), RowRepresentableDataSource, LiveDataDataSource, RowEditable, ObjectSavable, RowRepresentable {
@PrimaryKey
var id = UUID.randomUUID().toString()
@ -29,6 +29,13 @@ open class Location : RealmObject(), RowRepresentableDataSource, LiveDataDataSou
override val title: String get() = this.name
override val primaryKey: String get() = this.id
override fun getDisplayName(): String {
return this.name
}
override fun uniqueIdentifier(): String {
return this.id
}
override fun adapterRows(): ArrayList<RowRepresentable> {
val rows = ArrayList<RowRepresentable>()

@ -12,7 +12,7 @@ import net.pokeranalytics.android.ui.view.SimpleRow
import net.pokeranalytics.android.ui.view.TournamentFeatureRow
import java.util.*
open class TournamentFeature : RealmObject(), RowRepresentableDataSource, LiveDataDataSource, RowEditable, ObjectSavable {
open class TournamentFeature : RealmObject(), RowRepresentableDataSource, LiveDataDataSource, RowEditable, ObjectSavable, RowRepresentable {
@PrimaryKey
var id = UUID.randomUUID().toString()
@ -22,6 +22,13 @@ open class TournamentFeature : RealmObject(), RowRepresentableDataSource, LiveDa
override val title: String get() = this.name
override val primaryKey: String get() = this.id
override fun getDisplayName(): String {
return this.name
}
override fun uniqueIdentifier(): String {
return this.id
}
override fun adapterRows(): ArrayList<RowRepresentable> {
val rows = ArrayList<RowRepresentable>()

@ -13,7 +13,7 @@ import net.pokeranalytics.android.ui.view.TransactionTypeRow
import java.util.*
open class TransactionType : RealmObject(), RowRepresentableDataSource, LiveDataDataSource, RowEditable, ObjectSavable {
open class TransactionType : RealmObject(), RowRepresentableDataSource, LiveDataDataSource, RowEditable, ObjectSavable, RowRepresentable {
@PrimaryKey
var id = UUID.randomUUID().toString()
@ -32,6 +32,13 @@ open class TransactionType : RealmObject(), RowRepresentableDataSource, LiveData
override val title: String get() = this.name
override val primaryKey: String get() = this.id
override fun getDisplayName(): String {
return this.name
}
override fun uniqueIdentifier(): String {
return this.id
}
override fun adapterRows(): ArrayList<RowRepresentable> {
val rows = ArrayList<RowRepresentable>()

@ -1,23 +1,49 @@
package net.pokeranalytics.android.ui.adapter.components
import android.inputmethodservice.Keyboard
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import net.pokeranalytics.android.ui.view.BindableHolder
import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.RowRepresentableDiffCallback
import net.pokeranalytics.android.ui.view.RowViewType
import net.pokeranalytics.android.ui.view.*
interface RowRepresentableDataSource : DisplayableDataSource {
fun rowRepresentableForPosition(position:Int): RowRepresentable {
if (this.numberOfRows() > position) {
return this.adapterRows()[position]
} else {
throw IllegalStateException("Need to implement Data Source")
}
}
fun numberOfRows(): Int {
return this.adapterRows().size
}
fun viewTypeForPosition(position:Int): Int {
return this.rowRepresentableForPosition(position).viewType
}
fun indexForRow(row:RowRepresentable): Int {
return this.adapterRows().indexOf(row)
}
}
interface RowRepresentableDelegate : DisplayableDelegate {
fun onIndexSelected(position: Int) {}
}
/**
* An interface used to provide RowRepresentableAdapter content and value in the form of rows
*/
interface RowRepresentableDataSource {
interface DisplayableDataSource {
/**
* Returns a list of rows
*/
fun adapterRows(): ArrayList<RowRepresentable>
fun adapterRows(): ArrayList<RowRepresentable> {
return ArrayList()
}
/**
* Returns a boolean for a specific row
@ -48,39 +74,29 @@ interface RowRepresentableDataSource {
* - switch (bool)
* - static content
* */
}
/**
* A delegate used to propagate UI actions
*/
interface RowRepresentableDelegate {
interface DisplayableDelegate {
fun onRowSelected(row: RowRepresentable) {}
fun onActionSelected(row: RowRepresentable) {}
}
/**
* An adapter capable of displaying a list of RowRepresentables
* @param rowRepresentableDataSource the datasource providing rows
* @param rowRepresentableDelegate the delegate, notified of UI actions
* @param dataSource the datasource providing rows
* @param delegate the delegate, notified of UI actions
*/
class RowRepresentableAdapter(
var rowRepresentableDataSource: RowRepresentableDataSource,
var rowRepresentableDelegate: RowRepresentableDelegate? = null
var dataSource: RowRepresentableDataSource,
var delegate: RowRepresentableDelegate? = null
) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
/**
* The list of rows to display
*/
private var rows: ArrayList<RowRepresentable> = ArrayList()
init {
this.rows = rowRepresentableDataSource.adapterRows()
}
override fun getItemViewType(position: Int): Int {
return this.rows[position].viewType
return this.dataSource.viewTypeForPosition(position)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
@ -89,28 +105,29 @@ class RowRepresentableAdapter(
}
override fun getItemCount(): Int {
return this.rows.size
return this.dataSource.numberOfRows()
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val dynamicRow = this.rows[position]
val dynamicRow = this.dataSource.rowRepresentableForPosition(position)
val listener = View.OnClickListener {
rowRepresentableDelegate?.onRowSelected(dynamicRow)
delegate?.onRowSelected(dynamicRow)
delegate?.onIndexSelected(position)
}
val actionListener = View.OnClickListener {
rowRepresentableDelegate?.onActionSelected(dynamicRow)
delegate?.onActionSelected(dynamicRow)
}
(holder as BindableHolder).bind(dynamicRow, this.rowRepresentableDataSource, listener, actionListener)
(holder as BindableHolder).bind(dynamicRow, this.dataSource, listener, actionListener)
}
/**
* Refresh the row in the adapter
*/
fun refreshRow(row: RowRepresentable) {
val index = rows.indexOf(row)
val index = this.dataSource.indexForRow(row)
if (index >= 0) {
notifyItemChanged(index)
}
@ -120,9 +137,11 @@ class RowRepresentableAdapter(
* Update UI
*/
fun updateRows(newRows: ArrayList<RowRepresentable>) {
val diffResult = DiffUtil.calculateDiff(RowRepresentableDiffCallback(newRows, rows, rowRepresentableDataSource))
/*
val diffResult = DiffUtil.calculateDiff(RowRepresentableDiffCallback(newRows, rows, dataSource))
this.rows = newRows
diffResult.dispatchUpdatesTo(this)
*/
}
}

@ -5,17 +5,21 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import io.realm.ObjectChangeSet
import io.realm.Realm
import io.realm.RealmResults
import kotlinx.android.synthetic.main.fragment_data_list.*
import net.pokeranalytics.android.R
import net.pokeranalytics.android.model.ObjectSavable
import net.pokeranalytics.android.ui.activity.EditableDataActivity
import net.pokeranalytics.android.ui.adapter.components.*
import net.pokeranalytics.android.ui.fragment.components.PokerAnalyticsFragment
import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.RowViewType
import net.pokeranalytics.android.ui.view.SettingRow
import timber.log.Timber
class DataListFragment : PokerAnalyticsFragment(), LiveDataDelegate {
class DataListFragment : PokerAnalyticsFragment(), RowRepresentableDataSource, RowRepresentableDelegate{
private lateinit var dataType: SettingRow
@ -31,20 +35,28 @@ class DataListFragment : PokerAnalyticsFragment(), LiveDataDelegate {
initUI()
}
override fun data(position: Int): LiveDataDataSource {
return (items[position] as LiveDataDataSource)
override fun rowRepresentableForPosition(position: Int): RowRepresentable {
return this.items[position] as RowRepresentable
}
override fun onRowSelected(position: Int) {
override fun numberOfRows(): Int {
return this.items.size
}
override fun viewTypeForPosition(position: Int): Int {
return RowViewType.DATA.ordinal
}
override fun indexForRow(row: RowRepresentable): Int {
return this.items.indexOf(row)
}
override fun onIndexSelected(position: Int) {
this.dataType.relatedResultsRepresentable?.let {
EditableDataActivity.newInstance(requireContext(), it.ordinal, this.data(position).primaryKey)
EditableDataActivity.newInstance(requireContext(), it.ordinal, (this.items[position] as ObjectSavable).uniqueIdentifier())
}
}
override fun size(): Int {
return items.size
}
private fun initData() {
}
@ -54,7 +66,7 @@ class DataListFragment : PokerAnalyticsFragment(), LiveDataDelegate {
private fun initUI() {
val viewManager = LinearLayoutManager(requireContext())
val dataListAdapter = LiveDataAdapter(this)
val dataListAdapter = RowRepresentableAdapter(this, this)
recyclerView.apply {
setHasFixedSize(true)

@ -12,10 +12,7 @@ import net.pokeranalytics.android.R
import net.pokeranalytics.android.model.LiveData
import net.pokeranalytics.android.model.ObjectSavable
import net.pokeranalytics.android.ui.activity.components.PokerAnalyticsActivity
import net.pokeranalytics.android.ui.adapter.components.LiveDataDataSource
import net.pokeranalytics.android.ui.adapter.components.RowRepresentableAdapter
import net.pokeranalytics.android.ui.adapter.components.RowRepresentableDataSource
import net.pokeranalytics.android.ui.adapter.components.RowRepresentableDelegate
import net.pokeranalytics.android.ui.adapter.components.*
import net.pokeranalytics.android.ui.fragment.components.PokerAnalyticsFragment
import net.pokeranalytics.android.ui.fragment.components.bottomsheet.BottomSheetDelegate
import net.pokeranalytics.android.ui.fragment.components.bottomsheet.BottomSheetFragment
@ -66,16 +63,16 @@ class EditableDataFragment : PokerAnalyticsFragment(), RowRepresentableDelegate,
override fun clickOnClear(row: RowRepresentable) {
Toast.makeText(requireContext(), "Clear: $row", Toast.LENGTH_SHORT).show()
(this.item as RowEditable).updateValue(null, row)
this.getRealm().executeTransaction {
(this.item as RowEditable).updateValue(null, row)
it.copyToRealmOrUpdate(this.item)
}
rowRepresentableAdapter.refreshRow(row)
}
override fun setValue(value: Any?, row: RowRepresentable) {
(this.item as RowEditable).updateValue(value, row)
this.getRealm().executeTransaction {
(this.item as RowEditable).updateValue(value, row)
it.copyToRealmOrUpdate(this.item)
}
rowRepresentableAdapter.refreshRow(row)

@ -29,11 +29,17 @@ interface Localizable {
}
}
interface RowRepresentable : Displayable {
fun getDisplayName(): String {
return "UNKNOWN NAME"
}
}
/**
* An interface used so that enums values can be represented visually
* as rows in RecyclerViews
*/
interface RowRepresentable : Localizable {
interface Displayable: Localizable {
/**
* The type of view associated with the row
*/

@ -3,6 +3,8 @@ package net.pokeranalytics.android.ui.view
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.AppCompatTextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.row_header_title_value.view.*
import kotlinx.android.synthetic.main.row_title.view.*
@ -26,7 +28,9 @@ enum class RowViewType {
EDIT_TEXT,
TITLE,
TITLE_VALUE,
TITLE_VALUE_ACTION;
TITLE_VALUE_ACTION,
DATA,
BOTTOM_SHEET_DATA;
inner class FakeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView),
BindableHolder {
@ -34,6 +38,20 @@ enum class RowViewType {
}
}
inner class DataViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), BindableHolder {
override fun bind(row: RowRepresentable, rowRepresentableDataSource: RowRepresentableDataSource?, listener: View.OnClickListener, actionListener: View.OnClickListener?) {
itemView.findViewById<AppCompatTextView>(R.id.rowTitle_title).text = row.getDisplayName()
itemView.findViewById<ConstraintLayout>(R.id.rowTitle_container).setOnClickListener(listener)
}
}
inner class BottomSheetDataViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), BindableHolder {
override fun bind(row: RowRepresentable, rowRepresentableDataSource: RowRepresentableDataSource?, listener: View.OnClickListener, actionListener: View.OnClickListener?) {
itemView.findViewById<AppCompatTextView>(R.id.title).text = row.getDisplayName()
itemView.findViewById<ConstraintLayout>(R.id.container).setOnClickListener(listener)
}
}
inner class TitleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView),
BindableHolder {
override fun bind(row: RowRepresentable, rowRepresentableDataSource: RowRepresentableDataSource?, listener: View.OnClickListener, actionListener: View.OnClickListener?) {
@ -115,7 +133,20 @@ enum class RowViewType {
false
)
)
DATA -> DataViewHolder(
LayoutInflater.from(parent.context).inflate(
R.layout.row_title,
parent,
false
)
)
BOTTOM_SHEET_DATA -> BottomSheetDataViewHolder(
LayoutInflater.from(parent.context).inflate(
R.layout.row_bottom_sheet_title,
parent,
false
)
)
else -> FakeViewHolder(parent)
}
}

Loading…
Cancel
Save