1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-28 21:23:04 +08:00

Formatting and ctor usage for required parameters

This commit is contained in:
Dean Herbert 2018-07-24 15:19:50 +02:00
parent 53f556de9a
commit da730269a9
3 changed files with 21 additions and 20 deletions

View File

@ -84,13 +84,11 @@ namespace osu.Game.Online.Chat
{ {
CurrentChannel.ValueChanged += currentChannelChanged; CurrentChannel.ValueChanged += currentChannelChanged;
channelMessagesHandler = new IncomingMessagesHandler(); channelMessagesHandler = new IncomingMessagesHandler(
channelMessagesHandler.CreateMessagesRequest = () => new GetMessagesRequest(JoinedChannels.Where(c => c.Target == TargetType.Channel), channelMessagesHandler.LastMessageId); () => new GetMessagesRequest(JoinedChannels.Where(c => c.Target == TargetType.Channel), channelMessagesHandler.LastMessageId), handleChannelMessages);
channelMessagesHandler.OnNewMessages = handleChannelMessages;
privateMessagesHandler = new IncomingMessagesHandler(); privateMessagesHandler = new IncomingMessagesHandler(
privateMessagesHandler.CreateMessagesRequest = () => new GetPrivateMessagesRequest(privateMessagesHandler.LastMessageId); () => new GetPrivateMessagesRequest(privateMessagesHandler.LastMessageId),handleUserMessages);
privateMessagesHandler.OnNewMessages = handleUserMessages;
} }
private void currentChannelChanged(Channel channel) private void currentChannelChanged(Channel channel)

View File

@ -4,39 +4,44 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using JetBrains.Annotations;
using osu.Framework.Logging; using osu.Framework.Logging;
using osu.Game.Online.API; using osu.Game.Online.API;
namespace osu.Game.Online.Chat namespace osu.Game.Online.Chat
{ {
/// <summary>
/// Handles tracking and updating of a specific message type, allowing polling and requesting of only new messages on an ongoing basis.
/// </summary>
public class IncomingMessagesHandler public class IncomingMessagesHandler
{ {
public long? LastMessageId { get; private set; } public long? LastMessageId { get; private set; }
private APIMessagesRequest getMessagesRequest; private APIMessagesRequest getMessagesRequest;
public Func<APIMessagesRequest> CreateMessagesRequest { set; private get; } private readonly Func<APIMessagesRequest> createRequest;
private readonly Action<List<Message>> onNewMessages;
public Action<List<Message>> OnNewMessages { set; private get; }
public bool CanRequestNewMessages => getMessagesRequest == null; public bool CanRequestNewMessages => getMessagesRequest == null;
public IncomingMessagesHandler([NotNull] Func<APIMessagesRequest> createRequest, [NotNull] Action<List<Message>> onNewMessages)
{
this.createRequest = createRequest ?? throw new ArgumentNullException(nameof(createRequest));
this.onNewMessages = onNewMessages ?? throw new ArgumentNullException(nameof(onNewMessages));
}
public void RequestNewMessages(IAPIProvider api) public void RequestNewMessages(IAPIProvider api)
{ {
if (!CanRequestNewMessages) if (!CanRequestNewMessages)
throw new InvalidOperationException("Requesting new messages is not possible yet, because the old request is still ongoing."); throw new InvalidOperationException("Requesting new messages is not possible yet, because the old request is still ongoing.");
if (OnNewMessages == null) getMessagesRequest = createRequest.Invoke();
throw new InvalidOperationException($"You need to set an handler for the new incoming messages ({nameof(OnNewMessages)}) first before using {nameof(RequestNewMessages)}.");
getMessagesRequest = CreateMessagesRequest.Invoke();
getMessagesRequest.Success += handleNewMessages; getMessagesRequest.Success += handleNewMessages;
getMessagesRequest.Failure += exception => getMessagesRequest.Failure += exception =>
{ {
Logger.Error(exception, "Fetching messages failed."); Logger.Error(exception, "Fetching messages failed.");
//allowing new messages to be requested even after the fail. // allowing new messages to be requested even after the fail.
getMessagesRequest = null; getMessagesRequest = null;
}; };
@ -45,15 +50,14 @@ namespace osu.Game.Online.Chat
private void handleNewMessages(List<Message> messages) private void handleNewMessages(List<Message> messages)
{ {
// allowing new messages to be requested.
//allowing new messages to be requested.
getMessagesRequest = null; getMessagesRequest = null;
//in case of no new messages we simply do nothing. // in case of no new messages we simply do nothing.
if (messages == null || messages.Count == 0) if (messages == null || messages.Count == 0)
return; return;
OnNewMessages.Invoke(messages); onNewMessages.Invoke(messages);
LastMessageId = messages.Max(m => m.Id) ?? LastMessageId; LastMessageId = messages.Max(m => m.Id) ?? LastMessageId;
} }

View File

@ -6,7 +6,6 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using OpenTK.Graphics; using OpenTK.Graphics;
using osu.Framework.Allocation;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Game.Graphics.Containers; using osu.Game.Graphics.Containers;