Skip to content

Commit

Permalink
Fix scorecard fragments getting duplicated when navigating back to th…
Browse files Browse the repository at this point in the history
…e scorecard after a screen rotation... Need to move towards using Lists/RecyclerView to show these dynamic UI elements rather than fragment transactions (GH issue #5)
  • Loading branch information
willieowens committed Aug 23, 2020
1 parent 5d9fc9c commit 285e27d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
10 changes: 6 additions & 4 deletions app/src/main/java/com/frolfr/ui/scorecard/ScorecardFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class ScorecardFragment : Fragment() {

private lateinit var binding: FragmentScorecardBinding

private var scorecardLoaded = false

private var roundId by Delegates.notNull<Int>()
private var courseName by Delegates.notNull<String>()
private var roundDate by Delegates.notNull<String>()
Expand Down Expand Up @@ -51,9 +49,8 @@ class ScorecardFragment : Fragment() {
binding.textRoundDate.text = roundDate

scorecardViewModel.scorecard.observe(viewLifecycleOwner, Observer {scorecard ->
if (savedInstanceState == null && !scorecardLoaded) {
if (savedInstanceState == null) {
loadContent(scorecard)
scorecardLoaded = true
}
})

Expand Down Expand Up @@ -81,6 +78,11 @@ class ScorecardFragment : Fragment() {
}

private fun loadContent(scorecard: Scorecard) {
if (childFragmentManager.fragments.isNotEmpty()) {
// Already loaded
return
}

val numSections = ceil(scorecard.holeMeta.size / 9.0).toInt()
val txn = childFragmentManager
.beginTransaction()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ class ScorecardSectionFragment : Fragment() {

private lateinit var binding: FragmentScorecardSectionBinding

private var sectionLoaded = false

private var roundId by Delegates.notNull<Int>()
private var sectionIndex by Delegates.notNull<Int>()

Expand Down Expand Up @@ -52,7 +50,7 @@ class ScorecardSectionFragment : Fragment() {
scorecardViewModel.toggleSectionVisibility(sectionIndex)
}

if (savedInstanceState == null && !sectionLoaded) {
if (savedInstanceState == null) {
loadSectionScores()
}

Expand All @@ -62,6 +60,11 @@ class ScorecardSectionFragment : Fragment() {
// TODO Would be nice if these scores updated (ie. clicking back from RoundReportingFragment)
// Probably easier adding Room as a write-through cache
private fun loadSectionScores() {
if (childFragmentManager.fragments.isNotEmpty()) {
// Already loaded
return
}

val txn = childFragmentManager.beginTransaction()

scorecardViewModel.scorecard.value?.users?.forEach { user ->
Expand All @@ -77,7 +80,5 @@ class ScorecardSectionFragment : Fragment() {
}

txn.commit()

sectionLoaded = true
}
}

0 comments on commit 285e27d

Please sign in to comment.