-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Derive day coordinates from item root layout
- Loading branch information
1 parent
ca4d959
commit 77a21b4
Showing
13 changed files
with
627 additions
and
528 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 23 additions & 49 deletions
72
...tform/library/src/commonMain/kotlin/com/kizitonwose/calendar/compose/ItemPlacementInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,58 @@ | ||
package com.kizitonwose.calendar.compose | ||
|
||
import androidx.compose.foundation.gestures.Orientation | ||
import androidx.compose.runtime.Immutable | ||
import androidx.compose.runtime.Stable | ||
import androidx.compose.runtime.withFrameNanos | ||
import androidx.compose.ui.geometry.Offset | ||
import androidx.compose.ui.layout.LayoutCoordinates | ||
import androidx.compose.ui.layout.positionInWindow | ||
import androidx.compose.ui.unit.round | ||
import kotlinx.coroutines.isActive | ||
import kotlin.coroutines.coroutineContext | ||
|
||
@Immutable | ||
internal data class ItemCoordinates( | ||
val itemRootCoordinates: LayoutCoordinates, | ||
val firstDayCoordinates: LayoutCoordinates, | ||
) | ||
|
||
@Stable | ||
internal class ItemPlacementInfo { | ||
private var calendarCoordinates: LayoutCoordinates? = null | ||
private var firstDayCoordinates: LayoutCoordinates? = null | ||
|
||
fun onCalendarPlaced(coordinates: LayoutCoordinates) { | ||
calendarCoordinates = coordinates | ||
} | ||
private var itemCoordinates: ItemCoordinates? = null | ||
|
||
fun onFirstDayPlaced(coordinates: LayoutCoordinates) { | ||
firstDayCoordinates = coordinates | ||
fun onItemPlaced(itemCoordinates: ItemCoordinates) { | ||
this.itemCoordinates = itemCoordinates | ||
} | ||
|
||
suspend fun awaitFistDayOffsetAndSize(orientation: Orientation): DayOffsetSize? { | ||
var calendarCoord: LayoutCoordinates? = null | ||
var firstDayCoord: LayoutCoordinates? = null | ||
while (coroutineContext.isActive && | ||
(calendarCoord == null || firstDayCoord == null) | ||
) { | ||
calendarCoord = calendarCoordinates | ||
firstDayCoord = firstDayCoordinates | ||
if (calendarCoord == null || firstDayCoord == null) { | ||
withFrameNanos {} | ||
} | ||
suspend fun awaitFistDayOffsetAndSize(orientation: Orientation): OffsetSize? { | ||
var itemCoordinates = this.itemCoordinates | ||
while (coroutineContext.isActive && itemCoordinates == null) { | ||
withFrameNanos {} | ||
itemCoordinates = this.itemCoordinates | ||
} | ||
if (calendarCoord == null || | ||
firstDayCoord == null || | ||
!calendarCoord.isAttached || | ||
!firstDayCoord.isAttached | ||
) { | ||
if (itemCoordinates == null) { | ||
return null | ||
} | ||
val itemViewCoord = findItemViewCoordinates(firstDayCoord, calendarCoord) | ||
val daySize = firstDayCoord.size | ||
val dayOffset = itemViewCoord.localPositionOf(firstDayCoord, Offset.Zero).round() | ||
val (itemRootCoordinates, firstDayCoordinates) = itemCoordinates | ||
val daySize = firstDayCoordinates.size | ||
val dayOffset = itemRootCoordinates.localPositionOf(firstDayCoordinates).round() | ||
return when (orientation) { | ||
Orientation.Vertical -> DayOffsetSize( | ||
Orientation.Vertical -> OffsetSize( | ||
offset = dayOffset.y, | ||
size = daySize.height, | ||
) | ||
|
||
Orientation.Horizontal -> { | ||
DayOffsetSize( | ||
OffsetSize( | ||
offset = dayOffset.x, | ||
size = daySize.width, | ||
) | ||
} | ||
} | ||
} | ||
|
||
internal data class DayOffsetSize( | ||
@Immutable | ||
internal data class OffsetSize( | ||
val offset: Int, | ||
val size: Int, | ||
) | ||
} | ||
|
||
internal fun findItemViewCoordinates( | ||
firstDayCoord: LayoutCoordinates, | ||
calendarCoord: LayoutCoordinates, | ||
): LayoutCoordinates { | ||
var itemViewCoord = firstDayCoord | ||
var parent = itemViewCoord.parentLayoutCoordinates | ||
// Find the coordinates that match the index item layout | ||
while (parent != null && | ||
parent.size != calendarCoord.size && | ||
parent.positionInWindow() != calendarCoord.positionInWindow() | ||
) { | ||
itemViewCoord = parent | ||
parent = itemViewCoord.parentLayoutCoordinates | ||
} | ||
return itemViewCoord | ||
} |
Oops, something went wrong.