Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3f3da09d authored by Aaron Liu's avatar Aaron Liu Committed by Android (Google) Code Review
Browse files

Merge "Decouple addview and bind data." into main

parents 7c140b16 19758ab9
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ constructor(
    defaultCommunalWidgetSection: DefaultCommunalWidgetSection,
) : KeyguardBlueprint {
    override val id: String = COMMUNAL
    override val sections: Array<KeyguardSection> = arrayOf(defaultCommunalWidgetSection)
    override val sections: Set<KeyguardSection> = setOf(defaultCommunalWidgetSection)

    companion object {
        const val COMMUNAL = "communal"
+7 −2
Original line number Diff line number Diff line
@@ -42,9 +42,12 @@ constructor(
    private val communalWidgetViewModel: CommunalWidgetViewModel,
    private val communalWidgetViewAdapter: CommunalWidgetViewAdapter,
    private val keyguardBlueprintInteractor: Lazy<KeyguardBlueprintInteractor>,
) : KeyguardSection {
) : KeyguardSection() {
    private val widgetAreaViewId = R.id.communal_widget_wrapper
    override fun addViews(constraintLayout: ConstraintLayout) {

    override fun addViews(constraintLayout: ConstraintLayout) {}

    override fun bindData(constraintLayout: ConstraintLayout) {
        if (!featureFlags.isEnabled(Flags.WIDGET_ON_KEYGUARD)) {
            return
        }
@@ -65,4 +68,6 @@ constructor(
            connect(widgetAreaViewId, END, PARENT_ID, END)
        }
    }

    override fun removeViews(constraintLayout: ConstraintLayout) {}
}
+24 −7
Original line number Diff line number Diff line
@@ -22,17 +22,34 @@ import androidx.constraintlayout.widget.ConstraintSet
/** Determines the constraints for the ConstraintSet in the lockscreen root view. */
interface KeyguardBlueprint {
    val id: String
    val sections: Array<KeyguardSection>
    val sections: Set<KeyguardSection>

    fun addViews(constraintLayout: ConstraintLayout) {
        sections.forEach { it.addViews(constraintLayout) }
    /**
     * Add views to new blueprint.
     *
     * Finds sections that did not exist in the previous blueprint and add the corresponding views.
     *
     * @param previousBluePrint: KeyguardBlueprint the blueprint we are transitioning from.
     */
    fun addViews(previousBlueprint: KeyguardBlueprint?, constraintLayout: ConstraintLayout) {
        sections.subtract((previousBlueprint?.sections ?: setOf()).toSet()).forEach {
            it.addViews(constraintLayout)
            it.bindData(constraintLayout)
        }
    }

    fun applyConstraints(constraintSet: ConstraintSet) {
        sections.forEach { it.applyConstraints(constraintSet) }
    /**
     * Remove views of old blueprint.
     *
     * Finds sections that are no longer in the next blueprint and remove the corresponding views.
     *
     * @param nextBluePrint: KeyguardBlueprint the blueprint we will transition to.
     */
    fun removeViews(nextBlueprint: KeyguardBlueprint, constraintLayout: ConstraintLayout) {
        sections.subtract(nextBlueprint.sections).forEach { it.removeViews(constraintLayout) }
    }

    fun onDestroy() {
        sections.forEach { it.onDestroy() }
    fun applyConstraints(constraintSet: ConstraintSet) {
        sections.forEach { it.applyConstraints(constraintSet) }
    }
}
+30 −4
Original line number Diff line number Diff line
@@ -23,8 +23,34 @@ import androidx.constraintlayout.widget.ConstraintSet
 * Lower level modules that determine constraints for a particular section in the lockscreen root
 * view.
 */
interface KeyguardSection {
    fun addViews(constraintLayout: ConstraintLayout)
    fun applyConstraints(constraintSet: ConstraintSet)
    fun onDestroy() {}
abstract class KeyguardSection {
    /** Adds the views to the root view. */
    abstract fun addViews(constraintLayout: ConstraintLayout)
    /** Binds the views to data. */
    abstract fun bindData(constraintLayout: ConstraintLayout)
    /** Applies layout constraints to the view in respect to the root view. */
    abstract fun applyConstraints(constraintSet: ConstraintSet)
    /** Removes views and does any data binding destruction. */
    abstract fun removeViews(constraintLayout: ConstraintLayout)

    /**
     * Defines equality as same class.
     *
     * This is to enable set operations to be done as an optimization to blueprint transitions.
     */
    override fun equals(other: Any?): Boolean {
        other?.let { other ->
            return this::class == other::class
        }
        return false
    }

    /**
     * Defines hashcode as class.
     *
     * This is to enable set operations to be done as an optimization to blueprint transitions.
     */
    override fun hashCode(): Int {
        return this::class.hashCode()
    }
}
+11 −18
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ import android.os.Trace
import android.util.Log
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import androidx.core.view.children
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.repeatOnLifecycle
import com.android.systemui.keyguard.ui.viewmodel.KeyguardBlueprintViewModel
@@ -37,26 +36,20 @@ class KeyguardBlueprintViewBinder {
                repeatOnLifecycle(Lifecycle.State.CREATED) {
                    launch {
                        viewModel.blueprint.collect { blueprint ->
                            val prevBluePrint = viewModel.currentBluePrint
                            Trace.beginSection("KeyguardBlueprint#applyBlueprint")
                            Log.d(TAG, "applying blueprint: $blueprint")
                            if (blueprint != viewModel.currentBluePrint) {
                                viewModel.currentBluePrint?.onDestroy()
                            }
                            val constraintSet =
                            // Add and remove views of sections that are not contained by the other.
                            prevBluePrint?.removeViews(blueprint, constraintLayout)
                            blueprint.addViews(prevBluePrint, constraintLayout)

                            ConstraintSet().apply {
                                clone(constraintLayout)
                                val emptyLayout = ConstraintSet.Layout()
                                    knownIds.forEach {
                                        getConstraint(it).layout.copyFrom(emptyLayout)
                                    }
                                    blueprint.addViews(constraintLayout)
                                knownIds.forEach { getConstraint(it).layout.copyFrom(emptyLayout) }
                                blueprint.applyConstraints(this)
                                applyTo(constraintLayout)
                            }
                            // Remove all unconstrained views.
                            constraintLayout.children
                                .filterNot { constraintSet.knownIds.contains(it.id) }
                                .forEach { constraintLayout.removeView(it) }

                            viewModel.currentBluePrint = blueprint
                            Trace.endSection()
Loading