1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-28 09:02:58 +08:00

Allow opening a new chat with right click on User

Allow faster viewing of the usertab using lasy loading
This commit is contained in:
miterosan 2018-04-09 23:14:39 +02:00
parent 762b4412e5
commit 85f736ae89
5 changed files with 96 additions and 33 deletions

View File

@ -11,6 +11,7 @@ using osu.Framework.Logging;
using osu.Framework.Threading;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Users;
namespace osu.Game.Online.Chat
{
@ -52,6 +53,40 @@ namespace osu.Game.Online.Chat
private long? lastChannelMsgId;
private long? lastUserMsgId;
public void OpenChannelChat(string name)
{
if (name == null)
throw new ArgumentNullException(nameof(name));
CurrentChat.Value = AvailableChannels.FirstOrDefault(c => c.Name == name)
?? throw new ArgumentException($"Channel {name} was not found.");
}
public void OpenUserChat(long userId)
{
var chat = OpenedUserChats.FirstOrDefault(c => c.ChatID == userId);
if (chat == null)
{
chat = new UserChat(new User
{
Id = userId
});
chat.RequestDetails(api);
}
CurrentChat.Value = chat;
}
public void OpenUserChat(User user)
{
if (user == null)
throw new ArgumentNullException(nameof(user));
CurrentChat.Value = OpenedUserChats.FirstOrDefault(c => c.ChatID == user.Id)
?? new UserChat(user);
}
public ChatManager(Scheduler scheduler)
{
this.scheduler = scheduler ?? throw new ArgumentNullException(nameof(scheduler));
@ -62,6 +97,9 @@ namespace osu.Game.Online.Chat
{
if (chatBase is ChannelChat channel && !JoinedChannels.Contains(channel))
JoinedChannels.Add(channel);
if (chatBase is UserChat userChat && !OpenedUserChats.Contains(userChat))
OpenedUserChats.Add(userChat);
}
/// <summary>
@ -171,23 +209,19 @@ namespace osu.Game.Online.Chat
chat.AddNewMessages(messageGroup.ToArray());
var outgoingTargetMessages = outgoingMessagesGroups.FirstOrDefault(g => g.Key == targetUser.Id);
chat.AddNewMessages(outgoingTargetMessages.ToArray());
if (outgoingTargetMessages != null)
chat.AddNewMessages(outgoingTargetMessages.ToArray());
}
var withoutReplyGroups = outgoingMessagesGroups.Where(g => OpenedUserChats.All(m => m.ChatID != g.Key));
foreach (var withoutReplyGroup in withoutReplyGroups)
{
var getUserRequest = new GetUserRequest(withoutReplyGroup.First().TargetId);
getUserRequest.Success += user =>
{
var chat = new UserChat(user);
{
var chat = new UserChat(new User {Id = withoutReplyGroup.First().TargetId });
chat.AddNewMessages(withoutReplyGroup.ToArray());
OpenedUserChats.Add(chat);
};
api.Queue(getUserRequest);
chat.AddNewMessages(withoutReplyGroup.ToArray());
OpenedUserChats.Add(chat);
chat.RequestDetails(api);
}
}

View File

@ -2,13 +2,20 @@
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using osu.Framework.Logging;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Users;
namespace osu.Game.Online.Chat
{
public class UserChat : ChatBase
{
public User User { get; }
public User User { get; private set; }
public override TargetType Target => TargetType.User;
public override long ChatID => User.Id;
public Action<User> DetailsArrived;
public UserChat(User user, Message[] messages = null)
{
@ -17,7 +24,19 @@ namespace osu.Game.Online.Chat
if (messages != null) AddNewMessages(messages);
}
public override TargetType Target => TargetType.User;
public override long ChatID => User.Id;
public void RequestDetails(IAPIProvider api)
{
if (api == null)
throw new ArgumentNullException(nameof(api));
var req = new GetUserRequest(User.Id);
req.Success += user =>
{
User = user;
DetailsArrived?.Invoke(user);
};
req.Failure += exception => Logger.Error(exception, $"Requesting details for user with Id:{User.Id} failed.");
api.Queue(req);
}
}
}

View File

@ -1,6 +1,7 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Linq;
using OpenTK;
using OpenTK.Graphics;
@ -236,20 +237,24 @@ namespace osu.Game.Overlays.Chat
{
private readonly User sender;
private Action startChatAction;
public MessageSender(User sender)
{
this.sender = sender;
}
[BackgroundDependencyLoader(true)]
private void load(UserProfileOverlay profile)
private void load(UserProfileOverlay profile, ChatManager chatManager)
{
Action = () => profile?.ShowUser(sender);
startChatAction = () => chatManager?.OpenUserChat(sender);
}
public MenuItem[] ContextMenuItems => new MenuItem[]
{
new OsuMenuItem("View Profile", MenuItemType.Highlighted, Action),
new OsuMenuItem("Start Chat", MenuItemType.Highlighted, startChatAction),
};
}
}

View File

@ -10,6 +10,7 @@ using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.UserInterface;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Online.API;
using osu.Game.Online.Chat;
using osu.Game.Screens.Menu;
using osu.Game.Users;
@ -21,7 +22,7 @@ namespace osu.Game.Overlays.Chat
public class UserChatTabItem : TabItem<UserChat>
{
private static readonly Vector2 shear = new Vector2(1f / 5f, 0);
private readonly UserChat chat;
public override bool IsRemovable => true;
private readonly Box highlightBox;
@ -33,6 +34,7 @@ namespace osu.Game.Overlays.Chat
public UserChatTabItem(UserChat value)
: base(value)
{
chat = value;
AutoSizeAxes = Axes.X;
RelativeSizeAxes = Axes.Y;
Origin = Anchor.BottomRight;
@ -189,9 +191,15 @@ namespace osu.Game.Overlays.Chat
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
private void load(OsuColour colours, IAPIProvider api)
{
backgroundBox.Colour = Value.User.Colour != null ? OsuColour.FromHex(Value.User.Colour) : colours.BlueDark;
if (chat.User.Username == null || chat.User.Id < 1)
{
chat.DetailsArrived += arrivedUser => { Scheduler.Add(() => { username.Text = arrivedUser.Username; }); };
chat.RequestDetails(api);
}
}
}
}

View File

@ -217,10 +217,10 @@ namespace osu.Game.Overlays
channelTabs.AddItem(newChannel);
newChannel.Joined.Value = true;
if (chatManager.CurrentChat.Value == null)
{
chatManager.CurrentChat.Value = newChannel;
}
if (chatManager.CurrentChat.Value == newChannel)
channelTabs.Current.Value = newChannel;
}
break;
case NotifyCollectionChangedAction.Remove:
@ -247,17 +247,8 @@ namespace osu.Game.Overlays
textbox.Current.Disabled = chat.ReadOnly;
switch (chat)
{
case ChannelChat channelChat:
channelTabs.Current.Value = channelChat;
userTabs.DeselectAll();
break;
case UserChat userChat:
userTabs.Current.Value = userChat;
channelTabs.DeselectAll();
break;
}
userTabs.DeselectAll();
channelTabs.DeselectAll();
var loaded = loadedChannels.Find(d => d.Chat == chat);
if (loaded == null)
@ -271,7 +262,6 @@ namespace osu.Game.Overlays
{
loading.Hide();
currentChannelContainer.Clear(false);
currentChannelContainer.Add(loaded);
currentChannelContainer.FadeIn(500, Easing.OutQuint);
@ -379,10 +369,17 @@ namespace osu.Game.Overlays
switch (args.Action)
{
case NotifyCollectionChangedAction.Add:
userTabs.AddItem(args.NewItems[0] as UserChat);
foreach (UserChat chat in args.NewItems)
{
userTabs.AddItem(args.NewItems[0] as UserChat);
if (chatManager.CurrentChat.Value == chat)
userTabs.Current.Value = chat;
}
break;
case NotifyCollectionChangedAction.Remove:
userTabs.RemoveItem(args.OldItems[0] as UserChat);
foreach (UserChat chat in args.OldItems)
userTabs.RemoveItem(chat);
break;
case NotifyCollectionChangedAction.Reset:
userTabs.Clear();