Skip to content

Commit

Permalink
fix: update service for smalltalk api
Browse files Browse the repository at this point in the history
  • Loading branch information
live-dev999 committed Apr 10, 2022
1 parent 15a1906 commit 0803488
Show file tree
Hide file tree
Showing 17 changed files with 287 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using O2NextGen.SmallTalk.Core.ViewModels;
using System;
using System.Linq;
using Xamarin.Forms;

namespace O2NextGen.SmallTalk.Core.Controls
{
public class ExtendedListView : ListView
{
public ExtendedListView() : this(ListViewCachingStrategy.RecycleElement)
{
}

public ExtendedListView(ListViewCachingStrategy cachingStrategy) : base(cachingStrategy)
{
//this.ItemAppearing += OnItemAppearing;
this.PropertyChanged += ExtendedListView_PropertyChanged;
}

private void ExtendedListView_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (this.BindingContext == null)
return;
var context = this.BindingContext as ChatDetailViewModel;
if (context.Messages == null) return;
//GET SCROLL CURRENT POSITION HERE!
var target = context.Messages[context.Messages.Count - 1];

ScrollTo(target, ScrollToPosition.End, true);
}

//private void OnItemAppearing(object sender, ItemVisibilityEventArgs e)
//{
// if (ItemAppearingCommand != null)
// {
// ItemAppearingCommand?.Execute(e.Item);
// }
//}
public void ScrollToLast()
{
Device.BeginInvokeOnMainThread(() =>
{
try
{
if (ItemsSource != null && ItemsSource.Cast<object>().Count() > 0)
{
var msg = ItemsSource.Cast<object>().LastOrDefault();
if (msg != null)
{
ScrollTo(msg, ScrollToPosition.End, false);
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +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";
public string HubConnectionURL { get; set; } = "https://signalr.o2bus.com/chathub";// "http://localhost:5000/chathub"; //
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ChatDetailViewModel : ViewModelBase

#region Commands
public ICommand SendMsgCommand { get; private set; }

public ICommand LoadItemsCommand { get; private set; }
private readonly HubConnection hubConnection;
#endregion

Expand All @@ -38,7 +38,7 @@ public ChatDetailViewModel()
this.MultipleInitialization = true;
_chatService = DependencyService.Get<IChatService>();
SendMsgCommand = new Command(async (item) => await SendMsgAsync());

LoadItemsCommand = new Command(async () => await RelaodData());
hubConnection = new HubConnectionBuilder()
.WithUrl(GlobalSetting.Instance.HubConnectionURL)
.Build();
Expand Down Expand Up @@ -84,11 +84,9 @@ public string Message
#region Methods
public override async Task InitializeAsync(IDictionary<string, string> query)
{


await hubConnection.StartAsync();
await hubConnection.InvokeAsync("NewUserAsync","Denis");

await RelaodData();
}

private async Task SendMsgAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
x:Class="O2NextGen.SmallTalk.Core.Views.ChatDetailView"
xmlns:viewModelBase="clr-namespace:O2NextGen.SmallTalk.Core.ViewModels.Base"
xmlns:viewmodels="clr-namespace:O2NextGen.SmallTalk.Core.ViewModels"
xmlns:smalltalk="clr-namespace:O2NextGen.Sdk.NetCore.Models.smalltalk;assembly=O2NextGen.Sdk.Models" xmlns:converter="clr-namespace:O2NextGen.SmallTalk.Core.Converters"
xmlns:smalltalk="clr-namespace:O2NextGen.Sdk.NetCore.Models.smalltalk;assembly=O2NextGen.Sdk.Models" xmlns:converter="clr-namespace:O2NextGen.SmallTalk.Core.Converters" xmlns:controls="clr-namespace:O2NextGen.SmallTalk.Core.Controls"
viewModelBase:ViewModelLocator.AutoWireViewModel="true"
x:DataType="viewmodels:ChatDetailViewModel">
<ContentPage.Resources>
Expand All @@ -19,9 +19,9 @@
<Label Text="Welcome to Xamarin.Forms!"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
<ListView ItemsSource="{Binding Messages}" SeparatorVisibility="None"
<controls:ExtendedListView ItemsSource="{Binding Messages}" SeparatorVisibility="None" x:Name="ItemsListView"
HasUnevenRows="True">
<ListView.ItemTemplate>
<controls:ExtendedListView.ItemTemplate>
<DataTemplate x:DataType="smalltalk:ChatMessage">
<ViewCell>
<Grid RowDefinitions="auto,auto">
Expand Down Expand Up @@ -60,8 +60,8 @@
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</controls:ExtendedListView.ItemTemplate>
</controls:ExtendedListView>
<StackLayout Grid.Row="1"
Orientation="Horizontal">
<Entry Text="{Binding Message}"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace O2NextGen.SmallTalk.Core.Views
using O2NextGen.SmallTalk.Core.ViewModels;
using Xamarin.Forms;

namespace O2NextGen.SmallTalk.Core.Views
{

public partial class ChatDetailView : ContentPageBase
Expand All @@ -7,5 +10,43 @@ public ChatDetailView()
{
InitializeComponent();
}

//protected override void OnBindingContextChanged()
//{
// base.OnBindingContextChanged();
// if (this.BindingContext == null)
// return;
// var context = this.BindingContext as ChatDetailViewModel;
// //if (context.Messages.Count == 0)
// //{
// // context.LoadItemsCommand.Execute(null);
// //}
// if (context.Messages == null)
// return;
// context.Messages.CollectionChanged += (sender, e) =>
// {
// //GET SCROLL CURRENT POSITION HERE!
// var target = context.Messages[context.Messages.Count - 1];

// ItemsListView.ScrollTo(target, ScrollToPosition.End, false);
// };
//}
protected override void OnAppearing()
{
base.OnAppearing();
//if (this.BindingContext == null)
// return;
//var context = this.BindingContext as ChatDetailViewModel;
//context.PropertyChanged += (sender, e) =>
//{
// if (context.Messages == null)
// return;
// //GET SCROLL CURRENT POSITION HERE!
// var target = context.Messages[context.Messages.Count - 1];

// ItemsListView.ScrollTo(target, ScrollToPosition.End, true);
//};
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": 1,
"isRoot": true,
"tools": {
"dotnet-ef": {
"version": "6.0.3",
"commands": [
"dotnet-ef"
]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Extensions.Logging;
using O2NextGen.Sdk.NetCore.Models.smalltalk;
using O2NextGen.SmallTalk.Api.Mappings;
using O2NextGen.SmallTalk.Api.Services;
using O2NextGen.SmallTalk.Business.Models;
using O2NextGen.SmallTalk.Business.Services;
using System.Threading;
Expand All @@ -18,18 +19,20 @@ public class ChatController : ControllerBase

private readonly IHostingEnvironment _environment;
private readonly ILogger<VersionController> _logger;
private readonly ISignalRService _signalRService;
private readonly IChatManager _chatManager;

#endregion


#region Ctors

public ChatController(IHostingEnvironment environment, ILogger<VersionController> logger,
public ChatController(IHostingEnvironment environment, ILogger<VersionController> logger, ISignalRService signalRService,
IChatManager chatManager)
{
_environment = environment;
_logger = logger;
_signalRService = signalRService;
_chatManager = chatManager;
}

Expand Down Expand Up @@ -71,7 +74,7 @@ public async Task<IActionResult> AddAsync(long sessionId, ChatMessage chatMessag
throw new System.ArgumentNullException(nameof(chatMessage));

ChatMessageModel resultSession = await _chatManager.AddMessage(sessionId, chatMessage.ToModel(), ct);

await _signalRService.GetAsync(ct);
return Ok(resultSession.ToViewModel());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<UserSecretsId>3e239bd5-b8d6-455f-8e46-ea4782668914</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileContext>..\..\..</DockerfileContext>
<DockerComposeProjectPath>..\..\..\docker-compose.dcproj</DockerComposeProjectPath>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<UserSecretsId>3e239bd5-b8d6-455f-8e46-ea4782668914</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileContext>..\..\..</DockerfileContext>
<DockerComposeProjectPath>..\..\..\docker-compose.dcproj</DockerComposeProjectPath>
</PropertyGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.14.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="2.2.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.14.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Http.Polly">
<Version>2.2.0</Version>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\SDKs\O2NG.Sdk.NetCore\O2NG.Sdk.NetCore.Models\O2NextGen.Sdk.NetCore.Models.csproj" />
<ProjectReference Include="..\O2NextGen.SmallTalk.Business\O2NextGen.SmallTalk.Business.csproj" />
<ProjectReference Include="..\O2NextGen.SmallTalk.Data\O2NextGen.SmallTalk.Data.csproj" />
<ProjectReference Include="..\O2NextGen.SmallTalk.Impl\O2NextGen.SmallTalk.Impl.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\SDKs\O2NG.Sdk.NetCore\O2NG.Sdk.NetCore.Models\O2NextGen.Sdk.NetCore.Models.csproj" />
<ProjectReference Include="..\O2NextGen.SmallTalk.Business\O2NextGen.SmallTalk.Business.csproj" />
<ProjectReference Include="..\O2NextGen.SmallTalk.Data\O2NextGen.SmallTalk.Data.csproj" />
<ProjectReference Include="..\O2NextGen.SmallTalk.Impl\O2NextGen.SmallTalk.Impl.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace O2NextGen.SmallTalk.Api.Services
{
public class SignalRService: ISignalRService
{
private readonly HttpClient _httpClient;
public SignalRService(HttpClient httpClient)
{
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
}

public async Task GetAsync(CancellationToken ct)
{
await _httpClient.GetAsync($"api/chat", ct);
}
}

public interface ISignalRService
{
Task GetAsync(CancellationToken ct);
}
}
27 changes: 26 additions & 1 deletion src/Services/smalltalk/O2NextGen.SmallTalk.Api/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using O2NextGen.SmallTalk.Api.Helpers;
using O2NextGen.SmallTalk.Api.Services;
using Polly;
using System;
using System.Threading.Tasks;

namespace O2NextGen.SmallTalk.Api
Expand All @@ -20,6 +24,7 @@ public void ConfigureServices(IServiceCollection services)
{
services.AddRequiredMvcComponents();
services.AddBusiness();
services.AddApplicationServices(Configuration);
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
Expand Down Expand Up @@ -49,5 +54,25 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
app.UseMvc();
}
}
}
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddApplicationServices(this IServiceCollection services, IConfiguration configuration)
{
//register delegating handlers
// services.AddTransient<HttpClientAuthorizationDelegatingHandler>();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

//register http services
services
.AddHttpClient<ISignalRService, SignalRService>("Signal-R", client =>
{
client.BaseAddress = new Uri(configuration.GetValue<string>("urls:SignalRUrl"));
})
.AddTransientHttpErrorPolicy(builder => builder.WaitAndRetryAsync(5, arrempt => TimeSpan.FromSeconds(arrempt * 2)
));

return services;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"Default": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"urls": {
"SignalRUrl": "http://localhost:5000"
}
}

Loading

0 comments on commit 0803488

Please sign in to comment.