Implement OverlayView component

This commit is contained in:
Andrei Zavatski 2020-04-16 11:42:21 +03:00
parent ef0da9e3e8
commit 29bea4e11c
3 changed files with 149 additions and 82 deletions

View File

@ -10,6 +10,7 @@
using osu.Game.Overlays;
using osu.Framework.Allocation;
using NUnit.Framework;
using osu.Game.Online.API;
namespace osu.Game.Tests.Visual.Online
{
@ -27,7 +28,7 @@ public class TestSceneFriendDisplay : OsuTestScene
[Cached]
private readonly OverlayColourProvider colourProvider = new OverlayColourProvider(OverlayColourScheme.Purple);
private FriendDisplay display;
private TestFriendDisplay display;
[SetUp]
public void Setup() => Schedule(() =>
@ -35,7 +36,7 @@ public void Setup() => Schedule(() =>
Child = new BasicScrollContainer
{
RelativeSizeAxes = Axes.Both,
Child = display = new FriendDisplay()
Child = display = new TestFriendDisplay()
};
});
@ -83,5 +84,17 @@ public void TestOnline()
LastVisit = DateTimeOffset.Now
}
};
private class TestFriendDisplay : FriendDisplay
{
public void Fetch()
{
base.APIStateChanged(API, APIState.Online);
}
public override void APIStateChanged(IAPIProvider api, APIState state)
{
}
}
}
}

View File

@ -16,7 +16,7 @@
namespace osu.Game.Overlays.Dashboard.Friends
{
public class FriendDisplay : CompositeDrawable
public class FriendDisplay : OverlayView<List<User>>
{
private List<User> users = new List<User>();
@ -26,15 +26,10 @@ public List<User> Users
set
{
users = value;
onlineStreamControl.Populate(value);
}
}
[Resolved]
private IAPIProvider api { get; set; }
private GetFriendsRequest request;
private CancellationTokenSource cancellationToken;
private Drawable currentContent;
@ -48,92 +43,85 @@ public List<User> Users
public FriendDisplay()
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
InternalChild = new FillFlowContainer
AddRange(new Drawable[]
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Children = new Drawable[]
new Container
{
new Container
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Children = new Drawable[]
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Children = new Drawable[]
controlBackground = new Box
{
controlBackground = new Box
RelativeSizeAxes = Axes.Both
},
new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding
{
RelativeSizeAxes = Axes.Both
Top = 20,
Horizontal = 45
},
new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding
{
Top = 20,
Horizontal = 45
},
Child = onlineStreamControl = new FriendOnlineStreamControl(),
}
Child = onlineStreamControl = new FriendOnlineStreamControl(),
}
},
new Container
}
},
new Container
{
Name = "User List",
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Children = new Drawable[]
{
Name = "User List",
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Children = new Drawable[]
background = new Box
{
background = new Box
RelativeSizeAxes = Axes.Both
},
new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Margin = new MarginPadding { Bottom = 20 },
Children = new Drawable[]
{
RelativeSizeAxes = Axes.Both
},
new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Margin = new MarginPadding { Bottom = 20 },
Children = new Drawable[]
new Container
{
new Container
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding
{
Horizontal = 40,
Vertical = 20
},
Child = userListToolbar = new UserListToolbar
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
}
Horizontal = 40,
Vertical = 20
},
new Container
Child = userListToolbar = new UserListToolbar
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Children = new Drawable[]
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
}
},
new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Children = new Drawable[]
{
itemsPlaceholder = new Container
{
itemsPlaceholder = new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding { Horizontal = 50 }
},
loading = new LoadingLayer(itemsPlaceholder)
}
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding { Horizontal = 50 }
},
loading = new LoadingLayer(itemsPlaceholder)
}
}
}
}
}
}
};
});
}
[BackgroundDependencyLoader]
@ -152,14 +140,11 @@ protected override void LoadComplete()
userListToolbar.SortCriteria.BindValueChanged(_ => recreatePanels());
}
public void Fetch()
{
if (!api.IsLoggedIn)
return;
protected override APIRequest<List<User>> CreateRequest() => new GetFriendsRequest();
request = new GetFriendsRequest();
request.Success += response => Schedule(() => Users = response);
api.Queue(request);
protected override void OnSuccess(List<User> response)
{
Users = response;
}
private void recreatePanels()
@ -258,9 +243,7 @@ private List<User> sortUsers(List<User> unsorted)
protected override void Dispose(bool isDisposing)
{
request?.Cancel();
cancellationToken?.Cancel();
base.Dispose(isDisposing);
}
}

View File

@ -0,0 +1,71 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Online.API;
namespace osu.Game.Overlays
{
/// <summary>
/// Drawable which used to represent online content in <see cref="FullscreenOverlay"/>.
/// </summary>
/// <typeparam name="T">Response type</typeparam>
public abstract class OverlayView<T> : Container, IOnlineComponent
where T : class
{
[Resolved]
protected IAPIProvider API { get; private set; }
protected override Container<Drawable> Content => content;
private readonly FillFlowContainer content;
protected OverlayView()
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
AddInternal(content = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
});
}
protected override void LoadComplete()
{
base.LoadComplete();
API.Register(this);
}
private APIRequest<T> request;
protected abstract APIRequest<T> CreateRequest();
protected abstract void OnSuccess(T response);
public virtual void APIStateChanged(IAPIProvider api, APIState state)
{
switch (state)
{
case APIState.Online:
request = CreateRequest();
request.Success += response => Schedule(() => OnSuccess(response));
api.Queue(request);
break;
default:
break;
}
}
protected override void Dispose(bool isDisposing)
{
request?.Cancel();
API?.Unregister(this);
base.Dispose(isDisposing);
}
}
}