This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Fragment.kt
105 lines (92 loc) · 3.31 KB
/
Fragment.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
package org.mozilla.fenix.ext
import android.view.WindowManager
import androidx.annotation.IdRes
import androidx.annotation.StringRes
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.navigation.NavDirections
import androidx.navigation.NavOptions
import androidx.navigation.fragment.NavHostFragment.findNavController
import androidx.navigation.fragment.findNavController
import mozilla.components.concept.base.crash.Breadcrumb
import org.mozilla.fenix.NavHostActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.components.Components
/**
* Get the requireComponents of this application.
*/
val Fragment.requireComponents: Components
get() = requireContext().components
fun Fragment.nav(@IdRes id: Int?, directions: NavDirections, options: NavOptions? = null) {
findNavController(this).nav(id, directions, options)
}
fun Fragment.getPreferenceKey(@StringRes resourceId: Int): String = getString(resourceId)
/**
* 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
activity?.setNavigationIcon(R.drawable.ic_back_button)
(activity as NavHostActivity).getSupportActionBarAndInflateIfNecessary().show()
}
/**
* Run the [block] only if the [Fragment] is attached.
*
* @param block A callback to be executed if the container [Fragment] is attached.
*/
internal inline fun Fragment.runIfFragmentIsAttached(block: () -> Unit) {
context?.let {
block()
}
}
/**
* Hides the activity toolbar.
* Throws if the fragment is not attached to an [AppCompatActivity].
*/
fun Fragment.hideToolbar() {
(requireActivity() as AppCompatActivity).supportActionBar?.hide()
}
/**
* 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)
}
}
fun Fragment.breadcrumb(
message: String,
data: Map<String, String> = emptyMap()
) {
val activityName = activity?.let { it::class.java.simpleName } ?: "null"
requireComponents.analytics.crashReporter.recordCrashBreadcrumb(
Breadcrumb(
category = this::class.java.simpleName,
message = message,
data = data + mapOf(
"instance" to hashCode().toString(),
"activityInstance" to activity?.hashCode().toString(),
"activityName" to activityName
),
level = Breadcrumb.Level.INFO
)
)
}
fun Fragment.secure() {
this.activity?.window?.addFlags(
WindowManager.LayoutParams.FLAG_SECURE
)
}
fun Fragment.removeSecure() {
this.activity?.window?.clearFlags(
WindowManager.LayoutParams.FLAG_SECURE
)
}