Skip to content

Commit

Permalink
For mozilla-mobile#17917: Use View binding in settings screens.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarare committed Jul 5, 2021
1 parent 0c720ba commit f1ce40a
Show file tree
Hide file tree
Showing 32 changed files with 487 additions and 386 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ android {
}
}

buildFeatures {
viewBinding true
}

aaptOptions {
// All JavaScript code used internally by GeckoView is packaged in a
// file called omni.ja. If this file is compressed in the APK,
Expand Down
15 changes: 8 additions & 7 deletions app/src/main/java/org/mozilla/fenix/settings/SettingsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreference
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.amo_collection_override_dialog.view.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
Expand All @@ -41,6 +40,7 @@ import org.mozilla.fenix.FeatureFlags
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.databinding.AmoCollectionOverrideDialogBinding
import org.mozilla.fenix.experiments.ExperimentBranch
import org.mozilla.fenix.experiments.FeatureId
import org.mozilla.fenix.ext.application
Expand Down Expand Up @@ -352,6 +352,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
val context = requireContext()
val dialogView = LayoutInflater.from(context).inflate(R.layout.amo_collection_override_dialog, null)

val binding = AmoCollectionOverrideDialogBinding.bind(dialogView)
AlertDialog.Builder(context).apply {
setTitle(context.getString(R.string.preferences_customize_amo_collection))
setView(dialogView)
Expand All @@ -360,8 +361,8 @@ class SettingsFragment : PreferenceFragmentCompat() {
}

setPositiveButton(R.string.customize_addon_collection_ok) { _, _ ->
context.settings().overrideAmoUser = dialogView.custom_amo_user.text.toString()
context.settings().overrideAmoCollection = dialogView.custom_amo_collection.text.toString()
context.settings().overrideAmoUser = binding.customAmoUser.text.toString()
context.settings().overrideAmoCollection = binding.customAmoCollection.text.toString()

Toast.makeText(
context,
Expand All @@ -374,10 +375,10 @@ class SettingsFragment : PreferenceFragmentCompat() {
}, AMO_COLLECTION_OVERRIDE_EXIT_DELAY)
}

dialogView.custom_amo_collection.setText(context.settings().overrideAmoCollection)
dialogView.custom_amo_user.setText(context.settings().overrideAmoUser)
dialogView.custom_amo_user.requestFocus()
dialogView.custom_amo_user.showKeyboard()
binding.customAmoCollection.setText(context.settings().overrideAmoCollection)
binding.customAmoUser.setText(context.settings().overrideAmoUser)
binding.customAmoUser.requestFocus()
binding.customAmoUser.showKeyboard()
create()
}.show()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import androidx.core.content.pm.PackageInfoCompat
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.DividerItemDecoration
import kotlinx.android.synthetic.main.fragment_about.*
import org.mozilla.fenix.BrowserDirection
import org.mozilla.fenix.BuildConfig
import org.mozilla.fenix.Config
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.crashes.CrashListActivity
import org.mozilla.fenix.databinding.FragmentAboutBinding
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.ext.showToolbar
Expand All @@ -43,27 +43,30 @@ class AboutFragment : Fragment(), AboutPageListener {
private lateinit var headerAppName: String
private lateinit var appName: String
private var aboutPageAdapter: AboutPageAdapter? = AboutPageAdapter(this)
private var _binding: FragmentAboutBinding? = null

private val binding get() = _binding!!

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val rootView = inflater.inflate(R.layout.fragment_about, container, false)
): View {
val binding = FragmentAboutBinding.inflate(inflater, container, false)
appName = getString(R.string.app_name)
headerAppName =
if (Config.channel.isRelease) getString(R.string.daylight_app_name) else appName
showToolbar(getString(R.string.preferences_about, appName))

return rootView
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
if (aboutPageAdapter == null) {
aboutPageAdapter = AboutPageAdapter(this)
}

about_list.run {
binding.aboutList.run {
adapter = aboutPageAdapter
addItemDecoration(
DividerItemDecoration(
Expand All @@ -75,7 +78,7 @@ class AboutFragment : Fragment(), AboutPageListener {

lifecycle.addObserver(
SecretDebugMenuTrigger(
logoView = wordmark,
logoView = binding.wordmark,
settings = view.context.settings()
)
)
Expand All @@ -87,6 +90,7 @@ class AboutFragment : Fragment(), AboutPageListener {
override fun onDestroyView() {
super.onDestroyView()
aboutPageAdapter = null
_binding = null
}

private fun populateAboutHeader() {
Expand Down Expand Up @@ -121,9 +125,9 @@ class AboutFragment : Fragment(), AboutPageListener {
val content = getString(R.string.about_content, headerAppName)
val buildDate = BuildConfig.BUILD_DATE

about_text.text = aboutText
about_content.text = content
build_date.text = buildDate
binding.aboutText.text = aboutText
binding.aboutContent.text = content
binding.buildDate.text = buildDate
}

private fun populateAboutList(): List<AboutPageItem> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import android.widget.ListView
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import kotlinx.android.synthetic.main.fragment_about_libraries.view.*
import org.mozilla.fenix.R
import org.mozilla.fenix.databinding.FragmentAboutLibrariesBinding
import org.mozilla.fenix.ext.showToolbar
import java.nio.charset.Charset
import java.util.Locale
Expand All @@ -34,11 +34,16 @@ import java.util.Locale
* to show the extracted licenses to the end-user.
*/
class AboutLibrariesFragment : Fragment(R.layout.fragment_about_libraries) {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val appName = getString(R.string.app_name)
val binding = FragmentAboutLibrariesBinding.bind(view)
showToolbar(getString(R.string.open_source_licenses_title, appName))
setupLibrariesListView(binding.aboutLibrariesListview)
}

setupLibrariesListView(view.about_libraries_listview)
override fun onDestroyView() {
super.onDestroyView()
}

private fun setupLibrariesListView(listView: ListView) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import org.mozilla.fenix.databinding.AboutListItemBinding
import org.mozilla.fenix.settings.about.viewholders.AboutItemViewHolder

class AboutPageAdapter(private val listener: AboutPageListener) :
ListAdapter<AboutPageItem, AboutItemViewHolder>(DiffCallback) {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AboutItemViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(AboutItemViewHolder.LAYOUT_ID, parent, false)
return AboutItemViewHolder(view, listener)
val binding = AboutListItemBinding
.inflate(LayoutInflater.from(parent.context), parent, false)
return AboutItemViewHolder(binding, listener)
}

override fun onBindViewHolder(holder: AboutItemViewHolder, position: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@

package org.mozilla.fenix.settings.about.viewholders

import android.view.View
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.about_list_item.view.*
import org.mozilla.fenix.R
import org.mozilla.fenix.databinding.AboutListItemBinding
import org.mozilla.fenix.settings.about.AboutPageItem
import org.mozilla.fenix.settings.about.AboutPageListener

class AboutItemViewHolder(
view: View,
binding: AboutListItemBinding,
listener: AboutPageListener
) : RecyclerView.ViewHolder(view) {
) : RecyclerView.ViewHolder(binding.root) {

private val title = view.about_item_title
private val title = binding.aboutItemTitle
private lateinit var item: AboutPageItem

init {
Expand All @@ -29,8 +27,4 @@ class AboutItemViewHolder(
this.item = item
title.text = item.title
}

companion object {
const val LAYOUT_ID = R.layout.about_list_item
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@ import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import com.google.android.material.bottomsheet.BottomSheetBehavior
import androidx.appcompat.app.AppCompatDialogFragment
import kotlinx.android.synthetic.main.fragment_sign_out.view.*
import kotlinx.coroutines.launch
import mozilla.components.service.fxa.manager.FxaAccountManager
import org.mozilla.fenix.R
import org.mozilla.fenix.databinding.FragmentSignOutBinding
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.ext.runIfFragmentIsAttached

class SignOutFragment : AppCompatDialogFragment() {
private lateinit var accountManager: FxaAccountManager

private var _binding: FragmentSignOutBinding? = null
private val binding get() = _binding!!

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(STYLE_NO_TITLE, R.style.BottomSheet)
Expand All @@ -46,20 +49,21 @@ class SignOutFragment : AppCompatDialogFragment() {
savedInstanceState: Bundle?
): View? {
accountManager = requireComponents.backgroundServices.accountManager
val view = inflater.inflate(R.layout.fragment_sign_out, container, false)
view.sign_out_message.text = String.format(
view.context.getString(
_binding = FragmentSignOutBinding.inflate(inflater, container, false)

binding.signOutMessage.text = String.format(
binding.root.context.getString(
R.string.sign_out_confirmation_message_2
),
view.context.getString(R.string.app_name)
binding.root.context.getString(R.string.app_name)
)
return view
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

view.signOutDisconnect.setOnClickListener {
binding.signOutDisconnect.setOnClickListener {
lifecycleScope.launch {
requireComponents
.backgroundServices.accountAbnormalities.userRequestedLogout()
Expand All @@ -74,8 +78,13 @@ class SignOutFragment : AppCompatDialogFragment() {
}
}

view.signOutCancel.setOnClickListener {
binding.signOutCancel.setOnClickListener {
dismiss()
}
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import androidx.fragment.app.Fragment
import androidx.navigation.findNavController
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import kotlinx.android.synthetic.main.fragment_turn_on_sync.view.*
import mozilla.components.concept.sync.AccountObserver
import mozilla.components.concept.sync.AuthType
import mozilla.components.concept.sync.OAuthAccount
Expand All @@ -26,6 +25,7 @@ import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.components.FenixSnackbar
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.databinding.FragmentTurnOnSyncBinding
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.ext.showToolbar
Expand Down Expand Up @@ -57,6 +57,9 @@ class TurnOnSyncFragment : Fragment(), AccountObserver {
requireContext().settings().setCameraPermissionNeededState = false
}

private var _binding: FragmentTurnOnSyncBinding? = null
private val binding get() = _binding!!

private fun navigateToPairFragment() {
val directions = TurnOnSyncFragmentDirections.actionTurnOnSyncFragmentToPairFragment()
requireView().findNavController().navigate(directions)
Expand Down Expand Up @@ -84,6 +87,7 @@ class TurnOnSyncFragment : Fragment(), AccountObserver {
override fun onDestroy() {
super.onDestroy()
requireComponents.analytics.metrics.track(Event.SyncAuthClosed)
_binding = null
}

override fun onResume() {
Expand All @@ -110,11 +114,11 @@ class TurnOnSyncFragment : Fragment(), AccountObserver {
// Headless fragment. Don't need UI if we're taking the user to another screen.
return null
}
_binding = FragmentTurnOnSyncBinding.inflate(inflater, container, false)

val view = inflater.inflate(R.layout.fragment_turn_on_sync, container, false)
view.signInScanButton.setOnClickListener(paringClickListener)
view.signInEmailButton.setOnClickListener(signInClickListener)
view.signInInstructions.text = HtmlCompat.fromHtml(
binding.signInScanButton.setOnClickListener(paringClickListener)
binding.signInEmailButton.setOnClickListener(signInClickListener)
binding.signInInstructions.text = HtmlCompat.fromHtml(
if (requireContext().settings().allowDomesticChinaFxaServer && Config.channel.isMozillaOnline)
getString(R.string.sign_in_instructions_cn)
else getString(R.string.sign_in_instructions),
Expand All @@ -125,14 +129,14 @@ class TurnOnSyncFragment : Fragment(), AccountObserver {
DefaultSyncController(activity = activity as HomeActivity)
)

view.createAccount.apply {
binding.createAccount.apply {
text = HtmlCompat.fromHtml(
getString(R.string.sign_in_create_account_text),
HtmlCompat.FROM_HTML_MODE_LEGACY
)
setOnClickListener(createAccountClickListener)
}
return view
return binding.root
}

override fun onAuthenticated(account: OAuthAccount, authType: AuthType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import org.mozilla.fenix.R
import org.mozilla.fenix.databinding.LocaleSettingsItemBinding
import java.util.Locale

class LocaleAdapter(private val interactor: LocaleSettingsViewInteractor) :
Expand All @@ -21,14 +22,16 @@ class LocaleAdapter(private val interactor: LocaleSettingsViewInteractor) :
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.locale_settings_item, parent, false)

val localeSettingsItemBinding = LocaleSettingsItemBinding.bind(view)

return when (viewType) {
ItemType.DEFAULT.ordinal -> SystemLocaleViewHolder(
view,
localeSettingsItemBinding,
selectedLocale,
interactor
)
ItemType.LOCALE.ordinal -> LocaleViewHolder(
view,
localeSettingsItemBinding,
selectedLocale,
interactor
)
Expand Down
Loading

0 comments on commit f1ce40a

Please sign in to comment.