Skip to content

Commit

Permalink
IntroFragments: use (factory,order) List instead of (order,factory) M…
Browse files Browse the repository at this point in the history
…ap to store them
  • Loading branch information
rfc2822 committed Oct 17, 2023
1 parent 8ffed42 commit 2c08b1d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,10 @@ class AccountsActivity: AppCompatActivity(), NavigationView.OnNavigationItemSele
super.onCreate(savedInstanceState)

if (savedInstanceState == null) {
// use a separate thread to check whether IntroActivity should be shown
CoroutineScope(Dispatchers.Default).launch {
// use a separate thread to check whether IntroActivity should be shown
if (IntroActivity.shouldShowIntroActivity(this@AccountsActivity)) {
if (IntroActivity.shouldShowIntroActivity(this@AccountsActivity))
introActivityLauncher.launch(null)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,4 @@ class BatteryOptimizationsFragment: Fragment() {
override fun create() = BatteryOptimizationsFragment()
}

}
}
25 changes: 15 additions & 10 deletions app/src/main/kotlin/at/bitfire/davdroid/ui/intro/IntroActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.content.Intent
import android.os.Bundle
import androidx.activity.result.contract.ActivityResultContract
import androidx.activity.addCallback
import androidx.annotation.WorkerThread
import androidx.core.content.res.ResourcesCompat
import androidx.fragment.app.Fragment
import at.bitfire.davdroid.R
Expand All @@ -33,32 +34,36 @@ class IntroActivity: AppIntro2() {

companion object {

@WorkerThread
fun shouldShowIntroActivity(activity: Activity): Boolean {
val factories = EntryPointAccessors.fromActivity(activity, IntroActivityEntryPoint::class.java).introFragmentFactories()
return factories.any {
val order = it.getOrder(activity)
Logger.log.fine("Found intro fragment factory ${it::class.java} with order $order")
order > 0
it.getOrder(activity) > 0
}
}

}

private var currentSlide = 0

@Inject lateinit var introFragmentFactories: Set<@JvmSuppressWildcards IntroFragmentFactory>


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val factoriesWithOrder = introFragmentFactories
.associateBy { it.getOrder(this) }
.filterKeys { it != IntroFragmentFactory.DONT_SHOW }
val factories = EntryPointAccessors.fromActivity(this, IntroActivityEntryPoint::class.java).introFragmentFactories()
for (factory in factories)
Logger.log.fine("Found intro fragment factory ${factory::class.java} with order ${factory.getOrder(this)}")

val factoriesWithOrder = factories
.associateWith { it.getOrder(this) }
.filterValues { it != IntroFragmentFactory.DONT_SHOW }

val anyPositiveOrder = factoriesWithOrder.keys.any { it > 0 }
val anyPositiveOrder = factoriesWithOrder.values.any { it > 0 }
if (anyPositiveOrder) {
for ((_, factory) in factoriesWithOrder.toSortedMap())
val factoriesSortedByOrder = factoriesWithOrder
.toList()
.sortedBy { (_, v) -> v } // sort by value (= getOrder())
for ((factory, _) in factoriesSortedByOrder)
addSlide(factory.create())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface IntroFragmentFactory {
* @return Order with which an instance of this fragment type shall be created and shown. Possible values:
*
* * <0: only show the fragment when there is at least one other fragment with positive order (lower numbers are shown first)
* * 0: don't show the fragment
* * [DONT_SHOW] (0): don't show the fragment
* * ≥0: show the fragment (lower numbers are shown first)
*/
fun getOrder(context: Context): Int
Expand Down

0 comments on commit 2c08b1d

Please sign in to comment.