Skip to content

Commit

Permalink
feat: support click link in search detail page
Browse files Browse the repository at this point in the history
  • Loading branch information
AuroraZiling committed Oct 26, 2024
1 parent d2dc408 commit d07a5ee
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 53 deletions.
7 changes: 2 additions & 5 deletions src/Services/Action/ActionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public void Runner()
? $"[Runner] {item} uninstall sub-task completed"
: $"[Runner] {item} uninstall sub-task failed\n Reason:{result.Message}");
}
Log.Information($"[Runner] Task {currentAction.OperationType} Completed");
break;
}

Expand All @@ -93,7 +92,6 @@ public void Runner()
? $"[Runner] {item} install sub-task completed"
: $"[Runner] {item} install sub-task failed\n Reason:{result.Message}");
}
Log.Information($"[Runner] Task {currentAction.OperationType} Completed");
break;
}
case ActionType.InstallByRequirements:
Expand All @@ -108,7 +106,6 @@ public void Runner()
currentAction.DetectIssue = true;
consoleError.AppendLine(result.Message);
}
Log.Information($"[Runner] Task {currentAction.OperationType} Completed");
break;
}
case ActionType.Download:
Expand All @@ -129,7 +126,6 @@ public void Runner()
? $"[Runner] {item} download sub-task completed"
: $"[Runner] {item} download sub-task failed\n Reason:{result.Message}");
}
Log.Information($"[Runner] Task {currentAction.OperationType} Completed");
break;
}
case ActionType.Update:
Expand All @@ -150,12 +146,13 @@ public void Runner()
? $"[Runner] {item} update sub-task completed"
: $"[Runner] {item} update sub-task failed\n Reason:{result.Message}");
}
Log.Information($"[Runner] Task {currentAction.OperationType} Completed");
break;
}
default:
throw new ArgumentOutOfRangeException();
}

Log.Information($"[Runner] Task {currentAction.OperationType} Completed");
currentAction.CompletedSubTaskNumber = currentAction.TotalSubTaskNumber;
currentAction.OperationStatus = "Completed";
if (errorDetection)
Expand Down
114 changes: 68 additions & 46 deletions src/ViewModels/Pages/Search/SearchDetailViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,58 +129,81 @@ private void Receive(object recipient, SearchDetailMessage message)
{
Package = message.Package;

SearchDetailPage.ProjectDescriptionWebView!.Loaded += async (_, _) =>
SearchDetailPage.ProjectDescriptionWebView!.Loaded += async (_, _) => await LoadPackageDetailsAsync(message);
}

private async Task LoadPackageDetailsAsync(SearchDetailMessage message)
{
try
{
ProjectDescriptionVisibility = false;
var packageVersions = await _environmentService.GetVersions(Package!.Name, new CancellationToken(), Configuration.AppConfig!.PackageSource.AllowNonRelease);
switch (packageVersions.Status)
{
case 1 or 2:
_toastService.Error(Lang.SearchDetail_Exception_NetworkError);
await Task.Delay(1000);
_navigationService.GoBack();
return;

default:
AvailableVersions = new ObservableCollection<string>(packageVersions.Versions!.Reverse());
TargetVersion = AvailableVersions.First();
break;
}
await CoreWebView2Environment.CreateAsync(null, AppInfo.CachesDir);
await SearchDetailPage.ProjectDescriptionWebView.EnsureCoreWebView2Async().ConfigureAwait(true);
try
{
var projectDescriptionUrl = message.Package.Url;
var html = await _httpClient.GetStringAsync(projectDescriptionUrl);
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(html);
string projectDescriptionHtml = string.Format(HtmlModel, _themeType, ThemeTypeInHex, htmlDocument.DocumentNode.SelectSingleNode("//*[@id=\"description\"]/div").InnerHtml);

SearchDetailPage.ProjectDescriptionWebView.CoreWebView2.Profile.PreferredColorScheme = CoreWebView2PreferredColorScheme.Dark;
SearchDetailPage.ProjectDescriptionWebView.NavigateToString(projectDescriptionHtml);
}
catch (Exception ex)
{
Log.Error(ex.Message);
_toastService.Error(Lang.SearchDetail_ProjectDescription_LoadFailed);
string projectDescriptionHtml = string.Format(HtmlModel, _themeType, ThemeTypeInHex, $"<p>{Lang.SearchDetail_ProjectDescription_LoadFailed}</p>");

SearchDetailPage.ProjectDescriptionWebView.CoreWebView2.Profile.PreferredColorScheme = CoreWebView2PreferredColorScheme.Dark;
SearchDetailPage.ProjectDescriptionWebView.NavigateToString(projectDescriptionHtml);
}
finally
{
await Task.Delay(500);
ProjectDescriptionVisibility = true;
}
};
await SetupWebViewAsync(message.Package);
}
catch (Exception ex)
{
HandleLoadingError(ex);
}
finally
{
await Task.Delay(500);
ProjectDescriptionVisibility = true;
}
}

public Task OnNavigatedToAsync()
private async Task SetupWebViewAsync(QueryListItemModel package)
{
var packageVersions = await _environmentService.GetVersions(package.Name, new CancellationToken(), Configuration.AppConfig!.PackageSource.AllowNonRelease);
if (packageVersions.Status is 1 or 2)
{
_toastService.Error(Lang.SearchDetail_Exception_NetworkError);
await Task.Delay(1000);
_navigationService.GoBack();
return;
}

AvailableVersions = new ObservableCollection<string>(packageVersions.Versions!.Reverse());
TargetVersion = AvailableVersions.First();

await CoreWebView2Environment.CreateAsync(null, AppInfo.CachesDir);
await SearchDetailPage.ProjectDescriptionWebView!.EnsureCoreWebView2Async().ConfigureAwait(true);
await LoadProjectDescriptionAsync(package.Url);
}

private async Task LoadProjectDescriptionAsync(string projectDescriptionUrl)
{
try
{
var html = await _httpClient.GetStringAsync(projectDescriptionUrl);
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(html);
string projectDescriptionHtml = string.Format(HtmlModel, _themeType, ThemeTypeInHex, htmlDocument.DocumentNode.SelectSingleNode("//*[@id=\"description\"]/div").InnerHtml);

SearchDetailPage.ProjectDescriptionWebView!.CoreWebView2.Profile.PreferredColorScheme = CoreWebView2PreferredColorScheme.Dark;
SearchDetailPage.ProjectDescriptionWebView.NavigateToString(projectDescriptionHtml);
}
catch (Exception ex)
{
HandleLoadingError(ex);
}
}

private void HandleLoadingError(Exception ex)
{
Log.Error(ex.Message);
_toastService.Error(Lang.SearchDetail_ProjectDescription_LoadFailed);
string projectDescriptionHtml = string.Format(HtmlModel, _themeType, ThemeTypeInHex, $"<p>{Lang.SearchDetail_ProjectDescription_LoadFailed}</p>");
SearchDetailPage.ProjectDescriptionWebView!.CoreWebView2.Profile.PreferredColorScheme = CoreWebView2PreferredColorScheme.Dark;
SearchDetailPage.ProjectDescriptionWebView.NavigateToString(projectDescriptionHtml);
}

public async Task OnNavigatedToAsync()
{
if (!_isInitialized)
InitializeViewModel();
_navigationService.GetNavigationControl().BreadcrumbBar!.Visibility = Visibility.Collapsed;
await Application.Current.Dispatcher.InvokeAsync(() =>
{
_navigationService.GetNavigationControl().BreadcrumbBar!.Visibility = Visibility.Collapsed;
});
switch (_themeService.GetTheme())
{
case ApplicationTheme.Light:
Expand All @@ -200,7 +223,6 @@ public Task OnNavigatedToAsync()
throw new ArgumentOutOfRangeException();
}
SearchDetailPage.ProjectDescriptionWebView!.DefaultBackgroundColor = Color.FromArgb(_themeTypeInInteger);
return Task.CompletedTask;
}

public Task OnNavigatedFromAsync()
Expand Down
20 changes: 18 additions & 2 deletions src/Views/Pages/Search/SearchDetailPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Microsoft.Web.WebView2.Core;
using System.Diagnostics;
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.Wpf;
using Serilog;
using Wpf.Ui.Abstractions.Controls;
using SearchDetailViewModel = PipManager.Windows.ViewModels.Pages.Search.SearchDetailViewModel;

Expand All @@ -21,9 +23,23 @@ public SearchDetailPage(SearchDetailViewModel viewModel)

private void SearchDetailProjectDescriptionWebView_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
{
if (e.Uri.StartsWith("http://") || e.Uri.StartsWith("https://"))
var uri = e.Uri;
if (uri.StartsWith("http://") || uri.StartsWith("https://"))
{
e.Cancel = true;

try
{
Process.Start(new ProcessStartInfo
{
FileName = uri,
UseShellExecute = true
});
}
catch (Exception ex)
{
Log.Error("Failed to open link in external browser: " + ex.Message);
}
}
}

Expand Down

0 comments on commit d07a5ee

Please sign in to comment.