-
-
Notifications
You must be signed in to change notification settings - Fork 2
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
Comments
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 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 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;
}
} |
Amazing, thank you! I will be using a modified HistoryRouter for now. |
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?
The text was updated successfully, but these errors were encountered: