Skip to content

Commit e1dd978

Browse files
committed
Improve LayoutHelper usage
1 parent aea1992 commit e1dd978

File tree

6 files changed

+16
-20
lines changed

6 files changed

+16
-20
lines changed

view/api/view.api

+3-6
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,9 @@ public final class com/kizitonwose/calendar/view/DaySize : java/lang/Enum {
7474
public static fun values ()[Lcom/kizitonwose/calendar/view/DaySize;
7575
}
7676

77-
public abstract interface class com/kizitonwose/calendar/view/LayoutHelper {
78-
public abstract fun calculateExtraLayoutSpace (Landroidx/recyclerview/widget/RecyclerView$State;[I)V
79-
}
80-
81-
public final class com/kizitonwose/calendar/view/LayoutHelper$DefaultImpls {
82-
public static fun calculateExtraLayoutSpace (Lcom/kizitonwose/calendar/view/LayoutHelper;Landroidx/recyclerview/widget/RecyclerView$State;[I)V
77+
public abstract class com/kizitonwose/calendar/view/LayoutHelper {
78+
public fun <init> ()V
79+
public fun getCalculateExtraLayoutSpace ()Lkotlin/jvm/functions/Function2;
8380
}
8481

8582
public final class com/kizitonwose/calendar/view/MarginValues {

view/src/main/java/com/kizitonwose/calendar/view/CalendarView.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public open class CalendarView : RecyclerView {
184184
}
185185

186186
/**
187-
* Interface with methods that can be overridden
187+
* Helper class with methods that can be overridden
188188
* in the internal layout manager.
189189
*/
190190
public var layoutHelper: LayoutHelper? = null

view/src/main/java/com/kizitonwose/calendar/view/LayoutHelper.kt

+7-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ import androidx.recyclerview.widget.RecyclerView
55
import com.kizitonwose.calendar.view.internal.CalendarLayoutManager
66

77
/**
8-
* An interface with methods that can be overridden
9-
* in the internal [LinearLayoutManager].
8+
* Helper class with properties that match the methods that can
9+
* be overridden in the internal [LinearLayoutManager]. This is
10+
* an abstract class instead of an interface so we can have
11+
* default values for properties as we need to call `super`
12+
* for properties that are not provided (null).
1013
*/
11-
public interface LayoutHelper {
14+
public abstract class LayoutHelper {
1215
/**
1316
* Calculates the amount of extra space (in pixels) that should be laid out by
1417
* [CalendarLayoutManager] and stores the result in [extraLayoutSpace].
@@ -18,5 +21,5 @@ public interface LayoutHelper {
1821
*
1922
* @see [LinearLayoutManager.calculateExtraLayoutSpace]
2023
*/
21-
public fun calculateExtraLayoutSpace(state: RecyclerView.State, extraLayoutSpace: IntArray) {}
24+
public open val calculateExtraLayoutSpace: ((state: RecyclerView.State, extraLayoutSpace: IntArray) -> Unit)? = null
2225
}

view/src/main/java/com/kizitonwose/calendar/view/WeekCalendarView.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public open class WeekCalendarView : RecyclerView {
151151
}
152152

153153
/**
154-
* Interface with methods that can be overridden
154+
* Helper class with methods that can be overridden
155155
* in the internal layout manager.
156156
*/
157157
public var layoutHelper: LayoutHelper? = null

view/src/main/java/com/kizitonwose/calendar/view/YearCalendarView.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ public open class YearCalendarView : RecyclerView {
327327
}
328328

329329
/**
330-
* Interface with methods that can be overridden
330+
* Helper class with methods that can be overridden
331331
* in the internal layout manager.
332332
*/
333333
public var layoutHelper: LayoutHelper? = null

view/src/main/java/com/kizitonwose/calendar/view/internal/CalendarLayoutManager.kt

+3-7
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,9 @@ internal abstract class CalendarLayoutManager<IndexData, DayData>(
7373
}
7474

7575
override fun calculateExtraLayoutSpace(state: RecyclerView.State, extraLayoutSpace: IntArray) {
76-
val layoutHelper = getLayoutHelper()
77-
if (layoutHelper != null) {
78-
layoutHelper.calculateExtraLayoutSpace(state, extraLayoutSpace)
79-
// If the interface is provided but the method is not overridden.
80-
if (extraLayoutSpace.isEmpty()) {
81-
super.calculateExtraLayoutSpace(state, extraLayoutSpace)
82-
}
76+
val calculateExtraLayoutSpace = getLayoutHelper()?.calculateExtraLayoutSpace
77+
if (calculateExtraLayoutSpace != null) {
78+
calculateExtraLayoutSpace(state, extraLayoutSpace)
8379
} else {
8480
super.calculateExtraLayoutSpace(state, extraLayoutSpace)
8581
}

0 commit comments

Comments
 (0)