Commit 5be424a4 authored by MozLando's avatar MozLando
Browse files

Merge #5494



5494: Closes #5436: Lazy inflate ReaderViewControlsBar r=csadilek a=jonalmeida




Co-authored-by: default avatarColin Lee <mncolinlee@gmail.com>
Co-authored-by: default avatarJonathan Almeida <jalmeida@mozilla.com>
parents e8e19a6b e0832d3d
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ internal class ReaderViewControlsPresenter(
     */
    fun show() {
        view.apply {
            tryInflate()
            setColorScheme(config.colorScheme)
            setFont(config.fontType)
            setFontSize(config.fontSize)
+19 −4
Original line number Diff line number Diff line
@@ -35,13 +35,11 @@ class ReaderViewControlsBar @JvmOverloads constructor(
    private lateinit var fontGroup: RadioGroup
    private lateinit var colorSchemeGroup: RadioGroup

    init {
        View.inflate(getContext(), R.layout.mozac_feature_readerview_view, this)
    private var view: View? = null

    init {
        isFocusableInTouchMode = true
        isClickable = true

        bindViews()
    }

    /**
@@ -111,6 +109,23 @@ class ReaderViewControlsBar @JvmOverloads constructor(
        visibility = View.GONE
    }

    /**
     * Tries to inflate the view if needed.
     *
     * See: https://github.com/mozilla-mobile/android-components/issues/5491
     *
     * @return true if the inflation was completed, false if the view was already inflated.
     */
    override fun tryInflate(): Boolean {
        return if (view == null) {
            view = View.inflate(context, R.layout.mozac_feature_readerview_view, this)
            bindViews()
            true
        } else {
            false
        }
    }

    override fun onFocusChanged(gainFocus: Boolean, direction: Int, previouslyFocusedRect: Rect?) {
        if (!gainFocus) {
            hideControls()
+9 −0
Original line number Diff line number Diff line
@@ -45,6 +45,15 @@ interface ReaderViewControlsView {
     */
    fun asView(): View = (this as View)

    /**
     * Tries to inflate the view if needed.
     *
     * See: https://github.com/mozilla-mobile/android-components/issues/5491
     *
     * @return true if the inflation was completed, false if the view was already inflated.
     */
    fun tryInflate(): Boolean

    interface Listener {
        fun onFontChanged(font: FontType)
        fun onFontSizeIncreased(): Int
+1 −0
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ class ReaderViewControlsPresenterTest {

        presenter.show()

        verify(view).tryInflate()
        verify(view).setColorScheme(any())
        verify(view).setFontSize(5)
        verify(view).setFont(any())
+16 −0
Original line number Diff line number Diff line
@@ -35,6 +35,8 @@ class ReaderViewControlsBarTest {
    @Test
    fun `font options are set`() {
        val bar = ReaderViewControlsBar(appCompatContext)
        bar.tryInflate()

        val serifButton = bar.findViewById<AppCompatRadioButton>(R.id.mozac_feature_readerview_font_serif)
        val sansSerifButton = bar.findViewById<AppCompatRadioButton>(R.id.mozac_feature_readerview_font_sans_serif)

@@ -54,6 +56,7 @@ class ReaderViewControlsBarTest {
    @Test
    fun `font size buttons are enabled or disabled`() {
        val bar = ReaderViewControlsBar(appCompatContext)
        bar.tryInflate()

        val sizeDecreaseButton = bar.findViewById<AppCompatButton>(R.id.mozac_feature_readerview_font_size_decrease)
        val sizeIncreaseButton = bar.findViewById<AppCompatButton>(R.id.mozac_feature_readerview_font_size_increase)
@@ -87,6 +90,8 @@ class ReaderViewControlsBarTest {
    @Test
    fun `color scheme is set`() {
        val bar = ReaderViewControlsBar(appCompatContext)
        bar.tryInflate()

        val colorOptionDark = bar.findViewById<AppCompatRadioButton>(R.id.mozac_feature_readerview_color_dark)
        val colorOptionSepia = bar.findViewById<AppCompatRadioButton>(R.id.mozac_feature_readerview_color_sepia)
        val colorOptionLight = bar.findViewById<AppCompatRadioButton>(R.id.mozac_feature_readerview_color_light)
@@ -152,6 +157,7 @@ class ReaderViewControlsBarTest {
        assertNull(bar.listener)

        bar.listener = listener
        bar.tryInflate()

        bar.findViewById<AppCompatRadioButton>(R.id.mozac_feature_readerview_font_sans_serif).performClick()

@@ -166,6 +172,7 @@ class ReaderViewControlsBarTest {
        assertNull(bar.listener)

        bar.listener = listener
        bar.tryInflate()

        bar.findViewById<AppCompatButton>(R.id.mozac_feature_readerview_font_size_increase).performClick()

@@ -180,9 +187,18 @@ class ReaderViewControlsBarTest {
        assertNull(bar.listener)

        bar.listener = listener
        bar.tryInflate()

        bar.findViewById<AppCompatRadioButton>(R.id.mozac_feature_readerview_color_sepia).performClick()

        verify(listener).onColorSchemeChanged(ReaderViewFeature.ColorScheme.SEPIA)
    }

    @Test
    fun `tryInflate is only successfully once`() {
        val bar = ReaderViewControlsBar(appCompatContext)

        assertTrue(bar.tryInflate())
        assertFalse(bar.tryInflate())
    }
}