oleksandrbalan / programguide

Lazy layout to display program guide data on the two directional plane.
Apache License 2.0
40 stars 7 forks source link

Focus on first item by default #1

Closed techker closed 1 year ago

techker commented 1 year ago

Hey, Great work on this. i work in Android TV aps and adapted it for TV, i was wondering if there is a way to dynamicly set focus on a program ?

By default first program or from history last channel..

Thx

oleksandrbalan commented 1 year ago

Hi 👋 Thank you!

if there is a way to dynamicly set focus on a program

Yes, first of all you can use state to scroll to the offset you need.

For example to scroll to the current time line (the vertical one):

val scope = rememberCoroutineScope()
val state = rememberProgramGuideState()
Button(
    onClick = {
        scope.launch {
            state.animateToCurrentTime()
        }
    }
) {
    Text("Focus current time")
}

There also methods to scroll to the channel, program or timeline:

state.animateToChannel(index = ...)
state.animateToProgram(index = ...)
state.animateToTimeline(index = ...)

You may also use snapTo* methods to scroll immediately without animation. Each method accepts alignment to control where item is positioned after scroll, by default alignment is set to center.

Secondly, when state is created you could also specify the initial offset:

val state = rememberProgramGuideState(
    initialOffset = { getProgramOffset(index = ...) }
)

The initialOffset lambda should return Offset where scroll position is initialized. This way you may control which item is focused when ProgramGuide is loaded. There are bunch of handful methods to use inside initialOffset scope. These methods also accept alignment which works the same as in animateTo* and snapTo* methods.

getProgramOffset(index = ...)
getChannelPosition(index = ...)
getTimelinePosition(index = ...)
getCurrentTimePosition()

Also do not forget to pass the state instance to the ProgramGuide.

ProgramGuide(
    state = state, 
) { ... }

Check the ProgramGuideStateScreen sample to see how it could be used.

techker commented 1 year ago

ah great! il test out the different ways.thx!👍

On Mon, Jun 5, 2023, 4:08 p.m. Oleksandr Balan @.***> wrote:

Hi 👋 Thank you!

if there is a way to dynamicly set focus on a program

Yes, first of all you can use state to scroll to the offset you need.

For example to scroll to the current time line (the vertical one):

val scope = rememberCoroutineScope()val state = rememberProgramGuideState()Button( onClick = { scope.launch { state.animateToCurrentTime() } } ) { Text("Focus current time") }

There also methods to scroll to the channel, program or timeline:

state.animateToChannel(index = ...) state.animateToProgram(index = ...) state.animateToTimeline(index = ...)

You may also use snapTo* methods to scroll immediately without animation. Each method accepts alignment to control where item is positioned after scroll, by default alignment is set to center.

Secondly, when state is created you could also specify the initial offset:

val state = rememberProgramGuideState( initialOffset = { getProgramOffset(index = ...) } )

The initialOffset lambda should return Offset where scroll position is initialized. This way you may control which item is focused when ProgramGuide is loaded. There are bunch of handful methods to use inside initialOffset scope. These methods also accept alignment which works the same as in animateTo and snapTo methods.

getProgramOffset(index = ...) getChannelPosition(index = ...) getTimelinePosition(index = ...) getCurrentTimePosition()

Also do not forget to pass the state instance to the ProgramGuide.

ProgramGuide( state = state, ) { ... }

Check the ProgramGuideStateScreen https://github.com/oleksandrbalan/programguide/blob/main/demo/src/main/java/eu/wewox/programguide/demo/screens/ProgramGuideStateScreen.kt sample to see how it could be used.

— Reply to this email directly, view it on GitHub https://github.com/oleksandrbalan/programguide/issues/1#issuecomment-1577406634, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABOFGYTHWY3HTOCQXKULS73XJY4DLANCNFSM6AAAAAAY27S4II . You are receiving this because you authored the thread.Message ID: @.***>