Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: Make history public or some GetPreviousViewModel method #4

Open
AlexanderSjo opened this issue Apr 5, 2024 · 2 comments

Comments

@AlexanderSjo
Copy link

Hi! Thanks for an amazing and simple to use library!

I miss a simple feature to get the history. Or mainly the previous ViewModel. My use case is that I want to put Focus on a certain Button depending on where the user previously was.

Do you think this could be done?
image

@sandreas
Copy link
Owner

sandreas commented Apr 5, 2024

You maybe could use:

// check history if there was a previous item
if(_router.HasPrev) {
    var lastViewModel = _router.Back(); // go back to last ViewModel
    _router.Forward();
}

But this would create an unwanted transition, so you are right... the history is something I could at least make protected or even public (readonly). I'll try to integrate this into the next version.

Maybe I'm gonna use something like this in addition:

// optional: Add a GoTo with a delegate
_router.GoTo(history => history.ElementAt(0));

// optional: get history item by index.
var previousItem = _router.GetHistoryItem(-1);

However, the HistoryRouter is so simple, you could just to copy and paste it (e.g. MyHistoryRouter) to solve this temporarily for your use case:

public class MyHistoryRouter<TViewModelBase>: Router<TViewModelBase> where TViewModelBase: class
{
    
    private int _historyIndex = -1;
    private List<TViewModelBase> _history = new();
    private readonly uint _historyMaxSize = 100;

    public bool HasNext => _history.Count > 0 && _historyIndex < _history.Count - 1;
    public bool HasPrev => _historyIndex > 0;
    
    // HERE IS THE NEW PROPERTY
    public List<TViewModelBase> History => _history.AsReadOnly();


    public MyHistoryRouter(Func<Type, TViewModelBase> createViewModel) : base(createViewModel)
    {
    }
    
    public void Push(TViewModelBase item)
    {
        if (HasNext)
        {
            _history = _history.Take(_historyIndex + 1).ToList();
        }
        _history.Add(item);
        _historyIndex = _history.Count - 1;
        if (_history.Count > _historyMaxSize)
        {
            _history.RemoveAt(0);
        }
    }
    
    public TViewModelBase? Go(int offset = 0)
    {
        if (offset == 0)
        {
            return default;
        }

        var newIndex = _historyIndex + offset;
        if (newIndex < 0 || newIndex > _history.Count - 1)
        {
            return default;
        }
        _historyIndex = newIndex;
        var viewModel = _history.ElementAt(_historyIndex);
        CurrentViewModel = viewModel;
        return viewModel;
    }
    
    public TViewModelBase? Back() => HasPrev ? Go(-1) : default;
    
    public TViewModelBase? Forward() => HasNext ? Go(1) : default;
    
    public override T GoTo<T>()
    {
        var destination = InstantiateViewModel<T>();
        CurrentViewModel = destination;
        Push(destination);
        return destination;
    }
}

@AlexanderSjo
Copy link
Author

Amazing, thank you! I will be using a modified HistoryRouter for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants