Skip to content

Commit

Permalink
Improve LayoutHelper usage
Browse files Browse the repository at this point in the history
  • Loading branch information
kizitonwose committed Jan 15, 2025
1 parent aea1992 commit e1dd978
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 20 deletions.
9 changes: 3 additions & 6 deletions view/api/view.api
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,9 @@ public final class com/kizitonwose/calendar/view/DaySize : java/lang/Enum {
public static fun values ()[Lcom/kizitonwose/calendar/view/DaySize;
}

public abstract interface class com/kizitonwose/calendar/view/LayoutHelper {
public abstract fun calculateExtraLayoutSpace (Landroidx/recyclerview/widget/RecyclerView$State;[I)V
}

public final class com/kizitonwose/calendar/view/LayoutHelper$DefaultImpls {
public static fun calculateExtraLayoutSpace (Lcom/kizitonwose/calendar/view/LayoutHelper;Landroidx/recyclerview/widget/RecyclerView$State;[I)V
public abstract class com/kizitonwose/calendar/view/LayoutHelper {
public fun <init> ()V
public fun getCalculateExtraLayoutSpace ()Lkotlin/jvm/functions/Function2;
}

public final class com/kizitonwose/calendar/view/MarginValues {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public open class CalendarView : RecyclerView {
}

/**
* Interface with methods that can be overridden
* Helper class with methods that can be overridden
* in the internal layout manager.
*/
public var layoutHelper: LayoutHelper? = null
Expand Down
11 changes: 7 additions & 4 deletions view/src/main/java/com/kizitonwose/calendar/view/LayoutHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import androidx.recyclerview.widget.RecyclerView
import com.kizitonwose.calendar.view.internal.CalendarLayoutManager

/**
* An interface with methods that can be overridden
* in the internal [LinearLayoutManager].
* Helper class with properties that match the methods that can
* be overridden in the internal [LinearLayoutManager]. This is
* an abstract class instead of an interface so we can have
* default values for properties as we need to call `super`
* for properties that are not provided (null).
*/
public interface LayoutHelper {
public abstract class LayoutHelper {
/**
* Calculates the amount of extra space (in pixels) that should be laid out by
* [CalendarLayoutManager] and stores the result in [extraLayoutSpace].
Expand All @@ -18,5 +21,5 @@ public interface LayoutHelper {
*
* @see [LinearLayoutManager.calculateExtraLayoutSpace]
*/
public fun calculateExtraLayoutSpace(state: RecyclerView.State, extraLayoutSpace: IntArray) {}
public open val calculateExtraLayoutSpace: ((state: RecyclerView.State, extraLayoutSpace: IntArray) -> Unit)? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public open class WeekCalendarView : RecyclerView {
}

/**
* Interface with methods that can be overridden
* Helper class with methods that can be overridden
* in the internal layout manager.
*/
public var layoutHelper: LayoutHelper? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ public open class YearCalendarView : RecyclerView {
}

/**
* Interface with methods that can be overridden
* Helper class with methods that can be overridden
* in the internal layout manager.
*/
public var layoutHelper: LayoutHelper? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,9 @@ internal abstract class CalendarLayoutManager<IndexData, DayData>(
}

override fun calculateExtraLayoutSpace(state: RecyclerView.State, extraLayoutSpace: IntArray) {
val layoutHelper = getLayoutHelper()
if (layoutHelper != null) {
layoutHelper.calculateExtraLayoutSpace(state, extraLayoutSpace)
// If the interface is provided but the method is not overridden.
if (extraLayoutSpace.isEmpty()) {
super.calculateExtraLayoutSpace(state, extraLayoutSpace)
}
val calculateExtraLayoutSpace = getLayoutHelper()?.calculateExtraLayoutSpace
if (calculateExtraLayoutSpace != null) {
calculateExtraLayoutSpace(state, extraLayoutSpace)
} else {
super.calculateExtraLayoutSpace(state, extraLayoutSpace)
}
Expand Down

0 comments on commit e1dd978

Please sign in to comment.