Skip to content

Commit

Permalink
Adding Languages into settings to modify multiple countries + design
Browse files Browse the repository at this point in the history
  • Loading branch information
Damien LEROY committed Jan 16, 2024
1 parent 57225e6 commit 1a9e7c0
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 7 deletions.
5 changes: 5 additions & 0 deletions PowerAccent.Core/PowerAccent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public void ReloadSettings()
_settingService.Reload();
}

public void CheckVersion()
{
_settingService.UpdateSettingsVersion();
}

public void Dispose()
{
_keyboardListener.Dispose();
Expand Down
31 changes: 31 additions & 0 deletions PowerAccent.Core/Services/SettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ public Language SelectedLanguage
set { this["SelectedLanguage"] = value; Save(); }
}

[UserScopedSetting]
[DefaultSettingValue("ALL")]
public Language[] SelectedLanguages
{
get { return (Language[])this["SelectedLanguages"]; }
set { this["SelectedLanguages"] = value; Save(); }
}

[UserScopedSetting]
[DefaultSettingValue("Top")]
public Position Position
Expand Down Expand Up @@ -61,6 +69,14 @@ public bool InsertSpaceAfterSelection
set { this["InsertSpaceAfterSelection"] = value; Save(); }
}

[UserScopedSetting]
[DefaultSettingValue("0")]
public int SettingsVersion
{
get { return (int)this["SettingsVersion"]; }
set { this["SettingsVersion"] = value; Save(); }
}

#region LetterKey

public void SetLetterKey(LetterKey letter, char[] value)
Expand Down Expand Up @@ -103,6 +119,21 @@ private void AddingProperty(string key)
}
}

// Check and update properties from previous version
public void UpdateSettingsVersion()
{
if (this.SettingsVersion == 1)
return;

if (this.SettingsVersion == 0)
{
SelectedLanguages = new[] { SelectedLanguage };
}

SettingsVersion = 1;
this.Save();
}

#endregion
}

Expand Down
1 change: 1 addition & 0 deletions PowerAccent.UI/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ protected override void OnSourceInitialized(EventArgs e)
_powerAccent.OnChangeDisplay += PowerAccent_OnChangeDisplay;
_powerAccent.OnSelectCharacter += PowerAccent_OnSelectionCharacter;
this.Visibility = Visibility.Hidden;
_powerAccent.CheckVersion();
}

private void PowerAccent_OnSelectionCharacter(int index)
Expand Down
8 changes: 4 additions & 4 deletions PowerAccent.UI/SettingsPage/CountriesPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<Grid>
<Grid.Resources>
<Style x:Key="Flag" TargetType="RadioButton">
<Style x:Key="Flag" TargetType="CheckBox">
<Style.Triggers>
<Trigger Property="IsChecked" Value="False">
<Setter Property="Opacity" Value="0.4"/>
Expand All @@ -27,7 +27,7 @@

<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="RadioButton">
<ControlTemplate TargetType="CheckBox">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
Expand All @@ -54,11 +54,11 @@
</d:ListView.ItemsSource>
<ListView.ItemTemplate>
<DataTemplate>
<RadioButton Tag="{Binding Path=Name}" IsChecked="{Binding Path=IsChecked}" Style="{StaticResource Flag}" Margin="25,0,25,0" GroupName="Countries" Checked="RadioButton_Checked">
<CheckBox Tag="{Binding Path=Name}" IsChecked="{Binding Path=IsChecked}" Style="{StaticResource Flag}" Margin="25,0,25,0" Checked="CheckBox_OnChanged" Unchecked="CheckBox_OnChanged">
<WrapPanel>
<Image Source="{Binding Path=ImageUrl, Converter={StaticResource stringToImageSourceConverter}}" Width="75" Height="75" Cursor="Hand" />
</WrapPanel>
</RadioButton>
</CheckBox>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Expand Down
36 changes: 33 additions & 3 deletions PowerAccent.UI/SettingsPage/CountriesPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,48 @@ protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);

RefreshData();
}

private void RefreshData()
{
Countries.ItemsSource = Enum.GetNames<Language>().Select(l => new Country
{
Name = l,
ImageUrl = $"/Resources/Flags/{l}.jpg",
IsChecked = l == _settingService.SelectedLanguage.ToString()
IsChecked = _settingService.SelectedLanguages.Any(s => s.ToString() == l)
}).ToList();
}

private void RadioButton_Checked(object sender, RoutedEventArgs e)
private void CheckBox_OnChanged(object sender, RoutedEventArgs e)
{
_settingService.SelectedLanguage = Enum.Parse<Language>((((RadioButton)sender).DataContext as Country).Name);
var selectedCountry = ((CheckBox)sender).DataContext as Country;
var doRefresh = false;
var selectedLanguages = Countries.Items.Cast<Country>().Where(c => c.IsChecked).Select(c => Enum.Parse<Language>(c.Name)).ToArray();
if (selectedCountry.Name == Core.Language.ALL.ToString() && selectedCountry.IsChecked)
{
selectedLanguages = new[] { Core.Language.ALL };
Countries.ItemsSource.Cast<Country>().ToList().ForEach(c => c.IsChecked = false);
Countries.ItemsSource.Cast<Country>().First(c => c.Name == Core.Language.ALL.ToString()).IsChecked = true;
doRefresh = true;
}
else if (selectedLanguages.Length == 0)
{
selectedLanguages = new[] { Core.Language.ALL };
Countries.ItemsSource.Cast<Country>().First(c => c.Name == Core.Language.ALL.ToString()).IsChecked = true;
doRefresh = true;
}
else if (selectedLanguages.Length > 1 && selectedLanguages.Any(s => s == Core.Language.ALL))
{
selectedLanguages = selectedLanguages.Where(s => s != Core.Language.ALL).ToArray();
Countries.ItemsSource.Cast<Country>().First(c => c.Name == Core.Language.ALL.ToString()).IsChecked = false;
doRefresh = true;
}

_settingService.SelectedLanguages = selectedLanguages;
(Application.Current.MainWindow as MainWindow).RefreshSettings();
if (doRefresh)
RefreshData();
}
}

Expand Down

0 comments on commit 1a9e7c0

Please sign in to comment.