Improve Dynamic row management

dev_raz_wip
Aurelien Hubert 7 years ago
parent f3f28b5886
commit e10e10797a
  1. 5
      app/src/main/java/net/pokeranalytics/android/PokerAnalyticsApplication.kt
  2. 207
      app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt
  3. 1
      app/src/main/java/net/pokeranalytics/android/ui/adapter/HistoryAdapter.kt
  4. 2
      app/src/main/java/net/pokeranalytics/android/ui/adapter/components/DynamicListAdapter.kt
  5. 8
      app/src/main/java/net/pokeranalytics/android/ui/adapter/components/DynamicRowInterface.kt
  6. 35
      app/src/main/java/net/pokeranalytics/android/ui/adapter/components/RowViewType.kt
  7. 3
      app/src/main/java/net/pokeranalytics/android/ui/fragment/NewSessionFragment.kt
  8. 4
      app/src/main/java/net/pokeranalytics/android/ui/view/SessionRowView.kt
  9. 56
      app/src/main/res/layout/row_session.xml
  10. 25
      app/src/main/res/layout/row_session_view.xml

@ -1,10 +1,7 @@
package net.pokeranalytics.android package net.pokeranalytics.android
import android.app.Application import android.app.Application
import com.crashlytics.android.Crashlytics
import io.fabric.sdk.android.Fabric
import io.realm.Realm import io.realm.Realm
import io.realm.Realm.setDefaultConfiguration
import io.realm.RealmConfiguration import io.realm.RealmConfiguration
import timber.log.Timber import timber.log.Timber
@ -26,7 +23,7 @@ class PokerAnalyticsApplication: Application() {
// Logs // Logs
Timber.plant(Timber.DebugTree()) Timber.plant(Timber.DebugTree())
} else { } else {
Fabric.with(this, Crashlytics()) //Fabric.with(this, Crashlytics())
} }
} }

@ -7,86 +7,109 @@ import io.realm.RealmResults
import io.realm.Sort import io.realm.Sort
import io.realm.annotations.PrimaryKey import io.realm.annotations.PrimaryKey
import net.pokeranalytics.android.* import net.pokeranalytics.android.*
import net.pokeranalytics.android.ui.adapter.components.DynamicRowDelegate
import net.pokeranalytics.android.ui.adapter.components.DynamicRowInterface
import net.pokeranalytics.android.ui.adapter.components.SessionRow
import net.pokeranalytics.android.util.data.sessionDao import net.pokeranalytics.android.util.data.sessionDao
import java.util.* import java.util.*
import java.util.UUID.randomUUID import java.util.UUID.randomUUID
import kotlin.collections.ArrayList
open class Session(comment: String = "") : RealmObject(), DynamicRowDelegate {
open class Session(comment: String = "") : RealmObject() { @PrimaryKey
var id = UUID.randomUUID().toString()
@PrimaryKey // The time frame of the Session, i.e. the start & end date
var id = UUID.randomUUID().toString() var timeFrame: TimeFrame? = null
// The time frame of the Session, i.e. the start & end date // The time frame group, which can contain multiple sessions
var timeFrame: TimeFrame? = null var timeFrameGroup: TimeFrameGroup? = null
// The time frame group, which can contain multiple sessions // the date of creation of the app
var timeFrameGroup: TimeFrameGroup? = null
// the date of creation of the app
var creationDate: Date = Date() var creationDate: Date = Date()
// The limit type: NL, PL... // The limit type: NL, PL...
var limit: Int? = null var limit: Int? = null
// The number of tables played at the same time // The number of tables played at the same time
var numberOfTables: Int = 1 var numberOfTables: Int = 1
// The number of players at the table // The number of players at the table
var tableSize: Int? = null var tableSize: Int? = null
// The game played during the Session // The game played during the Session
var game: Game? = null var game: Game? = null
// The bankroll hosting the results // The bankroll hosting the results
var bankroll: Bankroll? = null var bankroll: Bankroll? = null
// The hands list associated with the Session // The hands list associated with the Session
var hands: RealmList<HandHistory> = RealmList() var hands: RealmList<HandHistory> = RealmList()
// the location where the session is played // the location where the session is played
var location: Location? = null var location: Location? = null
// The result of the main user // The result of the main user
var result: Result? = null var result: Result? = null
// The list of opponents who participated to the session // The list of opponents who participated to the session
var opponents: RealmList<Player> = RealmList() var opponents: RealmList<Player> = RealmList()
// A comment written by the user // A comment written by the user
var comment: String = "" var comment: String = ""
// Cash Game // Cash Game
// The small blind value // The small blind value
var cgSmallBlind: Double? = null var cgSmallBlind: Double? = null
// The big blind value // The big blind value
var cgBigBlind: Double? = null var cgBigBlind: Double? = null
// Tournament // Tournament
// The entry fee of the tournament // The entry fee of the tournament
var tournamentEntryFee: Double? = null var tournamentEntryFee: Double? = null
// The total number of players who participated in the tournament // The total number of players who participated in the tournament
var tournamentNumberOfPlayers: Int? = null var tournamentNumberOfPlayers: Int? = null
// The name of the tournament // The name of the tournament
var tournamentType: TournamentName? = null var tournamentType: TournamentName? = null
// The kind of the tournament, MTT or SnG
var tournamentKind: Int? = null
// The features of the tournament, like Knockout, Shootout, Turbo...
var tournamentFeatures: RealmList<TournamentFeature> = RealmList()
override fun adapterRows(): ArrayList<DynamicRowInterface> {
val rows = ArrayList<DynamicRowInterface>()
rows.addAll(SessionRow.values())
return rows
}
// The kind of the tournament, MTT or SnG override fun boolForRow(row: DynamicRowInterface): Boolean {
var tournamentKind: Int? = null
// The features of the tournament, like Knockout, Shootout, Turbo... return false
var tournamentFeatures: RealmList<TournamentFeature> = RealmList() }
override fun stringForRow(row: DynamicRowInterface): String {
return when (row) {
SessionRow.BLINDS -> "Blinds"
SessionRow.GAME -> "Game"
SessionRow.DATE -> "Date"
else -> "--"
}
}
} }
enum class TournamentKind { enum class TournamentKind {
MTT, MTT,
SNG SNG
} }
@ -95,63 +118,63 @@ enum class TournamentKind {
*/ */
class SessionDao(realmDb: Realm) { class SessionDao(realmDb: Realm) {
var realm: Realm = realmDb var realm: Realm = realmDb
/** /**
* Create or update session * Create or update session
*/ */
fun createOrUpdateSession(session: Session): Session { fun createOrUpdateSession(session: Session): Session {
realm.beginTransaction() realm.beginTransaction()
val sessionToSave = realm.copyToRealmOrUpdate(session) val sessionToSave = realm.copyToRealmOrUpdate(session)
realm.commitTransaction() realm.commitTransaction()
return realm.copyFromRealm(sessionToSave) return realm.copyFromRealm(sessionToSave)
} }
/** /**
* Create or update sessions * Create or update sessions
*/ */
fun createOrUpdateSessions(sessions: List<Session>): List<Session> { fun createOrUpdateSessions(sessions: List<Session>): List<Session> {
realm.beginTransaction() realm.beginTransaction()
// Update // Update
val sessionsToSave = realm.copyToRealmOrUpdate(sessions) val sessionsToSave = realm.copyToRealmOrUpdate(sessions)
realm.commitTransaction() realm.commitTransaction()
return realm.copyFromRealm(sessionsToSave) return realm.copyFromRealm(sessionsToSave)
} }
/** /**
* Find all sessions * Find all sessions
*/ */
fun findAllSessions(): RealmResults<Session> { fun findAllSessions(): RealmResults<Session> {
return realm.where(Session::class.java).findAll().sort("creationDate", Sort.DESCENDING) return realm.where(Session::class.java).findAll().sort("creationDate", Sort.DESCENDING)
} }
/** /**
* Find session by id * Find session by id
*/ */
fun findSessionById(sessionId: Int): Session? { fun findSessionById(sessionId: Int): Session? {
return realm.copyFromRealm(realm.where(Session::class.java).equalTo("id", sessionId).findFirst()) return realm.copyFromRealm(realm.where(Session::class.java).equalTo("id", sessionId).findFirst())
} }
/** /**
* Delete session * Delete session
*/ */
fun deleteSession(sessionId: Int) { fun deleteSession(sessionId: Int) {
realm.beginTransaction() realm.beginTransaction()
realm.sessionDao().findSessionById(sessionId)?.deleteFromRealm() realm.sessionDao().findSessionById(sessionId)?.deleteFromRealm()
realm.commitTransaction() realm.commitTransaction()
} }
/** /**
* Delete all sessions * Delete all sessions
*/ */
fun deleteAllSessions() { fun deleteAllSessions() {
realm.beginTransaction() realm.beginTransaction()
realm.sessionDao().findAllSessions().deleteAllFromRealm() realm.sessionDao().findAllSessions().deleteAllFromRealm()
realm.commitTransaction() realm.commitTransaction()
} }
} }

@ -8,7 +8,6 @@ import io.realm.RealmResults
import kotlinx.android.synthetic.main.row_history_session.view.* import kotlinx.android.synthetic.main.row_history_session.view.*
import net.pokeranalytics.android.R import net.pokeranalytics.android.R
import net.pokeranalytics.android.model.realm.Session import net.pokeranalytics.android.model.realm.Session
import net.pokeranalytics.android.ui.view.SessionRowView
import timber.log.Timber import timber.log.Timber
class HistoryAdapter(private var sessions: RealmResults<Session>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() { class HistoryAdapter(private var sessions: RealmResults<Session>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

@ -23,7 +23,7 @@ interface DynamicRowDelegate {
class DynamicListAdapter(delegate: DynamicRowDelegate) : RecyclerView.Adapter<RecyclerView.ViewHolder>() { class DynamicListAdapter(delegate: DynamicRowDelegate) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private var rows: ArrayList<DynamicRowInterface> = ArrayList<DynamicRowInterface>() private var rows: ArrayList<DynamicRowInterface> = ArrayList()
private var delegate: DynamicRowDelegate = delegate private var delegate: DynamicRowDelegate = delegate
init { init {

@ -25,8 +25,8 @@ interface DynamicRowInterface {
enum class SessionRow(val resId: Int) : DynamicRowInterface { enum class SessionRow(val resId: Int) : DynamicRowInterface {
BLINDS(R.string.app_name), BLINDS(R.string.app_name),
GAME(R.string.app_name), GAME(R.string.title_history),
DATE(R.string.app_name); DATE(R.string.title_settings);
override fun localizedTitle(context: Context): String { override fun localizedTitle(context: Context): String {
return context.getString(this.resId) return context.getString(this.resId)
@ -35,9 +35,7 @@ enum class SessionRow(val resId: Int) : DynamicRowInterface {
override var viewType: Int = RowViewType.HEADER.ordinal override var viewType: Int = RowViewType.HEADER.ordinal
get() { get() {
return when (this) { return when (this) {
BLINDS -> 1 BLINDS, GAME, DATE -> RowViewType.TITLE_VALUE.ordinal
GAME -> 2
DATE -> 1
} }
} }

@ -1,8 +1,12 @@
package net.pokeranalytics.android.ui.adapter.components package net.pokeranalytics.android.ui.adapter.components
import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.row_session.view.*
import net.pokeranalytics.android.R
interface DynamicHolder { interface DynamicHolder {
@ -12,22 +16,35 @@ interface DynamicHolder {
enum class RowViewType { enum class RowViewType {
HEADER, HEADER,
TEXTFIELD, EDIT_TEXT,
LEFTRIGHTLABEL; TITLE_VALUE;
inner class FakeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), DynamicHolder { inner class FakeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), DynamicHolder {
override fun bind(row: DynamicRowInterface, delegate: DynamicRowDelegate) { override fun bind(row: DynamicRowInterface, delegate: DynamicRowDelegate) {
// this.textView.text = delegate.stringForRow(row)
} }
}
inner class TitleValueViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), DynamicHolder {
override fun bind(row: DynamicRowInterface, delegate: DynamicRowDelegate) {
itemView.title.text = row.localizedTitle(itemView.context)
itemView.value.text = delegate.stringForRow(row)
itemView.container.setOnClickListener {
Toast.makeText(itemView.context, "Clicked", Toast.LENGTH_SHORT).show()
}
}
} }
fun viewHolder(parent: ViewGroup) : RecyclerView.ViewHolder { fun viewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
return FakeViewHolder(parent) return when (this) {
TITLE_VALUE -> TitleValueViewHolder(
LayoutInflater.from(parent.context).inflate(
R.layout.row_session,
parent,
false
)
)
else -> FakeViewHolder(parent)
}
} }
} }

@ -9,6 +9,7 @@ import kotlinx.android.synthetic.main.fragment_new_session.*
import net.pokeranalytics.android.R import net.pokeranalytics.android.R
import net.pokeranalytics.android.model.realm.Session import net.pokeranalytics.android.model.realm.Session
import net.pokeranalytics.android.ui.adapter.NewSessionAdapter import net.pokeranalytics.android.ui.adapter.NewSessionAdapter
import net.pokeranalytics.android.ui.adapter.components.DynamicListAdapter
import net.pokeranalytics.android.util.PokerAnalyticsFragment import net.pokeranalytics.android.util.PokerAnalyticsFragment
class NewSessionFragment: PokerAnalyticsFragment() { class NewSessionFragment: PokerAnalyticsFragment() {
@ -36,7 +37,7 @@ class NewSessionFragment: PokerAnalyticsFragment() {
private fun initUI() { private fun initUI() {
val viewManager = LinearLayoutManager(requireContext()) val viewManager = LinearLayoutManager(requireContext())
val newSessionAdapter = NewSessionAdapter(newSession) val newSessionAdapter = DynamicListAdapter(newSession)
recyclerView.apply { recyclerView.apply {
setHasFixedSize(true) setHasFixedSize(true)

@ -6,7 +6,7 @@ import android.graphics.Color
import android.util.AttributeSet import android.util.AttributeSet
import android.view.LayoutInflater import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintLayout
import kotlinx.android.synthetic.main.row_session.view.* import kotlinx.android.synthetic.main.row_session_view.view.*
import net.pokeranalytics.android.R import net.pokeranalytics.android.R
import net.pokeranalytics.android.model.realm.Session import net.pokeranalytics.android.model.realm.Session
import timber.log.Timber import timber.log.Timber
@ -38,7 +38,7 @@ class SessionRowView : FrameLayout {
*/ */
private fun init() { private fun init() {
val layoutInflater = LayoutInflater.from(context) val layoutInflater = LayoutInflater.from(context)
rowHistorySession = layoutInflater.inflate(R.layout.row_session, this, false) as ConstraintLayout rowHistorySession = layoutInflater.inflate(R.layout.row_session_view, this, false) as ConstraintLayout
val layoutParams = FrameLayout.LayoutParams( val layoutParams = FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT FrameLayout.LayoutParams.WRAP_CONTENT

@ -1,25 +1,41 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:padding="16dp"> android:background="?selectableItemBackground"
android:id="@+id/container"
android:padding="16dp">
<androidx.appcompat.widget.AppCompatTextView <androidx.appcompat.widget.AppCompatTextView
android:id="@+id/date" android:id="@+id/title"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"
android:layout_marginEnd="8dp" android:layout_marginTop="8dp"
android:layout_marginTop="8dp" android:textSize="18sp"
android:textSize="16sp" app:layout_constraintTop_toTopOf="parent"
tools:text="This date" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" android:layout_marginStart="8dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginBottom="8dp"
android:layout_marginStart="8dp" app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp" app:layout_constraintEnd_toStartOf="@+id/value"
app:layout_constraintBottom_toBottomOf="parent"/> tools:text="Title"/>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/value"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:textSize="16sp"
tools:text="Value"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/date"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:textSize="16sp"
tools:text="This date"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Loading…
Cancel
Save