Use viewmodel for reports

od
Laurent 6 years ago
parent 7df72426c4
commit 10c601281d
  1. 24
      app/src/main/java/net/pokeranalytics/android/ui/activity/ComparisonReportActivity.kt
  2. 35
      app/src/main/java/net/pokeranalytics/android/ui/activity/ProgressReportActivity.kt
  3. 24
      app/src/main/java/net/pokeranalytics/android/ui/activity/TableReportActivity.kt
  4. 29
      app/src/main/java/net/pokeranalytics/android/ui/activity/components/ReportActivity.kt
  5. 25
      app/src/main/java/net/pokeranalytics/android/ui/fragment/components/bottomsheet/BottomSheetFragment.kt
  6. 51
      app/src/main/java/net/pokeranalytics/android/ui/fragment/report/AbstractReportFragment.kt
  7. 15
      app/src/main/java/net/pokeranalytics/android/ui/fragment/report/ComparisonReportFragment.kt
  8. 30
      app/src/main/java/net/pokeranalytics/android/ui/fragment/report/ProgressReportFragment.kt
  9. 10
      app/src/main/java/net/pokeranalytics/android/ui/fragment/report/TableReportFragment.kt
  10. 20
      app/src/main/java/net/pokeranalytics/android/ui/viewmodel/ReportViewModel.kt

@ -19,17 +19,19 @@ class ComparisonReportActivity : ReportActivity() {
*/
private fun initUI() {
parameters?.let {
val report = it.report
val title = it.title
val fragmentTransaction = supportFragmentManager.beginTransaction()
val reportDetailsFragment = ComparisonReportFragment.newInstance(report, title)
fragmentTransaction.add(R.id.reportDetailsContainer, reportDetailsFragment)
fragmentTransaction.commit()
}
parameters = null
val fragmentTransaction = supportFragmentManager.beginTransaction()
val reportDetailsFragment = ComparisonReportFragment.newInstance()
fragmentTransaction.add(R.id.reportDetailsContainer, reportDetailsFragment)
fragmentTransaction.commit()
//
// parameters?.let {
//
// val report = it.report
// val title = it.title
//
// }
// parameters = null
}

@ -7,29 +7,27 @@ import androidx.fragment.app.Fragment
import net.pokeranalytics.android.R
import net.pokeranalytics.android.calculus.Report
import net.pokeranalytics.android.calculus.Stat
import net.pokeranalytics.android.exceptions.PAIllegalStateException
import net.pokeranalytics.android.ui.activity.components.ReportActivity
import net.pokeranalytics.android.ui.activity.components.ReportParameters
import net.pokeranalytics.android.ui.activity.components.RequestCode
import net.pokeranalytics.android.ui.fragment.report.ProgressReportFragment
import net.pokeranalytics.android.ui.viewmodel.ReportViewModel
class ProgressReportActivity : ReportActivity() {
companion object {
// Unparcel fails when setting a custom Parcelable object on Entry so we use a static reference to passe objects
/**
* Default constructor
*/
fun newInstance(context: Context, report: Report, title: String, stat: Stat? = null, displayAggregationChoices: Boolean = true) {
parameters = ReportParameters(report, title, stat, showAggregationChoices = displayAggregationChoices)
ReportViewModel.parameters = ReportParameters(report, title, stat, showAggregationChoices = displayAggregationChoices)
val intent = Intent(context, ProgressReportActivity::class.java)
context.startActivity(intent)
}
fun newInstanceForResult(fragment: Fragment, report: Report, title: String, stat: Stat? = null, displayAggregationChoices: Boolean = true) {
parameters = ReportParameters(report, title, stat, showAggregationChoices = displayAggregationChoices)
ReportViewModel.parameters = ReportParameters(report, title, stat, showAggregationChoices = displayAggregationChoices)
val intent = Intent(fragment.context, ProgressReportActivity::class.java)
fragment.startActivityForResult(intent, RequestCode.DEFAULT.value)
}
@ -47,17 +45,26 @@ class ProgressReportActivity : ReportActivity() {
*/
private fun initUI() {
val statisticDetailsFragment = ProgressReportFragment()
parameters?.let {
val report = it.report
val stat = it.stat ?: report.options.stats.first()
statisticDetailsFragment.setData(report, stat, it.showAggregationChoices, it.title)
parameters = null
} ?: run {
throw PAIllegalStateException("Request to show Progress Activity with null ReportParameters")
ReportViewModel.parameters?.let {
this.viewModel.report = it.report
this.viewModel.title = it.title
this.viewModel.stat = it.stat ?: it.report.options.stats.first()
this.viewModel.showAggregationChoices = it.showAggregationChoices
}
ReportViewModel.parameters = null
//
// parameters?.let {
// val report = it.report
// val stat = it.stat ?: report.options.stats.first()
// statisticDetailsFragment.setData(report, stat, it.showAggregationChoices, it.title)
// parameters = null
// } ?: run {
// throw PAIllegalStateException("Request to show Progress Activity with null ReportParameters")
// }
val statisticDetailsFragment = ProgressReportFragment()
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.add(R.id.statisticDetailsContainer, statisticDetailsFragment)
fragmentTransaction.commit()

@ -18,18 +18,18 @@ class TableReportActivity : ReportActivity() {
*/
private fun initUI() {
parameters?.let {
val report = it.report
val title = it.title
val fragmentTransaction = supportFragmentManager.beginTransaction()
val fragment = TableReportFragment.newInstance(report, title)
fragmentTransaction.add(R.id.reportDetailsContainer, fragment)
fragmentTransaction.commit()
}
parameters = null
// parameters?.let {
//
// val report = it.report
// val title = it.title
//
// }
// parameters = null
val fragmentTransaction = supportFragmentManager.beginTransaction()
val fragment = TableReportFragment.newInstance()
fragmentTransaction.add(R.id.reportDetailsContainer, fragment)
fragmentTransaction.commit()
}
}

@ -2,36 +2,55 @@ package net.pokeranalytics.android.ui.activity.components
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import net.pokeranalytics.android.calculus.Report
import net.pokeranalytics.android.calculus.Stat
import net.pokeranalytics.android.ui.viewmodel.ReportViewModel
class ReportParameters(var report: Report, var title: String, var stat: Stat? = null, var showAggregationChoices: Boolean = true)
abstract class ReportActivity : PokerAnalyticsActivity() {
companion object {
protected val viewModel: ReportViewModel by lazy {
ViewModelProviders.of(this).get(ReportViewModel::class.java)
}
// Unparcel fails when setting a custom Parcelable object on Entry so we use a static reference to passe objects
var parameters: ReportParameters? = null
companion object {
/**
* Default constructor
*/
fun newInstance(context: Context, report: Report, reportTitle: String, stat: Stat? = null) {
val options = report.options
this.parameters = ReportParameters(report, reportTitle, stat)
ReportViewModel.parameters = ReportParameters(report, reportTitle, stat)
val intent = Intent(context, options.display.activityClass)
context.startActivity(intent)
}
fun newInstanceForResult(fragment: Fragment, report: Report, reportTitle: String, stat: Stat? = null) {
val options = report.options
this.parameters = ReportParameters(report, reportTitle, stat)
ReportViewModel.parameters = ReportParameters(report, reportTitle, stat)
val intent = Intent(fragment.requireContext(), options.display.activityClass)
fragment.startActivityForResult(intent, RequestCode.DEFAULT.value)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
ReportViewModel.parameters?.let {
this.viewModel.report = it.report
this.viewModel.title = it.title
this.viewModel.stat = it.stat
this.viewModel.showAggregationChoices = it.showAggregationChoices
}
ReportViewModel.parameters = null
}
}

@ -11,6 +11,7 @@ import android.view.ViewGroup
import android.view.WindowManager
import androidx.appcompat.view.ContextThemeWrapper
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.ViewModel
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import io.realm.RealmModel
import kotlinx.android.synthetic.main.fragment_bottom_sheet.*
@ -26,6 +27,17 @@ import net.pokeranalytics.android.ui.view.rowrepresentable.SessionRow
import net.pokeranalytics.android.ui.view.rowrepresentable.TransactionRow
import java.util.*
class BottomSheetViewModel(var row: RowRepresentable,
var delegate: RowRepresentableDelegate,
var currentCurrency: Currency? = null,
var valueHasPlaceholder: Boolean = false,
var isClearable: Boolean = true,
var isDeletable: Boolean = false,
var rowRepresentableEditDescriptors: ArrayList<RowRepresentableEditDescriptor>? = null)
: ViewModel() {
}
open class BottomSheetFragment : BottomSheetDialogFragment() {
lateinit var row: RowRepresentable
@ -38,6 +50,19 @@ open class BottomSheetFragment : BottomSheetDialogFragment() {
private var isDeletable: Boolean = false
private var rowRepresentableEditDescriptors: ArrayList<RowRepresentableEditDescriptor>? = null
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
}
companion object {
const val REQUEST_CODE_ADD_NEW_OBJECT = 100

@ -7,61 +7,48 @@ import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.fragment_progress_report.*
import net.pokeranalytics.android.calculus.Calculator
import androidx.lifecycle.ViewModelProviders
import net.pokeranalytics.android.calculus.Report
import net.pokeranalytics.android.calculus.Stat
import net.pokeranalytics.android.exceptions.PAIllegalStateException
import net.pokeranalytics.android.model.CustomFieldCriteria
import net.pokeranalytics.android.model.LiveData
import net.pokeranalytics.android.model.realm.ReportSetup
import net.pokeranalytics.android.ui.fragment.data.DataManagerFragment
import net.pokeranalytics.android.ui.viewmodel.ReportViewModel
import net.pokeranalytics.android.util.extensions.findById
abstract class AbstractReportFragment : DataManagerFragment() {
private lateinit var _selectedReport: Report
protected val viewModel: ReportViewModel by lazy {
ViewModelProviders.of(requireActivity()).get(ReportViewModel::class.java)
}
val selectedReport: Report
get() {
return this._selectedReport
return this.viewModel.report!!
}
fun setReport(report: Report) {
this._selectedReport = report
this.primaryKey = report.options.reportSetupId
val stat: Stat
get() {
return this.viewModel.stat!!
}
protected var reportTitle: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
this.primaryKey = this.selectedReport.options.reportSetupId
this.liveDataType = LiveData.REPORT_SETUP
this.deleteButtonShouldAppear = (this.primaryKey != null)
}
override fun onStart() {
super.onStart()
// we don't want to use this._selectedReport before onActivityCreated could initialize the variable if necessary
this.saveButtonShouldAppear = this._selectedReport.options.userGenerated
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
if (!this::_selectedReport.isInitialized) {
this._selectedReport = Report(Calculator.Options())
}
this.saveButtonShouldAppear = this.selectedReport.options.userGenerated
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setDisplayHomeAsUpEnabled(true)
setToolbarTitle(reportTitle)
setToolbarTitle(this.viewModel.title)
}
override fun saveData() {
@ -114,12 +101,13 @@ abstract class AbstractReportFragment : DataManagerFragment() {
private fun saveReport(name: String) {
this.viewModel.title = name
val rs = this.item as ReportSetup
getRealm().executeTransaction { realm ->
val rs = this.item as ReportSetup
val firstSave = (this.primaryKey == null)
if (firstSave) {
val options = this._selectedReport.options
val options = this.selectedReport.options
rs.name = name
rs.display = options.display.ordinal
options.stats.forEach {
@ -141,12 +129,11 @@ abstract class AbstractReportFragment : DataManagerFragment() {
realm.insertOrUpdate(rs)
}
this.primaryKey = rs.id
this.deleteButtonShouldAppear = true
toolbar.title = name
}
this.primaryKey = rs.id
this.deleteButtonShouldAppear = true
setToolbarTitle(this.viewModel.title)
}
}

@ -7,30 +7,20 @@ import android.view.ViewGroup
import com.google.android.material.tabs.TabLayout
import kotlinx.android.synthetic.main.fragment_report_details.*
import net.pokeranalytics.android.R
import net.pokeranalytics.android.calculus.Report
import net.pokeranalytics.android.ui.adapter.ReportPagerAdapter
class ComparisonReportFragment : AbstractReportFragment() {
companion object {
fun newInstance(report: Report, reportTitle: String): ComparisonReportFragment {
fun newInstance(): ComparisonReportFragment {
val fragment = ComparisonReportFragment()
fragment.reportTitle = reportTitle
fragment.setReport(report)
val bundle = Bundle()
fragment.arguments = bundle
return fragment
}
}
/**
* Set data
*/
// fun setData(report: Report) {
// this.setReport(report)
// }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
super.onCreateView(inflater, container, savedInstanceState)
return inflater.inflate(R.layout.fragment_report_details, container, false)
@ -46,8 +36,7 @@ class ComparisonReportFragment : AbstractReportFragment() {
*/
private fun initUI() {
setDisplayHomeAsUpEnabled(true)
setToolbarTitle(reportTitle)
// setDisplayHomeAsUpEnabled(true)
parentActivity?.let {
val reportPagerAdapter = ReportPagerAdapter(requireContext(), it.supportFragmentManager, selectedReport)

@ -48,8 +48,8 @@ class ProgressReportFragment : AbstractReportFragment() {
private lateinit var graphFragment: GraphFragment
private var reports: MutableMap<AggregationType, Report> = hashMapOf()
private var stat: Stat = Stat.NET_RESULT
private var displayAggregationChoices: Boolean = true
// private var stat: Stat = Stat.NET_RESULT
// private var displayAggregationChoices: Boolean = true
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
super.onCreateView(inflater, container, savedInstanceState)
@ -64,16 +64,14 @@ class ProgressReportFragment : AbstractReportFragment() {
/**
* Set data
*/
fun setData(report: Report, stat: Stat, displayAggregationChoices: Boolean, title: String) {
this.stat = stat
this.setReport(report)
this.displayAggregationChoices = displayAggregationChoices
this.reportTitle = title
stat.aggregationTypes.firstOrNull()?.let {
reports[it] = report
}
}
// fun setData(report: Report, stat: Stat, displayAggregationChoices: Boolean, title: String) {
// this.stat = stat
// this.setReport(report)
// this.displayAggregationChoices = displayAggregationChoices
// this.reportTitle = title
//
// }
/**
* Init UI
@ -87,7 +85,11 @@ class ProgressReportFragment : AbstractReportFragment() {
fragmentTransaction?.add(R.id.graphContainer, graphFragment)
fragmentTransaction?.commit()
stat.aggregationTypes.firstOrNull()?.let { aggregationType ->
this.stat.aggregationTypes.firstOrNull()?.let {
reports[it] = this.selectedReport
}
this.stat.aggregationTypes.firstOrNull()?.let { aggregationType ->
reports[aggregationType]?.let { report ->
setGraphData(report, aggregationType)
}
@ -104,7 +106,7 @@ class ProgressReportFragment : AbstractReportFragment() {
this.chipGroup.addView(chip)
}
this.chipGroup.isVisible = displayAggregationChoices
this.chipGroup.isVisible = this.viewModel.showAggregationChoices
this.chipGroup.isSingleSelection = true
this.chipGroup.check(0)

@ -5,7 +5,6 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import net.pokeranalytics.android.R
import net.pokeranalytics.android.calculus.Report
class TableReportFragment : AbstractReportFragment() {
@ -13,10 +12,10 @@ class TableReportFragment : AbstractReportFragment() {
companion object {
fun newInstance(report: Report, title: String): TableReportFragment {
fun newInstance(): TableReportFragment {
val fragment = TableReportFragment()
fragment.reportTitle = title
fragment.setReport(report)
// fragment.reportTitle = title
// fragment.setReport(report)
val bundle = Bundle()
fragment.arguments = bundle
return fragment
@ -35,9 +34,6 @@ class TableReportFragment : AbstractReportFragment() {
private fun initUI() {
setDisplayHomeAsUpEnabled(true)
setToolbarTitle(reportTitle)
val fragmentTransaction = parentActivity?.supportFragmentManager?.beginTransaction()
val fragment = ComposableTableReportFragment.newInstance(this.selectedReport)
fragmentTransaction?.add(R.id.tableReportContainer, fragment)

@ -0,0 +1,20 @@
package net.pokeranalytics.android.ui.viewmodel
import androidx.lifecycle.ViewModel
import net.pokeranalytics.android.calculus.Report
import net.pokeranalytics.android.calculus.Stat
import net.pokeranalytics.android.ui.activity.components.ReportParameters
class ReportViewModel : ViewModel() {
companion object {
// Unparcel fails when setting a custom Parcelable object on Entry so we use a static reference to passe objects
var parameters: ReportParameters? = null
}
var report: Report? = null
var title: String = ""
var stat: Stat? = null
var showAggregationChoices: Boolean = true
}
Loading…
Cancel
Save