Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
qiuhaotc committed Dec 15, 2020
1 parent baab918 commit be0f373
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 60 deletions.
6 changes: 3 additions & 3 deletions src/CodeIndex.Server/Pages/Lucene.razor
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
</div>
</div>
<div class="col-sm">
<div class="input-group">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">Index Name</span>
</div>
Expand Down Expand Up @@ -116,11 +116,11 @@
<div class="alert alert-success" role="alert">
@if (HintWords.Result.Any())
{
<text>Find Hint: @string.Join(", ", HintWords.Result)</text>
<text>Find Hints: @string.Join(", ", HintWords.Result)</text>
}
else
{
<text>No hint</text>
<text>No Hint</text>
}
</div>
}
Expand Down
6 changes: 3 additions & 3 deletions src/CodeIndex.Server/Pages/LuceneWithLineNumber.razor
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
</div>
</div>
<div class="col-sm">
<div class="input-group">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">Index Name</span>
</div>
Expand Down Expand Up @@ -116,11 +116,11 @@
<div class="alert alert-success" role="alert">
@if (HintWords.Result.Any())
{
<text>Find Hint: @string.Join(", ", HintWords.Result)</text>
<text>Find Hints: @string.Join(", ", HintWords.Result)</text>
}
else
{
<text>No hint</text>
<text>No Hint</text>
}
</div>
}
Expand Down
10 changes: 7 additions & 3 deletions src/CodeIndex.Server/Pages/Management.razor
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,15 @@

public IndexStatusInfo IndexStatusInfoForEditOrAdd { get; set; } = new IndexStatusInfo(IndexStatus.Idle, new IndexConfig());

protected override async Task OnInitializedAsync()
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await base.OnInitializedAsync();
await base.OnAfterRenderAsync(firstRender);

await GetLists();
if (firstRender)
{
await GetLists();
StateHasChanged();
}
}

void Edit(IndexStatusInfo indexStatusInfo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<Compile Include="Converts\StringToXamlConverter.cs" />
<Compile Include="Models\BaseViewModel.cs" />
<Compile Include="Models\CodeIndexSearchViewModel.cs" />
<Compile Include="Models\Commands.cs" />
<Compile Include="Models\ConfigHelper.cs" />
<Compile Include="Models\HintWord.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@
<Label Content="Service Url:" Grid.Column="0" VerticalAlignment="Center"/>
<TextBox Height="23" Grid.Column="1" TextWrapping="NoWrap" Text="{Binding ServiceUrl}" Width="200"/>
<Label Content="Index:" Grid.Column="2" VerticalAlignment="Center"/>
<ComboBox SelectedValue="{Binding IndexPk}" Grid.Column="3" ItemsSource="{Binding IndexInfos}" DisplayMemberPath="Name" SelectedValuePath="Value" Width="100" VerticalAlignment="Center"/>
<ComboBox SelectedValue="{Binding IndexPk}" Grid.Column="3" ItemsSource="{Binding IndexInfos}" DisplayMemberPath="Name" SelectedValuePath="Value" Width="200" VerticalAlignment="Center"/>
<Button x:Name="RefreshButton" Grid.Column="4" Margin="10 0 0 0" Content="Refresh Index" Command="{Binding RefreshIndexCommand}" Style="{StaticResource InfoButton}" Height="26" Width="82"/>
<Label Content="Result Info:" VerticalAlignment="Center" Grid.Column="5"/>
<ScrollViewer VerticalScrollBarVisibility="Auto" MaxHeight="60" Grid.Column="6">
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" MaxHeight="60" Grid.Column="6">
<Label Content="{Binding ResultInfo}" Foreground="Yellow"/>
</ScrollViewer>
</Grid>
<DockPanel DockPanel.Dock="Top">
<Label Content="Hind Words:" DockPanel.Dock="Left"/>
<ScrollViewer DockPanel.Dock="Right" VerticalScrollBarVisibility="Auto" MaxHeight="52">
<ScrollViewer DockPanel.Dock="Right" VerticalScrollBarVisibility="Auto" MaxHeight="100">
<ItemsControl ItemsSource="{Binding HintWords}" ItemsPanel="{StaticResource ItemsPanelTemplateHorizontal}" HorizontalAlignment="Left">
<ItemsControl.ItemTemplate>
<DataTemplate>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class CodeIndexSearchViewModel : BaseViewModel
public CodeIndexSearchViewModel()
{
serviceUrl = ConfigHelper.Configuration.AppSettings.Settings[nameof(ServiceUrl)].Value;
LoadIndexInfos();
_ = LoadIndexInfosAsync();
}

public Guid IndexPk
Expand Down Expand Up @@ -49,13 +49,13 @@ public string ServiceUrl
serviceUrl = value;
}

LoadIndexInfos();
_ = LoadIndexInfosAsync();
}
}

CancellationTokenSource tokenToLoadIndexInfos;

async Task LoadIndexInfos()
async Task LoadIndexInfosAsync()
{
try
{
Expand All @@ -67,7 +67,16 @@ async Task LoadIndexInfos()
var result = await client.ApiLuceneGetindexviewlistAsync(tokenToLoadIndexInfos.Token);

IndexInfos = result.Status.Success ? result.Result.Select(u => new Item<Guid>(u.IndexName, u.Pk)).ToList() : IndexInfos;
IndexPk = IndexInfos.FirstOrDefault()?.Value ?? Guid.Empty;

if (IndexPk == Guid.Empty || IndexInfos.All(u => u.Value != IndexPk))
{
IndexPk = IndexInfos.FirstOrDefault()?.Value ?? Guid.Empty;
}
else
{
IndexPk = IndexPk;
}

ResultInfo = string.Empty;
}
catch (Exception ex)
Expand Down Expand Up @@ -190,10 +199,10 @@ public ICommand SearchIndexCommand
{
if (searchIndexCommand == null)
{
searchIndexCommand = new CommonCommand(
param => SearchCodeIndexAsync(),
param => true
);
searchIndexCommand = new AsyncCommand(
SearchCodeIndexAsync,
() => !IsSearching,
null);
}
return searchIndexCommand;
}
Expand Down Expand Up @@ -221,10 +230,10 @@ public ICommand RefreshIndexCommand
{
if (refreshIndexCommand == null)
{
refreshIndexCommand = new CommonCommand(
param => LoadIndexInfos(),
param => true
);
refreshIndexCommand = new AsyncCommand(
LoadIndexInfosAsync,
() => true,
null);
}

return refreshIndexCommand;
Expand Down Expand Up @@ -330,41 +339,5 @@ bool IsValidate()
}

#endregion

public class CommonCommand : ICommand
{
readonly Action<object> execute;
readonly Predicate<object> canExecute;

public CommonCommand(Action<object> execute) : this(execute, null)
{
}

public CommonCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");

this.execute = execute;
this.canExecute = canExecute;
}

[DebuggerStepThrough]
public bool CanExecute(object parameters)
{
return canExecute == null ? true : canExecute(parameters);
}

public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}

public void Execute(object parameters)
{
execute(parameters);
}
}
}
}
107 changes: 107 additions & 0 deletions src/CodeIndex.VisualStudioExtension/Models/Commands.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows.Input;

namespace CodeIndex.VisualStudioExtension
{
public class CommonCommand : ICommand
{
readonly Action<object> execute;
readonly Predicate<object> canExecute;

public CommonCommand(Action<object> execute) : this(execute, null)
{
}

public CommonCommand(Action<object> execute, Predicate<object> canExecute)
{
this.execute = execute ?? throw new ArgumentNullException(nameof(execute));
this.canExecute = canExecute;
}

[DebuggerStepThrough]
public bool CanExecute(object parameters)
{
return canExecute == null || canExecute(parameters);
}

public event EventHandler CanExecuteChanged
{
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}

public void Execute(object parameters)
{
execute(parameters);
}
}

public class AsyncCommand : ICommand
{
public event EventHandler CanExecuteChanged;
bool isExecuting;
readonly Func<Task> execute;
readonly Func<bool> canExecute;
readonly Action<Exception> errorHandler;

public AsyncCommand(
Func<Task> execute,
Func<bool> canExecute,
Action<Exception> errorHandler)
{
this.execute = execute;
this.canExecute = canExecute;
this.errorHandler = errorHandler;
}

public bool CanExecute()
{
return !isExecuting && (canExecute?.Invoke() ?? true);
}

public async Task ExecuteAsync()
{
if (CanExecute())
{
try
{
isExecuting = true;
await execute();
}
finally
{
isExecuting = false;
}
}

RaiseCanExecuteChanged();
}

public void RaiseCanExecuteChanged()
{
CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}

public bool CanExecute(object parameter)
{
return CanExecute();
}

public void Execute(object parameter)
{
Microsoft.VisualStudio.Shell.ThreadHelper.JoinableTaskFactory.Run(async () =>
{
try
{
await ExecuteAsync();
}
catch (Exception ex)
{
errorHandler?.Invoke(ex);
}
});
}
}
}

0 comments on commit be0f373

Please sign in to comment.