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

Commit 26483f8f authored by Mohammed Althaf T's avatar Mohammed Althaf T 😊
Browse files

feat: Fix random widget spancount issue

parent 38dbf53d
Loading
Loading
Loading
Loading
+21 −19
Original line number Diff line number Diff line
@@ -84,6 +84,7 @@ class WidgetContainer(context: Context, attrs: AttributeSet?) :
    private lateinit var mWidgetLinearLayout: LinearLayout
    private lateinit var mRecyclerView: RecyclerView
    private lateinit var mWidgetAdapter: StaggeredAdapter
    private var lastOrientation: Int = Configuration.ORIENTATION_UNDEFINED

    private val mResizeContainerRect = Rect()
    private val mInsetPadding =
@@ -121,6 +122,7 @@ class WidgetContainer(context: Context, attrs: AttributeSet?) :
            context.startActivity(intent)
        }

        lastOrientation = resources.configuration.orientation
        mRecyclerView = findViewWithTag("wrapper_children")
        mWidgetAdapter = mRecyclerView.adapter as StaggeredAdapter
        mWidgetAdapter.addOnDataChangedListener(this)
@@ -131,20 +133,19 @@ class WidgetContainer(context: Context, attrs: AttributeSet?) :

    override fun onConfigurationChanged(newConfig: Configuration?) {
        super.onConfigurationChanged(newConfig)

        // Reload layout if orientation has changed
        newConfig?.let {
            if (it.orientation != lastOrientation) {
                lastOrientation = it.orientation
                reloadStaggeredLayout()
            }
        }
    }

    fun reloadStaggeredLayout() {
        updatePadding()
        val spanCount =
            if (
                mLauncher != null &&
                    (mLauncher.deviceProfile.isTablet || mLauncher.deviceProfile.isLandscape)
            ) {
                2
            } else {
                1
            }
        val spanCount = getSpanCount(mLauncher)
        mRecyclerView.layoutManager =
            NoScrollStaggeredLayoutManager(spanCount, StaggeredGridLayoutManager.VERTICAL)
    }
@@ -354,15 +355,7 @@ class WidgetContainer(context: Context, attrs: AttributeSet?) :
        ): View {
            widgetsDbHelper = WidgetsDbHelper.getInstance(context)
            widgetsAdapter = StaggeredAdapter()
            val spanCount =
                if (
                    launcher != null &&
                        (launcher.deviceProfile.isTablet || launcher.deviceProfile.isLandscape)
                ) {
                    2
                } else {
                    1
                }
            val spanCount = getSpanCount(launcher)
            recyclerView =
                RecyclerView(context, null).apply {
                    tag = "wrapper_children"
@@ -713,5 +706,14 @@ class WidgetContainer(context: Context, attrs: AttributeSet?) :
                    ?.getInsets(WindowInsets.Type.systemBars())
            return windowInsets ?: Insets.NONE
        }

        fun getSpanCount(launcher: Launcher?): Int {
            val profile = launcher?.deviceProfile ?: return 1
            return when {
                profile.isTablet -> 2
                profile.isPhone && profile.isLandscape -> 2
                else -> 1
            }
        }
    }
}