Push project
This commit is contained in:
45
TextSort/SortController.cs
Normal file
45
TextSort/SortController.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user