-
Notifications
You must be signed in to change notification settings - Fork 10
/
RoomPage.xaml.cs
160 lines (140 loc) · 5.23 KB
/
RoomPage.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using JabbRPhone.ViewModels;
using JabbRPhone.Extensions;
using JabbRPhone.Helpers;
namespace JabbRPhone.Pages
{
public partial class RoomPage : PhoneApplicationPage
{
RoomViewModel _model;
public RoomPage()
{
InitializeComponent();
}
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
//Stop listening for the event (Sometimes caused duplicate messages)
App.Client.MessageReceived -= NewMessageAdded;
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
App.Client.MessageReceived += NewMessageAdded;
var roomName = NavigationContext.QueryString["name"];
string inviteCode = null;
NavigationContext.QueryString.TryGetValue("inviteCode", out inviteCode);
_model = new RoomViewModel(roomName, inviteCode);
_model.Prog = this.GetProgressIndicator();
this.DataContext = _model;
_model.SetStatus("Loading room...", true);
txtMessage.IsEnabled = false;
App.Client.GetRoomInfo(roomName)
.ContinueWith((roomTask) =>
{
JoinRoom();
//roomTask.Result.CheckOwners();
_model.LoadRoom(roomTask.Result);
_model.SetStatus("Joining room...", true);
ScrollToLastMessage();
});
}
private void JoinRoom()
{
App.Client.JoinRoom(_model.Name)
.ContinueWith((task) =>
{
//done...?
if (task.Status == System.Threading.Tasks.TaskStatus.RanToCompletion)
{
Dispatcher.BeginInvoke(() =>
{
txtMessage.IsEnabled = true;
});
_model.ClearStatus();
}
else if (task.Status == System.Threading.Tasks.TaskStatus.Faulted)
{
// Couldn't join room. What up?
DispatcherHelper.SafeDispatch(() =>
{
MessageBox.Show("Couldn't join the room.\n\nAre you sure the details are correct?", "Oops!", MessageBoxButton.OK);
NavigationService.SafeGoBack();
});
}
});
}
private void txtMessage_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
SendMessage(txtMessage.Text);
}
}
private void SendMessage(string content)
{
if (string.IsNullOrWhiteSpace(content)) return;
//Clear it
txtMessage.Text = string.Empty;
_model.SetStatus("Sending...", true);
App.Client.Send(content, _model.Name)
.ContinueWith((task) =>
{
//Add this 1 to the list?
if (task.IsFaulted)
{
try
{
_model.SetStatus(task.Exception.Message, false, 2000);
Dispatcher.BeginInvoke(() =>
{
txtMessage.Text = content;
});
}
catch { }
}
else
{
_model.SetStatus("Message sent!", false, 2000);
Dispatcher.BeginInvoke(() =>
{
txtMessage.Text = string.Empty;
});
}
});
}
private void ScrollToLastMessage()
{
Dispatcher.BeginInvoke(() =>
{
try
{
lsbMessages.ScrollIntoView(lsbMessages.Items[lsbMessages.Items.Count - 1]);
lsbMessages.UpdateLayout();
lsbMessages.ScrollIntoView(lsbMessages.Items[lsbMessages.Items.Count - 1]);
}
catch (Exception ex) { }
});
}
private void NewMessageAdded(JabbR.Client.Models.Message message, string room)
{
if (_model == null) return;
if (room.Equals(_model.Name, StringComparison.OrdinalIgnoreCase))
{
Dispatcher.BeginInvoke(() => _model.Messages.Add(new MessageViewModel(message)));
ScrollToLastMessage();
}
}
}
}