46 lines
1.4 KiB
C#
46 lines
1.4 KiB
C#
using SortingModel;
|
|
using System.Diagnostics;
|
|
|
|
namespace TextSortWindow
|
|
{
|
|
public class SortController
|
|
{
|
|
private readonly ISortWindow _view;
|
|
private readonly ISortingService _model;
|
|
|
|
public SortController(ISortWindow view, ISortingService model)
|
|
{
|
|
this._view = view;
|
|
this._model = model;
|
|
|
|
List<string> sortOptions = _model.GetAvailableAlgorithmNames().ToList();
|
|
_view.SetSortOptions(sortOptions);
|
|
|
|
_view.SortButtonClicked += OnSortRequested;
|
|
}
|
|
|
|
private async void OnSortRequested(object? sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
string inputText = _view.InputText;
|
|
string strategy = _view.SelectedStrategy ?? string.Empty;
|
|
|
|
Stopwatch watch = Stopwatch.StartNew();
|
|
string sortedText = await Task.Run(() => _model.SortText(inputText, strategy)).ConfigureAwait(true);
|
|
watch.Stop();
|
|
_view.DisplaySortedText(sortedText);
|
|
_view.DisplayTime(watch.Elapsed.TotalMilliseconds);
|
|
}
|
|
catch (ArgumentException ex)
|
|
{
|
|
_view.DisplaySortedText($"Error: {ex.Message}");
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
_view.DisplaySortedText($"Error: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
}
|