Skip to content

Commit fddf00d

Browse files
better
1 parent 207e515 commit fddf00d

File tree

4 files changed

+53
-37
lines changed

4 files changed

+53
-37
lines changed

samples/Views/Playlist.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public override View GetView()
1717
{
1818
Content = new VerticalStackLayout()
1919
{
20-
new PlaylistTransitionView().Ref(out _transitionView),
20+
UseAsTransition(new PlaylistTransitionView()),
2121

2222
new VerticalStackLayout()
2323
{

samples/Views/PlaylistCollection.cs

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ namespace Sample.Views;
77

88
public class PlaylistCollection(DataAccessLayer dal) : FluidView
99
{
10-
private PlaylistTransitionView? _activeUserView;
11-
private CollectionView _collectionView = new();
12-
1310
public override View GetView()
1411
{
1512
// the footer is a hack to get the CollectionView to scroll to the top always
@@ -28,36 +25,12 @@ public override View GetView()
2825
VerticalItemSpacing = 10
2926
}
3027
}
31-
.Ref(out _collectionView)
3228
.ItemsSource(dal.Users)
33-
.ItemTemplate(new DataTemplate(() =>
34-
{
35-
var transitionView = new PlaylistTransitionView();
36-
37-
transitionView._downloadButton.IsVisible = false;
38-
transitionView._moreButton.IsVisible = false;
39-
40-
_ = transitionView
41-
.OnTapped(p =>
42-
{
43-
_activeUserView = transitionView;
44-
FlowNavigation.Current.GetView<Playlist>().TransitionView.TransitionBounds = new(
45-
p.X, p.Y, transitionView.Content.Width, transitionView.Content.Height);
46-
transitionView.Opacity = 0;
47-
48-
var user = (PlaylistVM)transitionView.BindingContext;
49-
_ = FlowNavigation.Current.GoTo<Playlist>($"id={user.Id}");
50-
})
51-
.FlowToResult<PlaylistCollection>();
52-
53-
return transitionView;
54-
}));
55-
}
56-
57-
public override void OnEntering()
58-
{
59-
if (_activeUserView is null) return;
60-
//_activeUserView.Opacity = 1;
61-
//_ = await _activeUserView.Flow<PlaylistCollection>();
29+
.ItemTemplate(
30+
TransitionView.Navigate<PlaylistCollection, Playlist, PlaylistTransitionView>(item =>
31+
{
32+
var user = (PlaylistVM)item; // <- the item source
33+
return $"id={user.Id}"; // <- the route params
34+
}));
6235
}
6336
}

src/FluidView.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22

33
public abstract class FluidView : ContentView
44
{
5-
protected TransitionView _transitionView = null!;
6-
75
public FluidView()
86
{
97
Content = GetView();
108
}
119

12-
public TransitionView TransitionView => _transitionView;
10+
public TransitionView? TransitionView { get; private set; }
1311

1412
public abstract View GetView();
1513
public virtual void OnEntering() { }
1614
public virtual void OnLeaving() { }
15+
16+
public T UseAsTransition<T>(T view) where T : TransitionView
17+
{
18+
TransitionView = view;
19+
return view;
20+
}
1721
}

src/TransitionView.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,43 @@ public Task<bool> Flow(Type type)
3737
{
3838
return this.Flow(_flows[type]);
3939
}
40+
41+
/// <summary>
42+
/// Gets a template that animates the transition between two views.
43+
/// </summary>
44+
/// <typeparam name="TFromView">The type of the view that starts the navigation.</typeparam>
45+
/// <typeparam name="TToView">The type of the view where the navigation ends.</typeparam>
46+
/// <typeparam name="TTransitionView">The type of the transition view.</typeparam>
47+
/// <returns>The data template.</returns>
48+
public static DataTemplate Navigate<TFromView, TToView, TTransitionView>(
49+
Func<object, string>? routeParamsBuilder = null)
50+
where TTransitionView : TransitionView, new()
51+
where TFromView : FluidView
52+
where TToView : FluidView
53+
{
54+
return new DataTemplate(() =>
55+
{
56+
var transitionView = new TTransitionView();
57+
58+
_ = transitionView
59+
.OnTapped(p =>
60+
{
61+
var tv = FlowNavigation.Current.GetView<TToView>().TransitionView;
62+
63+
if (tv is not null)
64+
{
65+
tv.TransitionBounds = new(
66+
p.X,
67+
p.Y,
68+
transitionView.Content.Width,
69+
transitionView.Content.Height);
70+
}
71+
72+
_ = FlowNavigation.Current.GoTo<TToView>(routeParamsBuilder?.Invoke(transitionView.BindingContext));
73+
})
74+
.FlowToResult<TFromView>();
75+
76+
return transitionView;
77+
});
78+
}
4079
}

0 commit comments

Comments
 (0)