diff --git a/app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt b/app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt index c328cd12..e3bce209 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt @@ -17,6 +17,7 @@ import net.pokeranalytics.android.ui.view.RowRepresentable import net.pokeranalytics.android.ui.view.SessionRow import net.pokeranalytics.android.util.data.sessionDao import net.pokeranalytics.android.util.short +import net.pokeranalytics.android.util.toCurrency import java.util.* import kotlin.collections.ArrayList @@ -202,6 +203,7 @@ open class Session : RealmObject(), SessionInterface, RowRepresentableDataSource override fun stringForRow(row: RowRepresentable): String { return when (row) { + SessionRow.BUY_IN -> buyin.toCurrency() SessionRow.BLINDS -> if (cgSmallBlind != null && cgBigBlind != null) "$cgSmallBlind / $cgBigBlind" else "--" SessionRow.GAME -> game?.title ?: "--" SessionRow.LOCATION -> location?.title ?: "--" @@ -231,26 +233,35 @@ open class Session : RealmObject(), SessionInterface, RowRepresentableDataSource val data = ArrayList() when (row) { + + SessionRow.BUY_IN -> { + // Add first & second buttons values, current value & set the 2 edit texts + data.add(BottomSheetData(100.0 * (cgBigBlind ?: 0.0))) + data.add(BottomSheetData(200.0 * (cgBigBlind ?: 0.0))) + data.add(BottomSheetData(buyin)) + data.add(BottomSheetData("",inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL)) + data.add(BottomSheetData("",inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL)) + } + SessionRow.TIPS -> { + // Disable the buttons with value = 0, add current value & set the 2 edit texts + // TODO: manage tips + data.add(BottomSheetData(cgSmallBlind ?: 0.0)) + data.add(BottomSheetData(cgBigBlind ?: 0.0)) + data.add(BottomSheetData(0)) + data.add(BottomSheetData("", inputType = InputType.TYPE_CLASS_NUMBER)) + data.add(BottomSheetData("", inputType = InputType.TYPE_CLASS_NUMBER)) + } SessionRow.GAME -> { - data.add(BottomSheetData(game, inputType = InputType.TYPE_NULL, data = LiveData.GAME.items(realm))) + // Add current game & games list + data.add(BottomSheetData(game, data = LiveData.GAME.items(realm))) } SessionRow.LOCATION -> { - data.add( - BottomSheetData( - location, - inputType = InputType.TYPE_NULL, - data = LiveData.LOCATION.items(realm) - ) - ) + // Add current location and locations list + data.add(BottomSheetData(location, data = LiveData.LOCATION.items(realm))) } SessionRow.BANKROLL -> { - data.add( - BottomSheetData( - bankroll, - inputType = InputType.TYPE_NULL, - data = LiveData.BANKROLL.items(realm) - ) - ) + // Add current bankroll and bankrolls list + data.add(BottomSheetData(bankroll, data = LiveData.BANKROLL.items(realm))) } SessionRow.BLINDS -> { data.add(BottomSheetData(cgSmallBlind, R.string.smallblind, InputType.TYPE_CLASS_NUMBER)) @@ -267,6 +278,13 @@ open class Session : RealmObject(), SessionInterface, RowRepresentableDataSource override fun updateValue(value: Any?, row: RowRepresentable) { realm.beginTransaction() when (row) { + + SessionRow.BUY_IN -> { + val localResult = if (result != null) result as Result else realm.createObject(Result::class.java) + localResult.buyin = value as Double + result = localResult + } + SessionRow.GAME -> game = value as Game? SessionRow.BANKROLL -> bankroll = value as Bankroll? SessionRow.LOCATION -> location = value as Location? @@ -285,19 +303,15 @@ open class Session : RealmObject(), SessionInterface, RowRepresentableDataSource } //TODO: Update SessionRow.START_DATE -> if (value is Date) { - /* - val timeFrameToUpdate = timeFrame ?: TimeFrame() + val timeFrameToUpdate = if (timeFrame != null) timeFrame as TimeFrame else realm.createObject(TimeFrame::class.java) timeFrameToUpdate.setDate(value, null) timeFrame = timeFrameToUpdate - */ } //TODO: Update SessionRow.END_DATE -> if (value is Date) { - /* - val timeFrameToUpdate = timeFrame ?: TimeFrame() - timeFrameToUpdate.setDate(timeFrame?.startDate ?: Date(), value) + val timeFrameToUpdate = if (timeFrame != null) timeFrame as TimeFrame else realm.createObject(TimeFrame::class.java) + timeFrameToUpdate.setDate(null, value) timeFrame = timeFrameToUpdate - */ } } realm.commitTransaction() diff --git a/app/src/main/java/net/pokeranalytics/android/ui/fragment/components/bottomsheet/BottomSheetFragment.kt b/app/src/main/java/net/pokeranalytics/android/ui/fragment/components/bottomsheet/BottomSheetFragment.kt index 4247f05d..9406d9ef 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/fragment/components/bottomsheet/BottomSheetFragment.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/fragment/components/bottomsheet/BottomSheetFragment.kt @@ -15,10 +15,10 @@ enum class BottomSheetType { NONE, LIST, DOUBLE_LIST, - DATE, GRID, EDIT_TEXT, DOUBLE_EDIT_TEXT, + SUM } interface BottomSheetDelegate { @@ -48,6 +48,7 @@ open class BottomSheetFragment : BottomSheetDialogFragment() { BottomSheetType.DOUBLE_LIST -> BottomSheetDoubleListFragment() BottomSheetType.EDIT_TEXT -> BottomSheetEditTextFragment() BottomSheetType.DOUBLE_EDIT_TEXT -> BottomSheetDoubleEditTextFragment() + BottomSheetType.SUM -> BottomSheetSumFragment() else -> BottomSheetFragment() } diff --git a/app/src/main/java/net/pokeranalytics/android/ui/fragment/components/bottomsheet/BottomSheetSumFragment.kt b/app/src/main/java/net/pokeranalytics/android/ui/fragment/components/bottomsheet/BottomSheetSumFragment.kt new file mode 100644 index 00000000..a4ed4188 --- /dev/null +++ b/app/src/main/java/net/pokeranalytics/android/ui/fragment/components/bottomsheet/BottomSheetSumFragment.kt @@ -0,0 +1,124 @@ +package net.pokeranalytics.android.ui.fragment.components.bottomsheet + +import android.os.Bundle +import android.text.InputType +import android.view.LayoutInflater +import android.view.View +import android.view.inputmethod.EditorInfo +import androidx.core.widget.addTextChangedListener +import kotlinx.android.synthetic.main.bottom_sheet_sum.* +import kotlinx.android.synthetic.main.fragment_bottom_sheet.view.* +import net.pokeranalytics.android.R +import net.pokeranalytics.android.util.round +import net.pokeranalytics.android.util.toCurrency + + +class BottomSheetSumFragment : BottomSheetFragment() { + + private var value = 0.0 + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + initData() + initUI() + } + + override fun onStart() { + super.onStart() + editText1.requestFocus() + } + + override fun getValue(): Any { + return value + } + + /** + * Init data + */ + private fun initData() { + } + + /** + * Init UI + */ + private fun initUI() { + + val data = getData() + + setAddButtonVisible(false) + + LayoutInflater.from(requireContext()).inflate(R.layout.bottom_sheet_sum, view?.bottomSheetContainer, true) + + if (data.size == 5) { + + // Default value for the sum + val currentDefaultValue = try { + data[2].defaultValue as Double + } catch (e: Exception) { + 0.0 + } + + currentValue.text = currentDefaultValue.toCurrency() + + // First value + val defaultValue1 = try { + data[0].defaultValue as Double + } catch (e: Exception) { + 0.0 + } + + button1.text = defaultValue1.toCurrency() + button1.visibility = if (defaultValue1 > 0) View.VISIBLE else View.GONE + button1.setOnClickListener { + bottomSheetDelegate.setValue(currentDefaultValue + defaultValue1, row) + dismiss() + } + + // Second value + val defaultValue2 = try { + data[1].defaultValue as Double + } catch (e: Exception) { + 0.0 + } + + button2.text = defaultValue2.toCurrency() + button2.visibility = if (defaultValue2 > 0) View.VISIBLE else View.GONE + button2.setOnClickListener { + bottomSheetDelegate.setValue(currentDefaultValue + defaultValue2, row) + dismiss() + } + + // First edit text + data[3].hint?.let { editText1.hint = getString(it) } + editText1.inputType = data[3].inputType ?: InputType.TYPE_CLASS_TEXT + editText1.addTextChangedListener { + val valueToAdd = try { + editText1.text.toString().toDouble() + } catch (e: Exception) { + 0.0 + } + editText2.setText((currentDefaultValue + valueToAdd).round()) + + } + + // Second edit text + data[4].hint?.let { editText2.hint = getString(it) } + editText2.inputType = data[4].inputType ?: InputType.TYPE_CLASS_TEXT + editText2.addTextChangedListener { + value = it.toString().toDouble() + } + + editText2.setOnEditorActionListener { v, actionId, event -> + if (actionId == EditorInfo.IME_ACTION_DONE) { + bottomSheetDelegate.setValue(value, row) + dismiss() + true + } else { + false + } + } + } + + } + +} \ No newline at end of file diff --git a/app/src/main/java/net/pokeranalytics/android/ui/view/RowRepresentable.kt b/app/src/main/java/net/pokeranalytics/android/ui/view/RowRepresentable.kt index ea66210d..ad03ba40 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/view/RowRepresentable.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/view/RowRepresentable.kt @@ -142,10 +142,11 @@ enum class SessionRow : RowRepresentable { override val bottomSheetType: BottomSheetType get() { return when (this) { + CASHED_OUT, BREAK_TIME -> BottomSheetType.EDIT_TEXT + BUY_IN, TIPS -> BottomSheetType.SUM BLINDS -> BottomSheetType.DOUBLE_EDIT_TEXT - GAME -> BottomSheetType.LIST - LOCATION -> BottomSheetType.LIST - BANKROLL -> BottomSheetType.LIST + GAME, LOCATION, BANKROLL -> BottomSheetType.LIST + TABLE_SIZE -> BottomSheetType.GRID COMMENT -> BottomSheetType.EDIT_TEXT else -> BottomSheetType.NONE } diff --git a/app/src/main/java/net/pokeranalytics/android/util/Extensions.kt b/app/src/main/java/net/pokeranalytics/android/util/Extensions.kt index b931034e..d4d37783 100644 --- a/app/src/main/java/net/pokeranalytics/android/util/Extensions.kt +++ b/app/src/main/java/net/pokeranalytics/android/util/Extensions.kt @@ -4,9 +4,21 @@ import android.widget.Toast import net.pokeranalytics.android.ui.activity.components.PokerAnalyticsActivity import net.pokeranalytics.android.ui.fragment.components.PokerAnalyticsFragment import java.text.DateFormat +import java.text.DecimalFormat import java.util.* +// Double + +fun Double.round(): String { + val formatter = DecimalFormat("##.##") + return formatter.format(this) +} + +fun Double.toCurrency(): String { + val formatter = DecimalFormat("##.##") + return "$ ${formatter.format(this)}" +} // Date diff --git a/app/src/main/res/layout/bottom_sheet_sum.xml b/app/src/main/res/layout/bottom_sheet_sum.xml new file mode 100644 index 00000000..52f9a318 --- /dev/null +++ b/app/src/main/res/layout/bottom_sheet_sum.xml @@ -0,0 +1,119 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index bab3dce8..a32609a1 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -92,14 +92,27 @@ @font/roboto + + + + +