2019-01-24 22:07:52 +01:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2019-01-23 23:48:15 +01:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2019-01-24 22:07:52 +01:00
|
|
|
|
2019-01-23 22:39:53 +01:00
|
|
|
package org.mozilla.fenix.ext
|
|
|
|
|
2019-06-06 21:40:10 +02:00
|
|
|
import androidx.annotation.IdRes
|
2019-08-21 17:38:14 +02:00
|
|
|
import androidx.annotation.StringRes
|
2019-11-25 21:36:47 +01:00
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2019-06-06 21:40:10 +02:00
|
|
|
import androidx.fragment.app.Fragment
|
|
|
|
import androidx.navigation.NavDirections
|
|
|
|
import androidx.navigation.NavOptions
|
|
|
|
import androidx.navigation.Navigator
|
|
|
|
import androidx.navigation.fragment.NavHostFragment.findNavController
|
2020-05-29 14:00:38 +02:00
|
|
|
import androidx.navigation.fragment.findNavController
|
2020-07-06 22:04:43 +02:00
|
|
|
import org.mozilla.fenix.NavHostActivity
|
2020-05-29 14:00:38 +02:00
|
|
|
import org.mozilla.fenix.R
|
2019-01-23 22:39:53 +01:00
|
|
|
import org.mozilla.fenix.components.Components
|
|
|
|
|
2019-01-23 23:48:15 +01:00
|
|
|
/**
|
|
|
|
* Get the requireComponents of this application.
|
|
|
|
*/
|
2019-06-06 21:40:10 +02:00
|
|
|
val Fragment.requireComponents: Components
|
2019-01-30 17:36:14 +01:00
|
|
|
get() = requireContext().components
|
2019-06-06 21:40:10 +02:00
|
|
|
|
|
|
|
fun Fragment.nav(@IdRes id: Int?, directions: NavDirections) {
|
|
|
|
findNavController(this).nav(id, directions)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun Fragment.nav(@IdRes id: Int?, directions: NavDirections, extras: Navigator.Extras) {
|
|
|
|
findNavController(this).nav(id, directions, extras)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun Fragment.nav(@IdRes id: Int?, directions: NavDirections, options: NavOptions) {
|
|
|
|
findNavController(this).nav(id, directions, options)
|
|
|
|
}
|
2019-08-21 17:38:14 +02:00
|
|
|
|
|
|
|
fun Fragment.getPreferenceKey(@StringRes resourceId: Int): String = getString(resourceId)
|
2019-11-25 21:36:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays the activity toolbar with the given [title].
|
|
|
|
* Throws if the fragment is not attached to an [AppCompatActivity].
|
|
|
|
*/
|
|
|
|
fun Fragment.showToolbar(title: String) {
|
|
|
|
(requireActivity() as AppCompatActivity).title = title
|
2020-07-06 22:04:43 +02:00
|
|
|
(activity as NavHostActivity).getSupportActionBarAndInflateIfNecessary().show()
|
2019-11-25 21:36:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hides the activity toolbar.
|
|
|
|
* Throws if the fragment is not attached to an [AppCompatActivity].
|
|
|
|
*/
|
|
|
|
fun Fragment.hideToolbar() {
|
|
|
|
(requireActivity() as AppCompatActivity).supportActionBar?.hide()
|
|
|
|
}
|
2020-05-29 14:00:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pops the backstack to force users to re-auth if they put the app in the background and return to it
|
|
|
|
* while being inside the saved logins flow
|
|
|
|
*
|
|
|
|
* Does nothing if the user is currently navigating to any of the [destinations] given as a parameter
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
fun Fragment.redirectToReAuth(destinations: List<Int>, currentDestination: Int?) {
|
|
|
|
if (currentDestination !in destinations) {
|
|
|
|
findNavController().popBackStack(R.id.savedLoginsAuthFragment, false)
|
|
|
|
}
|
|
|
|
}
|