Commit 422faaf7 authored by Tiger Oakes's avatar Tiger Oakes Committed by Roger Yang
Browse files

Add toolbar helpers (#6531)

parent 7e8f0792
......@@ -15,7 +15,6 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.CallSuper
import androidx.appcompat.app.AppCompatActivity
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.net.toUri
import androidx.fragment.app.Fragment
......@@ -79,6 +78,7 @@ import org.mozilla.fenix.downloads.DownloadService
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.enterToImmersiveMode
import org.mozilla.fenix.ext.getRootView
import org.mozilla.fenix.ext.hideToolbar
import org.mozilla.fenix.ext.metrics
import org.mozilla.fenix.ext.nav
import org.mozilla.fenix.ext.requireComponents
......@@ -461,7 +461,7 @@ abstract class BaseBrowserFragment : Fragment(), BackHandler, SessionManager.Obs
components.core.engine.settings.preferredColorScheme = preferredColorScheme
components.useCases.sessionUseCases.reload()
}
(activity as AppCompatActivity).supportActionBar?.hide()
hideToolbar()
assignSitePermissionsRules()
}
......
......@@ -6,13 +6,13 @@ package org.mozilla.fenix.crashes
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import kotlinx.android.synthetic.main.fragment_crash_reporter.*
import mozilla.components.lib.crash.Crash
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.hideToolbar
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.ext.settings
......@@ -47,6 +47,6 @@ class CrashReporterFragment : Fragment(R.layout.fragment_crash_reporter) {
override fun onResume() {
super.onResume()
(activity as AppCompatActivity).supportActionBar?.hide()
hideToolbar()
}
}
......@@ -9,7 +9,6 @@ import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import kotlinx.android.synthetic.main.fragment_exceptions.view.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
......@@ -20,6 +19,7 @@ import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.components.StoreProvider
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.showToolbar
import org.mozilla.fenix.settings.SupportUtils
/**
......@@ -34,8 +34,7 @@ class ExceptionsFragment : Fragment() {
override fun onResume() {
super.onResume()
activity?.title = getString(R.string.preference_exceptions)
(activity as AppCompatActivity).supportActionBar?.show()
showToolbar(getString(R.string.preference_exceptions))
}
override fun onCreateView(
......
......@@ -6,6 +6,7 @@ package org.mozilla.fenix.ext
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
......@@ -32,3 +33,20 @@ fun Fragment.nav(@IdRes id: Int?, directions: NavDirections, options: NavOptions
}
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
(requireActivity() as AppCompatActivity).supportActionBar?.show()
}
/**
* Hides the activity toolbar.
* Throws if the fragment is not attached to an [AppCompatActivity].
*/
fun Fragment.hideToolbar() {
(requireActivity() as AppCompatActivity).supportActionBar?.hide()
}
......@@ -18,7 +18,6 @@ import android.widget.LinearLayout
import android.widget.PopupWindow
import androidx.annotation.StringRes
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout.LayoutParams.PARENT_ID
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
......@@ -72,6 +71,7 @@ import org.mozilla.fenix.components.PrivateShortcutCreateManager
import org.mozilla.fenix.components.TabCollectionStorage
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.hideToolbar
import org.mozilla.fenix.ext.metrics
import org.mozilla.fenix.ext.nav
import org.mozilla.fenix.ext.requireComponents
......@@ -329,7 +329,7 @@ class HomeFragment : Fragment() {
)
)
(activity as AppCompatActivity).supportActionBar?.hide()
hideToolbar()
requireComponents.backgroundServices.accountManager.register(currentMode, owner = this)
requireComponents.backgroundServices.accountManager.register(object : AccountObserver {
......
......@@ -19,6 +19,7 @@ import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.ext.nav
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.ext.setToolbarColors
import org.mozilla.fenix.ext.showToolbar
/**
* Displays buttons to navigate to library sections, such as bookmarks and history.
......@@ -69,15 +70,13 @@ class LibraryFragment : Fragment(R.layout.fragment_library) {
}
private fun initToolbar() {
val activity = activity as? AppCompatActivity
val toolbar = activity?.findViewById<Toolbar>(R.id.navigationToolbar)
context?.let { context ->
(activity as? AppCompatActivity)?.let { activity ->
val toolbar = activity.findViewById<Toolbar>(R.id.navigationToolbar)
toolbar?.setToolbarColors(
foreground = context.getColorFromAttr(R.attr.primaryText),
background = context.getColorFromAttr(R.attr.foundation)
foreground = activity.getColorFromAttr(R.attr.primaryText),
background = activity.getColorFromAttr(R.attr.foundation)
)
showToolbar(getString(R.string.library_title))
}
activity?.title = getString(R.string.library_title)
activity?.supportActionBar?.show()
}
}
......@@ -8,6 +8,7 @@ import androidx.fragment.app.Fragment
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.browser.browsingmode.BrowsingMode
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.hideToolbar
abstract class LibraryPageFragment<T> : Fragment() {
......@@ -24,6 +25,6 @@ abstract class LibraryPageFragment<T> : Fragment() {
}
(activity as HomeActivity).browsingModeManager.mode = BrowsingMode.fromBoolean(private)
(activity as HomeActivity).supportActionBar?.hide()
hideToolbar()
}
}
......@@ -10,7 +10,6 @@ import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.view.View.GONE
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.ViewModelProvider
......@@ -28,6 +27,7 @@ import org.mozilla.fenix.R
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.ext.nav
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.ext.showToolbar
import org.mozilla.fenix.library.bookmarks.BookmarksSharedViewModel
/**
......@@ -55,9 +55,7 @@ class AddBookmarkFolderFragment : Fragment(R.layout.fragment_edit_bookmark) {
override fun onResume() {
super.onResume()
(activity as AppCompatActivity).title =
getString(R.string.bookmark_add_folder_fragment_label)
(activity as AppCompatActivity).supportActionBar?.show()
showToolbar(getString(R.string.bookmark_add_folder_fragment_label))
lifecycleScope.launch(Main) {
sharedViewModel.selectedFolder = withContext(IO) {
......
......@@ -11,7 +11,6 @@ import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.ViewModelProvider
......@@ -30,6 +29,7 @@ import org.mozilla.fenix.R
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.nav
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.ext.showToolbar
import org.mozilla.fenix.library.bookmarks.BookmarksSharedViewModel
import org.mozilla.fenix.library.bookmarks.DesktopFolders
import org.mozilla.fenix.library.bookmarks.SignInView
......@@ -57,8 +57,7 @@ class SelectBookmarkFolderFragment : Fragment() {
override fun onResume() {
super.onResume()
activity?.title = getString(R.string.bookmark_select_folder_fragment_label)
(activity as? AppCompatActivity)?.supportActionBar?.show()
showToolbar(getString(R.string.bookmark_select_folder_fragment_label))
val accountManager = requireComponents.backgroundServices.accountManager
sharedViewModel.observeAccountManager(accountManager, owner = this)
......
......@@ -14,7 +14,6 @@ import android.view.View
import android.view.ViewGroup
import androidx.annotation.MenuRes
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.lifecycleScope
import kotlinx.android.synthetic.main.fragment_history.view.*
......@@ -35,6 +34,7 @@ import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.nav
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.ext.showToolbar
import org.mozilla.fenix.library.LibraryPageFragment
@SuppressWarnings("TooManyFunctions", "LargeClass")
......@@ -122,10 +122,7 @@ class HistoryFragment : LibraryPageFragment<HistoryItem>(), BackHandler {
override fun onResume() {
super.onResume()
(activity as AppCompatActivity).apply {
title = getString(R.string.library_history)
supportActionBar?.show()
}
showToolbar(getString(R.string.library_history))
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
......
......@@ -8,7 +8,6 @@ import android.os.Bundle
import android.text.InputType
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.google.android.material.snackbar.Snackbar
......@@ -17,6 +16,7 @@ import org.mozilla.fenix.R
import org.mozilla.fenix.components.FenixSnackbar
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.showToolbar
class SavedLoginSiteInfoFragment : Fragment(R.layout.fragment_saved_login_site_info) {
private val safeArguments get() = requireNotNull(arguments)
......@@ -97,7 +97,6 @@ class SavedLoginSiteInfoFragment : Fragment(R.layout.fragment_saved_login_site_i
WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE
)
activity?.title = savedLoginItem.url
(activity as AppCompatActivity).supportActionBar?.show()
showToolbar(savedLoginItem.url)
}
}
......@@ -9,7 +9,6 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
......@@ -25,6 +24,7 @@ import org.mozilla.fenix.R
import org.mozilla.fenix.components.StoreProvider
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.showToolbar
class SavedLoginsFragment : Fragment() {
private lateinit var savedLoginsStore: SavedLoginsFragmentStore
......@@ -37,8 +37,7 @@ class SavedLoginsFragment : Fragment() {
WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE
)
activity?.title = getString(R.string.preferences_passwords_saved_logins)
(activity as AppCompatActivity).supportActionBar?.show()
showToolbar(getString(R.string.preferences_passwords_saved_logins))
}
override fun onCreateView(
......
......@@ -16,7 +16,6 @@ import android.view.View
import android.view.ViewGroup
import android.view.ViewStub
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
......@@ -38,6 +37,7 @@ import org.mozilla.fenix.R
import org.mozilla.fenix.components.StoreProvider
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.ext.getSpannable
import org.mozilla.fenix.ext.hideToolbar
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.search.awesomebar.AwesomeBarView
import org.mozilla.fenix.search.toolbar.ToolbarView
......@@ -261,7 +261,7 @@ class SearchFragment : Fragment(), BackHandler {
updateClipboardSuggestion(searchStore.state, requireContext().components.clipboardHandler.url)
permissionDidUpdate = false
(activity as AppCompatActivity).supportActionBar?.hide()
hideToolbar()
}
override fun onPause() {
......
......@@ -5,13 +5,13 @@
package org.mozilla.fenix.settings
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreference
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.getPreferenceKey
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.ext.showToolbar
/**
* Displays font size controls for accessibility.
......@@ -22,8 +22,7 @@ import org.mozilla.fenix.ext.settings
class AccessibilityFragment : PreferenceFragmentCompat() {
override fun onResume() {
super.onResume()
(activity as AppCompatActivity).title = getString(R.string.preferences_accessibility)
(activity as AppCompatActivity).supportActionBar?.show()
showToolbar(getString(R.string.preferences_accessibility))
val forceZoomPreference = findPreference<SwitchPreference>(
getPreferenceKey(R.string.pref_key_accessibility_force_enable_zoom)
......
......@@ -6,7 +6,6 @@ package org.mozilla.fenix.settings
import android.content.SharedPreferences
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreference
import org.mozilla.fenix.Config
......@@ -14,6 +13,7 @@ import org.mozilla.fenix.R
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.getPreferenceKey
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.ext.showToolbar
/**
* Lets the user toggle telemetry on/off.
......@@ -42,8 +42,7 @@ class DataChoicesFragment : PreferenceFragmentCompat() {
override fun onResume() {
super.onResume()
(activity as AppCompatActivity).title = getString(R.string.preferences_data_collection)
(activity as AppCompatActivity).supportActionBar?.show()
showToolbar(getString(R.string.preferences_data_collection))
}
override fun onDestroy() {
......
......@@ -10,7 +10,6 @@ import android.os.Build
import android.os.Build.VERSION.SDK_INT
import android.os.Bundle
import android.provider.Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.CheckBoxPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
......@@ -21,6 +20,7 @@ import org.mozilla.fenix.R
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.getPreferenceKey
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.ext.showToolbar
/**
* Lets the user control their default browser preferences
......@@ -61,9 +61,7 @@ class DefaultBrowserSettingsFragment : PreferenceFragmentCompat() {
override fun onResume() {
super.onResume()
(activity as AppCompatActivity).title =
getString(R.string.preferences_set_as_default_browser)
(activity as AppCompatActivity).supportActionBar?.show()
showToolbar(getString(R.string.preferences_set_as_default_browser))
updatePreferences()
}
......
......@@ -14,7 +14,6 @@ import android.os.Build
import android.os.Build.VERSION_CODES.M
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.biometric.BiometricManager
import androidx.biometric.BiometricPrompt
import androidx.lifecycle.lifecycleScope
......@@ -34,6 +33,7 @@ import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.getPreferenceKey
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.ext.showToolbar
import java.util.concurrent.Executors
@Suppress("TooManyFunctions")
......@@ -85,8 +85,7 @@ class LoginsFragment : PreferenceFragmentCompat(), AccountObserver {
override fun onResume() {
super.onResume()
activity?.title = getString(R.string.preferences_passwords_logins_and_passwords)
(activity as AppCompatActivity).supportActionBar?.show()
showToolbar(getString(R.string.preferences_passwords_logins_and_passwords))
val savedLoginsKey = getPreferenceKey(R.string.pref_key_saved_logins)
findPreference<Preference>(savedLoginsKey)?.setOnPreferenceClickListener {
......
......@@ -9,7 +9,6 @@ import android.os.Bundle
import android.os.VibrationEffect
import android.os.Vibrator
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.getSystemService
import androidx.core.text.HtmlCompat
import androidx.fragment.app.Fragment
......@@ -21,6 +20,7 @@ import mozilla.components.support.base.feature.BackHandler
import mozilla.components.support.base.feature.ViewBoundFeatureWrapper
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.ext.showToolbar
class PairFragment : Fragment(R.layout.fragment_pair), BackHandler {
......@@ -74,8 +74,7 @@ class PairFragment : Fragment(R.layout.fragment_pair), BackHandler {
override fun onResume() {
super.onResume()
(activity as AppCompatActivity).title = getString(R.string.sync_scan_code)
(activity as AppCompatActivity).supportActionBar?.show()
showToolbar(getString(R.string.sync_scan_code))
}
override fun onBackPressed(): Boolean {
......
......@@ -10,7 +10,6 @@ import android.content.Intent
import android.content.SharedPreferences
import android.net.Uri
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import androidx.navigation.Navigation
import androidx.navigation.fragment.findNavController
......@@ -59,6 +58,7 @@ import org.mozilla.fenix.ext.getPreferenceKey
import org.mozilla.fenix.ext.metrics
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.ext.showToolbar
import org.mozilla.fenix.settings.account.AccountAuthErrorPreference
import org.mozilla.fenix.settings.account.AccountPreference
import org.mozilla.fenix.utils.ItsNotBrokenSnack
......@@ -112,8 +112,7 @@ class SettingsFragment : PreferenceFragmentCompat(), AccountObserver {
override fun onResume() {
super.onResume()
(activity as AppCompatActivity).title = getString(R.string.settings_title)
(activity as AppCompatActivity).supportActionBar?.show()
showToolbar(getString(R.string.settings_title))
val trackingProtectionPreference =
findPreference<Preference>(getPreferenceKey(pref_key_tracking_protection_settings))
......
......@@ -8,12 +8,12 @@ import android.annotation.SuppressLint
import android.os.Build
import android.os.Build.VERSION.SDK_INT
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.preference.PreferenceFragmentCompat
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.getPreferenceKey
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.ext.showToolbar
class ThemeFragment : PreferenceFragmentCompat() {
private lateinit var radioLightTheme: RadioButtonPreference
......@@ -27,8 +27,7 @@ class ThemeFragment : PreferenceFragmentCompat() {
override fun onResume() {
super.onResume()
(activity as AppCompatActivity).title = getString(R.string.preferences_theme)
(activity as AppCompatActivity).supportActionBar?.show()
showToolbar(getString(R.string.preferences_theme))
setupPreferences()
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment