Skip to content

Commit 35ea0f5

Browse files
committed
Android 5 - 6 support, minor improvements
1 parent 9a98b30 commit 35ea0f5

File tree

19 files changed

+975
-246
lines changed

19 files changed

+975
-246
lines changed

SpannedGridLayoutManager/src/main/java/com/arasthel/spannedgridlayoutmanager/InvalidMaxSpansException.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@ package com.arasthel.spannedgridlayoutmanager
77
/**
88
* Exception thrown when the span size of the layout manager is 0 or negative
99
*/
10-
class InvalidMaxSpansException(maxSpanSize: Int) :
11-
RuntimeException("Invalid layout spans: $maxSpanSize. Span size must be at least 1.")
10+
class InvalidMaxSpansException(maxSpanSize: Int) : RuntimeException("Invalid layout spans: $maxSpanSize. Span size must be at least 1.")

SpannedGridLayoutManager/src/main/java/com/arasthel/spannedgridlayoutmanager/InvalidSpanSizeException.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ package com.arasthel.spannedgridlayoutmanager
44
* Exception thrown when the SpanSize of the item is smaller than 1 or bigger than the layout
55
* manager's max span size.
66
*/
7-
class InvalidSpanSizeException(errorSize: Int, maxSpanSize: Int) :
8-
RuntimeException("Invalid item span size: $errorSize. Span size must be in the range: (1...$maxSpanSize)")
7+
class InvalidSpanSizeException(errorSize: Int, maxSpanSize: Int) : RuntimeException("Invalid item span size: $errorSize. Span size must be in the range: (1...$maxSpanSize)")

SpannedGridLayoutManager/src/main/java/com/arasthel/spannedgridlayoutmanager/RectExtensions.kt

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import android.graphics.Rect
1010
* Created by Jorge Martín on 4/6/17.
1111
*/
1212

13-
1413
fun Rect.isAdjacentTo(rect: Rect): Boolean {
1514
return (this.right == rect.left
1615
|| this.top == rect.bottom

SpannedGridLayoutManager/src/main/java/com/arasthel/spannedgridlayoutmanager/SpannedGridLayoutManager.kt

+23-171
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,6 @@ open class SpannedGridLayoutManager(
6666
* by the number of columns in the layout, but you can set your own dimension with [customWidth].
6767
*/
6868
val itemWidth: Int get() = if (customWidth > 0) customWidth else ceil(decoratedWidth.toFloat() / columnCount.toFloat()).toInt()
69-
/**
70-
* The height of each item. Normally this is the height of the RecyclerView, divided
71-
* by the number of rows in the layout, but you can set your own dimension with [customHeight].
72-
*/
73-
val itemHeight: Int get() = if (customHeight > 0) customHeight else ceil(decoratedHeight.toFloat() / rowCount.toFloat()).toInt()
7469

7570
/**
7671
* Delegate some orientation-specific logic.
@@ -387,8 +382,8 @@ open class SpannedGridLayoutManager(
387382
* TODO: Maybe add a new function to override instead.
388383
*/
389384
open class SpanSizeLookup(
390-
/** Used to provide an SpanSize for each item. */
391-
private var lookupFunction: ((Int) -> SpanSize)? = null
385+
/** Used to provide an SpanSize for each item. */
386+
private var lookupFunction: ((Int) -> SpanSize)? = null
392387
) {
393388

394389
/**
@@ -420,8 +415,8 @@ open class SpannedGridLayoutManager(
420415

421416
override fun generateDefaultLayoutParams(): RecyclerView.LayoutParams {
422417
return RecyclerView.LayoutParams(
423-
RecyclerView.LayoutParams.MATCH_PARENT,
424-
RecyclerView.LayoutParams.WRAP_CONTENT
418+
RecyclerView.LayoutParams.MATCH_PARENT,
419+
RecyclerView.LayoutParams.WRAP_CONTENT
425420
)
426421
}
427422

@@ -489,8 +484,6 @@ open class SpannedGridLayoutManager(
489484
fillGap(Direction.END, recycler, state)
490485
fillGap(Direction.START, recycler, state)
491486

492-
recycleChildrenOutOfBounds(Direction.END, recycler)
493-
recycleChildrenOutOfBounds(Direction.START, recycler)
494487
}
495488

496489
/**
@@ -511,8 +504,8 @@ open class SpannedGridLayoutManager(
511504
// Multiply the rect for item width and height to get positions
512505
val left = rect.left * itemWidth
513506
val right = rect.right * itemWidth
514-
val top = rect.top * itemHeight
515-
val bottom = rect.bottom * itemHeight
507+
val top = rect.top * itemWidth
508+
val bottom = rect.bottom * itemWidth
516509

517510
val insetsRect = Rect()
518511
calculateItemDecorationsForChild(view, insetsRect)
@@ -588,103 +581,11 @@ open class SpannedGridLayoutManager(
588581
protected open fun updateEdgesWithNewChild(view: View) {
589582
val childStart = getChildStart(view) + scroll + getPaddingStartForOrientation()
590583

591-
if (childStart < layoutStart) {
592-
layoutStart = childStart
593-
}
584+
if (childStart < layoutStart) layoutStart = childStart
594585

595586
val newLayoutEnd = childStart + getItemSizeForOrientation()
596587

597-
if (newLayoutEnd > layoutEnd) {
598-
layoutEnd = newLayoutEnd
599-
}
600-
}
601-
602-
//==============================================================================================
603-
// ~ Recycling methods
604-
//==============================================================================================
605-
606-
/**
607-
* Recycle any views that are out of bounds
608-
*/
609-
protected open fun recycleChildrenOutOfBounds(direction: Direction, recycler: RecyclerView.Recycler) {
610-
//No more lag.
611-
/**
612-
if (direction == Direction.END) {
613-
recycleChildrenFromStart(direction, recycler)
614-
} else {
615-
recycleChildrenFromEnd(direction, recycler)
616-
}
617-
*/
618-
}
619-
620-
/**
621-
* Recycle views from start to first visible item
622-
*/
623-
protected open fun recycleChildrenFromStart(direction: Direction, recycler: RecyclerView.Recycler) {
624-
//No more lag.
625-
/**
626-
val childCount = childCount
627-
val start = getPaddingStartForOrientation()
628-
629-
val toDetach = mutableListOf<View>()
630-
631-
for (i in 0 until childCount) {
632-
getChildAt(i)?.let { child ->
633-
val childEnd = getChildEnd(child)
634-
635-
if (childEnd < start) {
636-
toDetach.add(child)
637-
}
638-
}
639-
640-
}
641-
642-
for (child in toDetach) {
643-
removeAndRecycleView(child, recycler)
644-
updateEdgesWithRemovedChild(child, direction)
645-
} */
646-
}
647-
648-
/**
649-
* Recycle views from end to last visible item
650-
*/
651-
protected open fun recycleChildrenFromEnd(direction: Direction, recycler: RecyclerView.Recycler) {
652-
val childCount = childCount
653-
val end = size + getPaddingEndForOrientation()
654-
655-
val toDetach = mutableListOf<View>()
656-
657-
for (i in (0 until childCount).reversed()) {
658-
getChildAt(i)?.let { child ->
659-
val childStart = getChildStart(child)
660-
661-
if (childStart > end) {
662-
toDetach.add(child)
663-
}
664-
}
665-
}
666-
667-
for (child in toDetach) {
668-
removeAndRecycleView(child, recycler)
669-
updateEdgesWithRemovedChild(child, direction)
670-
}
671-
}
672-
673-
/**
674-
* Update layout edges when views are recycled
675-
*/
676-
protected open fun updateEdgesWithRemovedChild(view: View, direction: Direction) {
677-
//No more lag.
678-
/**
679-
val childStart = getChildStart(view) + scroll
680-
val childEnd = getChildEnd(view) + scroll
681-
682-
if (direction == Direction.END) { // Removed from start
683-
layoutStart = getPaddingStartForOrientation() + childEnd
684-
} else if (direction == Direction.START) { // Removed from end
685-
layoutEnd = getPaddingStartForOrientation() + childStart
686-
}
687-
**/
588+
if (newLayoutEnd > layoutEnd) layoutEnd = newLayoutEnd
688589
}
689590

690591
override fun computeVerticalScrollOffset(state: RecyclerView.State): Int {
@@ -747,9 +648,7 @@ open class SpannedGridLayoutManager(
747648

748649
protected open fun scrollBy(delta: Int, recycler: RecyclerView.Recycler, state: RecyclerView.State): Int {
749650
// If there are no view or no movement, return
750-
if (delta == 0) {
751-
return 0
752-
}
651+
if (delta == 0) return 0
753652

754653
val (childEnd, _, _) = getGreatestChildEnd()
755654

@@ -761,15 +660,11 @@ open class SpannedGridLayoutManager(
761660
(size) < (childEnd) &&
762661
delta > 0
763662
// If can't scroll forward or backwards, return
764-
if (!(canScrollBackwards || canScrollForward)) {
765-
return 0
766-
}
663+
if (!(canScrollBackwards || canScrollForward)) return 0
767664

768665
val correctedDistance = scrollBy(-delta, state, childEnd)
769666
val direction = if (delta > 0) Direction.END else Direction.START
770667

771-
recycleChildrenOutOfBounds(direction, recycler)
772-
773668
fillGap(direction, recycler, state)
774669

775670
return -correctedDistance
@@ -808,9 +703,7 @@ open class SpannedGridLayoutManager(
808703
pendingScrollToPosition = position.coerceAtMost(itemCount - 1)
809704
.coerceAtLeast(0)
810705

811-
if (wasNull && recyclerView?.isInLayout == false) {
812-
requestLayout()
813-
}
706+
if (wasNull && recyclerView?.isInLayout == false) requestLayout()
814707
}
815708

816709
/**
@@ -1062,11 +955,7 @@ open class SpannedGridLayoutManager(
1062955
}
1063956

1064957
protected open fun getItemSizeForOrientation(): Int {
1065-
return if (orientation == VERTICAL) {
1066-
itemHeight
1067-
} else {
1068-
itemWidth
1069-
}
958+
return itemWidth
1070959
}
1071960

1072961
open fun getSpanCountForOrientation(): Int {
@@ -1174,41 +1063,25 @@ open class RectsHelper(private val layoutManager: SpannedGridLayoutManager,
11741063
* Start row/column for free rects
11751064
*/
11761065
val start: Int get() {
1177-
return if (orientation == VERTICAL) {
1178-
freeRects[0].top * layoutManager.itemHeight
1179-
} else {
1180-
freeRects[0].left * layoutManager.itemWidth
1181-
}
1066+
return freeRects[0].left * layoutManager.itemWidth
11821067
}
11831068

11841069
/**
11851070
* End row/column for free rects
11861071
*/
11871072
val end: Int get() {
1188-
return if (orientation == VERTICAL) {
1189-
(freeRects.last().bottom + 1) * layoutManager.itemHeight
1190-
} else {
1191-
(freeRects.last().right + 1) * layoutManager.itemWidth
1192-
}
1073+
return (freeRects.last().right + 1) * layoutManager.itemWidth
11931074
}
11941075

11951076
val lastStart: Int get() {
1196-
return if (orientation == VERTICAL) {
1197-
(freeRects.last().top) * layoutManager.itemHeight
1198-
} else {
1199-
(freeRects.last().left) * layoutManager.itemWidth
1200-
}
1077+
return (freeRects.last().left) * layoutManager.itemWidth
12011078
}
12021079

12031080
val largestStart: Int get() {
12041081
var largest = 0
12051082

12061083
freeRects.forEach {
1207-
val s = if (orientation == VERTICAL) {
1208-
it.top * layoutManager.itemHeight
1209-
} else {
1210-
it.left * layoutManager.itemWidth
1211-
}
1084+
val s = it.left * layoutManager.itemWidth
12121085

12131086
if (s > largest) {
12141087
largest = s
@@ -1222,12 +1095,7 @@ open class RectsHelper(private val layoutManager: SpannedGridLayoutManager,
12221095
var largest = 0
12231096

12241097
freeRects.forEach {
1225-
val s = if (orientation == VERTICAL) {
1226-
it.bottom * layoutManager.itemHeight
1227-
} else {
1228-
it.right * layoutManager.itemWidth
1229-
}
1230-
1098+
val s = it.right * layoutManager.itemWidth
12311099
if (s > largest) {
12321100
largest = s
12331101
}
@@ -1261,19 +1129,11 @@ open class RectsHelper(private val layoutManager: SpannedGridLayoutManager,
12611129
}
12621130

12631131
fun getEndForPosition(position: Int): Int {
1264-
return if (orientation == VERTICAL) {
1265-
(rectsCache[position]!!.bottom) * layoutManager.itemHeight
1266-
} else {
1267-
(rectsCache[position]!!.right) * layoutManager.itemWidth
1268-
}
1132+
return (rectsCache[position]!!.right) * layoutManager.itemWidth
12691133
}
12701134

12711135
fun getStartForPosition(position: Int): Int {
1272-
return if (orientation == VERTICAL) {
1273-
(rectsCache[position]!!.top) * layoutManager.itemHeight
1274-
} else {
1275-
(rectsCache[position]!!.left) * layoutManager.itemWidth
1276-
}
1136+
return (rectsCache[position]!!.left) * layoutManager.itemWidth
12771137
}
12781138

12791139
/**
@@ -1336,21 +1196,13 @@ open class RectsHelper(private val layoutManager: SpannedGridLayoutManager,
13361196
} else {
13371197
freeRects.remove(free)
13381198

1339-
if (free.left < subtractedRect.left) { // Left
1340-
possibleNewRects.add(Rect(free.left, free.top, subtractedRect.left, free.bottom))
1341-
}
1199+
if (free.left < subtractedRect.left) possibleNewRects.add(Rect(free.left, free.top, subtractedRect.left, free.bottom))
13421200

1343-
if (free.right > subtractedRect.right) { // Right
1344-
possibleNewRects.add(Rect(subtractedRect.right, free.top, free.right, free.bottom))
1345-
}
1201+
if (free.right > subtractedRect.right) possibleNewRects.add(Rect(subtractedRect.right, free.top, free.right, free.bottom))
13461202

1347-
if (free.top < subtractedRect.top) { // Top
1348-
possibleNewRects.add(Rect(free.left, free.top, free.right, subtractedRect.top))
1349-
}
1203+
if (free.top < subtractedRect.top) possibleNewRects.add(Rect(free.left, free.top, free.right, subtractedRect.top))
13501204

1351-
if (free.bottom > subtractedRect.bottom) { // Bottom
1352-
possibleNewRects.add(Rect(free.left, subtractedRect.bottom, free.right, free.bottom))
1353-
}
1205+
if (free.bottom > subtractedRect.bottom) possibleNewRects.add(Rect(free.left, subtractedRect.bottom, free.right, free.bottom))
13541206
}
13551207
}
13561208

app/build.gradle

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ android {
1515
}
1616
defaultConfig {
1717
applicationId "ru.queuejw.mpl"
18-
minSdk 24
18+
minSdk 21
1919
targetSdk 35
2020
// 80xxxx ; 81xxxx ; 100xxxx
2121
versionCode 800001
@@ -48,16 +48,16 @@ android {
4848
}
4949
dependencies {
5050

51-
ksp "androidx.room:room-compiler:2.7.0-beta01"
51+
ksp 'androidx.room:room-compiler:2.7.0-rc02'
5252

5353
implementation 'androidx.appcompat:appcompat:1.7.0'
5454
implementation 'androidx.annotation:annotation:1.9.1'
55-
implementation 'com.google.android.material:material:1.13.0-alpha10'
55+
implementation 'com.google.android.material:material:1.13.0-alpha11'
5656
implementation 'androidx.recyclerview:recyclerview:1.4.0'
5757
implementation 'androidx.core:core-ktx:1.15.0'
5858
implementation 'androidx.fragment:fragment-ktx:1.8.6'
59-
implementation 'androidx.room:room-runtime:2.7.0-beta01'
60-
implementation 'androidx.room:room-ktx:2.7.0-beta01'
59+
implementation 'androidx.room:room-runtime:2.7.0-rc02'
60+
implementation 'androidx.room:room-ktx:2.7.0-rc02'
6161
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.8.7'
6262
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.8.7'
6363
implementation 'androidx.work:work-runtime-ktx:2.10.0'

app/src/main/AndroidManifest.xml

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
android:supportsRtl="true"
2727
android:enableOnBackInvokedCallback="true"
2828
tools:targetApi="33">
29+
2930
<activity
3031
android:icon="@mipmap/launcher_icon"
3132
android:name="ru.queuejw.mpl.Main"

0 commit comments

Comments
 (0)