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 12a256b
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 14 deletions.
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 12a256b

Please sign in to comment.