Skip to content

Commit 568ff19

Browse files
committed
make navtarget equatable and avoid routing to same target
1 parent 2733fe4 commit 568ff19

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

NavTarget.cs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
using System;
2+
13
namespace Plugins.Router
24
{
3-
public class NavTarget
5+
public class NavTarget : IEquatable<NavTarget>
46
{
57
public string Name;
68
public Params Params;
@@ -12,10 +14,42 @@ public static implicit operator NavTarget(string name)
1214
Name = name
1315
};
1416
}
15-
}
17+
18+
public bool Equals(NavTarget other)
19+
{
20+
if (ReferenceEquals(null, other)) return false;
21+
if (ReferenceEquals(this, other)) return true;
22+
return Name == other.Name && Equals(Params, other.Params);
23+
}
1624

17-
public class NavState : NavTarget
18-
{
19-
public Route Route;
25+
public override bool Equals(object obj)
26+
{
27+
if (ReferenceEquals(null, obj)) return false;
28+
if (ReferenceEquals(this, obj)) return true;
29+
if (obj.GetType() != this.GetType()) return false;
30+
return Equals((NavTarget)obj);
31+
}
32+
33+
34+
public static bool operator ==(NavTarget lhs, NavTarget rhs)
35+
{
36+
if (lhs is null)
37+
{
38+
if (rhs is null)
39+
{
40+
return true;
41+
}
42+
43+
return false;
44+
}
45+
return lhs.Equals(rhs);
46+
}
47+
48+
public static bool operator !=(NavTarget lhs, NavTarget rhs) => !(lhs == rhs);
49+
50+
public override int GetHashCode()
51+
{
52+
return HashCode.Combine(Name, Params);
53+
}
2054
}
2155
}

Router.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void BeforeEach(Func<NavTarget, NavTarget, NavTarget> beforeEach)
8383
public async Task<bool> Push(string name, Params @params = null)
8484
{
8585
var target = await DoRoute(name, @params);
86-
if (target == null)
86+
if (target == null || target == _currTarget)
8787
{
8888
return false;
8989
}
@@ -98,11 +98,11 @@ public async Task<bool> Push(string name, Params @params = null)
9898

9999
public async Task<bool> Back()
100100
{
101-
if (_history.Count <= 1)
101+
if (_history.Count <= 0)
102102
{
103103
return false;
104104
}
105-
105+
106106
_history.Pop();
107107

108108
var target = _history.Peek();

0 commit comments

Comments
 (0)