Attempts to fix lifecycle crash

od
Laurent 6 years ago
parent 72386d0dc6
commit 52b43cddf8
  1. 2
      app/src/main/java/net/pokeranalytics/android/ui/activity/ComparisonReportActivity.kt
  2. 2
      app/src/main/java/net/pokeranalytics/android/ui/activity/ProgressReportActivity.kt
  3. 10
      app/src/main/java/net/pokeranalytics/android/ui/activity/TableReportActivity.kt
  4. 33
      app/src/main/java/net/pokeranalytics/android/ui/activity/components/ReportActivity.kt
  5. 11
      app/src/main/java/net/pokeranalytics/android/ui/fragment/report/AbstractReportFragment.kt
  6. 4
      app/src/main/java/net/pokeranalytics/android/ui/fragment/report/ComparisonReportFragment.kt
  7. 4
      app/src/main/java/net/pokeranalytics/android/ui/fragment/report/ProgressReportFragment.kt
  8. 4
      app/src/main/java/net/pokeranalytics/android/ui/fragment/report/TableReportFragment.kt
  9. 25
      app/src/main/java/net/pokeranalytics/android/ui/viewmodel/ReportViewModel.kt

@ -20,7 +20,7 @@ class ComparisonReportActivity : ReportActivity() {
private fun initUI() {
val fragmentTransaction = supportFragmentManager.beginTransaction()
val reportDetailsFragment = ComparisonReportFragment.newInstance()
val reportDetailsFragment = ComparisonReportFragment.newInstance(this.viewModelFactory)
fragmentTransaction.add(R.id.reportDetailsContainer, reportDetailsFragment)
fragmentTransaction.commit()

@ -46,7 +46,7 @@ class ProgressReportActivity : ReportActivity() {
*/
private fun initUI() {
val progressFragment = ProgressReportFragment()
val progressFragment = ProgressReportFragment.newInstance(this.viewModelFactory)
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.add(R.id.statisticDetailsContainer, progressFragment)
fragmentTransaction.commit()

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

@ -5,17 +5,27 @@ import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import com.crashlytics.android.Crashlytics
import net.pokeranalytics.android.calculus.Report
import net.pokeranalytics.android.calculus.Stat
import net.pokeranalytics.android.exceptions.PAIllegalStateException
import net.pokeranalytics.android.ui.viewmodel.ReportViewModel
import net.pokeranalytics.android.ui.viewmodel.ReportViewModelFactory
class ReportParameters(var report: Report, var title: String, var stat: Stat? = null, var showAggregationChoices: Boolean = true)
abstract class ReportActivity : PokerAnalyticsActivity() {
protected val viewModelFactory: ReportViewModelFactory by lazy {
ReportViewModel.parameters?.let {
val stat = it.stat ?: it.report.options.stats.first()
ReportViewModelFactory(it.report, stat, it.title, it.showAggregationChoices)
} ?: run {
throw PAIllegalStateException("null report parameters")
}
}
protected val viewModel: ReportViewModel by lazy {
ViewModelProviders.of(this).get(ReportViewModel::class.java)
ViewModelProviders.of(this, viewModelFactory).get(ReportViewModel::class.java)
}
companion object {
@ -43,19 +53,24 @@ abstract class ReportActivity : PokerAnalyticsActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
this.initViewModelWithParameters()
// this.initViewModelWithParameters()
}
override fun onStart() {
super.onStart()
ReportViewModel.resetParameters()
}
private fun initViewModelWithParameters() {
ReportViewModel.parameters?.let {
this.viewModel.report = it.report
this.viewModel.title = it.title
val stat = it.stat ?: it.report.options.stats.first()
this.viewModel.stat = stat
this.viewModel.showAggregationChoices = it.showAggregationChoices
// this.viewModel.report = it.report
// this.viewModel.title = it.title
// val stat = it.stat ?: it.report.options.stats.first()
// this.viewModel.stat = stat
// this.viewModel.showAggregationChoices = it.showAggregationChoices
Crashlytics.log("initViewModelWithParameters, stat = ${stat.uniqueIdentifier}")
// Crashlytics.log("initViewModelWithParameters, stat = ${stat.uniqueIdentifier}")
ReportViewModel.resetParameters()
}

@ -16,23 +16,28 @@ 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.ui.viewmodel.ReportViewModelFactory
import net.pokeranalytics.android.util.extensions.findById
abstract class AbstractReportFragment : DataManagerFragment() {
protected var viewModelFactory: ReportViewModelFactory? = null
protected val viewModel: ReportViewModel by lazy {
ViewModelProviders.of(requireActivity()).get(ReportViewModel::class.java)
this.viewModelFactory?.let {
ViewModelProviders.of(requireActivity(), it).get(ReportViewModel::class.java)
} ?: throw PAIllegalStateException("no factory")
}
val selectedReport: Report
get() {
return this.viewModel.report!!
return this.viewModel.report
}
val stat: Stat
get() {
return this.viewModel.stat!!
return this.viewModel.stat
}
override fun onCreate(savedInstanceState: Bundle?) {

@ -8,13 +8,15 @@ import com.google.android.material.tabs.TabLayout
import kotlinx.android.synthetic.main.fragment_report_details.*
import net.pokeranalytics.android.R
import net.pokeranalytics.android.ui.adapter.ReportPagerAdapter
import net.pokeranalytics.android.ui.viewmodel.ReportViewModelFactory
class ComparisonReportFragment : AbstractReportFragment() {
companion object {
fun newInstance(): ComparisonReportFragment {
fun newInstance(viewModelFactory: ReportViewModelFactory): ComparisonReportFragment {
val fragment = ComparisonReportFragment()
fragment.viewModelFactory = viewModelFactory
val bundle = Bundle()
fragment.arguments = bundle
return fragment

@ -26,6 +26,7 @@ import net.pokeranalytics.android.ui.extensions.hideWithAnimation
import net.pokeranalytics.android.ui.extensions.px
import net.pokeranalytics.android.ui.extensions.showWithAnimation
import net.pokeranalytics.android.ui.fragment.GraphFragment
import net.pokeranalytics.android.ui.viewmodel.ReportViewModelFactory
import timber.log.Timber
import java.util.*
@ -37,8 +38,9 @@ class ProgressReportFragment : AbstractReportFragment() {
/**
* Creates new instance
*/
fun newInstance(): ProgressReportFragment {
fun newInstance(viewModelFactory: ReportViewModelFactory): ProgressReportFragment {
val fragment = ProgressReportFragment()
fragment.viewModelFactory = viewModelFactory
val bundle = Bundle()
fragment.arguments = bundle
return fragment

@ -5,6 +5,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import net.pokeranalytics.android.R
import net.pokeranalytics.android.ui.viewmodel.ReportViewModelFactory
class TableReportFragment : AbstractReportFragment() {
@ -12,8 +13,9 @@ class TableReportFragment : AbstractReportFragment() {
companion object {
fun newInstance(): TableReportFragment {
fun newInstance(viewModelFactory: ReportViewModelFactory): TableReportFragment {
val fragment = TableReportFragment()
fragment.viewModelFactory = viewModelFactory
// fragment.reportTitle = title
// fragment.setReport(report)
val bundle = Bundle()

@ -1,11 +1,30 @@
package net.pokeranalytics.android.ui.viewmodel
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import net.pokeranalytics.android.calculus.Report
import net.pokeranalytics.android.calculus.Stat
import net.pokeranalytics.android.ui.activity.components.ReportParameters
class ReportViewModel : ViewModel() {
class ReportViewModelFactory(val report: Report,
val stat: Stat,
val title: String,
val showAggregationChoices: Boolean = true) : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return ReportViewModel(report, stat, title, showAggregationChoices) as T
}
}
class ReportViewModel(
var report: Report,
var stat: Stat,
var title: String,
var showAggregationChoices: Boolean = true) : ViewModel() {
// var report: Report? = null
// var title: String = ""
// var stat: Stat? = null
// var showAggregationChoices: Boolean = true
companion object {
@ -27,9 +46,5 @@ class ReportViewModel : ViewModel() {
}
var report: Report? = null
var title: String = ""
var stat: Stat? = null
var showAggregationChoices: Boolean = true
}
Loading…
Cancel
Save