Skip to content

Commit f9facda

Browse files
committed
Improve code quality
1 parent 1906d68 commit f9facda

File tree

4 files changed

+38
-51
lines changed

4 files changed

+38
-51
lines changed

Plugins/Flow.Launcher.Plugin.Calculator/Main.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ public List<Result> Query(Query query)
8080
if (result == null || string.IsNullOrEmpty(result.ToString()))
8181
{
8282
if (!_settings.ShowErrorMessage) return EmptyResults;
83-
return new List<Result>
84-
{
83+
return
84+
[
8585
new Result
8686
{
8787
Title = Localize.flowlauncher_plugin_calculator_expression_not_complete(),
8888
IcoPath = IcoPath
8989
}
90-
};
90+
];
9191
}
9292

9393
if (result.ToString() == "NaN")
@@ -105,8 +105,8 @@ public List<Result> Query(Query query)
105105
decimal roundedResult = Math.Round(Convert.ToDecimal(result), _settings.MaxDecimalPlaces, MidpointRounding.AwayFromZero);
106106
string newResult = FormatResult(roundedResult);
107107

108-
return new List<Result>
109-
{
108+
return
109+
[
110110
new Result
111111
{
112112
Title = newResult,
@@ -128,21 +128,21 @@ public List<Result> Query(Query query)
128128
}
129129
}
130130
}
131-
};
131+
];
132132
}
133133
}
134134
catch (Exception)
135135
{
136136
// Mages engine can throw various exceptions, for simplicity we catch them all and show a generic message.
137137
if (!_settings.ShowErrorMessage) return EmptyResults;
138-
return new List<Result>
139-
{
138+
return
139+
[
140140
new Result
141141
{
142142
Title = Localize.flowlauncher_plugin_calculator_expression_not_complete(),
143143
IcoPath = IcoPath
144144
}
145-
};
145+
];
146146
}
147147

148148
return EmptyResults;
@@ -153,7 +153,7 @@ private static string PowMatchEvaluator(Match m)
153153
// m.Groups[1].Value will be `(...)` with parens
154154
var contentWithParen = m.Groups[1].Value;
155155
// remove outer parens. `(min(2,3), 4)` becomes `min(2,3), 4`
156-
var argsContent = contentWithParen.Substring(1, contentWithParen.Length - 2);
156+
var argsContent = contentWithParen[1..^1];
157157

158158
var bracketCount = 0;
159159
var splitIndex = -1;
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11

2-
namespace Flow.Launcher.Plugin.Calculator
2+
namespace Flow.Launcher.Plugin.Calculator;
3+
4+
public class Settings
35
{
4-
public class Settings
5-
{
6-
public DecimalSeparator DecimalSeparator { get; set; } = DecimalSeparator.UseSystemLocale;
6+
public DecimalSeparator DecimalSeparator { get; set; } = DecimalSeparator.UseSystemLocale;
77

8-
public int MaxDecimalPlaces { get; set; } = 10;
8+
public int MaxDecimalPlaces { get; set; } = 10;
99

10-
public bool ShowErrorMessage { get; set; } = false;
11-
}
10+
public bool ShowErrorMessage { get; set; } = false;
1211
}

Plugins/Flow.Launcher.Plugin.Calculator/ViewModels/SettingsViewModel.cs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,25 @@
11
using System.Collections.Generic;
22
using System.Linq;
33

4-
namespace Flow.Launcher.Plugin.Calculator.ViewModels
5-
{
6-
public class SettingsViewModel : BaseModel
7-
{
8-
public SettingsViewModel(Settings settings)
9-
{
10-
Settings = settings;
11-
}
4+
namespace Flow.Launcher.Plugin.Calculator.ViewModels;
125

13-
public Settings Settings { get; init; }
6+
public class SettingsViewModel(Settings settings) : BaseModel
7+
{
8+
public Settings Settings { get; init; } = settings;
149

15-
public static IEnumerable<int> MaxDecimalPlacesRange => Enumerable.Range(1, 20);
10+
public static IEnumerable<int> MaxDecimalPlacesRange => Enumerable.Range(1, 20);
1611

17-
public List<DecimalSeparatorLocalized> AllDecimalSeparator { get; } = DecimalSeparatorLocalized.GetValues();
12+
public List<DecimalSeparatorLocalized> AllDecimalSeparator { get; } = DecimalSeparatorLocalized.GetValues();
1813

19-
public DecimalSeparator SelectedDecimalSeparator
14+
public DecimalSeparator SelectedDecimalSeparator
15+
{
16+
get => Settings.DecimalSeparator;
17+
set
2018
{
21-
get => Settings.DecimalSeparator;
22-
set
19+
if (Settings.DecimalSeparator != value)
2320
{
24-
if (Settings.DecimalSeparator != value)
25-
{
26-
Settings.DecimalSeparator = value;
27-
OnPropertyChanged();
28-
}
21+
Settings.DecimalSeparator = value;
22+
OnPropertyChanged();
2923
}
3024
}
3125
}
Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
using System.Windows.Controls;
22
using Flow.Launcher.Plugin.Calculator.ViewModels;
33

4-
namespace Flow.Launcher.Plugin.Calculator.Views
4+
namespace Flow.Launcher.Plugin.Calculator.Views;
5+
6+
public partial class CalculatorSettings : UserControl
57
{
6-
/// <summary>
7-
/// Interaction logic for CalculatorSettings.xaml
8-
/// </summary>
9-
public partial class CalculatorSettings : UserControl
10-
{
11-
private readonly SettingsViewModel _viewModel;
12-
private readonly Settings _settings;
8+
private readonly SettingsViewModel _viewModel;
139

14-
public CalculatorSettings(Settings settings)
15-
{
16-
_viewModel = new SettingsViewModel(settings);
17-
_settings = _viewModel.Settings;
18-
DataContext = _viewModel;
19-
InitializeComponent();
20-
}
10+
public CalculatorSettings(Settings settings)
11+
{
12+
_viewModel = new SettingsViewModel(settings);
13+
DataContext = _viewModel;
14+
InitializeComponent();
2115
}
2216
}

0 commit comments

Comments
 (0)