From f110ba28613c3def2320d0b09d5339cf5aa2d8dc Mon Sep 17 00:00:00 2001 From: Nikit Bhandari Date: Sun, 9 Jun 2019 13:48:16 +0530 Subject: [PATCH] use synthetic binding instead of findviewbyid --- .../fenix/components/toolbar/TabCounter.kt | 54 ++++++++--------- .../fenix/crashes/CrashReporterFragment.kt | 3 +- .../quickactionsheet/QuickActionSheet.kt | 13 ++-- .../mozilla/fenix/settings/PairFragment.kt | 11 +--- .../quicksettings/QuickSettingsUIView.kt | 60 +++++-------------- 5 files changed, 48 insertions(+), 93 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/components/toolbar/TabCounter.kt b/app/src/main/java/org/mozilla/fenix/components/toolbar/TabCounter.kt index d6fc74817..f6571a0b8 100644 --- a/app/src/main/java/org/mozilla/fenix/components/toolbar/TabCounter.kt +++ b/app/src/main/java/org/mozilla/fenix/components/toolbar/TabCounter.kt @@ -12,9 +12,8 @@ import android.util.AttributeSet import android.util.TypedValue import android.view.LayoutInflater import android.view.ViewTreeObserver -import android.widget.ImageView import android.widget.RelativeLayout -import android.widget.TextView +import kotlinx.android.synthetic.main.mozac_ui_tabcounter_layout.view.* import mozilla.components.ui.tabcounter.R import java.text.NumberFormat @@ -24,9 +23,6 @@ open class TabCounter @JvmOverloads constructor( defStyle: Int = 0 ) : RelativeLayout(context, attrs, defStyle) { - private val box: ImageView - private val text: TextView - private val animationSet: AnimatorSet private var count: Int = 0 private var currentTextRatio: Float = 0.toFloat() @@ -35,13 +31,11 @@ open class TabCounter @JvmOverloads constructor( val inflater = LayoutInflater.from(context) inflater.inflate(R.layout.mozac_ui_tabcounter_layout, this) - box = findViewById(R.id.counter_box) - text = findViewById(R.id.counter_text) - text.text = DEFAULT_TABS_COUNTER_TEXT + counter_text.text = DEFAULT_TABS_COUNTER_TEXT val shiftThreeDp = TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, TWO_DIGIT_PADDING, context.resources.displayMetrics ).toInt() - text.setPadding(0, shiftThreeDp, shiftThreeDp, 0) + counter_text.setPadding(0, shiftThreeDp, shiftThreeDp, 0) animationSet = createAnimatorSet() } @@ -65,7 +59,7 @@ open class TabCounter @JvmOverloads constructor( adjustTextSize(count) - text.text = formatForDisplay(count) + counter_text.text = formatForDisplay(count) this.count = count // Cancel previous animations if necessary. @@ -79,7 +73,7 @@ open class TabCounter @JvmOverloads constructor( fun setCount(count: Int) { adjustTextSize(count) - text.text = formatForDisplay(count) + counter_text.text = formatForDisplay(count) this.count = count } @@ -93,56 +87,56 @@ open class TabCounter @JvmOverloads constructor( private fun createBoxAnimatorSet(animatorSet: AnimatorSet) { // The first animator, fadeout in 33 ms (49~51, 2 frames). val fadeOut = ObjectAnimator.ofFloat( - box, "alpha", + counter_box, "alpha", ANIM_BOX_FADEOUT_FROM, ANIM_BOX_FADEOUT_TO ).setDuration(ANIM_BOX_FADEOUT_DURATION) // Move up on y-axis, from 0.0 to -5.3 in 50ms, with fadeOut (49~52, 3 frames). val moveUp1 = ObjectAnimator.ofFloat( - box, "translationY", + counter_box, "translationY", ANIM_BOX_MOVEUP1_TO, ANIM_BOX_MOVEUP1_FROM ).setDuration(ANIM_BOX_MOVEUP1_DURATION) // Move down on y-axis, from -5.3 to -1.0 in 116ms, after moveUp1 (52~59, 7 frames). val moveDown2 = ObjectAnimator.ofFloat( - box, "translationY", + counter_box, "translationY", ANIM_BOX_MOVEDOWN2_FROM, ANIM_BOX_MOVEDOWN2_TO ).setDuration(ANIM_BOX_MOVEDOWN2_DURATION) // FadeIn in 66ms, with moveDown2 (52~56, 4 frames). val fadeIn = ObjectAnimator.ofFloat( - box, "alpha", + counter_box, "alpha", ANIM_BOX_FADEIN_FROM, ANIM_BOX_FADEIN_TO ).setDuration(ANIM_BOX_FADEIN_DURATION) // Move down on y-axis, from -1.0 to 2.7 in 116ms, after moveDown2 (59~66, 7 frames). val moveDown3 = ObjectAnimator.ofFloat( - box, "translationY", + counter_box, "translationY", ANIM_BOX_MOVEDOWN3_FROM, ANIM_BOX_MOVEDOWN3_TO ).setDuration(ANIM_BOX_MOVEDOWN3_DURATION) // Move up on y-axis, from 2.7 to 0 in 133ms, after moveDown3 (66~74, 8 frames). val moveUp4 = ObjectAnimator.ofFloat( - box, "translationY", + counter_box, "translationY", ANIM_BOX_MOVEDOWN4_FROM, ANIM_BOX_MOVEDOWN4_TO ).setDuration(ANIM_BOX_MOVEDOWN4_DURATION) // Scale up height from 2% to 105% in 100ms, after moveUp1 and delay 16ms (53~59, 6 frames). val scaleUp1 = ObjectAnimator.ofFloat( - box, "scaleY", + counter_box, "scaleY", ANIM_BOX_SCALEUP1_FROM, ANIM_BOX_SCALEUP1_TO ).setDuration(ANIM_BOX_SCALEUP1_DURATION) scaleUp1.startDelay = ANIM_BOX_SCALEUP1_DELAY // delay 1 frame after moveUp1 // Scale down height from 105% to 99% in 116ms, after scaleUp1 (59~66, 7 frames). val scaleDown2 = ObjectAnimator.ofFloat( - box, "scaleY", + counter_box, "scaleY", ANIM_BOX_SCALEDOWN2_FROM, ANIM_BOX_SCALEDOWN2_TO ).setDuration(ANIM_BOX_SCALEDOWN2_DURATION) // Scale up height from 99% to 100% in 133ms, after scaleDown2 (66~74, 8 frames). val scaleUp3 = ObjectAnimator.ofFloat( - box, "scaleY", + counter_box, "scaleY", ANIM_BOX_SCALEUP3_FROM, ANIM_BOX_SCALEUP3_TO ).setDuration(ANIM_BOX_SCALEUP3_DURATION) @@ -162,27 +156,27 @@ open class TabCounter @JvmOverloads constructor( // Fadeout in 100ms, with firstAnimator (49~51, 2 frames). val fadeOut = ObjectAnimator.ofFloat( - text, "alpha", + counter_text, "alpha", ANIM_TEXT_FADEOUT_FROM, ANIM_TEXT_FADEOUT_TO ).setDuration(ANIM_TEXT_FADEOUT_DURATION) // FadeIn in 66 ms, after fadeOut with delay 96ms (57~61, 4 frames). val fadeIn = ObjectAnimator.ofFloat( - text, "alpha", + counter_text, "alpha", ANIM_TEXT_FADEIN_FROM, ANIM_TEXT_FADEIN_TO ).setDuration(ANIM_TEXT_FADEIN_DURATION) fadeIn.startDelay = (ANIM_TEXT_FADEIN_DELAY).toLong() // delay 6 frames after fadeOut // Move down on y-axis, from 0 to 4.4 in 66ms, with fadeIn (57~61, 4 frames). val moveDown = ObjectAnimator.ofFloat( - text, "translationY", + counter_text, "translationY", ANIM_TEXT_MOVEDOWN_FROM, ANIM_TEXT_MOVEDOWN_TO ).setDuration(ANIM_TEXT_MOVEDOWN_DURATION) moveDown.startDelay = (ANIM_TEXT_MOVEDOWN_DELAY) // delay 6 frames after fadeOut // Move up on y-axis, from 0 to 4.4 in 66ms, after moveDown (61~69, 8 frames). val moveUp = ObjectAnimator.ofFloat( - text, "translationY", + counter_text, "translationY", ANIM_TEXT_MOVEUP_FROM, ANIM_TEXT_MOVEUP_TO ).setDuration(ANIM_TEXT_MOVEUP_DURATION) @@ -207,21 +201,21 @@ open class TabCounter @JvmOverloads constructor( if (newRatio != currentTextRatio) { currentTextRatio = newRatio - text.viewTreeObserver.addOnGlobalLayoutListener(object : + counter_text.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener { override fun onGlobalLayout() { - text.viewTreeObserver.removeOnGlobalLayoutListener(this) - val sizeInPixel = (box.width * newRatio).toInt() + counter_text.viewTreeObserver.removeOnGlobalLayoutListener(this) + val sizeInPixel = (counter_box.width * newRatio).toInt() if (sizeInPixel > 0) { // Only apply the size when we calculate a valid value. - text.setTextSize(TypedValue.COMPLEX_UNIT_PX, sizeInPixel.toFloat()) - text.setTypeface(null, Typeface.BOLD) + counter_text.setTextSize(TypedValue.COMPLEX_UNIT_PX, sizeInPixel.toFloat()) + counter_text.setTypeface(null, Typeface.BOLD) val shiftDp = TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, if (newRatio == TWO_DIGITS_SIZE_RATIO) TWO_DIGIT_PADDING else ONE_DIGIT_PADDING, context.resources.displayMetrics ).toInt() - text.setPadding(0, shiftDp, shiftDp, 0) + counter_text.setPadding(0, shiftDp, shiftDp, 0) } } }) diff --git a/app/src/main/java/org/mozilla/fenix/crashes/CrashReporterFragment.kt b/app/src/main/java/org/mozilla/fenix/crashes/CrashReporterFragment.kt index bb14c53ff..60dae8ac7 100644 --- a/app/src/main/java/org/mozilla/fenix/crashes/CrashReporterFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/crashes/CrashReporterFragment.kt @@ -8,7 +8,6 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import androidx.fragment.app.Fragment import androidx.navigation.Navigation @@ -32,7 +31,7 @@ class CrashReporterFragment : Fragment() { super.onViewCreated(view, savedInstanceState) val crash = Crash.fromIntent(CrashReporterFragmentArgs.fromBundle(arguments!!).crashIntent) - view.findViewById(R.id.title).text = + title.text = getString(R.string.tab_crash_title_2, context!!.getString(R.string.app_name)) requireContext().components.analytics.metrics.track(Event.CrashReporterOpened) diff --git a/app/src/main/java/org/mozilla/fenix/quickactionsheet/QuickActionSheet.kt b/app/src/main/java/org/mozilla/fenix/quickactionsheet/QuickActionSheet.kt index 2c81ffbed..58261c1a1 100644 --- a/app/src/main/java/org/mozilla/fenix/quickactionsheet/QuickActionSheet.kt +++ b/app/src/main/java/org/mozilla/fenix/quickactionsheet/QuickActionSheet.kt @@ -16,7 +16,7 @@ import org.mozilla.fenix.R import android.os.Bundle import android.view.accessibility.AccessibilityEvent import android.view.accessibility.AccessibilityNodeInfo -import android.widget.ImageButton +import kotlinx.android.synthetic.main.layout_quick_action_sheet.view.* import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch import kotlinx.coroutines.Dispatchers @@ -39,8 +39,6 @@ class QuickActionSheet @JvmOverloads constructor( override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job - private lateinit var handle: ImageButton - private lateinit var linearLayout: LinearLayout private lateinit var quickActionSheetBehavior: QuickActionSheetBehavior init { @@ -50,9 +48,8 @@ class QuickActionSheet @JvmOverloads constructor( override fun onAttachedToWindow() { super.onAttachedToWindow() job = Job() - handle = findViewById(R.id.quick_action_sheet_handle) - linearLayout = findViewById(R.id.quick_action_sheet) - quickActionSheetBehavior = BottomSheetBehavior.from(linearLayout.parent as View) as QuickActionSheetBehavior + quickActionSheetBehavior = BottomSheetBehavior.from(quick_action_sheet.parent as View) + as QuickActionSheetBehavior quickActionSheetBehavior.isHideable = false setupHandle() } @@ -63,14 +60,14 @@ class QuickActionSheet @JvmOverloads constructor( } private fun setupHandle() { - handle.setOnClickListener { + quick_action_sheet_handle.setOnClickListener { quickActionSheetBehavior.state = when (quickActionSheetBehavior.state) { BottomSheetBehavior.STATE_EXPANDED -> BottomSheetBehavior.STATE_COLLAPSED else -> BottomSheetBehavior.STATE_EXPANDED } } - handle.setAccessibilityDelegate(HandleAccessibilityDelegate(quickActionSheetBehavior)) + quick_action_sheet_handle.setAccessibilityDelegate(HandleAccessibilityDelegate(quickActionSheetBehavior)) } fun bounceSheet() { diff --git a/app/src/main/java/org/mozilla/fenix/settings/PairFragment.kt b/app/src/main/java/org/mozilla/fenix/settings/PairFragment.kt index 5a72a1de8..ca023566f 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/PairFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/PairFragment.kt @@ -8,11 +8,11 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import androidx.core.text.HtmlCompat import androidx.fragment.app.Fragment import androidx.navigation.fragment.NavHostFragment.findNavController +import kotlinx.android.synthetic.main.fragment_pair.* import mozilla.components.feature.qr.QrFeature import mozilla.components.support.base.feature.BackHandler import mozilla.components.support.base.feature.ViewBoundFeatureWrapper @@ -33,13 +33,8 @@ class PairFragment : Fragment(), BackHandler { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - val instructionsText = view.findViewById(R.id.pair_instructions) as TextView - instructionsText.setText( - HtmlCompat.fromHtml( - getString(R.string.pair_instructions), - HtmlCompat.FROM_HTML_MODE_LEGACY - ) - ) + pair_instructions.text = HtmlCompat.fromHtml(getString(R.string.pair_instructions), + HtmlCompat.FROM_HTML_MODE_LEGACY) qrFeature.set( QrFeature( diff --git a/app/src/main/java/org/mozilla/fenix/settings/quicksettings/QuickSettingsUIView.kt b/app/src/main/java/org/mozilla/fenix/settings/quicksettings/QuickSettingsUIView.kt index 65159b255..c5f0e0711 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/quicksettings/QuickSettingsUIView.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/quicksettings/QuickSettingsUIView.kt @@ -9,15 +9,14 @@ import android.view.View import android.view.View.GONE import android.view.View.VISIBLE import android.view.ViewGroup -import android.widget.Switch import android.widget.TextView import androidx.appcompat.content.res.AppCompatResources -import androidx.appcompat.widget.AppCompatTextView import androidx.core.content.ContextCompat import androidx.core.net.toUri import io.reactivex.Observable import io.reactivex.Observer import io.reactivex.functions.Consumer +import kotlinx.android.synthetic.main.fragment_quick_settings_dialog_sheet.* import mozilla.components.feature.sitepermissions.SitePermissions import mozilla.components.feature.sitepermissions.SitePermissions.Status.BLOCKED import mozilla.components.feature.sitepermissions.SitePermissions.Status.NO_DECISION @@ -40,39 +39,10 @@ class QuickSettingsUIView( ) : UIView( container, actionEmitter, changesObservable ) { - private val securityInfoLabel: TextView - private val urlLabel: TextView - private val trackingProtectionSwitch: Switch - private val trackingProtectionAction: TextView - private val reportSiteIssueAction: TextView - private val cameraActionLabel: TextView - private val cameraLabel: TextView - private val microphoneActionLabel: TextView - private val microphoneLabel: TextView - private val locationActionLabel: TextView - private val locationLabel: TextView - private val notificationActionLabel: TextView - private val notificationLabel: TextView private val blockedByAndroidPhoneFeatures = mutableListOf() private val context get() = view.context private val settings: Settings = Settings.getInstance(context) - init { - urlLabel = view.findViewById(R.id.url) - securityInfoLabel = view.findViewById(R.id.security_info) - trackingProtectionSwitch = view.findViewById(R.id.tracking_protection) - trackingProtectionAction = view.findViewById(R.id.tracking_protection_action) - reportSiteIssueAction = view.findViewById(R.id.report_site_issue_action) - cameraActionLabel = view.findViewById(R.id.camera_action_label) - cameraLabel = view.findViewById(R.id.camera_icon) - microphoneActionLabel = view.findViewById(R.id.microphone_action_label) - microphoneLabel = view.findViewById(R.id.microphone_icon) - locationLabel = view.findViewById(R.id.location_icon) - locationActionLabel = view.findViewById(R.id.location_action_label) - notificationActionLabel = view.findViewById(R.id.notification_action_label) - notificationLabel = view.findViewById(R.id.notification_icon) - } - override fun updateView() = Consumer { state -> when (state.mode) { is QuickSettingsState.Mode.Normal -> { @@ -96,7 +66,7 @@ class QuickSettingsUIView( } private fun bindUrl(url: String) { - urlLabel.text = url.toUri().hostWithoutCommonPrefixes + this.url.text = url.toUri().hostWithoutCommonPrefixes } private fun bindTrackingProtectionInfo(isTrackingProtectionOn: Boolean) { @@ -105,11 +75,11 @@ class QuickSettingsUIView( if (isTrackingProtectionOn) R.drawable.ic_tracking_protection else R.drawable.ic_tracking_protection_disabled val icon = AppCompatResources.getDrawable(context, drawableId) - trackingProtectionSwitch.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null) - trackingProtectionSwitch.isChecked = isTrackingProtectionOn - trackingProtectionSwitch.isEnabled = globalTPSetting + tracking_protection.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null) + tracking_protection.isChecked = isTrackingProtectionOn + tracking_protection.isEnabled = globalTPSetting - trackingProtectionSwitch.setOnCheckedChangeListener { _, isChecked -> + tracking_protection.setOnCheckedChangeListener { _, isChecked -> actionEmitter.onNext( QuickSettingsAction.ToggleTrackingProtection( isChecked @@ -120,8 +90,8 @@ class QuickSettingsUIView( private fun bindTrackingProtectionAction() { val globalTPSetting = Settings.getInstance(context).shouldUseTrackingProtection - trackingProtectionAction.visibility = if (globalTPSetting) View.GONE else View.VISIBLE - trackingProtectionAction.setOnClickListener { + tracking_protection_action.visibility = if (globalTPSetting) View.GONE else View.VISIBLE + tracking_protection_action.setOnClickListener { actionEmitter.onNext( QuickSettingsAction.SelectTrackingProtectionSettings ) @@ -129,7 +99,7 @@ class QuickSettingsUIView( } private fun bindReportSiteIssueAction(url: String) { - reportSiteIssueAction.setOnClickListener { + report_site_issue_action.setOnClickListener { actionEmitter.onNext( QuickSettingsAction.SelectReportProblem(url) ) @@ -153,8 +123,8 @@ class QuickSettingsUIView( val icon = AppCompatResources.getDrawable(context, drawableId) icon?.setTint(ContextCompat.getColor(context, drawableTint)) - securityInfoLabel.setText(stringId) - securityInfoLabel.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null) + security_info.setText(stringId) + security_info.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null) } private fun bindPhoneFeatureItem(phoneFeature: PhoneFeature, sitePermissions: SitePermissions? = null) { @@ -249,10 +219,10 @@ class QuickSettingsUIView( private val PhoneFeature.labelAndAction get(): Pair { return when (this) { - CAMERA -> cameraLabel to cameraActionLabel - LOCATION -> locationLabel to locationActionLabel - MICROPHONE -> microphoneLabel to microphoneActionLabel - NOTIFICATION -> notificationLabel to notificationActionLabel + CAMERA -> camera_icon to camera_action_label + LOCATION -> location_icon to location_action_label + MICROPHONE -> microphone_icon to microphone_action_label + NOTIFICATION -> notification_icon to notification_action_label } }