Improve date management

dev_raz_wip
Aurelien Hubert 7 years ago
parent 5acf6970eb
commit ec5d419f61
  1. 19
      app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt
  2. 29
      app/src/main/java/net/pokeranalytics/android/ui/adapter/components/DynamicListAdapter.kt
  3. 8
      app/src/main/java/net/pokeranalytics/android/ui/adapter/components/DynamicRowInterface.kt
  4. 43
      app/src/main/java/net/pokeranalytics/android/ui/adapter/components/RowViewType.kt
  5. 24
      app/src/main/java/net/pokeranalytics/android/ui/fragment/NewSessionFragment.kt
  6. 9
      app/src/main/java/net/pokeranalytics/android/ui/fragment/components/BottomSheetDateFragment.kt
  7. 77
      app/src/main/java/net/pokeranalytics/android/ui/fragment/components/DateTimeDialogFragment.kt
  8. 17
      app/src/main/java/net/pokeranalytics/android/util/Extensions.kt
  9. 56
      app/src/main/res/layout/row_title_value_action.xml
  10. 2
      app/src/main/res/values/strings.xml

@ -4,10 +4,12 @@ import android.text.InputType
import io.realm.*
import io.realm.annotations.Ignore
import io.realm.annotations.PrimaryKey
import net.pokeranalytics.android.R
import net.pokeranalytics.android.calculus.SessionInterface
import net.pokeranalytics.android.ui.adapter.components.*
import net.pokeranalytics.android.ui.fragment.components.BottomSheetData
import net.pokeranalytics.android.util.data.sessionDao
import net.pokeranalytics.android.util.short
import java.util.*
import kotlin.collections.ArrayList
@ -141,12 +143,23 @@ open class Session : RealmObject(), SessionInterface, DynamicRowDelegate, Displa
SessionRow.GAME -> game?.title ?: "--"
SessionRow.LOCATION -> location?.title ?: "--"
SessionRow.BANKROLL -> bankroll?.title ?: "--"
SessionRow.DATE -> if (timeFrame != null) timeFrame?.startDate.toString() else "--"
SessionRow.START_DATE -> if (timeFrame != null) timeFrame?.startDate?.short() ?: "--" else "--"
SessionRow.END_DATE -> if (timeFrame != null) timeFrame?.endDate?.short() ?: "--" else "--"
SessionRow.COMMENT -> if (comment.isNotEmpty()) comment else "--"
else -> "--"
}
}
override fun actionIconForRow(row: DynamicRowInterface): Int? {
return when (row) {
SessionRow.START_DATE, SessionRow.END_DATE -> {
R.drawable.ic_close_white_24dp
}
else -> null
}
}
override var title: String = "Change that: $creationDate"
override val primaryKey: String get() = this.id
@ -165,10 +178,6 @@ open class Session : RealmObject(), SessionInterface, DynamicRowDelegate, Displa
SessionRow.BANKROLL -> {
data.add(BottomSheetData(bankroll, "", 0, DataList.BANKROLL.items(realm)))
}
SessionRow.DATE -> {
data.add(BottomSheetData(timeFrame?.startDate, "Start date"))
data.add(BottomSheetData(timeFrame?.endDate, "End date"))
}
SessionRow.BLINDS -> {
data.add(BottomSheetData(cgSmallBlind, "Small blind", InputType.TYPE_CLASS_NUMBER))
data.add(BottomSheetData(cgBigBlind, "Big blind", InputType.TYPE_CLASS_NUMBER))

@ -10,10 +10,19 @@ interface EditableDataDelegate {
interface DynamicRowDelegate {
fun adapterRows() : ArrayList<DynamicRowInterface>
fun adapterRows(): ArrayList<DynamicRowInterface>
fun boolForRow(row: DynamicRowInterface) : Boolean { return false }
fun stringForRow(row: DynamicRowInterface) : String { return "" }
fun boolForRow(row: DynamicRowInterface): Boolean {
return false
}
fun stringForRow(row: DynamicRowInterface): String {
return ""
}
fun actionIconForRow(row: DynamicRowInterface): Int? {
return 0
}
/**
* Manages:
@ -27,10 +36,12 @@ interface DynamicRowDelegate {
}
interface DynamicRowCallback {
fun onRowSelected(row: DynamicRowInterface)
fun onRowSelected(row: DynamicRowInterface) {}
fun onActionSelected(row: DynamicRowInterface) {}
}
class DynamicListAdapter(var delegate: DynamicRowDelegate, var callBackDelegate: DynamicRowCallback? = null) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
class DynamicListAdapter(var delegate: DynamicRowDelegate, var callBackDelegate: DynamicRowCallback? = null) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private var rows: ArrayList<DynamicRowInterface> = ArrayList()
@ -53,10 +64,16 @@ class DynamicListAdapter(var delegate: DynamicRowDelegate, var callBackDelegate:
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val dynamicRow = this.rows[position]
val listener = View.OnClickListener {
callBackDelegate?.onRowSelected(dynamicRow)
}
(holder as DynamicHolder).bind(dynamicRow, this.delegate, listener)
val actionListener = View.OnClickListener {
callBackDelegate?.onActionSelected(dynamicRow)
}
(holder as DynamicHolder).bind(dynamicRow, this.delegate, listener, actionListener)
}
}

@ -38,7 +38,8 @@ enum class SessionRow(val resId: Int) : DynamicRowInterface {
BLINDS(R.string.blinds),
LOCATION(R.string.location),
BANKROLL(R.string.bankroll),
DATE(R.string.date),
START_DATE(R.string.start_date),
END_DATE(R.string.end_date),
COMMENT(R.string.comment);
override fun localizedTitle(context: Context): String {
@ -48,7 +49,8 @@ enum class SessionRow(val resId: Int) : DynamicRowInterface {
override val viewType: Int
get() {
return when (this) {
BLINDS, GAME, DATE, BANKROLL, LOCATION, COMMENT -> RowViewType.TITLE_VALUE.ordinal
BLINDS, GAME, BANKROLL, LOCATION, COMMENT -> RowViewType.TITLE_VALUE.ordinal
START_DATE, END_DATE -> RowViewType.TITLE_VALUE_ACTION.ordinal
}
}
@ -59,8 +61,8 @@ enum class SessionRow(val resId: Int) : DynamicRowInterface {
GAME -> BottomSheetType.LIST
LOCATION -> BottomSheetType.LIST
BANKROLL -> BottomSheetType.LIST
DATE -> BottomSheetType.DATE
COMMENT -> BottomSheetType.EDIT_TEXT
else -> BottomSheetType.NONE
}
}

@ -4,12 +4,12 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.row_session.view.*
import kotlinx.android.synthetic.main.row_title_value_action.view.*
import net.pokeranalytics.android.R
interface DynamicHolder {
fun bind(row: DynamicRowInterface, delegate: DynamicRowDelegate? = null, listener: View.OnClickListener)
fun bind(row: DynamicRowInterface, delegate: DynamicRowDelegate? = null, listener: View.OnClickListener, actionListener: View.OnClickListener? = null) {}
}
@ -17,15 +17,23 @@ enum class RowViewType {
HEADER,
EDIT_TEXT,
TITLE,
TITLE_VALUE;
TITLE_VALUE,
TITLE_VALUE_ACTION;
inner class FakeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), DynamicHolder {
override fun bind(row: DynamicRowInterface, delegate: DynamicRowDelegate?, listener: View.OnClickListener) {
override fun bind(row: DynamicRowInterface, delegate: DynamicRowDelegate?, listener: View.OnClickListener, actionListener: View.OnClickListener?) {
}
}
inner class TitleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), DynamicHolder {
override fun bind(row: DynamicRowInterface, delegate: DynamicRowDelegate?, listener: View.OnClickListener, actionListener: View.OnClickListener?) {
itemView.title.text = row.localizedTitle(itemView.context)
itemView.container.setOnClickListener(listener)
}
}
inner class TitleValueViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), DynamicHolder {
override fun bind(row: DynamicRowInterface, delegate: DynamicRowDelegate?, listener: View.OnClickListener) {
override fun bind(row: DynamicRowInterface, delegate: DynamicRowDelegate?, listener: View.OnClickListener, actionListener: View.OnClickListener?) {
itemView.title.text = row.localizedTitle(itemView.context)
delegate?.let {
@ -35,16 +43,33 @@ enum class RowViewType {
}
}
inner class TitleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), DynamicHolder {
override fun bind(row: DynamicRowInterface, delegate: DynamicRowDelegate?, listener: View.OnClickListener) {
inner class TitleValueActionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), DynamicHolder {
override fun bind(row: DynamicRowInterface, delegate: DynamicRowDelegate?, listener: View.OnClickListener, actionListener: View.OnClickListener?) {
itemView.title.text = row.localizedTitle(itemView.context)
delegate?.let { rowDelegate ->
val value = rowDelegate.stringForRow(row)
itemView.value.text = value
itemView.action.visibility = if (value == "--") View.GONE else View.VISIBLE
rowDelegate.actionIconForRow(row)?.let {icon ->
itemView.action.setImageResource(icon)
}
}
itemView.container.setOnClickListener(listener)
itemView.action.setOnClickListener(actionListener)
}
}
fun viewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
return when (this) {
TITLE -> TitleViewHolder(
LayoutInflater.from(parent.context).inflate(
R.layout.row_title,
parent,
false
)
)
TITLE_VALUE -> TitleValueViewHolder(
LayoutInflater.from(parent.context).inflate(
R.layout.row_title_value,
@ -52,9 +77,9 @@ enum class RowViewType {
false
)
)
TITLE -> TitleViewHolder(
TITLE_VALUE_ACTION -> TitleValueActionViewHolder(
LayoutInflater.from(parent.context).inflate(
R.layout.row_title,
R.layout.row_title_value_action,
parent,
false
)

@ -17,7 +17,9 @@ import net.pokeranalytics.android.ui.adapter.components.DynamicRowInterface
import net.pokeranalytics.android.ui.adapter.components.SessionRow
import net.pokeranalytics.android.ui.fragment.components.BottomSheetDelegate
import net.pokeranalytics.android.ui.fragment.components.BottomSheetFragment
import net.pokeranalytics.android.ui.fragment.components.DateTimePickerManager
import net.pokeranalytics.android.ui.fragment.components.PokerAnalyticsFragment
import java.util.*
class NewSessionFragment : PokerAnalyticsFragment(), DynamicRowCallback, BottomSheetDelegate {
@ -36,7 +38,15 @@ class NewSessionFragment : PokerAnalyticsFragment(), DynamicRowCallback, BottomS
override fun onRowSelected(row: DynamicRowInterface) {
val data = currentSession.getBottomSheetData(row, getRealm())
BottomSheetFragment.create(fragmentManager, row, this, data)
when(row) {
SessionRow.START_DATE -> DateTimePickerManager.create(requireContext(), row, this, currentSession.timeFrame?.startDate)
SessionRow.END_DATE -> DateTimePickerManager.create(requireContext(), row, this, currentSession.timeFrame?.endDate)
else -> BottomSheetFragment.create(fragmentManager, row, this, data)
}
}
override fun onActionSelected(row: DynamicRowInterface) {
Toast.makeText(requireContext(), "Action for row: $row", Toast.LENGTH_SHORT).show()
}
override fun clickOnAdd(row: DynamicRowInterface) {
@ -70,11 +80,21 @@ class NewSessionFragment : PokerAnalyticsFragment(), DynamicRowCallback, BottomS
SessionRow.GAME -> if (value is Game) currentSession.game = value
SessionRow.BANKROLL -> if (value is Bankroll) currentSession.bankroll = value
SessionRow.LOCATION -> if (value is Location) currentSession.location = value
SessionRow.COMMENT -> if (value is String) currentSession.comment = value
SessionRow.BLINDS -> if (value is ArrayList<*>) {
currentSession.cgSmallBlind = (value[0] as String? ?: "0").toDouble()
currentSession.cgBigBlind = (value[1] as String? ?: "0").toDouble()
}
SessionRow.COMMENT -> if (value is String) currentSession.comment = value
SessionRow.START_DATE -> if (value is Date) {
val timeFrame = currentSession.timeFrame ?: TimeFrame()
timeFrame.setDate(value, null)
currentSession.timeFrame = timeFrame
}
SessionRow.END_DATE -> if (value is Date) {
val timeFrame = currentSession.timeFrame ?: TimeFrame()
timeFrame.setDate(timeFrame.startDate, value)
currentSession.timeFrame = timeFrame
}
}
sessionAdapter.notifyItemChanged(SessionRow.values().indexOf(row))
}

@ -4,12 +4,10 @@ import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.os.Bundle
import android.text.format.DateFormat
import android.view.LayoutInflater
import android.view.View
import android.widget.DatePicker
import android.widget.TimePicker
import kotlinx.android.synthetic.main.bottom_sheet_date.*
import kotlinx.android.synthetic.main.fragment_bottom_sheet.view.*
import timber.log.Timber
import java.util.*
@ -79,9 +77,9 @@ class BottomSheetDateFragment : BottomSheetFragment(), DatePickerDialog.OnDateSe
*/
private fun initUI() {
LayoutInflater.from(requireContext()).inflate(net.pokeranalytics.android.R.layout.bottom_sheet_date, view?.bottomSheetContainer, true)
//LayoutInflater.from(requireContext()).inflate(net.pokeranalytics.android.R.layout.bottom_sheet_date, view?.bottomSheetContainer, true)
setAddButtonVisible(false)
//setAddButtonVisible(false)
startDate.setOnClickListener {
currentDateEdition = DateEdition.START
@ -93,6 +91,9 @@ class BottomSheetDateFragment : BottomSheetFragment(), DatePickerDialog.OnDateSe
showDatePicker()
}
currentDateEdition = DateEdition.START
showDatePicker()
}
/**

@ -0,0 +1,77 @@
package net.pokeranalytics.android.ui.fragment.components
import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.content.Context
import android.text.format.DateFormat
import android.widget.DatePicker
import android.widget.TimePicker
import net.pokeranalytics.android.ui.adapter.components.DynamicRowInterface
import java.util.*
class DateTimePickerManager : DatePickerDialog.OnDateSetListener,
TimePickerDialog.OnTimeSetListener {
private var context: Context? = null
private val calendar = Calendar.getInstance()
lateinit var row: DynamicRowInterface
lateinit var bottomSheetDelegate: BottomSheetDelegate
companion object {
fun create(
context: Context,
row: DynamicRowInterface,
bottomSheetDelegate: BottomSheetDelegate,
date: Date?
) : DateTimePickerManager {
val dateTimePickerManager = DateTimePickerManager()
dateTimePickerManager.context = context
dateTimePickerManager.showDatePicker()
dateTimePickerManager.row = row
dateTimePickerManager.bottomSheetDelegate = bottomSheetDelegate
return dateTimePickerManager
}
}
override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
calendar.set(Calendar.YEAR, year)
calendar.set(Calendar.MONTH, month)
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth)
showTimePicker()
}
override fun onTimeSet(view: TimePicker?, hourOfDay: Int, minute: Int) {
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay)
calendar.set(Calendar.MINUTE, minute)
bottomSheetDelegate.setValue(calendar.time, row)
}
/**
* Show the date picker
*/
private fun showDatePicker() {
val year = calendar.get(Calendar.YEAR)
val month = calendar.get(Calendar.MONTH)
val day = calendar.get(Calendar.DAY_OF_MONTH)
context?.let {
val datePickerDialog = DatePickerDialog(it, this, year, month, day)
datePickerDialog.show()
}
}
/**
* Show the time picker
*/
private fun showTimePicker() {
val hour = calendar.get(Calendar.YEAR)
val minute = calendar.get(Calendar.MONTH)
context?.let {
val timePickerDialog = TimePickerDialog(context, this, hour, minute, DateFormat.is24HourFormat(context))
timePickerDialog.show()
}
}
}

@ -0,0 +1,17 @@
package net.pokeranalytics.android.util
import java.text.DateFormat
import java.util.*
fun Date.short(): String {
return DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(this)
}
fun Date.medium(): String {
return DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM).format(this)
}
fun Date.full(): String {
return DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(this)
}

@ -0,0 +1,56 @@
<?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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?selectableItemBackground"
android:padding="16dp">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Title" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/value"
android:layout_width="0dp"
android:layout_height="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:ellipsize="end"
android:gravity="end"
android:maxLines="1"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/action"
app:layout_constraintStart_toEndOf="@+id/title"
app:layout_constraintTop_toTopOf="parent"
tools:text="Value" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/action"
android:layout_width="32dp"
android:layout_height="32dp"
android:padding="4dp"
android:visibility="visible"
android:background="?selectableItemBackgroundBorderless"
tools:src="@drawable/ic_close_white_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

@ -9,6 +9,8 @@
<string name="blinds">Blinds</string>
<string name="game">Game</string>
<string name="date">Date</string>
<string name="start_date">Start date</string>
<string name="end_date">End date</string>
<string name="location">Location</string>
<string name="session">Session</string>
<string name="tournament_type">Tournament Type</string>

Loading…
Cancel
Save