Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #11631 from K0bin/pref-divider
Android: Only show divider in settings for headers
  • Loading branch information
t895 committed Mar 9, 2023
2 parents 2856723 + 1b47144 commit a6b1b2f
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 12 deletions.
Expand Up @@ -22,8 +22,8 @@ class CheatItem {
}

companion object {
const val TYPE_CHEAT = 0
const val TYPE_HEADER = 1
const val TYPE_HEADER = 0
const val TYPE_CHEAT = 1
const val TYPE_ACTION = 2
}
}
Expand Up @@ -13,11 +13,11 @@ import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.color.MaterialColors
import com.google.android.material.divider.MaterialDividerItemDecoration
import com.google.android.material.elevation.ElevationOverlayProvider
import org.dolphinemu.dolphinemu.R
import org.dolphinemu.dolphinemu.databinding.FragmentCheatListBinding
import org.dolphinemu.dolphinemu.features.cheats.model.CheatsViewModel
import org.dolphinemu.dolphinemu.features.settings.ui.SettingsDividerItemDecoration

class CheatListFragment : Fragment() {
private var _binding: FragmentCheatListBinding? = null
Expand All @@ -39,8 +39,7 @@ class CheatListFragment : Fragment() {
binding.cheatList.adapter = CheatsAdapter(activity, viewModel)
binding.cheatList.layoutManager = LinearLayoutManager(activity)

val divider = MaterialDividerItemDecoration(requireActivity(), LinearLayoutManager.VERTICAL)
divider.isLastItemDecorated = false
val divider = SettingsDividerItemDecoration(requireActivity())
binding.cheatList.addItemDecoration(divider)

@ColorInt val color =
Expand Down
@@ -0,0 +1,118 @@
// SPDX-License-Identifier: GPL-2.0-or-later

/*
* Partially based on: MaterialDividerItemDecoration
*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.dolphinemu.dolphinemu.features.settings.ui

import android.content.Context
import android.graphics.Canvas
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.graphics.drawable.ShapeDrawable
import android.view.View
import androidx.annotation.Px
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.DrawableCompat
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import org.dolphinemu.dolphinemu.R
import org.dolphinemu.dolphinemu.features.settings.model.view.SettingsItem
import kotlin.math.max
import kotlin.math.roundToInt

class SettingsDividerItemDecoration(context: Context) : ItemDecoration() {
private var dividerDrawable: Drawable

@Px
private var dividerThickness: Int

private val tempRect = Rect()

init {
val attrs = context.obtainStyledAttributes(intArrayOf(R.attr.colorSurfaceVariant))
val dividerColor =
attrs.getColor(0, ContextCompat.getColor(context, R.color.dolphin_surfaceVariant))
attrs.recycle()
val shape = ShapeDrawable()
dividerThickness = max(context.resources.displayMetrics.density.roundToInt(), 1)

dividerDrawable = DrawableCompat.wrap(shape)
DrawableCompat.setTint(dividerDrawable, dividerColor)
}

private fun isNextViewAHeader(view: View, parent: RecyclerView): Boolean {
val index = parent.indexOfChild(view)
if (index == parent.childCount - 1) {
return false
}

val nextChild = parent.getChildAt(index + 1)
val viewHolder = parent.getChildViewHolder(nextChild)
// CheatsItem.TYPE_HEADER == SettingsItem.TYPE_HEADER
return viewHolder.itemViewType == SettingsItem.TYPE_HEADER
}

override fun onDraw(
canvas: Canvas, parent: RecyclerView, state: RecyclerView.State
) {
if (parent.layoutManager == null) {
return
}

canvas.save()
val left: Int
val right: Int
if (parent.clipToPadding) {
left = parent.paddingLeft
right = parent.width - parent.paddingRight
canvas.clipRect(
left, parent.paddingTop, right, parent.height - parent.paddingBottom
)
} else {
left = 0
right = parent.width
}

val dividerCount = parent.childCount - 1
for (i in 0 until dividerCount) {
val child = parent.getChildAt(i)

if (!isNextViewAHeader(child, parent)) {
continue
}

parent.getDecoratedBoundsWithMargins(child, tempRect)
// Take into consideration any translationY added to the view.
val bottom = tempRect.bottom + child.translationY.roundToInt()
val top = bottom - dividerDrawable.intrinsicHeight - dividerThickness
dividerDrawable.setBounds(left, top, right, bottom)
dividerDrawable.draw(canvas)
}
canvas.restore()
}

override fun getItemOffsets(
outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State
) {
if (!isNextViewAHeader(view, parent)) {
return
}
outRect.bottom = dividerDrawable.intrinsicHeight + dividerThickness
}
}
Expand Up @@ -17,8 +17,6 @@
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.divider.MaterialDividerItemDecoration;

import org.dolphinemu.dolphinemu.R;
import org.dolphinemu.dolphinemu.databinding.FragmentSettingsBinding;
import org.dolphinemu.dolphinemu.features.settings.model.Settings;
Expand Down Expand Up @@ -146,9 +144,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
recyclerView.setAdapter(mAdapter);
recyclerView.setLayoutManager(manager);

MaterialDividerItemDecoration divider =
new MaterialDividerItemDecoration(requireActivity(), LinearLayoutManager.VERTICAL);
divider.setLastItemDecorated(false);
SettingsDividerItemDecoration divider = new SettingsDividerItemDecoration(requireActivity());
recyclerView.addItemDecoration(divider);

setInsets();
Expand Down
3 changes: 1 addition & 2 deletions Source/Android/app/src/main/res/layout/list_item_header.xml
Expand Up @@ -14,8 +14,7 @@
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="@dimen/spacing_large"
android:layout_marginBottom="2dp"
android:gravity="start|bottom"
android:gravity="start|center_vertical"
android:textColor="?attr/colorPrimary"
android:textStyle="bold"
android:textAlignment="viewStart"
Expand Down

0 comments on commit a6b1b2f

Please sign in to comment.