This commit is contained in:
Paul Schneider 2016-10-26 20:29:46 +02:00
commit ca1e89a818
14 changed files with 399 additions and 171 deletions

View file

@ -21,6 +21,7 @@ namespace BookAStar
using Plugin.Connectivity;
using ViewModels;
using Microsoft.AspNet.SignalR.Client;
using Model.Social.Messaging;
public partial class App : Application // superclass new in 1.3
{
@ -56,6 +57,14 @@ namespace BookAStar
app.Rotation += OnRotation;
app.Startup += OnStartup;
app.Suspended += OnSuspended;
MainSettings.UserChanged += MainSettings_UserChanged;
CrossConnectivity.Current.ConnectivityChanged += (conSender, args) =>
{
App.IsConnected = args.IsConnected;
};
SetupHubConnection();
if (CrossConnectivity.Current.IsConnected)
StartHubConnection();
}
// omg
@ -70,13 +79,10 @@ namespace BookAStar
}
// Called Once, at app init
// FIXME Never called.
private void OnInitialize(object sender, EventArgs e)
{
CrossConnectivity.Current.ConnectivityChanged += (conSender, args) =>
{
App.IsConnected = args.IsConnected;
};
}
// called on app startup, not on rotation
@ -243,23 +249,33 @@ namespace BookAStar
// TODO Start all cloud related stuff
CurrentApp.StartHubConnection();
}
}
}
}
private HubConnection chatHubConnection = null;
public HubConnection ChatHubConnection
{
get
{
return chatHubConnection;
}
}
// Start the Hub connection
public async void StartHubConnection ()
{
if (chatHubConnection != null)
chatHubConnection.Dispose();
await chatHubConnection.Start();
}
public void SetupHubConnection()
{
chatHubConnection = new HubConnection(Constants.SignalRHubsUrl);
if (MainSettings.CurrentUser != null)
chatHubConnection.Headers.Add("Bearer", MainSettings.CurrentUser.YavscTokens.AccessToken);
chatHubConnection.Error += ChatHubConnection_Error;
chatHubProxy = chatHubConnection.CreateHubProxy("ChatHub");
chatHubProxy.On<string, string>("PV", (n, m) => {
DataManager.Current.PrivateMessages.Add(
new Model.Social.PrivateMessage
new UserMessage
{
Message = m,
SenderId = n,
@ -267,11 +283,32 @@ namespace BookAStar
}
);
});
await chatHubConnection.Start();
}
private HubConnection chatHubConnection=null;
private void MainSettings_UserChanged(object sender, EventArgs e)
{
if (MainSettings.CurrentUser != null)
{
if (chatHubConnection.Headers.ContainsKey("Bearer"))
chatHubConnection.Headers.Remove("Bearer");
chatHubConnection.Headers.Add("Bearer", MainSettings.CurrentUser.YavscTokens.AccessToken);
}
else chatHubConnection.Headers.Remove("Bearer");
}
private void ChatHubConnection_Error(Exception obj)
{
// TODO log in debug binaries
}
private IHubProxy chatHubProxy = null;
public IHubProxy ChatHubProxy
{
get
{
return chatHubProxy;
}
}
public static Task<bool> HasSomeCloud {
get
{
@ -281,9 +318,9 @@ namespace BookAStar
public void PostDeviceInfo()
{
var res = PlatformSpecificInstance.InvokeApi(
"gcm/register",
PlatformSpecificInstance.GetDeviceInfo());
var info = PlatformSpecificInstance.GetDeviceInfo();
if (!string.IsNullOrWhiteSpace(info.GCMRegistrationId))
PlatformSpecificInstance.InvokeApi("gcm/register", info);
}
public static void ShowBookQuery (BookQueryData query)

View file

@ -50,7 +50,7 @@
<Compile Include="Constants.cs" />
<Compile Include="Data\LocalState.cs" />
<Compile Include="Model\Social\Messaging\ChatStatus.cs" />
<Compile Include="Model\Social\PrivateMessage.cs" />
<Compile Include="Model\Social\Messaging\PrivateMessage.cs" />
<Compile Include="Model\UI\PageState.cs" />
<Compile Include="Settings\ChatSettings.cs" />
<Compile Include="ViewModels\EditingViewModel.cs" />
@ -169,6 +169,9 @@
<Compile Include="Views\RatingView.xaml.cs">
<DependentUpon>RatingView.xaml</DependentUpon>
</Compile>
<Compile Include="Views\UserListView.xaml.cs">
<DependentUpon>UserListView.xaml</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="App.xaml">
@ -367,6 +370,12 @@
<ItemGroup>
<EmbeddedResource Include="Images\Chat\chat_icon_s.png" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Views\UserListView.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
<SubType>Designer</SubType>
</EmbeddedResource>
</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">

View file

@ -5,7 +5,7 @@
using Model.Workflow;
using Model.UI;
using ViewModels;
using Model.Social;
using Model.Social.Messaging;
public class DataManager
{
@ -20,7 +20,7 @@
/// </summary>
internal LocalEntity<EditEstimateViewModel, long> EstimationCache { get; set; }
internal LocalEntity<BillingLine, string> EstimateLinesTemplates { get; set; }
internal LocalEntity<PrivateMessage, int> PrivateMessages { get; set; }
internal LocalEntity<UserMessage, int> PrivateMessages { get; set; }
protected static DataManager current ;
public static DataManager Current
@ -42,7 +42,7 @@
AppState = new LocalEntity<PageState, int>(s => s.Position);
EstimationCache = new LocalEntity<EditEstimateViewModel, long>(e => e.Query.Id);
EstimateLinesTemplates = new LocalEntity<BillingLine, string>(l => l.Description);
PrivateMessages = new LocalEntity<PrivateMessage, int>(m=> m.GetHashCode());
PrivateMessages = new LocalEntity<UserMessage, int>(m=> m.GetHashCode());
PrivateMessages.Load();
BookQueries.Load();

View file

@ -25,5 +25,9 @@ namespace BookAStar.Model
return UserHelpers.Avatar(Avatar);
}
}
public override string ToString()
{
return UserName;
}
}
}

View file

@ -4,9 +4,9 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BookAStar.Model.Social
namespace BookAStar.Model.Social.Messaging
{
class PrivateMessage
public class UserMessage
{
public DateTime Date { get; set; }
public string SenderId { get; set; }

View file

@ -70,6 +70,7 @@ namespace BookAStar.Pages
if (estimateToEdit == null)
{
DataManager.Current.Contacts.Merge(BookQuery.Client);
DataManager.Current.Contacts.SaveCollection();
estimateToEdit = new Estimate()
{
ClientId = BookQuery.Client.UserId,

View file

@ -2,28 +2,95 @@
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
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"
>
<TabbedPage.Children>
<ContentPage Title="Public" Icon="chat.png" >
<StackLayout Padding="5, 5, 5, 5">
<StackLayout Spacing = "12"
Orientation = "Horizontal"
Padding="5, 0, 5, 0">
<Entry x:Name="messageEntry" Placeholder = "enter your Message"
VerticalOptions = "Center"
HorizontalOptions = "FillAndExpand"></Entry>
<Button x:Name="sendButton" Text = "Send" HorizontalOptions = "End"
VerticalOptions = "Center"></Button>
<Label Text="{Binding MainText}" VerticalOptions="Center" HorizontalOptions="Center" />
<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">
<Entry x:Name="messageEntry" Placeholder = "enter your Message"
VerticalOptions = "Center"
HorizontalOptions = "FillAndExpand"></Entry>
<Button x:Name="sendButton" Text = "Send" HorizontalOptions = "End"
VerticalOptions = "Center"></Button>
</StackLayout>
<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" />
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
<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" />
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</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">
<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>
</StackLayout>
<ListView x:Name="PVList" 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" />
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<!-- <views:UserListView ItemsSource="" /> -->
</StackLayout>
<ListView x:Name="messageList"></ListView>
</StackLayout>
</ContentPage>
<ContentPage Title="Notifications" Icon="exclam.png" >
</ContentPage>
<ContentPage Title="Privé" Icon="nicubunu-Peer-to-peer.png" >
</ContentPage>
</TabbedPage.Children>
</ContentPage>
</TabbedPage.Children>
</TabbedPage>

View file

@ -1,56 +1,83 @@
using Microsoft.AspNet.SignalR.Client;
using BookAStar.Data;
using BookAStar.Model.Social.Messaging;
using Microsoft.AspNet.SignalR.Client;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using Xamarin.Forms;
using XLabs.Caching;
using XLabs.Forms.Controls;
using XLabs.Ioc;
namespace BookAStar.Pages
{
public partial class ChatPage : TabbedPage
{
public ObservableCollection<string> Messages { get; set; }
public ObservableCollection<UserMessage> Messages { get; set; }
public string ChatUser { get; set; }
private HubConnection chatHubConnection;
private IHubProxy chatHubProxy;
public ChatPage()
{
InitializeComponent();
Title = "Chat";
sendButton.Clicked += async (sender, args) => {
sendButton.Clicked += async (sender, args) =>
{
IsBusy = true;
try
{
await chatHubProxy.Invoke<string>("Send", ChatUser, messageEntry.Text);
await App.CurrentApp.ChatHubProxy.Invoke<string>("Send", ChatUser, messageEntry.Text);
messageEntry.Text = null;
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
IsBusy = false;
};
messageList.ItemsSource = Messages = new ObservableCollection<string>();
// contactPicker.DisplayProperty = "UserName";
messageList.ItemsSource = Messages = new ObservableCollection<UserMessage>();
PVList.ItemsSource = DataManager.Current.PrivateMessages;
App.CurrentApp.ChatHubConnection.StateChanged += ChatHubConnection_StateChanged;
// DataManager.Current.Contacts
MainSettings.UserChanged += MainSettings_UserChanged;
MainSettings_UserChanged(this, null);
App.CurrentApp.ChatHubProxy.On<string, string>("addMessage", (n, m) =>
{
Messages.Add(new UserMessage
{
Message = m,
SenderId = n,
Date = DateTime.Now
});
});
App.CurrentApp.ChatHubProxy.On<string, string>("addMessage", (n, m) =>
{
Messages.Add(new UserMessage
{
Message = m,
SenderId = n,
Date = DateTime.Now
});
});
}
protected override async void OnAppearing()
private void MainSettings_UserChanged(object sender, EventArgs e)
{
IsBusy = true;
chatHubConnection = new HubConnection(Constants.SignalRHubsUrl);
chatHubProxy = chatHubConnection.CreateHubProxy("ChatHub");
chatHubProxy.On<string, string>("AddMessage", (n, m) => {
Messages.Add(string.Format("{0} says: {1}", n, m));
});
await chatHubConnection.Start();
IsBusy = false;
sendButton.IsEnabled = true;
ChatUser = MainSettings.UserName;
contactPicker.ItemsSource = DataManager.Current.Contacts;
}
private void ChatHubConnection_StateChanged(StateChange obj)
{
sendButton.IsEnabled = obj.NewState == ConnectionState.Connected;
}
}
}

View file

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="BookAStar.Views.UserListView"
xmlns:local="clr-namespace:BookAStar;assembly=BookAStar"
xmlns:extensions="clr-namespace:BookAStar.Extensions;assembly=BookAStar">
<ContentView.Resources>
<ResourceDictionary>
<Style TargetType="Label">
<Setter Property="Style" Value="{StaticResource ContentLabelStyle}" />
</Style>
<Style TargetType="Button">
<Setter Property="Style" Value="{StaticResource ButtonStyle}" />
</Style>
</ResourceDictionary>
</ContentView.Resources>
<ContentView.Content>
<ListView x:Name="list" ItemTapped="OnViewDetail" HasUnevenRows="true" RowHeight="80">
<ListView.ItemTemplate HeightRequest="80" VerticalOptions="StartAndExpand">
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout Orientation="Horizontal" Padding="10,10,10,10" VerticalOptions="StartAndExpand">
<Image Source="{Binding AvatarOrNot}" Aspect="AspectFit" />
<Label Text="{Binding UserName}" />
<Label Text="{Binding EMail}" />
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentView.Content>
</ContentView>

View file

@ -0,0 +1,37 @@
using BookAStar.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace BookAStar.Views
{
public partial class UserListView : ContentView
{
public BindableProperty ItemsSourceProperty = BindableProperty.Create(
"ItemsSource", typeof(IEnumerable), typeof(UserListView));
public IEnumerable ItemsSource
{
get { return list.ItemsSource; }
set { list.ItemsSource = value; }
}
/* public IEnumerable ItemsSource
{
get { return GetValue(ItemsSourceProperty) as IEnumerable; }
set { SetValue(ItemsSourceProperty, value); }
}
*/
public UserListView()
{
InitializeComponent();
}
}
}