rusinikita / trainer

GoLang interview prep questions. Terminal app with Go challenges and learning links
GNU General Public License v3.0
262 stars 8 forks source link

I want return back to challenge list without app restart #2

Closed rusinikita closed 6 months ago

rusinikita commented 1 year ago

Add 'b/backspace' button handling to return to list

mortum5 commented 6 months ago

I can try help with it but as i see it not obvious now. Choose returns play.New and i dont know how to return to the previous view

rusinikita commented 6 months ago

I'll look and write you instructions later today

mortum5 commented 6 months ago

I've found solution. Run play.New with function callback that returns choose.Update.

rusinikita commented 6 months ago

@mortum5 best option is something like that in (m model) Update

case key.Matches(msg, m.b.Back):
    chooseModel := choose.New()
    commands := tea.Sequence(chooseModel.Init(), func() tea.Msg {
        return m.w // pass tea.WindowSizeMsg to sync window size
    })

    return chooseModel, commands

Here is navigation idea

sequenceDiagram
    participant App as App event loop
    App->>+Choose: Show Choose
    Choose-->>Choose: Challenge selected
    Choose-->>-App: Play model
    App->>+Play: Show Play
    Play-->>Play: Back pressed
    Play-->>-App: Choose model
    App->>+Choose: Show Choose

So, you need to define key bindings, add navigation as in code sample, and check it works (check window resize as well).

Important: check if b/⌫ - back showed in help message on screen bottom (when h pressed)

rusinikita commented 6 months ago

Your solution works as alternative. But there is no need for callbacks.

You can pass Choose model instance in Play and return it in Play.Update. But you still need to sync window size.

Downside is state coupling. So I think creating choose.New instance is better.

rusinikita commented 6 months ago

Mmm. I forgot about import cycle. @mortum5 so, create PR, let's see your solution.

mortum5 commented 6 months ago

Yes, I tried to use a new select instance first, but ran into a problem with the imports

mortum5 commented 6 months ago

But you are right about window size. I will think how to sync them.

P.S. Found, just override msg

rusinikita commented 6 months ago

Create PR draft, I can help