Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve variable manager #438

Merged
merged 2 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion MacroDeck/ActionButton/ActionButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void UpdateBindingState()
{
if (!string.IsNullOrWhiteSpace(StateBindingVariable))
{
var variable = VariableManager.ListVariables.ToList().Find(v => v.Name.Equals(StateBindingVariable));
var variable = VariableManager.ListVariables.FirstOrDefault(v => v.Name == StateBindingVariable);
if (variable != null)
{
UpdateBindingState(variable);
Expand Down
2 changes: 1 addition & 1 deletion MacroDeck/ActionButton/ConditionAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private void UpdateConfiguration()
public override void Trigger(string clientId, ActionButton actionButton) {
var result = false;
var conditionValue2 = ConditionValue2;
var variable = VariableManager.ListVariables.ToList().Find(v => v.Name.Equals(_conditionValue1Source));
var variable = VariableManager.ListVariables.FirstOrDefault(v => v.Name == _conditionValue1Source);

foreach (var v in VariableManager.ListVariables)
{
Expand Down
4 changes: 2 additions & 2 deletions MacroDeck/GUI/CustomControls/ButtonEditor/ConditionItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ private void MethodBox_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
var variable = VariableManager.ListVariables.ToList().Find(v => v.Name.Equals(source.Text));
var variable = VariableManager.ListVariables.FirstOrDefault(v => v.Name == source.Text);
if (variable != null)
{
valueToCompare.Text = variable.Value;
Expand All @@ -356,7 +356,7 @@ private void Source_SelectedIndexChanged(object sender, EventArgs e)

((ConditionAction)Action).ConditionValue1Source = source.Text;

var variable = VariableManager.ListVariables.ToList().Find(v => v.Name.Equals(source.Text));
var variable = VariableManager.ListVariables.FirstOrDefault(v => v.Name == source.Text);

valueToCompare.Items.Clear();

Expand Down
3 changes: 2 additions & 1 deletion MacroDeck/GUI/Dialogs/VariableDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ private void BtnOk_Click(object sender, EventArgs e)
{
variableName.Text = "new_variable";
}
var variableCount = VariableManager.ListVariables.ToList().FindAll(v => v.Name.Equals(variableName.Text)).Count;

var variableCount = VariableManager.ListVariables.Count(v => v.Name == variableName.Text);
if (variableCount > 0)
{
variableName.Text = string.Format(variableName.Text + " _{0}", variableCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private void Variables_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
var variable = VariableManager.ListVariables.Where(v => v.Name == variables.Text).FirstOrDefault();
var variable = VariableManager.ListVariables.FirstOrDefault(v => v.Name == variables.Text);
if (variable != null)
{
switch (variable.Type)
Expand Down Expand Up @@ -119,7 +119,7 @@ public override bool OnActionSave()
private void LoadVariables()
{
variables.Items.Clear();
foreach (var variable in VariableManager.ListVariables.Where(v => v.Creator.Equals("User")))
foreach (var variable in VariableManager.ListVariables.Where(v => v.Creator == "User"))
{
variables.Items.Add(variable.Name);
}
Expand Down
34 changes: 18 additions & 16 deletions MacroDeck/Variables/VariableManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,37 @@ public static class VariableManager

public static List<Variable> GetVariables(MacroDeckPlugin macroDeckPlugin)
{
var pluginVariables = ListVariables
.Where(x => x.Creator.Equals(macroDeckPlugin.Name, StringComparison.OrdinalIgnoreCase))
return _database.Table<Variable>()
.Where(x => x.Creator.ToLower() == macroDeckPlugin.Name.ToLower())
.OrderBy(x => x.Name)
.ToList();

return pluginVariables;
}

public static Variable GetVariable(MacroDeckPlugin macroDeckPlugin, string variableName)
{
return ListVariables.FirstOrDefault(x =>
x.Creator == macroDeckPlugin.Name &&
string.Equals(x.Name, variableName, StringComparison.CurrentCultureIgnoreCase));
x.Name.ToLower() == variableName.ToLower());
}

internal static void InsertVariable(Variable variable)
{
if (ListVariables.Any(x => string.Equals(x.Name, variable.Name, StringComparison.CurrentCultureIgnoreCase)))
if (ListVariables.Any(x => x.Name.ToLower() == variable.Name.ToLower()))
{
return;
}
_database.Insert(variable);
}

internal static Variable SetValue(string name, object value, VariableType type, string creator = "User")
internal static Variable? SetValue(string name, object value, VariableType type, string creator = "User")
{
if (string.IsNullOrWhiteSpace(name)) return null;
name = ConvertNameString(name);

var variable =
ListVariables.FirstOrDefault(v => v.Name == name);
if (string.IsNullOrWhiteSpace(name))
{
return null;
}

var variable = _database.Table<Variable>().FirstOrDefault(v => v.Name == name);
if (variable == null)
{
variable = new Variable
Expand Down Expand Up @@ -119,7 +119,11 @@ internal static Variable SetValue(string name, object value, VariableType type,
try
{
_database.Update(variable);
} catch { }
}
catch (Exception ex)
{
MacroDeckLogger.Error($"Error while updating variable {variable.Name}:\n{ex.Message}");
}

return variable;
}
Expand Down Expand Up @@ -181,7 +185,7 @@ internal static void Initialize()
_database = new SQLiteConnection(ApplicationPaths.VariablesFilePath);

_database.CreateTable<Variable>();
_database.Execute("delete from Variable where 'Name'='';");
_database.Table<Variable>().Where(x => x.Name == "").Delete();
MacroDeckLogger.Info(typeof(VariableManager), ListVariables.Count() + " variables found");
}

Expand All @@ -202,9 +206,7 @@ public static string ConvertNameString(string str)

var regexUmlauts = new Regex("[äöüß]");
var evaluator = new MatchEvaluator(UmlautsReplacer);
str = regexUmlauts.Replace(str, evaluator);

return str;
return regexUmlauts.Replace(str, evaluator);
}


Expand Down