Skip to content

Commit

Permalink
ApplyTmTemplate -> refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ealbu committed Apr 25, 2019
1 parent bd7260e commit b66a5bc
Showing 1 changed file with 16 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public ObservableCollection<TranslationMemory> TmCollection

public ICommand RemoveTMsCommand => _removeTMsCommand ?? (_removeTMsCommand = new CommandHandler(RemoveTMs, true));

public bool CanExecuteApply => (int)_templateValidity > 1 && IsThereAnyTmSelected();
public bool CanExecuteApply => (int)_templateValidity > 1 && SelectedTmsList.Count > 0;

public async void StartLoadingResourcesAndValidate(object sender, EventArgs e)
{
Expand Down Expand Up @@ -233,40 +233,16 @@ private string CreateNewFile(string filePath)

private void Tm_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == "IsSelected")
{
if (!(sender is TranslationMemory translationMemorySender)) return;

if (AreAllTmsSelected())
{
OnPropertyChanged(nameof(AllTmsChecked));
}

if (translationMemorySender.IsSelected)
{
OnPropertyChanged(nameof(CanExecuteApply));
}
if (e.PropertyName != "IsSelected") return;

if (!AreAllTmsSelected())
{
OnPropertyChanged(nameof(CanExecuteApply));
}
OnPropertyChanged(nameof(AllTmsChecked));
OnPropertyChanged(nameof(CanExecuteApply));

if (!translationMemorySender.IsSelected)
{
OnPropertyChanged(nameof(AllTmsChecked));
}
}
}

private bool AreAllTmsSelected()
{
for (var i = 0; i < TmCollection.Count - 1; i++)
{
if (TmCollection[i].IsSelected != TmCollection[i + 1].IsSelected) return false;
}

return TmCollection[0].IsSelected;
return TmCollection.Count > 0 && TmCollection.All(tm => tm.IsSelected);
}

private void AddFolder()
Expand Down Expand Up @@ -339,21 +315,17 @@ private async void ApplyTmTemplate()

if (!isValid) return;

var selectedTms = TmCollection.Where(tm => tm.IsSelected).ToList();
UnmarkTms(selectedTms);
UnmarkTms(SelectedTmsList);

if (selectedTms.Count == 0)
if (SelectedTmsList.Count == 0)
{
await _dialogCoordinator.ShowMessageAsync(this, PluginResources.Warning,
PluginResources.Select_at_least_one_TM);
return;
}

var settings = new Settings(AbbreviationsChecked, VariablesChecked, OrdinalFollowersChecked,
SegmentationRulesChecked);

ProgressVisibility = "Visible";
await Task.Run(() => _template.ApplyTmTemplate(selectedTms));
await Task.Run(() => _template.ApplyTmTemplate(SelectedTmsList));
ProgressVisibility = "Hidden";
}

Expand All @@ -370,14 +342,12 @@ await _dialogCoordinator.ShowMessageAsync(this, PluginResources.Warning,
private bool ValidateTemplate(bool checkIfBundlesPresent = true)
{
var isValid = true;

_unIDedLanguagesAsString = _unIDedLanguages?.Aggregate("", (i, j) => i + "\n \u2022" + j);

if (_template != null)
{
if (_template.LanguageResourceBundles != null && checkIfBundlesPresent)
{
if ( _template.LanguageResourceBundles.Count == 0)
if (_template.LanguageResourceBundles.Count == 0)
{
isValid = false;

Expand All @@ -402,7 +372,7 @@ private bool ValidateTemplate(bool checkIfBundlesPresent = true)
else
{
if (_message == PluginResources.Template_corrupted_or_file_not_template ||
_message == PluginResources.Template_filePath_Not_Correct)
_message == PluginResources.Template_filePath_Not_Correct)
{
isValid = false;
}
Expand All @@ -415,19 +385,17 @@ private bool ValidateTemplate(bool checkIfBundlesPresent = true)

return isValid;
}
private bool IsThereAnyTmSelected()
{
return TmCollection.Any(tm => tm.IsSelected);
}

private void UnmarkTms(List<TranslationMemory> tms)
{
foreach (var tm in tms)
{
tm.UnmarkTm();
tm.UnmarkTm();
}
}

public List<TranslationMemory> SelectedTmsList => TmCollection.Where(tm => tm.IsSelected).ToList();

private async void Import(object parameter)
{
var isValid = ValidateTemplate(false);
Expand Down Expand Up @@ -472,14 +440,14 @@ await Task.Run(() =>
else
{
ProgressVisibility = "Visible";
var selectedTmList = TmCollection.Where(tm => tm.IsSelected).ToList();
if (selectedTmList.Count > 0)

if (SelectedTmsList.Count > 0)
{
try
{
await Task.Run(() =>
{
_template.ImportResourcesFromSdltm(selectedTmList);
_template.ImportResourcesFromSdltm(SelectedTmsList);
});
}
catch (Exception e)
Expand Down

0 comments on commit b66a5bc

Please sign in to comment.