From b0a3b44a8d4d22fc99132f24cc10cef61e08e49e Mon Sep 17 00:00:00 2001 From: Denis Prokhorchik Date: Wed, 6 Apr 2022 20:05:33 +0300 Subject: [PATCH 1/7] feat(issue291): add signalr for smalltalk solution --- .../O2NextGen.SmallTalk.SignalrHub.csproj | 16 +++++++++ .../O2NextGen.SmallTalk.SignalrHub/Program.cs | 24 +++++++++++++ .../Properties/launchSettings.json | 27 +++++++++++++++ .../O2NextGen.SmallTalk.SignalrHub/Startup.cs | 34 +++++++++++++++++++ .../smalltalk/O2NextGen.SmallTalk.sln | 6 ++++ 5 files changed, 107 insertions(+) create mode 100644 src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/O2NextGen.SmallTalk.SignalrHub.csproj create mode 100644 src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Program.cs create mode 100644 src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Properties/launchSettings.json create mode 100644 src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Startup.cs diff --git a/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/O2NextGen.SmallTalk.SignalrHub.csproj b/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/O2NextGen.SmallTalk.SignalrHub.csproj new file mode 100644 index 00000000..ffe2b91d --- /dev/null +++ b/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/O2NextGen.SmallTalk.SignalrHub.csproj @@ -0,0 +1,16 @@ + + + + netcoreapp2.1 + + + + + + + + + + + + diff --git a/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Program.cs b/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Program.cs new file mode 100644 index 00000000..9b32e3df --- /dev/null +++ b/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Program.cs @@ -0,0 +1,24 @@ +using Microsoft.AspNetCore; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; + +namespace O2NextGen.SmallTalk.SignalrHub +{ + public class Program + { + public static void Main(string[] args) + { + CreateWebHostBuilder(args).Build().Run(); + } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + WebHost.CreateDefaultBuilder(args) + .UseStartup(); + } +} diff --git a/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Properties/launchSettings.json b/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Properties/launchSettings.json new file mode 100644 index 00000000..c47cdfeb --- /dev/null +++ b/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Properties/launchSettings.json @@ -0,0 +1,27 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:65388", + "sslPort": 44398 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "O2NextGen.SmallTalk.SignalrHub": { + "commandName": "Project", + "launchBrowser": true, + "applicationUrl": "https://localhost:5001;http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} \ No newline at end of file diff --git a/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Startup.cs b/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Startup.cs new file mode 100644 index 00000000..d5d75b2d --- /dev/null +++ b/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Startup.cs @@ -0,0 +1,34 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace O2NextGen.SmallTalk.SignalrHub +{ + public class Startup + { + // This method gets called by the runtime. Use this method to add services to the container. + // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 + public void ConfigureServices(IServiceCollection services) + { + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IHostingEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + + app.Run(async (context) => + { + await context.Response.WriteAsync("Hello World!"); + }); + } + } +} diff --git a/src/Services/smalltalk/O2NextGen.SmallTalk.sln b/src/Services/smalltalk/O2NextGen.SmallTalk.sln index 4589eddc..6b029700 100644 --- a/src/Services/smalltalk/O2NextGen.SmallTalk.sln +++ b/src/Services/smalltalk/O2NextGen.SmallTalk.sln @@ -27,6 +27,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SDKs", "SDKs", "{B03A4DA5-C EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "O2NextGen.Sdk.NetCore.Models", "..\..\SDKs\O2NG.Sdk.NetCore\O2NG.Sdk.NetCore.Models\O2NextGen.Sdk.NetCore.Models.csproj", "{011B24B8-B1CA-4585-9862-916BE9A090AF}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "O2NextGen.SmallTalk.SignalrHub", "O2NextGen.SmallTalk.SignalrHub\O2NextGen.SmallTalk.SignalrHub.csproj", "{03A09C6B-B835-4748-839E-33E395D8197C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -73,6 +75,10 @@ Global {011B24B8-B1CA-4585-9862-916BE9A090AF}.Debug|Any CPU.Build.0 = Debug|Any CPU {011B24B8-B1CA-4585-9862-916BE9A090AF}.Release|Any CPU.ActiveCfg = Release|Any CPU {011B24B8-B1CA-4585-9862-916BE9A090AF}.Release|Any CPU.Build.0 = Release|Any CPU + {03A09C6B-B835-4748-839E-33E395D8197C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {03A09C6B-B835-4748-839E-33E395D8197C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {03A09C6B-B835-4748-839E-33E395D8197C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {03A09C6B-B835-4748-839E-33E395D8197C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE From 323a4a846ac03a4695bb12b913ef9f644a848f56 Mon Sep 17 00:00:00 2001 From: Denis Prokhorchik Date: Wed, 6 Apr 2022 20:55:31 +0300 Subject: [PATCH 2/7] feat(issue-291): add dockerfile for small-talk --- .../Views/ChatDetailView.xaml | 3 +-- src/Services/smalltalk/.dockerignore | 25 +++++++++++++++++++ .../O2NextGen.SmallTalk.SignalrHub/Dockerfile | 22 ++++++++++++++++ .../O2NextGen.SmallTalk.SignalrHub.csproj | 3 +++ 4 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 src/Services/smalltalk/.dockerignore create mode 100644 src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Dockerfile diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Views/ChatDetailView.xaml b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Views/ChatDetailView.xaml index e791f78e..e59d17b9 100644 --- a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Views/ChatDetailView.xaml +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Views/ChatDetailView.xaml @@ -26,8 +26,7 @@ + Grid.Row="1"> netcoreapp2.1 + bc1a7d3c-820d-4431-a0c7-d51b4a253b6d + Linux @@ -11,6 +13,7 @@ + From ca3ac7f1859ac3656fa4b91773416a7cbf933408 Mon Sep 17 00:00:00 2001 From: Denis Prokhorchik Date: Wed, 6 Apr 2022 21:13:33 +0300 Subject: [PATCH 3/7] feat(issue-291): add hub class for small-talk --- .../Hubs/ChatHub.cs | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Hubs/ChatHub.cs diff --git a/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Hubs/ChatHub.cs b/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Hubs/ChatHub.cs new file mode 100644 index 00000000..0ff3e1e5 --- /dev/null +++ b/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Hubs/ChatHub.cs @@ -0,0 +1,21 @@ +using Microsoft.AspNetCore.SignalR; +using System; +using System.Threading.Tasks; + +namespace O2NextGen.SmallTalk.SignalrHub.Hubs +{ + public class ChatHub : Hub + { + public override async Task OnConnectedAsync() + { + await Groups.AddToGroupAsync(Context.ConnectionId, Context.User.Identity.Name); + await base.OnConnectedAsync(); + } + + public override async Task OnDisconnectedAsync(Exception ex) + { + await Groups.RemoveFromGroupAsync(Context.ConnectionId, Context.User.Identity.Name); + await base.OnDisconnectedAsync(ex); + } + } +} From 00616eb6a5d8c622fa94c456b91ebbb0ef7b9753 Mon Sep 17 00:00:00 2001 From: Denis Prokhorchik Date: Wed, 6 Apr 2022 21:35:28 +0300 Subject: [PATCH 4/7] feat(issue-291): update program.cs and startup.cs for small-talk --- .../O2NextGen.SmallTalk.SignalrHub.csproj | 2 ++ .../O2NextGen.SmallTalk.SignalrHub/Program.cs | 7 ---- .../O2NextGen.SmallTalk.SignalrHub/Startup.cs | 32 ++++++++++++++----- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/O2NextGen.SmallTalk.SignalrHub.csproj b/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/O2NextGen.SmallTalk.SignalrHub.csproj index c8a516bf..06b1256c 100644 --- a/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/O2NextGen.SmallTalk.SignalrHub.csproj +++ b/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/O2NextGen.SmallTalk.SignalrHub.csproj @@ -7,6 +7,8 @@ + + diff --git a/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Program.cs b/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Program.cs index 9b32e3df..93e339b4 100644 --- a/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Program.cs +++ b/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Program.cs @@ -1,12 +1,5 @@ using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.Logging; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading.Tasks; namespace O2NextGen.SmallTalk.SignalrHub { diff --git a/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Startup.cs b/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Startup.cs index d5d75b2d..886af72e 100644 --- a/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Startup.cs +++ b/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Startup.cs @@ -1,11 +1,6 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; namespace O2NextGen.SmallTalk.SignalrHub { @@ -15,6 +10,16 @@ public class Startup // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { + services.AddCors(options => + { + options.AddPolicy("CorsPolicy", + builder => builder + .AllowAnyMethod() + .AllowAnyHeader() + .SetIsOriginAllowed((host) => true) + .AllowCredentials()); + }); + services.AddSignalR(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -25,10 +30,21 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env) app.UseDeveloperExceptionPage(); } - app.Run(async (context) => + app.UseCors("CorsPolicy"); + //app.UseRouting(); + + app.UseAuthentication(); + //app.UseAuthorization(); + + app.UseEndpoints(endpoints => { - await context.Response.WriteAsync("Hello World!"); - }); + endpoints.MapHub("/hub/chathub", + options => options.Transports = Microsoft.AspNetCore.Http.Connections.HttpTransports.All); + }; + // app.Run(async (context) => + //{ + // await context.Response.WriteAsync("Hello World!"); + //}); } } } From 5742bc8f97501c917f2397559f0bd1057a2a31f8 Mon Sep 17 00:00:00 2001 From: Denis Prokhorchik Date: Sat, 9 Apr 2022 19:47:13 +0300 Subject: [PATCH 5/7] feat(issue-291): add real service for smalltalk --- .../MainActivity.cs | 3 +- .../O2NextGen.SmallTalk.Android.csproj | 5 + .../Properties/AssemblyInfo.cs | 1 - .../Resources/Resource.designer.cs | 2 +- .../O2NextGen.SmallTalk.UWP/App.xaml.cs | 10 -- .../O2NextGen.SmallTalk.UWP/MainPage.xaml.cs | 17 +- .../Properties/AssemblyInfo.cs | 3 +- .../O2NextGen.SmallTalk.iOS/AppDelegate.cs | 5 +- .../O2NextGen.SmallTalk.iOS/Main.cs | 7 +- .../Properties/AssemblyInfo.cs | 1 - .../O2NextGen.SmallTalk/App.xaml.cs | 2 +- .../Converters/OwnerConverter.cs | 2 - .../ServiceAuthenticationException.cs | 18 ++ .../Extensions/ObservableExtension.cs | 20 +++ .../O2NextGen.SmallTalk/GlobalSetting.cs | 8 + .../O2NextGen.SmallTalk/Helpers/UriHelper.cs | 28 +++ .../O2NextGen.SmallTalk.Core.csproj | 2 + .../Services/Chat/ChatService.cs | 52 +++++- .../Services/Chat/ChatServiceMock.cs | 19 +- .../Services/Chat/IChatService.cs | 4 +- .../RequestProvider/HttpRequestExceptionEx.cs | 24 +++ .../RequestProvider/IRequestProvider.cs | 17 ++ .../RequestProvider/RequestProvider.cs | 164 ++++++++++++++++++ .../ViewModels/Base/ViewModelLocator.cs | 14 +- .../ViewModels/ChatDetailViewModel.cs | 3 +- .../Views/ChatView.xaml.cs | 12 +- .../Views/MainPage.xaml.cs | 6 - .../Controllers/ChatController.cs | 8 + .../Services/IChatManager.cs | 1 + .../O2NextGen.SmallTalk.SignalrHub/Startup.cs | 10 +- 30 files changed, 377 insertions(+), 91 deletions(-) create mode 100644 src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Exceptions/ServiceAuthenticationException.cs create mode 100644 src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Extensions/ObservableExtension.cs create mode 100644 src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/GlobalSetting.cs create mode 100644 src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Helpers/UriHelper.cs create mode 100644 src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/RequestProvider/HttpRequestExceptionEx.cs create mode 100644 src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/RequestProvider/IRequestProvider.cs create mode 100644 src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/RequestProvider/RequestProvider.cs diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.Android/MainActivity.cs b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.Android/MainActivity.cs index 58e94388..4e959173 100644 --- a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.Android/MainActivity.cs +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.Android/MainActivity.cs @@ -1,5 +1,4 @@ -using System; - + using Android.App; using Android.Content.PM; using Android.Runtime; diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.Android/O2NextGen.SmallTalk.Android.csproj b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.Android/O2NextGen.SmallTalk.Android.csproj index 829991a9..e8387156 100644 --- a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.Android/O2NextGen.SmallTalk.Android.csproj +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.Android/O2NextGen.SmallTalk.Android.csproj @@ -93,4 +93,9 @@ + + + + + \ No newline at end of file diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.Android/Properties/AssemblyInfo.cs b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.Android/Properties/AssemblyInfo.cs index be674359..93aa86c7 100644 --- a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.Android/Properties/AssemblyInfo.cs +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.Android/Properties/AssemblyInfo.cs @@ -1,5 +1,4 @@ using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using Android.App; diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.Android/Resources/Resource.designer.cs b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.Android/Resources/Resource.designer.cs index 7223b592..606a6ad9 100644 --- a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.Android/Resources/Resource.designer.cs +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.Android/Resources/Resource.designer.cs @@ -14,7 +14,7 @@ namespace O2NextGen.SmallTalk.Droid { - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Android.Build.Tasks", "12.1.0.11")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Android.Build.Tasks", "12.2.0.155")] public partial class Resource { diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.UWP/App.xaml.cs b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.UWP/App.xaml.cs index edb207bb..ba1521d1 100644 --- a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.UWP/App.xaml.cs +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.UWP/App.xaml.cs @@ -1,18 +1,8 @@ using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Runtime.InteropServices.WindowsRuntime; using Windows.ApplicationModel; using Windows.ApplicationModel.Activation; -using Windows.Foundation; -using Windows.Foundation.Collections; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; -using Windows.UI.Xaml.Controls.Primitives; -using Windows.UI.Xaml.Data; -using Windows.UI.Xaml.Input; -using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; namespace O2NextGen.SmallTalk.UWP diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.UWP/MainPage.xaml.cs b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.UWP/MainPage.xaml.cs index ac7dfaa2..b9ef38e4 100644 --- a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.UWP/MainPage.xaml.cs +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.UWP/MainPage.xaml.cs @@ -1,19 +1,4 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Runtime.InteropServices.WindowsRuntime; -using Windows.Foundation; -using Windows.Foundation.Collections; -using Windows.UI.Xaml; -using Windows.UI.Xaml.Controls; -using Windows.UI.Xaml.Controls.Primitives; -using Windows.UI.Xaml.Data; -using Windows.UI.Xaml.Input; -using Windows.UI.Xaml.Media; -using Windows.UI.Xaml.Navigation; - -namespace O2NextGen.SmallTalk.UWP +namespace O2NextGen.SmallTalk.UWP { public sealed partial class MainPage { diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.UWP/Properties/AssemblyInfo.cs b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.UWP/Properties/AssemblyInfo.cs index cd81754d..0a5b1b5e 100644 --- a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.UWP/Properties/AssemblyInfo.cs +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.UWP/Properties/AssemblyInfo.cs @@ -1,5 +1,4 @@ using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following @@ -26,4 +25,4 @@ // [assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyFileVersion("1.0.0.0")] -[assembly: ComVisible(false)] \ No newline at end of file +[assembly: ComVisible(false)] \ No newline at end of file diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.iOS/AppDelegate.cs b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.iOS/AppDelegate.cs index 55272962..76ffbd8d 100644 --- a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.iOS/AppDelegate.cs +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.iOS/AppDelegate.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; - + using Foundation; using UIKit; diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.iOS/Main.cs b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.iOS/Main.cs index 48d3bd57..cfa9d285 100644 --- a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.iOS/Main.cs +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.iOS/Main.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; - -using Foundation; -using UIKit; +using UIKit; namespace O2NextGen.SmallTalk.iOS { diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.iOS/Properties/AssemblyInfo.cs b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.iOS/Properties/AssemblyInfo.cs index bc6d9c59..55e2ccee 100644 --- a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.iOS/Properties/AssemblyInfo.cs +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.iOS/Properties/AssemblyInfo.cs @@ -1,5 +1,4 @@ using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/App.xaml.cs b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/App.xaml.cs index 3845a850..33ac2f14 100644 --- a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/App.xaml.cs +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/App.xaml.cs @@ -16,7 +16,7 @@ private void InitApp() { //_settingsService = ViewModelLocator.Resolve(); //if (!_settingsService.UseMocks) - ViewModelLocator.UpdateDependencies(true); + ViewModelLocator.UpdateDependencies(false); } } } diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Converters/OwnerConverter.cs b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Converters/OwnerConverter.cs index 29a15331..46ea2426 100644 --- a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Converters/OwnerConverter.cs +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Converters/OwnerConverter.cs @@ -1,7 +1,5 @@ using System; -using System.Collections.Generic; using System.Globalization; -using System.Text; using Xamarin.Forms; namespace O2NextGen.SmallTalk.Core.Converters diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Exceptions/ServiceAuthenticationException.cs b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Exceptions/ServiceAuthenticationException.cs new file mode 100644 index 00000000..9852437c --- /dev/null +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Exceptions/ServiceAuthenticationException.cs @@ -0,0 +1,18 @@ +using System; + +namespace O2NextGen.SmallTalk.Core.Exceptions +{ + public class ServiceAuthenticationException : Exception + { + public string Content { get; } + + public ServiceAuthenticationException() + { + } + + public ServiceAuthenticationException(string content) + { + Content = content; + } + } +} \ No newline at end of file diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Extensions/ObservableExtension.cs b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Extensions/ObservableExtension.cs new file mode 100644 index 00000000..d1a05462 --- /dev/null +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Extensions/ObservableExtension.cs @@ -0,0 +1,20 @@ +using System.Collections.Generic; +using System.Collections.ObjectModel; + +namespace O2NextGen.SmallTalk.Core.Extensions +{ + public static class ObservableExtension + { + public static ObservableCollection ToObservableCollection(this IEnumerable source) + { + ObservableCollection collection = new ObservableCollection(); + + foreach (T item in source) + { + collection.Add(item); + } + + return collection; + } + } +} diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/GlobalSetting.cs b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/GlobalSetting.cs new file mode 100644 index 00000000..cf0b0c36 --- /dev/null +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/GlobalSetting.cs @@ -0,0 +1,8 @@ +namespace O2NextGen.SmallTalk.Core +{ + public class GlobalSetting + { + public string GatewayChatEndpoint { get; set; } = "https://api-smalltalk.o2bus.com"; + public static GlobalSetting Instance { get; } = new GlobalSetting(); + } +} diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Helpers/UriHelper.cs b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Helpers/UriHelper.cs new file mode 100644 index 00000000..ae839d4f --- /dev/null +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Helpers/UriHelper.cs @@ -0,0 +1,28 @@ +using System; +using System.Linq; + +namespace O2NextGen.SmallTalk.Core.Helpers +{ + public static class UriHelper + { + public static string CombineUri(params string[] uriParts) + { + string uri = string.Empty; + if (uriParts != null && uriParts.Count() > 0) + { + char[] trims = new char[] { '\\', '/' }; + uri = (uriParts[0] ?? string.Empty).TrimEnd(trims); + for (int i = 1; i < uriParts.Count(); i++) + { + uri = string.Format("{0}/{1}", uri.TrimEnd(trims), (uriParts[i] ?? string.Empty).TrimStart(trims)); + } + } + return uri; + } + + internal static object CombineUri(object gatewayChatEndpoint, string v) + { + throw new NotImplementedException(); + } + } +} diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.Core.csproj b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.Core.csproj index 95e987bf..fdcf1b40 100644 --- a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.Core.csproj +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.Core.csproj @@ -11,6 +11,8 @@ + + diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/Chat/ChatService.cs b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/Chat/ChatService.cs index 51955800..40388bd2 100644 --- a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/Chat/ChatService.cs +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/Chat/ChatService.cs @@ -1,5 +1,9 @@ using O2NextGen.Sdk.NetCore.Models.smalltalk; +using O2NextGen.SmallTalk.Core.Extensions; +using O2NextGen.SmallTalk.Core.Helpers; +using O2NextGen.SmallTalk.Core.Services.RequestProvider; using System; +using System.Collections.Generic; using System.Collections.ObjectModel; using System.Threading.Tasks; @@ -7,9 +11,23 @@ namespace O2NextGen.SmallTalk.Core.Services.Chat { public class ChatService : IChatService { - public Task AddMessageToSessionAsync(string message) + public ChatService(IRequestProvider requestProvider) { - throw new NotImplementedException(); + this._requestProvider = requestProvider; + } + private const string _apiUrlBase = "/api/chat"; + private readonly IRequestProvider _requestProvider; + + public async Task AddMessageToSessionAsync(string message) + { + var uri = UriHelper.CombineUri(GlobalSetting.Instance.GatewayChatEndpoint, $"{_apiUrlBase}/session/1/messages"); + + var addMessage = await _requestProvider.PostAsync(uri, new ChatMessage() + { + Message = message,Id = 0,RecipientId=2, SenderId=1 + }); + + return addMessage; } public void GetByIdMessage(long sessionId, long id) @@ -17,19 +35,34 @@ public void GetByIdMessage(long sessionId, long id) throw new NotImplementedException(); } - public Task> GetMessageAsync() + public async Task> GetMessageAsync() { - throw new NotImplementedException(); + var uri = UriHelper.CombineUri(GlobalSetting.Instance.GatewayChatEndpoint, $"{_apiUrlBase}/session/1/messages"); + + var messages = await _requestProvider.GetAsync>(uri); + + if (messages != null) + return messages?.ToObservableCollection(); + else + return new ObservableCollection(); } - public void GetMessages(long sessionId) + public async Task GetMessagesAsync(long sessionId) { throw new NotImplementedException(); } - public Task GetSessionAsync() + public async Task GetSessionAsync() { - throw new NotImplementedException(); + var uri = UriHelper.CombineUri(GlobalSetting.Instance.GatewayChatEndpoint, $"{_apiUrlBase}/session/1"); + + var chatSession = await _requestProvider.GetAsync(uri); + + if (chatSession != null) + return chatSession; + else + return new ChatSession(); + //throw new NotImplementedException(); } public Task> GetSessionsAsync() @@ -41,5 +74,10 @@ public void Sessions(long sessionId) { throw new NotImplementedException(); } + + void IChatService.GetMessagesAsync(long sessionId) + { + throw new NotImplementedException(); + } } } diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/Chat/ChatServiceMock.cs b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/Chat/ChatServiceMock.cs index d08e8d0f..caf2d0e7 100644 --- a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/Chat/ChatServiceMock.cs +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/Chat/ChatServiceMock.cs @@ -13,12 +13,12 @@ public void GetByIdMessage(long sessionId, long id) throw new NotImplementedException(); } - public void GetMessages(long sessionId) + public void GetMessagesAsync(long sessionId) { throw new NotImplementedException(); } - private ObservableCollection MockSessions = new ObservableCollection + private ObservableCollection _mockSessions = new ObservableCollection { new ChatSession() { Id = 1, @@ -35,7 +35,6 @@ public void GetMessages(long sessionId) { Id = 2, Message = "Tests 2", - SenderId = 1, RecipientId = 2 }, new ChatMessage() @@ -80,7 +79,7 @@ public async Task> GetSessionsAsync() { await Task.Delay(10); - return MockSessions; + return _mockSessions; } public void Sessions(long sessionId) @@ -92,20 +91,22 @@ public async Task GetSessionAsync() { await Task.Delay(10); - return MockSessions[0]; + return _mockSessions[0]; } public async Task> GetMessageAsync() { await Task.Delay(10); - return new ObservableCollection(MockSessions[0].Messages); + return new ObservableCollection(_mockSessions[0].Messages); } - public async Task AddMessageToSessionAsync(string message) + public async Task AddMessageToSessionAsync(string message) { await Task.Delay(10); - long index = MockSessions.Count+1; - MockSessions[0].Messages.Add(new ChatMessage() { Id = index,Message=message,SenderId=1,RecipientId=2 }); + long index = _mockSessions.Count + 1; + var addMessage = new ChatMessage() { Id = index, Message = message, SenderId = 1, RecipientId = 2 }; + _mockSessions[0].Messages.Add(addMessage); + return addMessage; } } } diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/Chat/IChatService.cs b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/Chat/IChatService.cs index bf44cf68..802b71de 100644 --- a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/Chat/IChatService.cs +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/Chat/IChatService.cs @@ -7,11 +7,11 @@ namespace O2NextGen.SmallTalk.Core.Services.Chat public interface IChatService { void GetByIdMessage(long sessionId, long id); - void GetMessages(long sessionId); + void GetMessagesAsync(long sessionId); void Sessions(long sessionId); Task> GetSessionsAsync(); Task GetSessionAsync(); Task> GetMessageAsync(); - Task AddMessageToSessionAsync(string message); + Task AddMessageToSessionAsync(string message); } } diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/RequestProvider/HttpRequestExceptionEx.cs b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/RequestProvider/HttpRequestExceptionEx.cs new file mode 100644 index 00000000..efa2a144 --- /dev/null +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/RequestProvider/HttpRequestExceptionEx.cs @@ -0,0 +1,24 @@ +using System; +using System.Net.Http; + +namespace O2NextGen.SmallTalk.Core.Services.RequestProvider +{ + public class HttpRequestExceptionEx : HttpRequestException + { + public System.Net.HttpStatusCode HttpCode { get; } + public HttpRequestExceptionEx(System.Net.HttpStatusCode code) : this(code, null, null) + { + } + + public HttpRequestExceptionEx(System.Net.HttpStatusCode code, string message) : this(code, message, null) + { + } + + public HttpRequestExceptionEx(System.Net.HttpStatusCode code, string message, Exception inner) : base(message, + inner) + { + HttpCode = code; + } + + } +} diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/RequestProvider/IRequestProvider.cs b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/RequestProvider/IRequestProvider.cs new file mode 100644 index 00000000..128bc973 --- /dev/null +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/RequestProvider/IRequestProvider.cs @@ -0,0 +1,17 @@ +using System.Threading.Tasks; + +namespace O2NextGen.SmallTalk.Core.Services.RequestProvider +{ + public interface IRequestProvider + { + Task GetAsync(string uri, string token = ""); + + Task PostAsync(string uri, TResult data, string token = "", string header = ""); + + Task PostAsync(string uri, string data, string clientId, string clientSecret); + + Task PutAsync(string uri, TResult data, string token = "", string header = ""); + + Task DeleteAsync(string uri, string token = ""); + } +} \ No newline at end of file diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/RequestProvider/RequestProvider.cs b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/RequestProvider/RequestProvider.cs new file mode 100644 index 00000000..21491803 --- /dev/null +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/RequestProvider/RequestProvider.cs @@ -0,0 +1,164 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; +using Newtonsoft.Json.Serialization; +using O2NextGen.SmallTalk.Core.Exceptions; +using System; +using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Threading.Tasks; + +namespace O2NextGen.SmallTalk.Core.Services.RequestProvider +{ + public class RequestProvider : IRequestProvider + { + private readonly JsonSerializerSettings _serializerSettings; + + public RequestProvider() + { + _serializerSettings = new JsonSerializerSettings + { + ContractResolver = new CamelCasePropertyNamesContractResolver(), + DateTimeZoneHandling = DateTimeZoneHandling.Utc, + NullValueHandling = NullValueHandling.Ignore + }; + _serializerSettings.Converters.Add(new StringEnumConverter()); + } + + public async Task GetAsync(string uri, string token = "") + { + HttpClient httpClient = CreateHttpClient(token); + HttpResponseMessage response = await httpClient.GetAsync(uri); + + await HandleResponse(response); + string serialized = await response.Content.ReadAsStringAsync(); + + TResult result = await Task.Run(() => + JsonConvert.DeserializeObject(serialized, _serializerSettings)); + + return result; + } + + public async Task PostAsync(string uri, TResult data, string token = "", string header = "") + { + HttpClient httpClient = CreateHttpClient(token); + + if (!string.IsNullOrEmpty(header)) + { + AddHeaderParameter(httpClient, header); + } + + var content = new StringContent(JsonConvert.SerializeObject(data)); + content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); + HttpResponseMessage response = await httpClient.PostAsync(uri, content); + + await HandleResponse(response); + string serialized = await response.Content.ReadAsStringAsync(); + + TResult result = await Task.Run(() => + JsonConvert.DeserializeObject(serialized, _serializerSettings)); + + return result; + } + + public async Task PostAsync(string uri, string data, string clientId, string clientSecret) + { + HttpClient httpClient = CreateHttpClient(string.Empty); + + if (!string.IsNullOrWhiteSpace(clientId) && !string.IsNullOrWhiteSpace(clientSecret)) + { + AddBasicAuthenticationHeader(httpClient, clientId, clientSecret); + } + + var content = new StringContent(data); + content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); + HttpResponseMessage response = await httpClient.PostAsync(uri, content); + + await HandleResponse(response); + string serialized = await response.Content.ReadAsStringAsync(); + + TResult result = await Task.Run(() => + JsonConvert.DeserializeObject(serialized, _serializerSettings)); + + return result; + } + + public async Task PutAsync(string uri, TResult data, string token = "", string header = "") + { + HttpClient httpClient = CreateHttpClient(token); + + if (!string.IsNullOrEmpty(header)) + { + AddHeaderParameter(httpClient, header); + } + + var content = new StringContent(JsonConvert.SerializeObject(data)); + content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); + HttpResponseMessage response = await httpClient.PutAsync(uri, content); + + await HandleResponse(response); + string serialized = await response.Content.ReadAsStringAsync(); + + TResult result = await Task.Run(() => + JsonConvert.DeserializeObject(serialized, _serializerSettings)); + + return result; + } + + public async Task DeleteAsync(string uri, string token = "") + { + HttpClient httpClient = CreateHttpClient(token); + await httpClient.DeleteAsync(uri); + } + + private HttpClient CreateHttpClient(string token = "") + { + var httpClient = new HttpClient(); + httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + + if (!string.IsNullOrEmpty(token)) + { + httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); + } + return httpClient; + } + + private void AddHeaderParameter(HttpClient httpClient, string parameter) + { + if (httpClient == null) + return; + + if (string.IsNullOrEmpty(parameter)) + return; + + httpClient.DefaultRequestHeaders.Add(parameter, Guid.NewGuid().ToString()); + } + + private void AddBasicAuthenticationHeader(HttpClient httpClient, string clientId, string clientSecret) + { + if (httpClient == null) + return; + + if (string.IsNullOrWhiteSpace(clientId) || string.IsNullOrWhiteSpace(clientSecret)) + return; + + httpClient.DefaultRequestHeaders.Authorization = new BasicAuthenticationHeaderValue(clientId, clientSecret); + } + + private async Task HandleResponse(HttpResponseMessage response) + { + if (!response.IsSuccessStatusCode) + { + var content = await response.Content.ReadAsStringAsync(); + + if (response.StatusCode == HttpStatusCode.Forbidden || + response.StatusCode == HttpStatusCode.Unauthorized) + { + throw new ServiceAuthenticationException(content); + } + + throw new HttpRequestExceptionEx(response.StatusCode, content); + } + } + } +} diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/ViewModels/Base/ViewModelLocator.cs b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/ViewModels/Base/ViewModelLocator.cs index 98b5f0fa..0574c7c8 100644 --- a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/ViewModels/Base/ViewModelLocator.cs +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/ViewModels/Base/ViewModelLocator.cs @@ -1,4 +1,5 @@ using O2NextGen.SmallTalk.Core.Services.Chat; +using O2NextGen.SmallTalk.Core.Services.RequestProvider; using System; using System.Globalization; using System.Reflection; @@ -21,31 +22,38 @@ public static void SetAutoWireViewModel(BindableObject bindable, bool value) bindable.SetValue(ViewModelLocator.AutoWireViewModelProperty, value); } - public static bool UseMockService { get; set; } = true; + public static bool UseMockService { get; set; } = false; static ViewModelLocator() { + //var settingsService = new SettingsService(); + var requestProvider = new RequestProvider(); + // Services - by default, TinyIoC will register interface registrations as singletons. // Xamarin.Forms.DependencyService.RegisterSingleton(new Services.Dependency.DependencyService()); + Xamarin.Forms.DependencyService.RegisterSingleton(requestProvider); Xamarin.Forms.DependencyService.RegisterSingleton(new ChatServiceMock()); // View models - by default, TinyIoC will register concrete classes as multi-instance. Xamarin.Forms.DependencyService.Register(); Xamarin.Forms.DependencyService.Register(); - + UseMockService = false; } public static void UpdateDependencies(bool useMockServices) { + var requestProvider = DependencyService.Get(); // Change injected dependencies if (useMockServices) { + Xamarin.Forms.DependencyService.RegisterSingleton(new ChatServiceMock()); UseMockService = true; } else { - Xamarin.Forms.DependencyService.RegisterSingleton(new ChatService()); + + Xamarin.Forms.DependencyService.RegisterSingleton(new ChatService(requestProvider)); UseMockService = false; } } diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/ViewModels/ChatDetailViewModel.cs b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/ViewModels/ChatDetailViewModel.cs index 79ef9507..3ce8c2d7 100644 --- a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/ViewModels/ChatDetailViewModel.cs +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/ViewModels/ChatDetailViewModel.cs @@ -1,7 +1,6 @@ using O2NextGen.Sdk.NetCore.Models.smalltalk; using O2NextGen.SmallTalk.Core.Services.Chat; using O2NextGen.SmallTalk.Core.ViewModels.Base; -using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Threading.Tasks; @@ -84,7 +83,7 @@ private async Task SendMsgAsync() private async Task RelaodData() { IsBusy = true; - Session = await _chatService.GetSessionAsync(); + //Session = await _chatService.GetSessionAsync(); Messages = await _chatService.GetMessageAsync(); IsBusy = false; } diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Views/ChatView.xaml.cs b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Views/ChatView.xaml.cs index 00b752db..ed18bc99 100644 --- a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Views/ChatView.xaml.cs +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Views/ChatView.xaml.cs @@ -1,14 +1,4 @@ -using O2NextGen.SmallTalk.Core.ViewModels; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -using Xamarin.Forms; -using Xamarin.Forms.Xaml; - -namespace O2NextGen.SmallTalk.Core.Views +namespace O2NextGen.SmallTalk.Core.Views { public partial class ChatView : ContentPageBase { diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Views/MainPage.xaml.cs b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Views/MainPage.xaml.cs index ee4678cd..1cc41753 100644 --- a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Views/MainPage.xaml.cs +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Views/MainPage.xaml.cs @@ -1,10 +1,4 @@ using O2NextGen.SmallTalk.Core.ViewModels; -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using Xamarin.Forms; namespace O2NextGen.SmallTalk.Core.Views diff --git a/src/Services/smalltalk/O2NextGen.SmallTalk.Api/Controllers/ChatController.cs b/src/Services/smalltalk/O2NextGen.SmallTalk.Api/Controllers/ChatController.cs index c73f0b18..3b920138 100644 --- a/src/Services/smalltalk/O2NextGen.SmallTalk.Api/Controllers/ChatController.cs +++ b/src/Services/smalltalk/O2NextGen.SmallTalk.Api/Controllers/ChatController.cs @@ -54,6 +54,14 @@ public async Task GetMessagesAsync(long sessionId, CancellationTo return Ok(resultWithMessages.ToViewModel()); } + //[HttpGet] + //[Route("session/{sessionId}")] + //public async Task GetSessionsAsync(long sessionId, CancellationToken ct) + //{ + // var resultWithMessages = await _chatManager.GetSession(sessionId, ct); + // return Ok(resultWithMessages.ToViewModel()); + //} + [HttpPost] [HttpPut] [Route("session/{sessionId}/messages")] diff --git a/src/Services/smalltalk/O2NextGen.SmallTalk.Business/Services/IChatManager.cs b/src/Services/smalltalk/O2NextGen.SmallTalk.Business/Services/IChatManager.cs index e5e5731c..874db808 100644 --- a/src/Services/smalltalk/O2NextGen.SmallTalk.Business/Services/IChatManager.cs +++ b/src/Services/smalltalk/O2NextGen.SmallTalk.Business/Services/IChatManager.cs @@ -12,5 +12,6 @@ public interface IChatManager Task RemoveMessageAsync(long sessionId, long id, CancellationToken ct); Task UpdateMessageAsync(long sessionId, ChatMessageModel chatMessageModel, CancellationToken ct); Task GetMessageByIdAsync(long sessionId, long id, CancellationToken ct); + Task GetSession(long sessionId, CancellationToken ct); } } diff --git a/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Startup.cs b/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Startup.cs index 886af72e..1d9a4047 100644 --- a/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Startup.cs +++ b/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Startup.cs @@ -36,11 +36,11 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env) app.UseAuthentication(); //app.UseAuthorization(); - app.UseEndpoints(endpoints => - { - endpoints.MapHub("/hub/chathub", - options => options.Transports = Microsoft.AspNetCore.Http.Connections.HttpTransports.All); - }; + //app.UseEndpoints(endpoints => + //{ + // endpoints.MapHub("/hub/chathub", + // options => options.Transports = Microsoft.AspNetCore.Http.Connections.HttpTransports.All); + //}; // app.Run(async (context) => //{ // await context.Response.WriteAsync("Hello World!"); From b60eacde2f990e9eead68bc4a90c32662add5dfe Mon Sep 17 00:00:00 2001 From: Denis Prokhorchik Date: Sat, 9 Apr 2022 20:52:24 +0300 Subject: [PATCH 6/7] feat(issue-291): update smalltalk service --- .../O2NextGen.SmallTalk/Services/Chat/ChatService.cs | 5 ++++- .../O2NextGen.SmallTalk/ViewModels/ChatDetailViewModel.cs | 2 +- .../Services/InMemorySessionManager.cs | 8 ++++---- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/Chat/ChatService.cs b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/Chat/ChatService.cs index 40388bd2..37fa96dc 100644 --- a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/Chat/ChatService.cs +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/Chat/ChatService.cs @@ -24,7 +24,10 @@ public async Task AddMessageToSessionAsync(string message) var addMessage = await _requestProvider.PostAsync(uri, new ChatMessage() { - Message = message,Id = 0,RecipientId=2, SenderId=1 + Id = 0, + Message = message, + RecipientId = 2, + SenderId = 1 }); return addMessage; diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/ViewModels/ChatDetailViewModel.cs b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/ViewModels/ChatDetailViewModel.cs index 3ce8c2d7..d6218362 100644 --- a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/ViewModels/ChatDetailViewModel.cs +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/ViewModels/ChatDetailViewModel.cs @@ -75,9 +75,9 @@ public override async Task InitializeAsync(IDictionary query) private async Task SendMsgAsync() { await AddMessageAsync(); - await RelaodData(); Message = string.Empty; RaisePropertyChanged(() => Messages); + await RelaodData(); } private async Task RelaodData() diff --git a/src/Services/smalltalk/O2NextGen.SmallTalk.Impl/Services/InMemorySessionManager.cs b/src/Services/smalltalk/O2NextGen.SmallTalk.Impl/Services/InMemorySessionManager.cs index 5c5db9c4..19341dcb 100644 --- a/src/Services/smalltalk/O2NextGen.SmallTalk.Impl/Services/InMemorySessionManager.cs +++ b/src/Services/smalltalk/O2NextGen.SmallTalk.Impl/Services/InMemorySessionManager.cs @@ -82,13 +82,13 @@ public InMemorySessionManager() #endregion public async Task> GetAllAsync(CancellationToken ct) { - await Task.Delay(3000, ct); + await Task.Delay(1, ct); return await Task.FromResult>(Sessions.AsReadOnly()); } public async Task AddSessionAsync(ChatSessionModel chatSession, CancellationToken ct) { - await Task.Delay(3000, ct); + await Task.Delay(1, ct); chatSession.Id = ++_currentId; Sessions.Add(chatSession); return await Task.FromResult(chatSession); @@ -108,14 +108,14 @@ public async Task GetSessionAsync(long sessionId, Cancellation public async Task> GetMessages(long idSession, CancellationToken ct) { - await Task.Delay(3000, ct); + await Task.Delay(1, ct); var messages = Sessions.Single(_ => _.Id == idSession).Messages; return await Task.FromResult>(messages.AsReadOnly()); } public async Task GetMessageByIdAsync(long idSession, long id, CancellationToken ct) { - await Task.Delay(3000, ct); + await Task.Delay(1, ct); var message = Sessions.Single(_ => _.Id == idSession).Messages.Single(_ => _.Id == id); return await Task.FromResult(message); } From 45ec648b39e85fa699652b4374b7ff5c85edc937 Mon Sep 17 00:00:00 2001 From: Denis Prokhorchik Date: Sun, 10 Apr 2022 13:05:40 +0300 Subject: [PATCH 7/7] feat(issue-291): include seignalR to smalltalk mobile app --- .../O2NextGen.SmallTalk/GlobalSetting.cs | 1 + .../O2NextGen.SmallTalk.Core.csproj | 1 + .../Services/Chat/ChatService.cs | 4 +-- .../ViewModels/ChatDetailViewModel.cs | 25 +++++++++++++++--- .../Controllers/ChatController.cs | 22 ++++++++++++++++ .../Hubs/ChatHub.cs | 26 ++++++++++++++----- .../Properties/launchSettings.json | 2 +- .../O2NextGen.SmallTalk.SignalrHub/Startup.cs | 22 +++++++++++++--- 8 files changed, 88 insertions(+), 15 deletions(-) create mode 100644 src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Controllers/ChatController.cs diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/GlobalSetting.cs b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/GlobalSetting.cs index cf0b0c36..d8af2d53 100644 --- a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/GlobalSetting.cs +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/GlobalSetting.cs @@ -4,5 +4,6 @@ public class GlobalSetting { public string GatewayChatEndpoint { get; set; } = "https://api-smalltalk.o2bus.com"; public static GlobalSetting Instance { get; } = new GlobalSetting(); + public string HubConnectionURL { get; set; } = "http://localhost:5000/chathub"; } } diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.Core.csproj b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.Core.csproj index fdcf1b40..9796ffa8 100644 --- a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.Core.csproj +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/O2NextGen.SmallTalk.Core.csproj @@ -12,6 +12,7 @@ + diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/Chat/ChatService.cs b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/Chat/ChatService.cs index 37fa96dc..d7299645 100644 --- a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/Chat/ChatService.cs +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/Services/Chat/ChatService.cs @@ -26,8 +26,8 @@ public async Task AddMessageToSessionAsync(string message) { Id = 0, Message = message, - RecipientId = 2, - SenderId = 1 + RecipientId = 1, + SenderId = 2 }); return addMessage; diff --git a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/ViewModels/ChatDetailViewModel.cs b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/ViewModels/ChatDetailViewModel.cs index d6218362..313161c7 100644 --- a/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/ViewModels/ChatDetailViewModel.cs +++ b/src/MobileApps/Small-Talk/O2NextGen.SmallTalk/O2NextGen.SmallTalk/ViewModels/ChatDetailViewModel.cs @@ -1,6 +1,8 @@ -using O2NextGen.Sdk.NetCore.Models.smalltalk; +using Microsoft.AspNetCore.SignalR.Client; +using O2NextGen.Sdk.NetCore.Models.smalltalk; using O2NextGen.SmallTalk.Core.Services.Chat; using O2NextGen.SmallTalk.Core.ViewModels.Base; +using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Threading.Tasks; @@ -16,10 +18,13 @@ public class ChatDetailViewModel : ViewModelBase private ChatSession session; private ObservableCollection messages; private string message; + #endregion #region Commands public ICommand SendMsgCommand { get; private set; } + + private readonly HubConnection hubConnection; #endregion #region Props @@ -33,6 +38,18 @@ public ChatDetailViewModel() this.MultipleInitialization = true; _chatService = DependencyService.Get(); SendMsgCommand = new Command(async (item) => await SendMsgAsync()); + + hubConnection = new HubConnectionBuilder() + .WithUrl(GlobalSetting.Instance.HubConnectionURL) + .Build(); + + hubConnection.On("OnUpdateMessage", async () => + { + Console.WriteLine("TestMessage"); + await RelaodData(); + }); + + } public ChatSession Session @@ -68,8 +85,10 @@ public string Message public override async Task InitializeAsync(IDictionary query) { - await RelaodData(); - + + await hubConnection.StartAsync(); + await hubConnection.InvokeAsync("NewUserAsync","Denis"); + } private async Task SendMsgAsync() diff --git a/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Controllers/ChatController.cs b/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Controllers/ChatController.cs new file mode 100644 index 00000000..5b7fe224 --- /dev/null +++ b/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Controllers/ChatController.cs @@ -0,0 +1,22 @@ +using Microsoft.AspNetCore.Mvc; +using O2NextGen.SmallTalk.SignalrHub.Hubs; + +namespace O2NextGen.SmallTalk.SignalrHub.Controllers +{ + [Route("api/chat")] + [ApiController] + public class ChatController : ControllerBase + { + private readonly IChatHub chatHub; + + public ChatController(IChatHub chatHub) + { + this.chatHub = chatHub; + } + [HttpGet] + public void Test() + { + chatHub.UpdateMessages(); + } + } +} diff --git a/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Hubs/ChatHub.cs b/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Hubs/ChatHub.cs index 0ff3e1e5..44ce7f09 100644 --- a/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Hubs/ChatHub.cs +++ b/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Hubs/ChatHub.cs @@ -4,18 +4,32 @@ namespace O2NextGen.SmallTalk.SignalrHub.Hubs { - public class ChatHub : Hub + public interface IChatHub { + Task UpdateMessages(); + } + public class ChatHub : Hub, IChatHub { - public override async Task OnConnectedAsync() + public async Task NewUserAsync(string username) { - await Groups.AddToGroupAsync(Context.ConnectionId, Context.User.Identity.Name); + await Groups.AddToGroupAsync(Context.ConnectionId, username); await base.OnConnectedAsync(); } - public override async Task OnDisconnectedAsync(Exception ex) + public async Task UpdateMessages() { - await Groups.RemoveFromGroupAsync(Context.ConnectionId, Context.User.Identity.Name); - await base.OnDisconnectedAsync(ex); + await Clients.All.SendAsync("OnUpdateMessage"); } + + //public override async Task OnConnectedAsync() + //{ + // await Groups.AddToGroupAsync(Context.ConnectionId, Context.User.Identity.Name); + // await base.OnConnectedAsync(); + //} + + //public override async Task OnDisconnectedAsync(Exception ex) + //{ + // await Groups.RemoveFromGroupAsync(Context.ConnectionId, Context.User.Identity.Name); + // await base.OnDisconnectedAsync(ex); + //} } } diff --git a/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Properties/launchSettings.json b/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Properties/launchSettings.json index c47cdfeb..e4caa173 100644 --- a/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Properties/launchSettings.json +++ b/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Properties/launchSettings.json @@ -18,7 +18,7 @@ "O2NextGen.SmallTalk.SignalrHub": { "commandName": "Project", "launchBrowser": true, - "applicationUrl": "https://localhost:5001;http://localhost:5000", + "applicationUrl": "http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Startup.cs b/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Startup.cs index 1d9a4047..ff720698 100644 --- a/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Startup.cs +++ b/src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Startup.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; +using O2NextGen.SmallTalk.SignalrHub.Hubs; namespace O2NextGen.SmallTalk.SignalrHub { @@ -19,6 +20,7 @@ public void ConfigureServices(IServiceCollection services) .SetIsOriginAllowed((host) => true) .AllowCredentials()); }); + services.AddSingleton(); services.AddSignalR(); } @@ -29,13 +31,27 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseDeveloperExceptionPage(); } - + //else + //{ + // app.UseHsts(); + //} app.UseCors("CorsPolicy"); + //app.UseRouting(); - app.UseAuthentication(); + //app.UseAuthentication(); //app.UseAuthorization(); - + app.UseSignalR((routes) => + { + routes.MapHub("/chathub"); + }); + app.UseMvc(); + //app.UseEndpoints(endpoints => + //{ + // endpoints.MapControllers(); + // endpoints.MapHub("/signalrtc"); + // endpoints.MapHub("/o2hub"); + //}); //app.UseEndpoints(endpoints => //{ // endpoints.MapHub("/hub/chathub",