Improve Game management

feature/top10
Aurelien Hubert 7 years ago
parent 8fe25094de
commit 3c952a13c6
  1. 80
      app/src/main/java/net/pokeranalytics/android/PokerAnalyticsApplication.kt
  2. 2
      app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt
  3. 50
      app/src/main/java/net/pokeranalytics/android/ui/adapter/LimitTypesAdapter.kt
  4. 4
      app/src/main/java/net/pokeranalytics/android/ui/fragment/components/bottomsheet/BottomSheetFragment.kt
  5. 1
      app/src/main/java/net/pokeranalytics/android/ui/fragment/components/bottomsheet/BottomSheetListFragment.kt
  6. 47
      app/src/main/java/net/pokeranalytics/android/ui/fragment/components/bottomsheet/BottomSheetListGameFragment.kt
  7. 3
      app/src/main/java/net/pokeranalytics/android/ui/view/RowRepresentable.kt
  8. 24
      app/src/main/res/layout/bottom_sheet_game_list.xml

@ -4,14 +4,17 @@ import android.app.Application
import io.realm.Realm import io.realm.Realm
import io.realm.RealmConfiguration import io.realm.RealmConfiguration
import io.realm.RealmResults import io.realm.RealmResults
import io.realm.kotlin.where
import net.pokeranalytics.android.model.realm.Game
import net.pokeranalytics.android.model.realm.Session import net.pokeranalytics.android.model.realm.Session
import net.pokeranalytics.android.util.PokerAnalyticsLogs import net.pokeranalytics.android.util.PokerAnalyticsLogs
import timber.log.Timber import timber.log.Timber
import java.util.*
class PokerAnalyticsApplication: Application() { class PokerAnalyticsApplication : Application() {
var sessions: RealmResults<Session>? = null var sessions: RealmResults<Session>? = null
// private val listener: OrderedRealmCollectionChangeListener<RealmResults<Session>> = // private val listener: OrderedRealmCollectionChangeListener<RealmResults<Session>> =
// OrderedRealmCollectionChangeListener() { realmResults: RealmResults<Session>, changeSet: OrderedCollectionChangeSet -> // OrderedRealmCollectionChangeListener() { realmResults: RealmResults<Session>, changeSet: OrderedCollectionChangeSet ->
@ -27,34 +30,61 @@ class PokerAnalyticsApplication: Application() {
// //
// } // }
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
// Realm // Realm
Realm.init(this) Realm.init(this)
val realmConfiguration = RealmConfiguration.Builder() val realmConfiguration = RealmConfiguration.Builder()
.name(Realm.DEFAULT_REALM_NAME) .name(Realm.DEFAULT_REALM_NAME)
.deleteRealmIfMigrationNeeded() .deleteRealmIfMigrationNeeded()
.build() .build()
Realm.setDefaultConfiguration(realmConfiguration) Realm.setDefaultConfiguration(realmConfiguration)
val realm: Realm = Realm.getDefaultInstance() val realm: Realm = Realm.getDefaultInstance()
// Add observer on session time frame changes // Add observer on session time frame changes
this.sessions = realm.where(Session::class.java).findAll() // monitor session deletions this.sessions = realm.where(Session::class.java).findAll() // monitor session deletions
this.sessions?.addChangeListener { t, changeSet -> this.sessions?.addChangeListener { t, changeSet ->
val deletedSessions = realm.where(Session::class.java).`in`("id", changeSet.deletions.toTypedArray()).findAll() val deletedSessions =
deletedSessions.forEach { it.cleanup() } realm.where(Session::class.java).`in`("id", changeSet.deletions.toTypedArray()).findAll()
deletedSessions.forEach { it.cleanup() }
}
} if (BuildConfig.DEBUG) {
// Logs
Timber.plant(PokerAnalyticsLogs())
} else {
//Fabric.with(this, Crashlytics())
}
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
// Logs createDefaultData()
Timber.plant(PokerAnalyticsLogs()) }
} else {
//Fabric.with(this, Crashlytics())
}
} }
/**
* Create default data
*/
private fun createDefaultData() {
val gamesName = resources.getStringArray(R.array.game_name)
val gamesShortName = resources.getStringArray(R.array.game_name)
val realm = Realm.getDefaultInstance()
if (realm.where<Game>().findAll().isEmpty()) {
realm.executeTransaction {
for ((index, name) in gamesName.withIndex()) {
val game = Game()
game.id = UUID.randomUUID().toString()
game.name = name
game.shortName = gamesShortName[index]
realm.copyToRealmOrUpdate(game)
}
}
}
}
} }

@ -188,7 +188,6 @@ open class Session : RealmObject(), SessionInterface, RowRepresentableDataSource
return gameTitle return gameTitle
} }
companion object { companion object {
fun newInstance(): Session { fun newInstance(): Session {
var session: Session = Session() var session: Session = Session()
@ -296,6 +295,7 @@ open class Session : RealmObject(), SessionInterface, RowRepresentableDataSource
SessionRow.TABLE_SIZE -> {data.add(BottomSheetData(tableSize))} SessionRow.TABLE_SIZE -> {data.add(BottomSheetData(tableSize))}
SessionRow.GAME -> { SessionRow.GAME -> {
// Add current game & games list // Add current game & games list
data.add(BottomSheetData(limit))
data.add(BottomSheetData(game, data = LiveData.GAME.items(realm))) data.add(BottomSheetData(game, data = LiveData.GAME.items(realm)))
} }
SessionRow.LOCATION -> { SessionRow.LOCATION -> {

@ -0,0 +1,50 @@
package net.pokeranalytics.android.ui.adapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.row_bottom_sheet_grid_title.view.*
import net.pokeranalytics.android.R
class LimitTypesAdapter(private var tableSizes: ArrayList<String>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
companion object {
const val ROW_LIMIT: Int = 100
}
var onClickOnItem: ((position: Int) -> Unit)? = null
inner class CellSessionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind(tableSize: String) {
itemView.title.text = tableSize
itemView.container.setOnClickListener {
onClickOnItem?.invoke(adapterPosition)
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
when (viewType) {
ROW_LIMIT -> return CellSessionViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.row_bottom_sheet_title, parent, false))
else -> throw IllegalStateException("Need to implement type $viewType in HistoryAdapter")
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (getItemViewType(position)) {
ROW_LIMIT -> (holder as LimitTypesAdapter.CellSessionViewHolder).bind(tableSizes[position])
}
}
override fun getItemCount(): Int {
return tableSizes.size
}
override fun getItemViewType(position: Int): Int {
return ROW_LIMIT
}
}

@ -14,6 +14,7 @@ import net.pokeranalytics.android.ui.view.RowRepresentable
enum class BottomSheetType { enum class BottomSheetType {
NONE, NONE,
LIST, LIST,
LIST_GAME,
DOUBLE_LIST, DOUBLE_LIST,
GRID, GRID,
EDIT_TEXT, EDIT_TEXT,
@ -45,8 +46,9 @@ open class BottomSheetFragment : BottomSheetDialogFragment() {
val bottomSheetFragment = when (row.bottomSheetType) { val bottomSheetFragment = when (row.bottomSheetType) {
BottomSheetType.LIST -> BottomSheetListFragment() BottomSheetType.LIST -> BottomSheetListFragment()
BottomSheetType.LIST_GAME -> BottomSheetListGameFragment()
BottomSheetType.GRID -> BottomSheetTableSizeGridFragment() BottomSheetType.GRID -> BottomSheetTableSizeGridFragment()
BottomSheetType.DOUBLE_LIST -> BottomSheetDoubleListFragment() BottomSheetType.DOUBLE_LIST -> BottomSheetListGameFragment()
BottomSheetType.EDIT_TEXT -> BottomSheetEditTextFragment() BottomSheetType.EDIT_TEXT -> BottomSheetEditTextFragment()
BottomSheetType.DOUBLE_EDIT_TEXT -> BottomSheetDoubleEditTextFragment() BottomSheetType.DOUBLE_EDIT_TEXT -> BottomSheetDoubleEditTextFragment()
BottomSheetType.SUM -> BottomSheetSumFragment() BottomSheetType.SUM -> BottomSheetSumFragment()

@ -55,7 +55,6 @@ class BottomSheetListFragment : BottomSheetFragment(), LiveDataDelegate {
* Init data * Init data
*/ */
private fun initData() { private fun initData() {
val bottomSheetData = getData() val bottomSheetData = getData()
if (bottomSheetData.isNotEmpty() && bottomSheetData.first().data != null) { if (bottomSheetData.isNotEmpty() && bottomSheetData.first().data != null) {
this.realmData = bottomSheetData.first().data as RealmResults<*> this.realmData = bottomSheetData.first().data as RealmResults<*>

@ -4,6 +4,7 @@ import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import io.realm.RealmResults
import kotlinx.android.synthetic.main.bottom_sheet_game_list.* import kotlinx.android.synthetic.main.bottom_sheet_game_list.*
import kotlinx.android.synthetic.main.fragment_bottom_sheet.view.* import kotlinx.android.synthetic.main.fragment_bottom_sheet.view.*
import net.pokeranalytics.android.R import net.pokeranalytics.android.R
@ -12,9 +13,13 @@ import net.pokeranalytics.android.ui.adapter.LimitTypesAdapter
import net.pokeranalytics.android.ui.adapter.components.LiveDataAdapter import net.pokeranalytics.android.ui.adapter.components.LiveDataAdapter
import net.pokeranalytics.android.ui.adapter.components.LiveDataDataSource import net.pokeranalytics.android.ui.adapter.components.LiveDataDataSource
import net.pokeranalytics.android.ui.adapter.components.LiveDataDelegate import net.pokeranalytics.android.ui.adapter.components.LiveDataDelegate
import timber.log.Timber
class BottomSheetGameListFragment : BottomSheetFragment(), LiveDataDelegate { class BottomSheetListGameFragment : BottomSheetFragment(), LiveDataDelegate {
private var realmData: RealmResults<*>? = null
private lateinit var dataAdapter: LiveDataAdapter
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
@ -22,55 +27,77 @@ class BottomSheetGameListFragment : BottomSheetFragment(), LiveDataDelegate {
initUI() initUI()
} }
override fun onResume() {
super.onResume()
dataAdapter.notifyDataSetChanged()
}
override fun data(position: Int): LiveDataDataSource { override fun data(position: Int): LiveDataDataSource {
realmData?.let {
return it[position] as LiveDataDataSource
}
//TODO: Change that //TODO: Change that
return Game() return Game()
} }
override fun onRowSelected(position: Int) { override fun onRowSelected(position: Int) {
realmData?.let {
val selectedData = it[position]
selectedData?.let {data ->
bottomSheetDelegate.setValue(data, row)
dismiss()
}
}
} }
override fun size(): Int { override fun size(): Int {
return 1
Timber.d("Games: ${realmData?.size}")
return realmData?.size ?: 0
} }
/** /**
* Init data * Init data
*/ */
private fun initData() { private fun initData() {
val data = getData() val bottomSheetData = getData()
//game = if (data is Game) data else Game() if (bottomSheetData.isNotEmpty() && bottomSheetData.size >= 2 && bottomSheetData[1].data != null) {
//game.title = "Test" this.realmData = bottomSheetData[1].data as RealmResults<*>
}
} }
/** /**
* Init UI * Init UI
*/ */
private fun initUI() { private fun initUI() {
LayoutInflater.from(requireContext()).inflate(R.layout.bottom_sheet_game_list, view?.bottomSheetContainer, true) LayoutInflater.from(requireContext()).inflate(R.layout.bottom_sheet_game_list, view?.bottomSheetContainer, true)
val limits = ArrayList<String>() val limits = ArrayList<String>()
limits.add("--")
limits.addAll(resources.getStringArray(R.array.limit_short_name)) limits.addAll(resources.getStringArray(R.array.limit_short_name))
val viewManager1 = LinearLayoutManager(requireContext()) val viewManager1 = LinearLayoutManager(requireContext())
val gameDataAdapter1 = LimitTypesAdapter(limits) val gameDataAdapter1 = LimitTypesAdapter(limits)
reyclerView1.apply { recyclerView1.apply {
setHasFixedSize(true) setHasFixedSize(true)
layoutManager = viewManager1 layoutManager = viewManager1
adapter = gameDataAdapter1 adapter = gameDataAdapter1
isNestedScrollingEnabled = false
} }
val viewManager2 = LinearLayoutManager(requireContext()) val viewManager2 = LinearLayoutManager(requireContext())
val gameDataAdapter2 = LiveDataAdapter(this, R.layout.row_bottom_sheet_title) dataAdapter = LiveDataAdapter(this, R.layout.row_bottom_sheet_title)
reyclerView2.apply { recyclerView2.apply {
setHasFixedSize(true) setHasFixedSize(true)
layoutManager = viewManager2 layoutManager = viewManager2
adapter = gameDataAdapter2 adapter = dataAdapter
} }
} }
} }

@ -149,7 +149,8 @@ enum class SessionRow : RowRepresentable {
CASHED_OUT, BREAK_TIME -> BottomSheetType.EDIT_TEXT CASHED_OUT, BREAK_TIME -> BottomSheetType.EDIT_TEXT
BUY_IN, TIPS -> BottomSheetType.SUM BUY_IN, TIPS -> BottomSheetType.SUM
BLINDS -> BottomSheetType.DOUBLE_EDIT_TEXT BLINDS -> BottomSheetType.DOUBLE_EDIT_TEXT
GAME, LOCATION, BANKROLL -> BottomSheetType.LIST GAME -> BottomSheetType.LIST_GAME
LOCATION, BANKROLL -> BottomSheetType.LIST
TABLE_SIZE -> BottomSheetType.GRID TABLE_SIZE -> BottomSheetType.GRID
COMMENT -> BottomSheetType.EDIT_TEXT COMMENT -> BottomSheetType.EDIT_TEXT
else -> BottomSheetType.NONE else -> BottomSheetType.NONE

@ -1,19 +1,25 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:orientation="horizontal"> android:orientation="horizontal">
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/reyclerView1" android:id="@+id/recyclerView1"
android:layout_width="80dp" android:layout_width="80dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:minHeight="200dp" /> app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/reyclerView2" android:id="@+id/recyclerView2"
android:layout_width="match_parent" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="0dp"
android:minHeight="200dp" /> app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/recyclerView1"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
Loading…
Cancel
Save