2017-02-07 12:59:30 +08:00
|
|
|
|
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
2016-10-07 23:43:06 +08:00
|
|
|
|
|
2017-02-20 20:11:09 +08:00
|
|
|
|
using System;
|
2016-10-07 23:43:06 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
2016-10-13 22:57:05 +08:00
|
|
|
|
using OpenTK;
|
2017-08-21 16:43:26 +08:00
|
|
|
|
using OpenTK.Graphics;
|
2016-11-09 07:13:20 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
2017-05-12 12:54:02 +08:00
|
|
|
|
using osu.Framework.Configuration;
|
2016-10-07 23:43:06 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2017-06-20 13:54:23 +08:00
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
2017-02-19 17:02:25 +08:00
|
|
|
|
using osu.Framework.Graphics.UserInterface;
|
2017-02-20 20:11:09 +08:00
|
|
|
|
using osu.Framework.Input;
|
2017-08-21 16:43:26 +08:00
|
|
|
|
using osu.Framework.Threading;
|
2017-05-12 12:54:02 +08:00
|
|
|
|
using osu.Game.Configuration;
|
2017-05-12 13:21:57 +08:00
|
|
|
|
using osu.Game.Graphics;
|
2017-06-29 01:18:12 +08:00
|
|
|
|
using osu.Game.Graphics.Containers;
|
2017-08-21 16:43:26 +08:00
|
|
|
|
using osu.Game.Graphics.UserInterface;
|
|
|
|
|
using osu.Game.Online.API;
|
|
|
|
|
using osu.Game.Online.API.Requests;
|
|
|
|
|
using osu.Game.Online.Chat;
|
|
|
|
|
using osu.Game.Overlays.Chat;
|
2016-10-07 23:43:06 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays
|
|
|
|
|
{
|
2017-06-29 01:18:12 +08:00
|
|
|
|
public class ChatOverlay : OsuFocusedOverlayContainer, IOnlineComponent
|
2016-10-07 23:43:06 +08:00
|
|
|
|
{
|
2017-05-12 13:22:11 +08:00
|
|
|
|
private const float textbox_height = 60;
|
2017-05-30 08:23:03 +08:00
|
|
|
|
private const float channel_selection_min_height = 0.3f;
|
2017-02-22 12:38:22 +08:00
|
|
|
|
|
2016-10-07 23:43:06 +08:00
|
|
|
|
private ScheduledDelegate messageRequest;
|
|
|
|
|
|
2017-06-12 15:40:53 +08:00
|
|
|
|
private readonly Container<DrawableChannel> currentChannelContainer;
|
|
|
|
|
|
|
|
|
|
private readonly LoadingAnimation loading;
|
2016-10-07 23:43:06 +08:00
|
|
|
|
|
2017-08-21 16:43:26 +08:00
|
|
|
|
private readonly FocusedTextBox textbox;
|
2017-02-19 17:02:25 +08:00
|
|
|
|
|
2016-10-12 14:22:57 +08:00
|
|
|
|
private APIAccess api;
|
|
|
|
|
|
2017-04-19 18:15:21 +08:00
|
|
|
|
private const int transition_length = 500;
|
|
|
|
|
|
2017-05-12 12:54:02 +08:00
|
|
|
|
public const float DEFAULT_HEIGHT = 0.4f;
|
|
|
|
|
|
2017-05-12 14:32:52 +08:00
|
|
|
|
public const float TAB_AREA_HEIGHT = 50;
|
2017-05-12 13:22:11 +08:00
|
|
|
|
|
2017-04-19 18:15:21 +08:00
|
|
|
|
private GetMessagesRequest fetchReq;
|
|
|
|
|
|
2017-05-12 14:32:52 +08:00
|
|
|
|
private readonly ChatTabControl channelTabs;
|
|
|
|
|
|
2017-05-30 08:23:03 +08:00
|
|
|
|
private readonly Container chatContainer;
|
2017-07-21 18:37:22 +08:00
|
|
|
|
private readonly Container tabsArea;
|
2017-05-12 14:32:52 +08:00
|
|
|
|
private readonly Box chatBackground;
|
2017-05-12 18:15:04 +08:00
|
|
|
|
private readonly Box tabBackground;
|
2017-05-11 22:10:48 +08:00
|
|
|
|
|
2017-05-12 12:54:02 +08:00
|
|
|
|
private Bindable<double> chatHeight;
|
|
|
|
|
|
2017-06-02 12:00:09 +08:00
|
|
|
|
private readonly Container channelSelectionContainer;
|
2017-05-21 08:26:39 +08:00
|
|
|
|
private readonly ChannelSelectionOverlay channelSelection;
|
|
|
|
|
|
2017-06-30 14:54:03 +08:00
|
|
|
|
public override bool Contains(Vector2 screenSpacePos) => chatContainer.ReceiveMouseInputAt(screenSpacePos) || channelSelection.State == Visibility.Visible && channelSelection.ReceiveMouseInputAt(screenSpacePos);
|
2017-05-21 08:26:39 +08:00
|
|
|
|
|
2016-11-30 14:15:43 +08:00
|
|
|
|
public ChatOverlay()
|
2016-10-07 23:43:06 +08:00
|
|
|
|
{
|
2017-05-12 12:54:02 +08:00
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
|
|
|
|
RelativePositionAxes = Axes.Both;
|
2016-10-07 23:43:06 +08:00
|
|
|
|
Anchor = Anchor.BottomLeft;
|
|
|
|
|
Origin = Anchor.BottomLeft;
|
|
|
|
|
|
2017-05-12 13:22:11 +08:00
|
|
|
|
const float padding = 5;
|
|
|
|
|
|
2017-05-11 22:10:48 +08:00
|
|
|
|
Children = new Drawable[]
|
2016-10-07 23:43:06 +08:00
|
|
|
|
{
|
2017-06-02 12:00:09 +08:00
|
|
|
|
channelSelectionContainer = new Container
|
2017-05-21 08:26:39 +08:00
|
|
|
|
{
|
2017-05-30 08:23:03 +08:00
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Height = 1f - DEFAULT_HEIGHT,
|
2017-06-02 12:00:09 +08:00
|
|
|
|
Masking = true,
|
|
|
|
|
Children = new[]
|
|
|
|
|
{
|
|
|
|
|
channelSelection = new ChannelSelectionOverlay
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
},
|
|
|
|
|
},
|
2017-05-21 08:26:39 +08:00
|
|
|
|
},
|
2017-05-30 08:23:03 +08:00
|
|
|
|
chatContainer = new Container
|
2017-02-19 17:02:25 +08:00
|
|
|
|
{
|
2017-05-30 08:23:03 +08:00
|
|
|
|
Name = @"chat container",
|
|
|
|
|
Anchor = Anchor.BottomLeft,
|
|
|
|
|
Origin = Anchor.BottomLeft,
|
2017-05-12 13:22:11 +08:00
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
2017-05-30 08:23:03 +08:00
|
|
|
|
Height = DEFAULT_HEIGHT,
|
|
|
|
|
Children = new[]
|
2017-02-19 17:02:25 +08:00
|
|
|
|
{
|
2017-05-30 08:23:03 +08:00
|
|
|
|
new Container
|
2017-02-19 17:02:25 +08:00
|
|
|
|
{
|
2017-05-30 08:23:03 +08:00
|
|
|
|
Name = @"chat area",
|
2017-02-20 20:11:09 +08:00
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
2017-05-30 08:23:03 +08:00
|
|
|
|
Padding = new MarginPadding { Top = TAB_AREA_HEIGHT },
|
|
|
|
|
Children = new Drawable[]
|
2017-05-12 13:22:11 +08:00
|
|
|
|
{
|
2017-05-30 08:23:03 +08:00
|
|
|
|
chatBackground = new Box
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
},
|
2017-06-12 15:40:53 +08:00
|
|
|
|
currentChannelContainer = new Container<DrawableChannel>
|
2017-05-30 08:23:03 +08:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Padding = new MarginPadding
|
|
|
|
|
{
|
2017-07-18 17:26:27 +08:00
|
|
|
|
Bottom = textbox_height
|
2017-05-30 08:23:03 +08:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
new Container
|
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.BottomLeft,
|
|
|
|
|
Origin = Anchor.BottomLeft,
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
Height = textbox_height,
|
|
|
|
|
Padding = new MarginPadding
|
|
|
|
|
{
|
|
|
|
|
Top = padding * 2,
|
|
|
|
|
Bottom = padding * 2,
|
2017-06-07 22:00:14 +08:00
|
|
|
|
Left = ChatLine.LEFT_PADDING + padding * 2,
|
2017-05-30 08:23:03 +08:00
|
|
|
|
Right = padding * 2,
|
|
|
|
|
},
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
2017-08-21 16:43:26 +08:00
|
|
|
|
textbox = new FocusedTextBox
|
2017-05-30 08:23:03 +08:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Height = 1,
|
|
|
|
|
PlaceholderText = "type your message",
|
|
|
|
|
Exit = () => State = Visibility.Hidden,
|
|
|
|
|
OnCommit = postMessage,
|
2017-06-06 00:07:28 +08:00
|
|
|
|
ReleaseFocusOnCommit = false,
|
2017-05-30 08:23:03 +08:00
|
|
|
|
HoldFocus = true,
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-12 15:40:53 +08:00
|
|
|
|
},
|
|
|
|
|
loading = new LoadingAnimation(),
|
2017-05-30 08:23:03 +08:00
|
|
|
|
}
|
2017-05-12 13:22:11 +08:00
|
|
|
|
},
|
2017-07-21 18:37:22 +08:00
|
|
|
|
tabsArea = new Container
|
2017-05-12 13:22:11 +08:00
|
|
|
|
{
|
2017-05-30 08:23:03 +08:00
|
|
|
|
Name = @"tabs area",
|
2017-05-12 13:22:11 +08:00
|
|
|
|
RelativeSizeAxes = Axes.X,
|
2017-05-30 08:23:03 +08:00
|
|
|
|
Height = TAB_AREA_HEIGHT,
|
2017-05-12 13:22:11 +08:00
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
2017-05-30 08:23:03 +08:00
|
|
|
|
tabBackground = new Box
|
2017-05-12 13:22:11 +08:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
2017-05-30 08:23:03 +08:00
|
|
|
|
Colour = Color4.Black,
|
|
|
|
|
},
|
|
|
|
|
channelTabs = new ChatTabControl
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
2017-08-03 23:25:52 +08:00
|
|
|
|
OnRequestLeave = removeChannel,
|
2017-05-30 08:23:03 +08:00
|
|
|
|
},
|
2017-05-12 13:22:11 +08:00
|
|
|
|
}
|
2017-05-12 14:32:52 +08:00
|
|
|
|
},
|
2017-05-30 08:23:03 +08:00
|
|
|
|
},
|
2017-05-12 14:32:52 +08:00
|
|
|
|
},
|
2017-05-11 22:10:48 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
channelTabs.Current.ValueChanged += newChannel => CurrentChannel = newChannel;
|
2017-05-30 09:22:14 +08:00
|
|
|
|
channelTabs.ChannelSelectorActive.ValueChanged += value => channelSelection.State = value ? Visibility.Visible : Visibility.Hidden;
|
2017-09-04 08:10:04 +08:00
|
|
|
|
channelSelection.StateChanged += state =>
|
2017-05-30 08:23:03 +08:00
|
|
|
|
{
|
2017-06-05 17:04:40 +08:00
|
|
|
|
channelTabs.ChannelSelectorActive.Value = state == Visibility.Visible;
|
|
|
|
|
|
2017-06-01 16:17:45 +08:00
|
|
|
|
if (state == Visibility.Visible)
|
2017-06-01 09:29:52 +08:00
|
|
|
|
{
|
2017-08-21 16:43:26 +08:00
|
|
|
|
textbox.HoldFocus = false;
|
2017-06-01 16:17:45 +08:00
|
|
|
|
if (1f - chatHeight.Value < channel_selection_min_height)
|
|
|
|
|
{
|
2017-07-23 02:50:25 +08:00
|
|
|
|
chatContainer.ResizeHeightTo(1f - channel_selection_min_height, 800, Easing.OutQuint);
|
|
|
|
|
channelSelectionContainer.ResizeHeightTo(channel_selection_min_height, 800, Easing.OutQuint);
|
2017-06-01 16:17:45 +08:00
|
|
|
|
channelSelection.Show();
|
|
|
|
|
chatHeight.Value = 1f - channel_selection_min_height;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-08-21 16:43:26 +08:00
|
|
|
|
textbox.HoldFocus = true;
|
2017-06-01 09:29:52 +08:00
|
|
|
|
}
|
2017-05-30 08:23:03 +08:00
|
|
|
|
};
|
2016-10-07 23:43:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-28 20:11:46 +08:00
|
|
|
|
private double startDragChatHeight;
|
2017-07-27 21:33:10 +08:00
|
|
|
|
private bool isDragging;
|
2017-05-28 20:11:46 +08:00
|
|
|
|
|
2017-05-12 12:54:02 +08:00
|
|
|
|
protected override bool OnDragStart(InputState state)
|
|
|
|
|
{
|
2017-07-27 21:33:10 +08:00
|
|
|
|
isDragging = tabsArea.IsHovered;
|
2017-07-21 18:37:22 +08:00
|
|
|
|
|
2017-07-27 21:33:10 +08:00
|
|
|
|
if (!isDragging)
|
2017-05-28 20:11:46 +08:00
|
|
|
|
return base.OnDragStart(state);
|
2017-05-12 12:54:02 +08:00
|
|
|
|
|
2017-05-28 20:11:46 +08:00
|
|
|
|
startDragChatHeight = chatHeight.Value;
|
|
|
|
|
return true;
|
2017-05-12 12:54:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool OnDrag(InputState state)
|
|
|
|
|
{
|
2017-07-27 21:33:10 +08:00
|
|
|
|
if (isDragging)
|
2017-07-21 18:37:22 +08:00
|
|
|
|
{
|
|
|
|
|
Trace.Assert(state.Mouse.PositionMouseDown != null);
|
|
|
|
|
|
|
|
|
|
chatHeight.Value = startDragChatHeight - (state.Mouse.Position.Y - state.Mouse.PositionMouseDown.Value.Y) / Parent.DrawSize.Y;
|
|
|
|
|
}
|
2017-05-28 20:34:15 +08:00
|
|
|
|
|
2017-07-21 18:37:22 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool OnDragEnd(InputState state)
|
|
|
|
|
{
|
2017-07-27 21:33:10 +08:00
|
|
|
|
isDragging = false;
|
2017-07-21 18:37:22 +08:00
|
|
|
|
return base.OnDragEnd(state);
|
2017-05-12 12:54:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-19 18:15:21 +08:00
|
|
|
|
public void APIStateChanged(APIAccess api, APIState state)
|
|
|
|
|
{
|
|
|
|
|
switch (state)
|
|
|
|
|
{
|
|
|
|
|
case APIState.Online:
|
|
|
|
|
initializeChannels();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
messageRequest?.Cancel();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-30 15:33:26 +08:00
|
|
|
|
public override bool AcceptsFocus => true;
|
|
|
|
|
|
|
|
|
|
protected override bool OnClick(InputState state) => true;
|
|
|
|
|
|
|
|
|
|
protected override void OnFocus(InputState state)
|
2017-02-20 20:11:09 +08:00
|
|
|
|
{
|
2017-08-21 16:43:26 +08:00
|
|
|
|
//this is necessary as textbox is masked away and therefore can't get focus :(
|
|
|
|
|
GetContainingInputManager().ChangeFocus(textbox);
|
2017-05-30 15:33:26 +08:00
|
|
|
|
base.OnFocus(state);
|
2017-02-20 20:11:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-19 18:15:21 +08:00
|
|
|
|
protected override void PopIn()
|
2017-02-19 17:02:25 +08:00
|
|
|
|
{
|
2017-07-23 02:50:25 +08:00
|
|
|
|
this.MoveToY(0, transition_length, Easing.OutQuint);
|
|
|
|
|
this.FadeIn(transition_length, Easing.OutQuint);
|
2017-05-11 17:27:14 +08:00
|
|
|
|
|
2017-08-21 16:43:26 +08:00
|
|
|
|
textbox.HoldFocus = true;
|
2017-05-11 17:27:14 +08:00
|
|
|
|
base.PopIn();
|
2017-04-19 18:15:21 +08:00
|
|
|
|
}
|
2017-04-19 17:46:26 +08:00
|
|
|
|
|
2017-04-19 18:15:21 +08:00
|
|
|
|
protected override void PopOut()
|
|
|
|
|
{
|
2017-07-23 02:50:25 +08:00
|
|
|
|
this.MoveToY(Height, transition_length, Easing.InSine);
|
|
|
|
|
this.FadeOut(transition_length, Easing.InSine);
|
2017-05-11 17:27:14 +08:00
|
|
|
|
|
2017-08-21 16:43:26 +08:00
|
|
|
|
textbox.HoldFocus = false;
|
2017-05-11 17:27:14 +08:00
|
|
|
|
base.PopOut();
|
2017-02-19 17:02:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-12 18:44:16 +08:00
|
|
|
|
[BackgroundDependencyLoader]
|
2017-05-12 14:32:52 +08:00
|
|
|
|
private void load(APIAccess api, OsuConfigManager config, OsuColour colours)
|
2016-10-07 23:43:06 +08:00
|
|
|
|
{
|
2016-11-30 13:52:24 +08:00
|
|
|
|
this.api = api;
|
2016-11-30 14:49:15 +08:00
|
|
|
|
api.Register(this);
|
2017-05-12 12:54:02 +08:00
|
|
|
|
|
2017-05-15 09:56:27 +08:00
|
|
|
|
chatHeight = config.GetBindable<double>(OsuSetting.ChatDisplayHeight);
|
2017-05-12 18:15:04 +08:00
|
|
|
|
chatHeight.ValueChanged += h =>
|
|
|
|
|
{
|
2017-05-30 08:23:03 +08:00
|
|
|
|
chatContainer.Height = (float)h;
|
2017-06-02 12:00:09 +08:00
|
|
|
|
channelSelectionContainer.Height = 1f - (float)h;
|
2017-06-01 19:16:53 +08:00
|
|
|
|
tabBackground.FadeTo(h == 1 ? 1 : 0.8f, 200);
|
2017-05-12 18:15:04 +08:00
|
|
|
|
};
|
2017-05-12 12:54:02 +08:00
|
|
|
|
chatHeight.TriggerChange();
|
2017-05-12 14:32:52 +08:00
|
|
|
|
|
|
|
|
|
chatBackground.Colour = colours.ChatBlue;
|
2016-10-07 23:43:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private long? lastMessageId;
|
|
|
|
|
|
2017-05-17 21:13:56 +08:00
|
|
|
|
private readonly List<Channel> careChannels = new List<Channel>();
|
2016-10-07 23:43:06 +08:00
|
|
|
|
|
2017-05-12 12:25:50 +08:00
|
|
|
|
private readonly List<DrawableChannel> loadedChannels = new List<DrawableChannel>();
|
2017-05-11 22:51:26 +08:00
|
|
|
|
|
2017-04-19 18:15:21 +08:00
|
|
|
|
private void initializeChannels()
|
|
|
|
|
{
|
2017-06-12 15:40:53 +08:00
|
|
|
|
loading.Show();
|
2017-04-19 18:15:21 +08:00
|
|
|
|
|
|
|
|
|
messageRequest?.Cancel();
|
|
|
|
|
|
|
|
|
|
ListChannelsRequest req = new ListChannelsRequest();
|
|
|
|
|
req.Success += delegate (List<Channel> channels)
|
|
|
|
|
{
|
|
|
|
|
Scheduler.Add(delegate
|
|
|
|
|
{
|
|
|
|
|
addChannel(channels.Find(c => c.Name == @"#lazer"));
|
2017-05-11 22:10:48 +08:00
|
|
|
|
addChannel(channels.Find(c => c.Name == @"#osu"));
|
|
|
|
|
addChannel(channels.Find(c => c.Name == @"#lobby"));
|
2017-05-21 08:26:39 +08:00
|
|
|
|
|
2017-05-26 15:11:45 +08:00
|
|
|
|
channelSelection.OnRequestJoin = addChannel;
|
2017-08-03 23:25:52 +08:00
|
|
|
|
channelSelection.OnRequestLeave = removeChannel;
|
2017-05-21 08:26:39 +08:00
|
|
|
|
channelSelection.Sections = new[]
|
|
|
|
|
{
|
|
|
|
|
new ChannelSection
|
|
|
|
|
{
|
2017-05-28 04:40:20 +08:00
|
|
|
|
Header = "All Channels",
|
2017-05-21 08:26:39 +08:00
|
|
|
|
Channels = channels,
|
|
|
|
|
},
|
|
|
|
|
};
|
2017-04-19 18:15:21 +08:00
|
|
|
|
});
|
|
|
|
|
|
2017-05-16 14:56:53 +08:00
|
|
|
|
messageRequest = Scheduler.AddDelayed(fetchNewMessages, 1000, true);
|
2017-04-19 18:15:21 +08:00
|
|
|
|
};
|
2017-05-11 22:10:48 +08:00
|
|
|
|
|
2017-04-19 18:15:21 +08:00
|
|
|
|
api.Queue(req);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-11 22:10:48 +08:00
|
|
|
|
private Channel currentChannel;
|
|
|
|
|
|
|
|
|
|
protected Channel CurrentChannel
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return currentChannel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
2017-08-24 13:10:42 +08:00
|
|
|
|
if (currentChannel == value) return;
|
|
|
|
|
|
|
|
|
|
if (value == null)
|
|
|
|
|
{
|
|
|
|
|
currentChannel = null;
|
|
|
|
|
textbox.Current.Disabled = true;
|
|
|
|
|
currentChannelContainer.Clear(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-05-18 03:16:53 +08:00
|
|
|
|
|
2017-05-11 22:10:48 +08:00
|
|
|
|
currentChannel = value;
|
2017-05-11 22:51:26 +08:00
|
|
|
|
|
2017-08-21 16:43:26 +08:00
|
|
|
|
textbox.Current.Disabled = currentChannel.ReadOnly;
|
2017-06-01 17:29:34 +08:00
|
|
|
|
channelTabs.Current.Value = value;
|
2017-05-11 22:51:26 +08:00
|
|
|
|
|
2017-06-01 17:29:34 +08:00
|
|
|
|
var loaded = loadedChannels.Find(d => d.Channel == value);
|
|
|
|
|
if (loaded == null)
|
|
|
|
|
{
|
2017-07-23 02:50:25 +08:00
|
|
|
|
currentChannelContainer.FadeOut(500, Easing.OutQuint);
|
2017-06-12 15:40:53 +08:00
|
|
|
|
loading.Show();
|
2017-05-11 22:10:48 +08:00
|
|
|
|
|
2017-06-01 17:29:34 +08:00
|
|
|
|
loaded = new DrawableChannel(currentChannel);
|
|
|
|
|
loadedChannels.Add(loaded);
|
|
|
|
|
LoadComponentAsync(loaded, l =>
|
|
|
|
|
{
|
2017-06-12 15:40:53 +08:00
|
|
|
|
if (currentChannel.Messages.Any())
|
|
|
|
|
loading.Hide();
|
|
|
|
|
|
2017-06-01 17:29:34 +08:00
|
|
|
|
currentChannelContainer.Clear(false);
|
2017-06-12 15:40:53 +08:00
|
|
|
|
currentChannelContainer.Add(loaded);
|
2017-07-23 02:50:25 +08:00
|
|
|
|
currentChannelContainer.FadeIn(500, Easing.OutQuint);
|
2017-06-01 17:29:34 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
currentChannelContainer.Clear(false);
|
|
|
|
|
currentChannelContainer.Add(loaded);
|
|
|
|
|
}
|
2017-05-11 22:10:48 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-07 23:43:06 +08:00
|
|
|
|
private void addChannel(Channel channel)
|
|
|
|
|
{
|
2017-05-11 22:51:26 +08:00
|
|
|
|
if (channel == null) return;
|
|
|
|
|
|
2017-05-16 17:25:03 +08:00
|
|
|
|
var existing = careChannels.Find(c => c.Id == channel.Id);
|
2017-05-11 22:10:48 +08:00
|
|
|
|
|
2017-05-16 17:25:03 +08:00
|
|
|
|
if (existing != null)
|
|
|
|
|
{
|
|
|
|
|
// if we already have this channel loaded, we don't want to make a second one.
|
|
|
|
|
channel = existing;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
careChannels.Add(channel);
|
|
|
|
|
channelTabs.AddItem(channel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// let's fetch a small number of messages to bring us up-to-date with the backlog.
|
2017-05-16 14:56:53 +08:00
|
|
|
|
fetchInitialMessages(channel);
|
2017-05-11 22:10:48 +08:00
|
|
|
|
|
|
|
|
|
if (CurrentChannel == null)
|
|
|
|
|
CurrentChannel = channel;
|
2017-05-21 08:26:39 +08:00
|
|
|
|
|
|
|
|
|
channel.Joined.Value = true;
|
2016-10-07 23:43:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-03 23:25:52 +08:00
|
|
|
|
private void removeChannel(Channel channel)
|
|
|
|
|
{
|
|
|
|
|
if (channel == null) return;
|
|
|
|
|
|
2017-08-24 13:10:42 +08:00
|
|
|
|
if (channel == CurrentChannel) CurrentChannel = null;
|
2017-08-17 10:50:44 +08:00
|
|
|
|
|
2017-08-03 23:25:52 +08:00
|
|
|
|
careChannels.Remove(channel);
|
2017-08-17 10:50:44 +08:00
|
|
|
|
loadedChannels.Remove(loadedChannels.Find(c => c.Channel == channel));
|
2017-08-03 23:25:52 +08:00
|
|
|
|
channelTabs.RemoveItem(channel);
|
|
|
|
|
|
|
|
|
|
channel.Joined.Value = false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-16 14:56:53 +08:00
|
|
|
|
private void fetchInitialMessages(Channel channel)
|
|
|
|
|
{
|
|
|
|
|
var req = new GetMessagesRequest(new List<Channel> { channel }, null);
|
|
|
|
|
|
|
|
|
|
req.Success += delegate (List<Message> messages)
|
|
|
|
|
{
|
2017-06-12 15:40:53 +08:00
|
|
|
|
loading.Hide();
|
2017-05-16 14:56:53 +08:00
|
|
|
|
channel.AddNewMessages(messages.ToArray());
|
|
|
|
|
Debug.Write("success!");
|
|
|
|
|
};
|
|
|
|
|
req.Failure += delegate
|
|
|
|
|
{
|
|
|
|
|
Debug.Write("failure!");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
api.Queue(req);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void fetchNewMessages()
|
2016-10-07 23:43:06 +08:00
|
|
|
|
{
|
|
|
|
|
if (fetchReq != null) return;
|
|
|
|
|
|
2017-05-16 14:56:53 +08:00
|
|
|
|
fetchReq = new GetMessagesRequest(careChannels, lastMessageId);
|
2017-08-21 16:44:39 +08:00
|
|
|
|
|
2016-10-07 23:43:06 +08:00
|
|
|
|
fetchReq.Success += delegate (List<Message> messages)
|
|
|
|
|
{
|
2017-05-18 02:30:17 +08:00
|
|
|
|
foreach (var group in messages.Where(m => m.TargetType == TargetType.Channel).GroupBy(m => m.TargetId))
|
|
|
|
|
careChannels.Find(c => c.Id == group.Key)?.AddNewMessages(group.ToArray());
|
2016-10-07 23:43:06 +08:00
|
|
|
|
|
|
|
|
|
lastMessageId = messages.LastOrDefault()?.Id ?? lastMessageId;
|
|
|
|
|
|
|
|
|
|
Debug.Write("success!");
|
|
|
|
|
fetchReq = null;
|
|
|
|
|
};
|
2017-08-21 16:44:39 +08:00
|
|
|
|
|
2016-10-07 23:43:06 +08:00
|
|
|
|
fetchReq.Failure += delegate
|
|
|
|
|
{
|
|
|
|
|
Debug.Write("failure!");
|
|
|
|
|
fetchReq = null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
api.Queue(fetchReq);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-19 18:15:21 +08:00
|
|
|
|
private void postMessage(TextBox textbox, bool newText)
|
2016-10-13 22:57:05 +08:00
|
|
|
|
{
|
2017-04-19 18:15:21 +08:00
|
|
|
|
var postText = textbox.Text;
|
2016-11-30 14:15:43 +08:00
|
|
|
|
|
2017-08-21 16:43:26 +08:00
|
|
|
|
textbox.Text = string.Empty;
|
|
|
|
|
|
2017-05-16 18:55:45 +08:00
|
|
|
|
if (string.IsNullOrEmpty(postText))
|
|
|
|
|
return;
|
|
|
|
|
|
2017-08-21 16:43:26 +08:00
|
|
|
|
var target = currentChannel;
|
|
|
|
|
|
|
|
|
|
if (target == null) return;
|
|
|
|
|
|
2017-05-16 18:55:45 +08:00
|
|
|
|
if (!api.IsLoggedIn)
|
|
|
|
|
{
|
2017-08-21 16:43:26 +08:00
|
|
|
|
target.AddNewMessages(new ErrorMessage("Please login to participate in chat!"));
|
2017-05-16 18:55:45 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2016-11-30 16:07:09 +08:00
|
|
|
|
|
2017-05-15 12:26:35 +08:00
|
|
|
|
if (postText[0] == '/')
|
2016-11-30 16:07:09 +08:00
|
|
|
|
{
|
2017-05-15 12:26:35 +08:00
|
|
|
|
// TODO: handle commands
|
2017-08-21 16:43:26 +08:00
|
|
|
|
target.AddNewMessages(new ErrorMessage("Chat commands are not supported yet!"));
|
2017-05-15 12:26:35 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2016-11-30 16:07:09 +08:00
|
|
|
|
|
2017-08-21 16:43:26 +08:00
|
|
|
|
var message = new LocalEchoMessage
|
2017-05-15 12:26:35 +08:00
|
|
|
|
{
|
|
|
|
|
Sender = api.LocalUser.Value,
|
|
|
|
|
Timestamp = DateTimeOffset.Now,
|
2017-08-21 16:43:26 +08:00
|
|
|
|
TargetType = TargetType.Channel, //TODO: read this from channel
|
|
|
|
|
TargetId = target.Id,
|
2017-05-15 12:26:35 +08:00
|
|
|
|
Content = postText
|
|
|
|
|
};
|
2016-11-30 16:07:09 +08:00
|
|
|
|
|
2017-05-15 12:26:35 +08:00
|
|
|
|
var req = new PostMessageRequest(message);
|
2016-11-30 16:07:09 +08:00
|
|
|
|
|
2017-08-21 16:43:26 +08:00
|
|
|
|
target.AddLocalEcho(message);
|
|
|
|
|
req.Failure += e => target.ReplaceMessage(message, null);
|
|
|
|
|
req.Success += m => target.ReplaceMessage(message, m);
|
2016-11-30 16:07:09 +08:00
|
|
|
|
|
2017-05-15 12:26:35 +08:00
|
|
|
|
api.Queue(req);
|
2016-11-30 14:15:43 +08:00
|
|
|
|
}
|
2016-10-07 23:43:06 +08:00
|
|
|
|
}
|
|
|
|
|
}
|