Implemente l'accès authentifié au chat

main
Paul Schneider 9 years ago
parent 7d3f91219e
commit f8c77bd6b1
12 changed files with 179 additions and 62 deletions

@ -273,7 +273,7 @@ namespace BookAStar
chatHubConnection.Error += ChatHubConnection_Error;
chatHubProxy = chatHubConnection.CreateHubProxy("ChatHub");
chatHubProxy.On<string, string>("PV", (n, m) => {
chatHubProxy.On<string, string>("addPV", (n, m) => {
DataManager.Current.PrivateMessages.Add(
new ChatMessage
{
@ -283,17 +283,21 @@ namespace BookAStar
}
);
});
MainSettings_UserChanged(this, null);
}
private void MainSettings_UserChanged(object sender, EventArgs e)
{
if (MainSettings.CurrentUser != null)
if (MainSettings.CurrentUser == null)
{
if (chatHubConnection.Headers.ContainsKey("Bearer"))
chatHubConnection.Headers.Remove("Bearer");
chatHubConnection.Headers.Add("Bearer", MainSettings.CurrentUser.YavscTokens.AccessToken);
chatHubConnection.Headers.Clear();
}
else
{
var token = MainSettings.CurrentUser.YavscTokens.AccessToken;
chatHubConnection.Headers.Add(
"Authorization", $"Bearer {token}");
}
else chatHubConnection.Headers.Remove("Bearer");
}
private void ChatHubConnection_Error(Exception obj)

@ -48,6 +48,7 @@
<Compile Include="Behaviors\PickerBehavior.cs" />
<Compile Include="Behaviors\StarBehavior.cs" />
<Compile Include="Constants.cs" />
<Compile Include="Converters\SignalRConnectionStateToObject.cs" />
<Compile Include="Data\LocalState.cs" />
<Compile Include="Model\Social\Messaging\ChatStatus.cs" />
<Compile Include="Model\Social\Messaging\PrivateMessage.cs" />
@ -376,6 +377,12 @@
<SubType>Designer</SubType>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<Content Include="Images\Chat\connected.png" />
<Content Include="Images\Chat\connecting.png" />
<Content Include="Images\Chat\disconnected.png" />
<Content Include="Images\Chat\reconnecting.png" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<Import Project="..\..\packages\Xamarin.Forms.2.3.2.127\build\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+MonoTouch10+Xamarin.iOS10\Xamarin.Forms.targets" Condition="Exists('..\..\packages\Xamarin.Forms.2.3.2.127\build\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+MonoTouch10+Xamarin.iOS10\Xamarin.Forms.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">

@ -8,7 +8,7 @@ using Xamarin.Forms;
namespace BookAStar.Converters
{
class BooleanToObjectConverter<T> : IValueConverter
public class BooleanToObjectConverter<T> : IValueConverter
{
public T FalseObject { set; get; }

@ -0,0 +1,42 @@
using Microsoft.AspNet.SignalR.Client;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace BookAStar.Converters
{
public class SignalRConnectionStateToObject<T> : IValueConverter
{
public T ConnectedObject { set; get; }
public T ConnectingObject { set; get; }
public T DisconnectedObject { set; get; }
public T ReconnectingObject { set; get; }
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
switch ((ConnectionState)value) {
case ConnectionState.Connected: return ConnectedObject;
case ConnectionState.Connecting: return ConnectingObject;
case ConnectionState.Disconnected: return DisconnectedObject;
case ConnectionState.Reconnecting: return ReconnectingObject;
}
Debug.Assert(false);
return null;
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

@ -6,13 +6,20 @@ using XLabs.Platform.Services;
namespace BookAStar.Pages
{
using Data;
public partial class BookQueriesPage : ContentPage
{
public BookQueriesPage()
{
InitializeComponent();
BindingContext = new BookQueriesViewModel();
var model = new BookQueriesViewModel();
model.RefreshQueries =
new Command( () => {
DataManager.Current.BookQueries.Execute(null);
this.list.EndRefresh();
});
BindingContext = model;
}
private void OnViewDetail(object sender, ItemTappedEventArgs e)
@ -20,18 +27,5 @@ namespace BookAStar.Pages
BookQueryData data = e.Item as BookQueryData;
App.NavigationService.NavigateTo<BookQueryPage>(true,data);
}
protected override void OnSizeAllocated(double width, double height)
{
/* TODO find a responsive layout
if (width > height)
{
mainLayout.Orientation = StackOrientation.Horizontal;
}
else
{
mainLayout.Orientation = StackOrientation.Vertical;
} */
base.OnSizeAllocated(width, height);
}
}
}

@ -4,30 +4,74 @@
x:Class="BookAStar.Pages.ChatPage"
xmlns:views="clr-namespace:BookAStar.Views;assembly=BookAStar"
xmlns:controls="clr-namespace:XLabs.Forms.Controls;assembly=XLabs.Forms"
xmlns:toolkit="clr-namespace:XLabs.Forms.Mvvm;assembly=XLabs.Forms"
xmlns:toolkit="clr-namespace:XLabs.Forms.Mvvm;assembly=XLabs.Forms"
xmlns:converters="clr-namespace:BookAStar.Converters;assembly=BookAStar"
xmlns:local="clr-namespace:BookAStar;Assembly:BookAStar"
>
<!--
<TabbedPage.Resources>
<ResourceDictionary>
<converters:SignalRConnectionStateToObject x:Key="cxToStyleButton" x:TypeArguments="Style">
<converters:SignalRConnectionStateToObject.DisconnectedObject>
<Style TargetType="Button">
<Setter Property="Image"
Value="{extensions:ImageResource BookAStar.Images.Chat.disconnected.png}" />
</Style>
</converters:SignalRConnectionStateToObject.DisconnectedObject>
<converters:SignalRConnectionStateToObject.ConnectedObject>
<Style TargetType="Button">
<Setter Property="Image"
Value="{extensions:ImageResource BookAStar.Images.Chat.connected.png}" />
</Style>
</converters:SignalRConnectionStateToObject.ConnectedObject>
<converters:SignalRConnectionStateToObject.ConnectingObject>
<Style TargetType="Button">
<Setter Property="Source"
Value="{extensions:ImageResource BookAStar.Images.Chat.connecting.png}" />
</Style>
</converters:SignalRConnectionStateToObject.ConnectingObject>
<converters:SignalRConnectionStateToObject.ReconnectingObject>
<Style TargetType="Button">
<Setter Property="Source"
Value="{extensions:ImageResource BookAStar.Images.Chat.reconnecting.png}" />
</Style>
</converters:SignalRConnectionStateToObject.ReconnectingObject>
</converters:SignalRConnectionStateToObject>
</ResourceDictionary>
</TabbedPage.Resources> -->
<TabbedPage.Children>
<ContentPage Title="Public" Icon="chat_icon_s.png" >
<StackLayout Padding="5, 5, 5, 5">
<StackLayout Spacing = "12"
Orientation = "Horizontal"
Padding="5, 0, 5, 0">
<Button x:Name="reconnectButton" Clicked="ReconnectButton_Clicked"/>
<!-- Style="{Binding Source={x:Static local:App.CurrentApp.ChatHubConnection},
Path=State,
Converter={StaticResource cxToStyleButton}}" -->
<Entry x:Name="messageEntry" Placeholder = "enter your Message"
VerticalOptions = "Center"
VerticalOptions = "Center"
HorizontalOptions = "FillAndExpand"></Entry>
<Button x:Name="sendButton" Text = "Send" HorizontalOptions = "End"
VerticalOptions = "Center"></Button>
</StackLayout>
<ListView x:Name="messageList" HasUnevenRows="true">
<ListView x:Name="messageList" HasUnevenRows="true"
>
<ListView.ItemTemplate HeightRequest="80" VerticalOptions="StartAndExpand">
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout Orientation="Horizontal" VerticalOptions="StartAndExpand">
<Label Text="{Binding Date, StringFormat='{0:HH:mm}'}" FontAttributes="Italic" />
<Label Text="{Binding SenderId}" FontFamily="monospace" FontAttributes="Italic" />
<BoxView WidthRequest="1" HeightRequest="15" Color="#000090"/>
<Label Text="{Binding Message}" FontFamily="monospace" />
<Label Text="{Binding Date, StringFormat='{0:HH:mm}'}" FontAttributes="Italic" />
<Label Text="{Binding SenderId}" FontFamily="monospace" FontAttributes="Italic" />
<BoxView WidthRequest="1" HeightRequest="15" Color="#000090"/>
<Label Text="{Binding Message}" FontFamily="monospace" />
</StackLayout>
</ViewCell.View>
</ViewCell>
@ -36,61 +80,62 @@
</ListView>
</StackLayout>
</ContentPage>
<ContentPage Title="Notifications" Icon="exclam.png" >
<StackLayout Padding="5, 5, 5, 5">
<ListView x:Name="notifList" HasUnevenRows="true">
<ContentPage Title="Notifications" Icon="exclam.png" >
<StackLayout Padding="5, 5, 5, 5">
<ListView x:Name="notifList" HasUnevenRows="true">
<ListView.ItemTemplate HeightRequest="80" VerticalOptions="StartAndExpand">
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout Orientation="Horizontal" VerticalOptions="StartAndExpand">
<Label Text="{Binding Date, StringFormat='{0:hh:mm}'}" FontAttributes="Italic" />
<Label Text="{Binding SenderId}" FontFamily="monospace" FontAttributes="Italic" />
<BoxView Color="#000090" WidthRequest="1" HeightRequest="15" />
<Label Text="{Binding Message}" FontFamily="monospace" />
<Label Text="{Binding Date, StringFormat='{0:hh:mm}'}" FontAttributes="Italic" />
<Label Text="{Binding SenderId}" FontFamily="monospace" FontAttributes="Italic" />
<BoxView Color="#000090" WidthRequest="1" HeightRequest="15" />
<Label Text="{Binding Message}" FontFamily="monospace" />
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</StackLayout>
</ContentPage>
<ContentPage Title="Contacts" Icon="peer_to_peer.png" >
<StackLayout Padding="5, 5, 5, 5">
<StackLayout Spacing = "12"
Orientation = "Horizontal"
Padding="5, 0, 5, 0">
<StackLayout Spacing = "12"
Orientation = "Horizontal"
Padding="5, 0, 5, 0">
<Entry x:Name="pvEntry" Placeholder = "enter your private message"
VerticalOptions = "Center"
HorizontalOptions = "FillAndExpand"></Entry>
<!--
<!--
<controls:ExtendedPicker x:Name="contactPicker" Display="{Binding UserName}"></controls:ExtendedPicker>
-->
<controls:ExtendedPicker x:Name="contactPicker" />
<Button x:Name="sendPVButton" Text = "Send" HorizontalOptions = "End"
VerticalOptions = "Center"></Button>
-->
<controls:ExtendedPicker x:Name="contactPicker" />
<Button x:Name="sendPVButton" Text = "Send" HorizontalOptions = "End"
VerticalOptions = "Center"></Button>
</StackLayout>
<ListView x:Name="PVList" HasUnevenRows="true">
<ListView.ItemTemplate HeightRequest="80" VerticalOptions="StartAndExpand">
<ListView.ItemTemplate HeightRequest="80"
VerticalOptions="StartAndExpand">
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout Orientation="Horizontal" VerticalOptions="StartAndExpand">
<Label Text="{Binding Date, StringFormat='{0:hh:mm}'}" FontAttributes="Italic" />
<Label Text="{Binding SenderId}" FontFamily="monospace" FontAttributes="Italic" />
<BoxView Color="#000090" WidthRequest="1" HeightRequest="15" />
<Label Text="{Binding Message}" FontFamily="monospace" />
<Label Text="{Binding Date, StringFormat='{0:hh:mm}'}" FontAttributes="Italic" />
<Label Text="{Binding SenderId}" FontFamily="monospace" FontAttributes="Italic" />
<BoxView Color="#000090" WidthRequest="1" HeightRequest="15" />
<Label Text="{Binding Message}" FontFamily="monospace" />
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<!-- <views:UserListView ItemsSource="" /> -->
</StackLayout>
</ContentPage>
<!-- <views:UserListView ItemsSource="" /> -->
</StackLayout>
</ContentPage>
</TabbedPage.Children>
</TabbedPage>

@ -29,6 +29,8 @@ namespace BookAStar.Pages
try
{
ConnectionState cs = App.CurrentApp.ChatHubConnection.State;
await App.CurrentApp.ChatHubProxy.Invoke<string>("Send", ChatUser, messageEntry.Text);
messageEntry.Text = null;
}
@ -39,13 +41,25 @@ namespace BookAStar.Pages
IsBusy = false;
};
// contactPicker.DisplayProperty = "UserName";
sendPVButton.Clicked += async (sender, args) =>
{
IsBusy = true;
try
{
await App.CurrentApp.ChatHubProxy.Invoke<string>("SendPV", ChatUser, messageEntry.Text);
messageEntry.Text = null;
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
IsBusy = false;
};
messageList.ItemsSource = Messages = new ObservableCollection<ChatMessage>();
PVList.ItemsSource = DataManager.Current.PrivateMessages;
App.CurrentApp.ChatHubConnection.StateChanged += ChatHubConnection_StateChanged;
// DataManager.Current.Contacts
MainSettings.UserChanged += MainSettings_UserChanged;
MainSettings_UserChanged(this, null);
@ -70,15 +84,26 @@ namespace BookAStar.Pages
});
}
private void ReconnectButton_Clicked(object sender, EventArgs e)
{
App.CurrentApp.ChatHubConnection.Stop();
App.CurrentApp.ChatHubConnection.Start();
}
private void MainSettings_UserChanged(object sender, EventArgs e)
{
ChatUser = MainSettings.UserName;
contactPicker.ItemsSource = DataManager.Current.Contacts;
PVList.ItemsSource = DataManager.Current.PrivateMessages;
}
private void ChatHubConnection_StateChanged(StateChange obj)
{
sendButton.IsEnabled = obj.NewState == ConnectionState.Connected;
Xamarin.Forms.Device.BeginInvokeOnMainThread(
() => {
sendButton.IsEnabled = obj.NewState == ConnectionState.Connected;
reconnectButton.IsEnabled = obj.NewState != ConnectionState.Connected;
});
}
}
}

@ -22,7 +22,7 @@ namespace BookAStar.ViewModels
public ICommand RefreshQueries
{
get { return DataManager.Current.BookQueries; }
get; set;
}
}
}

Loading…