Skip to content

Commit 3364bb3

Browse files
authored
Merge pull request #206 from pticostaricags/development
Adding localization for NewVideoRecommendation. Converting to code-be…
2 parents af977ca + f22dc39 commit 3364bb3

File tree

3 files changed

+159
-128
lines changed

3 files changed

+159
-128
lines changed

src/FairPlayCombinedSln/FairPlayCombined.Services.Generators/ServiceOfTGenerator.cs

+14-7
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,26 @@ public class ServiceOfTGenerator : IIncrementalGenerator
1111
{
1212
public void Initialize(IncrementalGeneratorInitializationContext context)
1313
{
14+
try
15+
{
1416
#if DEBUG
1517

1618
#pragma warning disable S125 // Sections of code should not be commented out
17-
//System.Diagnostics.Debugger.Launch();
19+
//System.Diagnostics.Debugger.Launch();
1820
#endif
19-
var provider = context.SyntaxProvider.CreateSyntaxProvider(
20-
predicate: static (node, token) => node is ClassDeclarationSyntax,
21-
transform: static (ctx, token) => (ClassDeclarationSyntax)ctx.Node
22-
).Where(node => node is not null);
21+
var provider = context.SyntaxProvider.CreateSyntaxProvider(
22+
predicate: static (node, token) => node is ClassDeclarationSyntax,
23+
transform: static (ctx, token) => (ClassDeclarationSyntax)ctx.Node
24+
).Where(node => node is not null);
2325

24-
var compilation = context.CompilationProvider.Combine(provider.Collect());
26+
var compilation = context.CompilationProvider.Combine(provider.Collect());
2527

26-
context.RegisterSourceOutput(compilation, Execute);
28+
context.RegisterSourceOutput(compilation, Execute);
29+
}
30+
catch (Exception ex)
31+
{
32+
System.Diagnostics.Debug.WriteLine(ex.ToString());
33+
}
2734
}
2835

2936
#pragma warning disable S3776 // Cognitive Complexity of methods should not be too high

src/FairPlayCombinedSln/FairPlayTube.SharedUI/Components/Pages/Creator/NewVideoRecommendation.razor

+6-121
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,28 @@
1313
@using FairPlayCombined.Models.Pagination
1414
@using FairPlayTube.SharedUI.Components.Spinners
1515

16-
@inject IJSRuntime jsRuntime
17-
@inject IVideoInfoService videoInfoService
18-
@inject IUserProviderService userProviderService
19-
@inject IOpenAIService openAIService
20-
@inject IVideoCaptionsService videoCaptionsService
21-
@inject IPromptGeneratorService promptGeneratorService
22-
@inject IToastService toastService
23-
@inject INewVideoRecommendationService newVideoRecommendationService
24-
2516
<PageTitle>
26-
New Video Recommendations
17+
@Localizer![NewVideoRecommendationsTextKey]
2718
</PageTitle>
2819

2920
<FluentLabel Typo="Typography.H3">
30-
New Video Recommendations
21+
@Localizer![NewVideoRecommendationsTextKey]
3122
</FluentLabel>
3223

3324
<LoadingIndicator ShowSpinners="this.IsBusy"></LoadingIndicator>
3425

3526
<div>
3627
<FluentButton Type="ButtonType.Button" Appearance="Appearance.Accent" Loading="this.IsBusy"
3728
OnClick="OnCreateNewVideoRecommendationButtonclickedAsync">
38-
Create New Recommendation
29+
@Localizer![CreateNewRecommendationTextKey]
3930
</FluentButton>
4031
</div>
4132
@if (this.ItemsProvider != null)
4233
{
4334
<FluentDataGrid ItemsProvider="@this.ItemsProvider" Pagination="@this.paginationState">
44-
<TemplateColumn Class="multiline-text" Title="New Video Recommendations">
35+
<TemplateColumn Class="multiline-text" Title="@Localizer![NewVideoRecommendationsTextKey]">
4536
<FluentAccordion>
46-
<FluentAccordionItem Expanded="false" Heading="@($"{context.NewVideoRecommendationId} - Expand to see the content")">
37+
<FluentAccordionItem Expanded="false" Heading="@($"{context.NewVideoRecommendationId} - {Localizer![ExpandToSeeContentTextKey]}")">
4738
@((MarkupString)context.HtmlNewVideoRecommendation!)
4839
</FluentAccordionItem>
4940
</FluentAccordion>
@@ -57,110 +48,4 @@
5748
@((MarkupString)this.NewVideoRecommendationIdea)
5849
</p>
5950
}
60-
</div>
61-
62-
@code
63-
{
64-
private bool IsBusy { get; set; }
65-
private readonly CancellationTokenSource cancellationTokenSource = new();
66-
private string? NewVideoRecommendationIdea { get; set; }
67-
private string? RevisedPrompt { get; set; }
68-
private GridItemsProvider<NewVideoRecommendationModel>? ItemsProvider { get; set; }
69-
private readonly PaginationState paginationState = new()
70-
{
71-
ItemsPerPage = Constants.Pagination.PageSize
72-
};
73-
74-
protected override void OnInitialized()
75-
{
76-
base.OnInitialized();
77-
if (this.ItemsProvider is null)
78-
{
79-
ItemsProvider = async req =>
80-
{
81-
this.IsBusy = true;
82-
StateHasChanged();
83-
PaginationRequest paginationRequest = new()
84-
{
85-
PageSize = Constants.Pagination.PageSize,
86-
StartIndex = req.StartIndex,
87-
SortingItems = new SortingItem[]
88-
{
89-
new SortingItem()
90-
{
91-
PropertyName=nameof(NewVideoRecommendationModel.NewVideoRecommendationId),
92-
SortType= SortType.Descending
93-
}
94-
}
95-
};
96-
var items = await this.newVideoRecommendationService
97-
.GetPaginatedNewVideoRecommendationForUserIdAsync(
98-
paginationRequest, this.userProviderService.GetCurrentUserId()!, this.cancellationTokenSource.Token);
99-
var result = GridItemsProviderResult.From(items!.Items!, items.TotalItems);
100-
this.IsBusy = false;
101-
StateHasChanged();
102-
return result;
103-
};
104-
}
105-
}
106-
107-
private async Task OnCreateNewVideoRecommendationButtonclickedAsync()
108-
{
109-
try
110-
{
111-
this.IsBusy = true;
112-
StateHasChanged();
113-
var promptModel = await this.promptGeneratorService
114-
.GetPromptCompleteInfoAsync(Constants.PromptsNames.CreateNewVideoRecommendationIdea,
115-
this.cancellationTokenSource.Token);
116-
string currentUserId = this.userProviderService.GetCurrentUserId()!;
117-
var videos = await this.videoInfoService.GetPaginatedCompletedVideoInfobyUserIdAsync(new PaginationRequest()
118-
{
119-
PageSize = 5,
120-
SortingItems = new[]
121-
{
122-
new SortingItem()
123-
{
124-
PropertyName=nameof(VideoInfoModel.VideoInfoId),
125-
SortType = FairPlayCombined.Common.GeneratorsAttributes.SortType.Descending
126-
}
127-
},
128-
StartIndex = 0
129-
},
130-
currentUserId,
131-
this.cancellationTokenSource.Token);
132-
133-
var titles = videos!.Items!.Select(p => $"* Title: {p.Name}. Keywords: {String.Join(",", p.VideoKeywords!)}. Topics: {String.Join(",", p.VideoTopics!)}. Video Captions: {String.Join(",", p.EnglishCaptions)}\r\n");
134-
var userMessage = $"Video Titles: {String.Join(".", titles)}.";
135-
var result = await this.openAIService.GenerateChatCompletionAsync(promptModel!.BaseText!,
136-
userMessage, this.cancellationTokenSource.Token);
137-
if (result != null)
138-
{
139-
this.NewVideoRecommendationIdea = result!.choices![0]!.message!.content!;
140-
await newVideoRecommendationService.CreateNewVideoRecommendationAsync(
141-
createModel: new()
142-
{
143-
ApplicationUserId = currentUserId,
144-
HtmlNewVideoRecommendation = this.NewVideoRecommendationIdea
145-
},
146-
this.cancellationTokenSource.Token);
147-
await this.paginationState.SetCurrentPageIndexAsync(this.paginationState.CurrentPageIndex);
148-
}
149-
}
150-
catch (Exception ex)
151-
{
152-
this.toastService.ShowError(ex.Message);
153-
}
154-
finally
155-
{
156-
this.IsBusy = false;
157-
StateHasChanged();
158-
}
159-
}
160-
161-
public async ValueTask DisposeAsync()
162-
{
163-
await this.cancellationTokenSource.CancelAsync();
164-
this.cancellationTokenSource.Dispose();
165-
}
166-
}
51+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
using FairPlayCombined.Common;
2+
using FairPlayCombined.Common.CustomAttributes;
3+
using FairPlayCombined.Common.GeneratorsAttributes;
4+
using FairPlayCombined.Interfaces;
5+
using FairPlayCombined.Interfaces.Common;
6+
using FairPlayCombined.Interfaces.FairPlayTube;
7+
using FairPlayCombined.Models.FairPlayTube.NewVideoRecommendation;
8+
using FairPlayCombined.Models.FairPlayTube.VideoInfo;
9+
using FairPlayCombined.Models.Pagination;
10+
using Microsoft.AspNetCore.Components;
11+
using Microsoft.Extensions.Localization;
12+
using Microsoft.FluentUI.AspNetCore.Components;
13+
using Microsoft.JSInterop;
14+
15+
namespace FairPlayTube.SharedUI.Components.Pages.Creator
16+
{
17+
public partial class NewVideoRecommendation
18+
{
19+
[Inject] IJSRuntime? JsRuntime { get; set; }
20+
[Inject] IVideoInfoService? VideoInfoService { get; set; }
21+
[Inject] IUserProviderService? UserProviderService { get; set; }
22+
[Inject] IOpenAIService? OpenAIService { get; set; }
23+
[Inject] IVideoCaptionsService? VideoCaptionsService { get; set; }
24+
[Inject] IPromptGeneratorService? PromptGeneratorService { get; set; }
25+
[Inject] IToastService? ToastService { get; set; }
26+
[Inject] INewVideoRecommendationService? NewVideoRecommendationService { get; set; }
27+
[Inject] IStringLocalizer<NewVideoRecommendation>? Localizer { get; set; }
28+
private bool IsBusy { get; set; }
29+
private readonly CancellationTokenSource cancellationTokenSource = new();
30+
private string? NewVideoRecommendationIdea { get; set; }
31+
private GridItemsProvider<NewVideoRecommendationModel>? ItemsProvider { get; set; }
32+
private readonly PaginationState paginationState = new()
33+
{
34+
ItemsPerPage = Constants.Pagination.PageSize
35+
};
36+
37+
protected override void OnInitialized()
38+
{
39+
base.OnInitialized();
40+
if (this.ItemsProvider is null)
41+
{
42+
ItemsProvider = async req =>
43+
{
44+
this.IsBusy = true;
45+
StateHasChanged();
46+
PaginationRequest paginationRequest = new()
47+
{
48+
PageSize = Constants.Pagination.PageSize,
49+
StartIndex = req.StartIndex,
50+
SortingItems =
51+
[
52+
new SortingItem()
53+
{
54+
PropertyName=nameof(NewVideoRecommendationModel.NewVideoRecommendationId),
55+
SortType= SortType.Descending
56+
}
57+
]
58+
};
59+
var items = await this.NewVideoRecommendationService!
60+
.GetPaginatedNewVideoRecommendationForUserIdAsync(
61+
paginationRequest, this.UserProviderService!.GetCurrentUserId()!, this.cancellationTokenSource.Token);
62+
var result = GridItemsProviderResult.From(items!.Items!, items.TotalItems);
63+
this.IsBusy = false;
64+
StateHasChanged();
65+
return result;
66+
};
67+
}
68+
}
69+
70+
private async Task OnCreateNewVideoRecommendationButtonclickedAsync()
71+
{
72+
try
73+
{
74+
this.IsBusy = true;
75+
StateHasChanged();
76+
var promptModel = await this.PromptGeneratorService!
77+
.GetPromptCompleteInfoAsync(Constants.PromptsNames.CreateNewVideoRecommendationIdea,
78+
this.cancellationTokenSource.Token);
79+
string currentUserId = this.UserProviderService!.GetCurrentUserId()!;
80+
var videos = await this.VideoInfoService!.GetPaginatedCompletedVideoInfobyUserIdAsync(new PaginationRequest()
81+
{
82+
PageSize = 5,
83+
SortingItems = new[]
84+
{
85+
new SortingItem()
86+
{
87+
PropertyName=nameof(VideoInfoModel.VideoInfoId),
88+
SortType = SortType.Descending
89+
}
90+
},
91+
StartIndex = 0
92+
},
93+
currentUserId,
94+
this.cancellationTokenSource.Token);
95+
96+
var titles = videos!.Items!.Select(p => $"* Title: {p.Name}. Keywords: {String.Join(",", p.VideoKeywords!)}. Topics: {String.Join(",", p.VideoTopics!)}. Video Captions: {String.Join(",", p.EnglishCaptions)}\r\n");
97+
var userMessage = $"Video Titles: {String.Join(".", titles)}.";
98+
var result = await this.OpenAIService!.GenerateChatCompletionAsync(promptModel!.BaseText!,
99+
userMessage, this.cancellationTokenSource.Token);
100+
if (result != null)
101+
{
102+
this.NewVideoRecommendationIdea = result!.choices![0]!.message!.content!;
103+
await NewVideoRecommendationService!.CreateNewVideoRecommendationAsync(
104+
createModel: new()
105+
{
106+
ApplicationUserId = currentUserId,
107+
HtmlNewVideoRecommendation = this.NewVideoRecommendationIdea
108+
},
109+
this.cancellationTokenSource.Token);
110+
await this.paginationState.SetCurrentPageIndexAsync(this.paginationState.CurrentPageIndex);
111+
}
112+
}
113+
catch (Exception ex)
114+
{
115+
this.ToastService!.ShowError(ex.Message);
116+
}
117+
finally
118+
{
119+
this.IsBusy = false;
120+
StateHasChanged();
121+
}
122+
}
123+
124+
public async ValueTask DisposeAsync()
125+
{
126+
await this.cancellationTokenSource.CancelAsync();
127+
this.cancellationTokenSource.Dispose();
128+
}
129+
130+
#region Resource Keys
131+
[ResourceKey(defaultValue: "New Video Recommendations")]
132+
public const string NewVideoRecommendationsTextKey = "NewVideoRecommendationsText";
133+
[ResourceKey(defaultValue: "Create New Recommendation")]
134+
public const string CreateNewRecommendationTextKey = "CreateNewRecommendationText";
135+
[ResourceKey(defaultValue: "Expand to see the content")]
136+
public const string ExpandToSeeContentTextKey = "ExpandToSeeContentText";
137+
#endregion Resource Keys
138+
}
139+
}

0 commit comments

Comments
 (0)