Update History management & session dao

dev_raz_wip
Aurelien Hubert 7 years ago
parent 6398989dc4
commit 436385f217
  1. 76
      app/src/main/java/net/pokeranalytics/android/model/dao/SessionDao.kt
  2. 80
      app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt
  3. 101
      app/src/main/java/net/pokeranalytics/android/ui/adapter/HistoryAdapter.kt
  4. 51
      app/src/main/java/net/pokeranalytics/android/ui/fragment/HistoryFragment.kt
  5. 1
      app/src/main/java/net/pokeranalytics/android/ui/viewmodel/SessionViewModel.kt
  6. 3
      app/src/main/java/net/pokeranalytics/android/util/data/Realm+Dao.kt
  7. 23
      app/src/main/res/layout/row_history_session.xml

@ -1,76 +0,0 @@
package net.pokeranalytics.android.model.dao
import io.realm.Realm
import io.realm.RealmResults
import io.realm.Sort
import net.pokeranalytics.android.model.realm.Session
import net.pokeranalytics.android.util.data.sessionDao
/**
* Session Dao
*/
class SessionDao(realmDb: Realm) {
var realm: Realm = realmDb
/**
* Create or update session
*/
fun createOrUpdateSession(session: Session): Session {
realm.beginTransaction()
val sessionToSave = realm.copyToRealmOrUpdate(session)
realm.commitTransaction()
return realm.copyFromRealm(sessionToSave)
}
/**
* Create or update sessions
*/
fun createOrUpdateSessions(sessions: List<Session>): List<Session> {
realm.beginTransaction()
// Update
val sessionsToSave = realm.copyToRealmOrUpdate(sessions)
// Remove sessions not updated
realm.where(Session::class.java).equalTo("isUpdating", true).findAll().deleteAllFromRealm()
realm.commitTransaction()
return realm.copyFromRealm(sessionsToSave)
}
/**
* Find all sessions
*/
fun findAllSessions(): RealmResults<Session> {
return realm.where(Session::class.java).findAll().sort("updatedAt", Sort.DESCENDING)
}
/**
* Find session by id
*/
fun findSessionById(sessionId: Int): Session? {
return realm.where(Session::class.java).equalTo("id", sessionId).findFirst()
}
/**
* Delete session
*/
fun deleteSession(sessionId: Int) {
realm.beginTransaction()
realm.sessionDao().findSessionById(sessionId)?.deleteFromRealm()
realm.commitTransaction()
}
/**
* Delete all sessions
*/
fun deleteAllSessions() {
realm.beginTransaction()
realm.sessionDao().findAllSessions().deleteAllFromRealm()
realm.commitTransaction()
}
}

@ -1,8 +1,12 @@
package net.pokeranalytics.android.model.realm
import io.realm.Realm
import io.realm.RealmObject
import io.realm.RealmResults
import io.realm.Sort
import io.realm.annotations.PrimaryKey
import net.pokeranalytics.android.*
import net.pokeranalytics.android.util.data.sessionDao
import java.util.*
open class Session : RealmObject() {
@ -26,16 +30,12 @@ open class Session : RealmObject() {
//var bankroll: Bankroll? = bankroll
var bankroll: Bankroll? = null
var hands: List<HandHistory> = listOf()
//var timeFrame: TimeFrame = timeFrame
var timeFrame: TimeFrame? = null
var location: Location? = null
var result: Result = Result()
var opponents: List<Player> = listOf()
// @todo serie
@ -43,11 +43,81 @@ open class Session : RealmObject() {
// @todo cash game blind: 2/5/10, short deck ante, big bets in fixed limit
// Tournament
var entryFee: Double? = null
var numberOfPlayers: Int? = null
// @todo tournament type?
*/
}
/**
* Session Dao
*/
class SessionDao(realmDb: Realm) {
var realm: Realm = realmDb
/**
* Create or update session
*/
fun createOrUpdateSession(session: Session): Session {
realm.beginTransaction()
val sessionToSave = realm.copyToRealmOrUpdate(session)
realm.commitTransaction()
return realm.copyFromRealm(sessionToSave)
}
/**
* Create or update sessions
*/
fun createOrUpdateSessions(sessions: List<Session>): List<Session> {
realm.beginTransaction()
// Update
val sessionsToSave = realm.copyToRealmOrUpdate(sessions)
// Remove sessions not updated
realm.where(Session::class.java)
.equalTo("isUpdating", true).findAll().deleteAllFromRealm()
realm.commitTransaction()
return realm.copyFromRealm(sessionsToSave)
}
/**
* Find all sessions
*/
fun findAllSessions(): RealmResults<Session> {
return realm.where(Session::class.java).findAll().sort("updatedAt", Sort.DESCENDING)
}
/**
* Find session by id
*/
fun findSessionById(sessionId: Int): Session? {
return realm.where(Session::class.java).equalTo("id", sessionId).findFirst()
}
/**
* Delete session
*/
fun deleteSession(sessionId: Int) {
realm.beginTransaction()
realm.sessionDao().findSessionById(sessionId)?.deleteFromRealm()
realm.commitTransaction()
}
/**
* Delete all sessions
*/
fun deleteAllSessions() {
realm.beginTransaction()
realm.sessionDao().findAllSessions().deleteAllFromRealm()
realm.commitTransaction()
}
}

@ -0,0 +1,101 @@
package net.pokeranalytics.android.ui.adapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import net.pokeranalytics.android.R
import net.pokeranalytics.android.Session
class HistoryAdapter(private var sessions: ArrayList<Session>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
companion object {
const val ROW_SESSION: Int = 100
}
private var isLoadingItem = -1
private var lastSelectedItem = -1
var onClickOnProduct: ((position: Int, session: Session) -> Unit)? = null
var isPlaceholder = false
set(value) {
field = value
notifyDataSetChanged()
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind(session: Session) {
val context = itemView.context
/*
itemView.productName.text = product.name
itemView.productDescription.text = product.getMobileDescription()
itemView.productPrice.text = product.getMinPrice()
itemView.productSpecs.removeAllViews()
if (product.getMobilePageCount().isNotEmpty()) {
val specItemView = LayoutInflater.from(context).inflate(R.layout.layout_product_specifications, itemView.items, false)
specItemView.specIcon.setImageResource(R.drawable.icon_spec_pages_count)
specItemView.specDescription.text = product.getMobilePageCount()
itemView.productSpecs.addView(specItemView)
}
if (product.getMobilePhotoCount().isNotEmpty()) {
val specItemView = LayoutInflater.from(context).inflate(R.layout.layout_product_specifications, itemView.items, false)
specItemView.specIcon.setImageResource(R.drawable.icon_spec_photos_count)
specItemView.specDescription.text = product.getMobilePhotoCount()
itemView.productSpecs.addView(specItemView)
}
if (product.getMobileFormatCount().isNotEmpty()) {
val specItemView = LayoutInflater.from(context).inflate(R.layout.layout_product_specifications, itemView.items, false)
specItemView.specIcon.setImageResource(R.drawable.icon_spec_formats)
specItemView.specDescription.text = product.getMobileFormatCount()
itemView.productSpecs.addView(specItemView)
}
Glide.with(context)
.load(product.principalImagePath)
.transition(withCrossFade())
.into(itemView.productImage)
itemView.productOrder.setOnClickListener {
onClickOnProduct?.invoke(adapterPosition, product)
}
itemView.container.setOnClickListener {
onClickOnProduct?.invoke(adapterPosition, product)
}
*/
}
}
inner class PlaceHolderViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind() {
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
when (viewType) {
ROW_SESSION -> return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.row_history_session, parent, false))
else -> throw IllegalStateException("Need to implement type $viewType in HistoryAdapter")
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (getItemViewType(position)) {
HistoryAdapter.ROW_SESSION -> (holder as HistoryAdapter.ViewHolder).bind(sessions[position])
}
}
override fun getItemCount(): Int {
return sessions.size
}
override fun getItemViewType(position: Int) : Int {
return ROW_SESSION
}
}

@ -12,35 +12,40 @@ import net.pokeranalytics.android.util.data.SessionViewModel
class HistoryFragment : PokerAnalyticsFragment() {
companion object {
companion object {
fun newInstance(): HistoryFragment {
val fragment = HistoryFragment()
val bundle = Bundle()
fragment.arguments = bundle
return fragment
}
}
fun newInstance(): HistoryFragment {
val fragment = HistoryFragment()
val bundle = Bundle()
fragment.arguments = bundle
return fragment
}
private lateinit var sessionViewModel: SessionViewModel
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_history, container, false)
}
private lateinit var sessionViewModel: SessionViewModel
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_history, container, false)
}
sessionViewModel = ViewModelProviders.of(this).get(SessionViewModel::class.java)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initData()
}
sessionViewModel = ViewModelProviders.of(this).get(SessionViewModel::class.java)
/**
* Init data
*/
private fun initData() {
val sessions = sessionViewModel.getAllSessions()
}
initData()
}
/**
* Init data
*/
private fun initData() {
sessionViewModel.getAllSessions()
}
}

@ -54,6 +54,7 @@ class SessionViewModel(application: Application) : AndroidViewModel(application)
}
return null
}
/**
* Create or update a session
*/

@ -4,8 +4,7 @@ package net.pokeranalytics.android.util.data
import io.realm.Realm
import io.realm.RealmModel
import io.realm.RealmResults
import net.pokeranalytics.android.model.dao.SessionDao
import net.pokeranalytics.android.util.data.LiveRealmData
import net.pokeranalytics.android.model.realm.SessionDao
fun Realm.sessionDao(): SessionDao = SessionDao(this)

@ -0,0 +1,23 @@
<?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="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
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