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() { private fun initUI() {
parameters?.let { val fragmentTransaction = supportFragmentManager.beginTransaction()
val reportDetailsFragment = ComparisonReportFragment.newInstance()
val report = it.report fragmentTransaction.add(R.id.reportDetailsContainer, reportDetailsFragment)
val title = it.title fragmentTransaction.commit()
val fragmentTransaction = supportFragmentManager.beginTransaction() //
val reportDetailsFragment = ComparisonReportFragment.newInstance(report, title) // parameters?.let {
fragmentTransaction.add(R.id.reportDetailsContainer, reportDetailsFragment) //
fragmentTransaction.commit() // val report = it.report
} // val title = it.title
parameters = null //
// }
// parameters = null
} }

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

@ -18,18 +18,18 @@ class TableReportActivity : ReportActivity() {
*/ */
private fun initUI() { private fun initUI() {
parameters?.let { // parameters?.let {
//
val report = it.report // val report = it.report
val title = it.title // val title = it.title
//
val fragmentTransaction = supportFragmentManager.beginTransaction() // }
val fragment = TableReportFragment.newInstance(report, title) // parameters = null
fragmentTransaction.add(R.id.reportDetailsContainer, fragment)
fragmentTransaction.commit() val fragmentTransaction = supportFragmentManager.beginTransaction()
} val fragment = TableReportFragment.newInstance()
parameters = null 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.Context
import android.content.Intent import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import net.pokeranalytics.android.calculus.Report import net.pokeranalytics.android.calculus.Report
import net.pokeranalytics.android.calculus.Stat 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) class ReportParameters(var report: Report, var title: String, var stat: Stat? = null, var showAggregationChoices: Boolean = true)
abstract class ReportActivity : PokerAnalyticsActivity() { 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 companion object {
var parameters: ReportParameters? = null
/** /**
* Default constructor * Default constructor
*/ */
fun newInstance(context: Context, report: Report, reportTitle: String, stat: Stat? = null) { fun newInstance(context: Context, report: Report, reportTitle: String, stat: Stat? = null) {
val options = report.options val options = report.options
this.parameters = ReportParameters(report, reportTitle, stat) ReportViewModel.parameters = ReportParameters(report, reportTitle, stat)
val intent = Intent(context, options.display.activityClass) val intent = Intent(context, options.display.activityClass)
context.startActivity(intent) context.startActivity(intent)
} }
fun newInstanceForResult(fragment: Fragment, report: Report, reportTitle: String, stat: Stat? = null) { fun newInstanceForResult(fragment: Fragment, report: Report, reportTitle: String, stat: Stat? = null) {
val options = report.options val options = report.options
this.parameters = ReportParameters(report, reportTitle, stat) ReportViewModel.parameters = ReportParameters(report, reportTitle, stat)
val intent = Intent(fragment.requireContext(), options.display.activityClass) val intent = Intent(fragment.requireContext(), options.display.activityClass)
fragment.startActivityForResult(intent, RequestCode.DEFAULT.value) 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 android.view.WindowManager
import androidx.appcompat.view.ContextThemeWrapper import androidx.appcompat.view.ContextThemeWrapper
import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentManager
import androidx.lifecycle.ViewModel
import com.google.android.material.bottomsheet.BottomSheetDialogFragment import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import io.realm.RealmModel import io.realm.RealmModel
import kotlinx.android.synthetic.main.fragment_bottom_sheet.* 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 net.pokeranalytics.android.ui.view.rowrepresentable.TransactionRow
import java.util.* 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() { open class BottomSheetFragment : BottomSheetDialogFragment() {
lateinit var row: RowRepresentable lateinit var row: RowRepresentable
@ -38,6 +50,19 @@ open class BottomSheetFragment : BottomSheetDialogFragment() {
private var isDeletable: Boolean = false private var isDeletable: Boolean = false
private var rowRepresentableEditDescriptors: ArrayList<RowRepresentableEditDescriptor>? = null private var rowRepresentableEditDescriptors: ArrayList<RowRepresentableEditDescriptor>? = null
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
}
companion object { companion object {
const val REQUEST_CODE_ADD_NEW_OBJECT = 100 const val REQUEST_CODE_ADD_NEW_OBJECT = 100

@ -7,61 +7,48 @@ import android.view.inputmethod.InputMethodManager
import android.widget.EditText import android.widget.EditText
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.fragment_progress_report.* import androidx.lifecycle.ViewModelProviders
import net.pokeranalytics.android.calculus.Calculator
import net.pokeranalytics.android.calculus.Report import net.pokeranalytics.android.calculus.Report
import net.pokeranalytics.android.calculus.Stat
import net.pokeranalytics.android.exceptions.PAIllegalStateException import net.pokeranalytics.android.exceptions.PAIllegalStateException
import net.pokeranalytics.android.model.CustomFieldCriteria import net.pokeranalytics.android.model.CustomFieldCriteria
import net.pokeranalytics.android.model.LiveData import net.pokeranalytics.android.model.LiveData
import net.pokeranalytics.android.model.realm.ReportSetup import net.pokeranalytics.android.model.realm.ReportSetup
import net.pokeranalytics.android.ui.fragment.data.DataManagerFragment import net.pokeranalytics.android.ui.fragment.data.DataManagerFragment
import net.pokeranalytics.android.ui.viewmodel.ReportViewModel
import net.pokeranalytics.android.util.extensions.findById import net.pokeranalytics.android.util.extensions.findById
abstract class AbstractReportFragment : DataManagerFragment() { 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 val selectedReport: Report
get() { get() {
return this._selectedReport return this.viewModel.report!!
} }
fun setReport(report: Report) { val stat: Stat
this._selectedReport = report get() {
this.primaryKey = report.options.reportSetupId return this.viewModel.stat!!
} }
protected var reportTitle: String? = null
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
this.primaryKey = this.selectedReport.options.reportSetupId
this.liveDataType = LiveData.REPORT_SETUP this.liveDataType = LiveData.REPORT_SETUP
this.deleteButtonShouldAppear = (this.primaryKey != null) this.deleteButtonShouldAppear = (this.primaryKey != null)
} this.saveButtonShouldAppear = this.selectedReport.options.userGenerated
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())
}
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
setDisplayHomeAsUpEnabled(true) setDisplayHomeAsUpEnabled(true)
setToolbarTitle(reportTitle) setToolbarTitle(this.viewModel.title)
} }
override fun saveData() { override fun saveData() {
@ -114,12 +101,13 @@ abstract class AbstractReportFragment : DataManagerFragment() {
private fun saveReport(name: String) { private fun saveReport(name: String) {
this.viewModel.title = name
val rs = this.item as ReportSetup
getRealm().executeTransaction { realm -> getRealm().executeTransaction { realm ->
val rs = this.item as ReportSetup
val firstSave = (this.primaryKey == null) val firstSave = (this.primaryKey == null)
if (firstSave) { if (firstSave) {
val options = this._selectedReport.options val options = this.selectedReport.options
rs.name = name rs.name = name
rs.display = options.display.ordinal rs.display = options.display.ordinal
options.stats.forEach { options.stats.forEach {
@ -141,12 +129,11 @@ abstract class AbstractReportFragment : DataManagerFragment() {
realm.insertOrUpdate(rs) 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 com.google.android.material.tabs.TabLayout
import kotlinx.android.synthetic.main.fragment_report_details.* import kotlinx.android.synthetic.main.fragment_report_details.*
import net.pokeranalytics.android.R import net.pokeranalytics.android.R
import net.pokeranalytics.android.calculus.Report
import net.pokeranalytics.android.ui.adapter.ReportPagerAdapter import net.pokeranalytics.android.ui.adapter.ReportPagerAdapter
class ComparisonReportFragment : AbstractReportFragment() { class ComparisonReportFragment : AbstractReportFragment() {
companion object { companion object {
fun newInstance(report: Report, reportTitle: String): ComparisonReportFragment { fun newInstance(): ComparisonReportFragment {
val fragment = ComparisonReportFragment() val fragment = ComparisonReportFragment()
fragment.reportTitle = reportTitle
fragment.setReport(report)
val bundle = Bundle() val bundle = Bundle()
fragment.arguments = bundle fragment.arguments = bundle
return fragment return fragment
} }
} }
/**
* Set data
*/
// fun setData(report: Report) {
// this.setReport(report)
// }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
super.onCreateView(inflater, container, savedInstanceState) super.onCreateView(inflater, container, savedInstanceState)
return inflater.inflate(R.layout.fragment_report_details, container, false) return inflater.inflate(R.layout.fragment_report_details, container, false)
@ -46,8 +36,7 @@ class ComparisonReportFragment : AbstractReportFragment() {
*/ */
private fun initUI() { private fun initUI() {
setDisplayHomeAsUpEnabled(true) // setDisplayHomeAsUpEnabled(true)
setToolbarTitle(reportTitle)
parentActivity?.let { parentActivity?.let {
val reportPagerAdapter = ReportPagerAdapter(requireContext(), it.supportFragmentManager, selectedReport) val reportPagerAdapter = ReportPagerAdapter(requireContext(), it.supportFragmentManager, selectedReport)

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

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