Skip to content

Commit

Permalink
Multiple enhancements
Browse files Browse the repository at this point in the history
- Fixed various search issues, closes #78
- Added basic support for chat, updates every two seconds. Still some
bugs to iron out (see #8)
- Made "message" button on profile page functional (closes #69)
- Added SmartDispatcher, that only dispatches when needed, Should give
us a little performance increase
- Changed Dispatching in MainViewModel
- Added ability to suppress notifications
- Made Helpers.Notify thread safe
- Added auto updating to Chat
- Added client function to post a chat message
- Added client function to get chat beacon
  • Loading branch information
ChrisK91 committed Sep 5, 2013
1 parent 774a7d1 commit 6a1cc12
Show file tree
Hide file tree
Showing 17 changed files with 306 additions and 61 deletions.
2 changes: 1 addition & 1 deletion SparklrLib/Objects/Requests/Work/Chat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace SparklrLib.Objects.Requests.Work
{
public class Chat
{
public string to { get; set; }
public int to { get; set; }
public string message { get; set; }
}
}
5 changes: 2 additions & 3 deletions SparklrLib/Objects/Responses/Beacon/Chat.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;


namespace SparklrLib.Objects.Responses.Beacon
{
public class ChatMessage
Expand All @@ -12,6 +11,6 @@ public class ChatMessage

public class Chat : BeaconBase
{
public List<ChatMessage> data { get; set; }
public ChatMessage[] data { get; set; }
}
}
1 change: 1 addition & 0 deletions SparklrLib/PortableSparklrLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<Compile Include="Objects\JSONRequestEventArgs.cs" />
<Compile Include="Objects\LoginEventArgs.cs" />
<Compile Include="Objects\NotificationEventArgs.cs" />
<Compile Include="Objects\Requests\Work\Chat.cs" />
<Compile Include="Objects\Requests\Work\Comment.cs" />
<Compile Include="Objects\Requests\Work\Like.cs" />
<Compile Include="Objects\Requests\Work\Post.cs" />
Expand Down
17 changes: 15 additions & 2 deletions SparklrLib/SparklrClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Newtonsoft.Json;
using SparklrLib.Objects;
using SparklrLib.Objects.Responses;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -488,7 +487,7 @@ public async Task<SparklrEventArgs> PostAsync(string message, Stream image)
int count;
while ((count = image.Read(array, 0, array.Length)) != 0)
{
ms.Write(array, 0, count);
ms.Write(array, 0, count);
}
#else
image.CopyTo(ms);
Expand Down Expand Up @@ -666,6 +665,20 @@ public async Task<SparklrEventArgs> PostAsync(string message, Stream image)
return requestJsonObjectAsync<Objects.Responses.Work.Chat[]>("/work/chat/" + otherid.ToString());
}

public Task<JSONRequestEventArgs<Objects.Responses.Beacon.Chat>> GetBeaconChatAsync(int otherid, int since, int limit = 0)
{
return requestJsonObjectAsync<Objects.Responses.Beacon.Chat>("/beacon/chat/" + otherid.ToString() + "?since=" + since.ToString() + "&n=" + limit.ToString());
}

public Task<JSONRequestEventArgs<Objects.Responses.Generic>> PostChatMessageAsync(int recipient, string message)
{
return requestJsonObjectAsync<Objects.Responses.Generic>("/work/chat", new Objects.Requests.Work.Chat()
{
to = recipient,
message = message
}, "", "POST");
}

private void raiseCredentialsExpired()
{
//Fire only once
Expand Down
4 changes: 3 additions & 1 deletion SparklrWP/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public App()

}

static internal bool SuppressNotifications = false;
private int previousNotifications = 0;
void SparklrClient_NotificationsReceived(object sender, SparklrLib.Objects.NotificationEventArgs e)
{
Expand All @@ -76,7 +77,8 @@ void SparklrClient_NotificationsReceived(object sender, SparklrLib.Objects.Notif
{
if (e.Notifications.Length != previousNotifications)
{
Helpers.Notify(String.Format("You have {0} notifications.", e.Notifications.Length));
if (!SuppressNotifications)
Helpers.Notify(String.Format("You have {0} notifications.", e.Notifications.Length));
previousNotifications = e.Notifications.Length;
}

Expand Down
2 changes: 1 addition & 1 deletion SparklrWP/Pages/ChatPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
<codingForFun:ChatBubbleTextBox Grid.Row="1" ChatBubbleDirection="LowerRight" Margin="75,0,0,5" />
<codingForFun:ChatBubbleTextBox Grid.Row="1" ChatBubbleDirection="LowerRight" Margin="75,0,0,5" KeyDown="ChatBubbleTextBox_KeyDown" Text="{Binding Message, Mode=TwoWay}" x:Name="chatBubbleTextBox" InputScope="Chat" />
</Grid>
<!-- Visibility set in code behind -->
<sparklrControls:LoadingOverlay x:Name="loadingOverlay" Grid.Row="0" Grid.RowSpan="2" Visibility="Collapsed" />
Expand Down
12 changes: 12 additions & 0 deletions SparklrWP/Pages/ChatPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Microsoft.Phone.Controls;
using SparklrWP.Utils;
using System;
using System.Windows;
using System.Windows.Input;

namespace SparklrWP.Pages
{
Expand Down Expand Up @@ -58,5 +60,15 @@ void model_LoadingFinished(object sender, EventArgs e)
if (loadingOverlay.Visibility == System.Windows.Visibility.Visible)
loadingOverlay.FinishLoading();
}

private void ChatBubbleTextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
chatBubbleTextBox.UpdateBinding();
model.sendMessage();
Focus();
}
}
}
}
2 changes: 1 addition & 1 deletion SparklrWP/Pages/Profile.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@

<controls:PanoramaItem Header="more">
<StackPanel>
<Button>message</Button>
<Button Click="MessageButton_Click">message</Button>
<Button Click="MentionButton_Click">mention</Button>
<Button Content="{Binding FollowButtonCaption}" Click="FollowButton_Click" />
</StackPanel>
Expand Down
5 changes: 5 additions & 0 deletions SparklrWP/Pages/Profile.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,10 @@ private async void FollowButton_Click(object sender, System.Windows.RoutedEventA
if (userargs.IsSuccessful)
refreshUserDetails(userargs);
}

private void MessageButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Pages/ChatPage.xaml?id=" + model.ID, UriKind.Relative));
}
}
}
4 changes: 2 additions & 2 deletions SparklrWP/Pages/SearchPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!-- todo: replace with toolkit:PhoneTextBox once we got the newest toolkit version working -->
<toolkit:PhoneTextBox Grid.Row="0" Hint="Search" Text="{Binding Keyword}" x:Name="searchTextBox" ActionIcon="../Assets/Search.png" KeyDown="TextBox_KeyDown" ActionIconTapped="searchTextBox_ActionIconTapped" />
<toolkit:PhoneTextBox Grid.Row="0" Hint="Search" Text="{Binding Keyword, Mode=TwoWay}" x:Name="searchTextBox" ActionIcon="../Assets/Search.png" KeyDown="TextBox_KeyDown" ActionIconTapped="searchTextBox_ActionIconTapped" />
<!--<TextBox Grid.Row="0" Grid.Column="0" Text="{Binding Keyword, Mode=TwoWay}" IsEnabled="{Binding IsReady}" KeyDown="TextBox_KeyDown" x:Name="searchTextBox" />
<Button Grid.Row="0" Grid.Column="1" Height="72" Width="72" IsEnabled="{Binding IsReady}" Click="Button_Click">
<Button.Background>
Expand All @@ -39,7 +39,7 @@
<ListBox ItemsSource="{Binding Users}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Tag="{Binding Id}" Orientation="Horizontal">
<StackPanel Tag="{Binding Id}" Orientation="Horizontal" Tap="UserStackPanel_Tap" toolkit:TiltEffect.IsTiltEnabled="True">
<sparklrControls:ExtendedImageControl Height="75" Width="75" ImageSource="{Binding Image, FallbackValue='http://d.sparklr.me/i/t1.jpg'}" Margin="12,6" HorizontalAlignment="Left" />
<TextBlock Text="{Binding Name}" Style="{StaticResource PhoneTextExtraLargeStyle}" VerticalAlignment="Center" Margin="0,15,12,15" />
</StackPanel>
Expand Down
12 changes: 10 additions & 2 deletions SparklrWP/Pages/SearchPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ public SearchPage()

private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
((TextBox)sender).UpdateBinding();

if (e.Key == Key.Enter)
doSearch();
}

private void doSearch()
{
searchTextBox.UpdateBinding();
this.Focus();
model.Search();
}
Expand All @@ -39,5 +38,14 @@ private void searchTextBox_ActionIconTapped(object sender, System.EventArgs e)
{
doSearch();
}

private void UserStackPanel_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
FriendViewModel i = ((StackPanel)sender).DataContext as FriendViewModel;
if (i != null)
{
NavigationService.Navigate(new System.Uri("/Pages/Profile.xaml?userId=" + i.Id.ToString(), System.UriKind.Relative));
}
}
}
}
1 change: 1 addition & 0 deletions SparklrWP/SparklrWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@
<Compile Include="Utils\ListToGroupedListConverter.cs" />
<Compile Include="Utils\MemoryDiagnosticsHelper.cs" />
<Compile Include="Utils\ObservableCollectionWithItemNotification.cs" />
<Compile Include="Utils\SmartDispatcher.cs" />
<Compile Include="Utils\StringExtensions.cs" />
<Compile Include="Utils\Task.cs" />
<Compile Include="ViewModels\FriendViewModel.cs" />
Expand Down
11 changes: 7 additions & 4 deletions SparklrWP/Utils/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,17 @@ public static Task<BitmapImage> LoadImageFromUrlAsync(string location)
}

/// <summary>
/// Displays a non intrusive toast notification
/// Displays a non intrusive toast notification. Thread safe.
/// </summary>
/// <param name="text">The text to display</param>
public static void Notify(string text)
{
ToastPrompt p = new ToastPrompt();
p.Message = text;
p.Show();
SmartDispatcher.BeginInvoke(() =>
{
ToastPrompt p = new ToastPrompt();
p.Message = text;
p.Show();
});
}

/// <summary>
Expand Down
34 changes: 34 additions & 0 deletions SparklrWP/Utils/SmartDispatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Windows.Threading;

namespace SparklrWP.Utils
{
/// <summary>
/// Provides access to a smart invoke method, that only dispatchs if required.
/// </summary>
public static class SmartDispatcher
{
private static Dispatcher dispatcher;

static SmartDispatcher()
{
dispatcher = System.Windows.Deployment.Current.Dispatcher;
}

/// <summary>
/// Dispatches the given action to the UI thread
/// </summary>
/// <param name="a">The action to execute</param>
public static void BeginInvoke(Action a)
{
if (dispatcher.CheckAccess())
{
a();
}
else
{
dispatcher.BeginInvoke(a);
}
}
}
}
Loading

0 comments on commit 6a1cc12

Please sign in to comment.