Skip to content

Commit

Permalink
Settings improved
Browse files Browse the repository at this point in the history
  • Loading branch information
rappen committed Jul 29, 2021
1 parent 7d1e274 commit 385d251
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 28 deletions.
1 change: 1 addition & 0 deletions XRMTokensRun/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace XRMTokensRun
/// </remarks>
public class Settings
{
public bool ExeAuto { get; set; }
public string Table { get; set; }
public List<KeyValuePair> Token { get; set; } = new List<KeyValuePair>();
}
Expand Down
71 changes: 45 additions & 26 deletions XRMTokensRun/XRMTR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,13 @@ public XRMTR()

private void XRMTR_Load(object sender, EventArgs e)
{
if (!SettingsManager.Instance.TryLoad(GetType(), out settings))
{
settings = new Settings();
}
LoadSetting();
Enable(true);
}

public override void ClosingPlugin(PluginCloseInfo info)
{
if (settings != null)
{
SettingsManager.Instance.Save(GetType(), settings);
}
SaveSettings();
}

/// <summary>
Expand All @@ -63,10 +57,7 @@ public override void ClosingPlugin(PluginCloseInfo info)
public override void UpdateConnection(IOrganizationService newService, ConnectionDetail detail, string actionName, object parameter)
{
base.UpdateConnection(newService, detail, actionName, parameter);
if (settings == null && !SettingsManager.Instance.TryLoad(GetType(), out settings))
{
settings = new Settings();
}
LoadSetting();
tableselect.DataSource = newService?.LoadEntities()?.EntityMetadata;
record.Service = newService;
if (settings.Table != null)
Expand All @@ -75,6 +66,34 @@ public override void UpdateConnection(IOrganizationService newService, Connectio
}
}

private void LoadSetting()
{
if (settings == null && !SettingsManager.Instance.TryLoad(GetType(), out settings))
{
settings = new Settings();
}
chkAuto.Checked = settings.ExeAuto;
}

private void SaveSettings()
{
if (settings == null)
{
settings = new Settings();
}
settings.ExeAuto = chkAuto.Checked;
settings.Table = tableselect.SelectedEntity.LogicalName;
if (settings.Token?.FirstOrDefault(t => t.key == tableselect.SelectedEntity.LogicalName) is KeyValuePair token)
{
token.value = txtTokensIn.Text;
}
else
{
settings.Token.Add(new KeyValuePair { key = tableselect.SelectedEntity.LogicalName, value = txtTokensIn.Text });
}
SettingsManager.Instance.Save(GetType(), settings);
}

private void tableselect_SelectedIndexChanged(object sender, EventArgs e)
{
var entity = tableselect.SelectedEntity;
Expand All @@ -98,6 +117,10 @@ private void btnGetRecurd_Click(object sender, EventArgs e)
record.Record = look.Record;
}
ShowColumns();
if (chkAuto.Checked)
{
Execute();
}
Enable(true);
}

Expand All @@ -109,7 +132,16 @@ private void Execute()
return;
}
SaveSettings();
txtTokensOut.Text = record.Record.Substitute(Service, txtTokensIn.Text);
try
{
lblError.Text = "";
txtTokensOut.Text = "";
txtTokensOut.Text = record.Record.Substitute(Service, txtTokensIn.Text);
}
catch (Exception ex)
{
lblError.Text = ex.Message;
}
}

private void Enable(bool on)
Expand All @@ -121,19 +153,6 @@ private void Enable(bool on)
btnSmart.Enabled = on && record?.Record != null && cmbTokenHelp.SelectedItem is TokenHelp;
}

private void SaveSettings()
{
settings.Table = tableselect.SelectedEntity.LogicalName;
if (settings.Token?.FirstOrDefault(t => t.key == tableselect.SelectedEntity.LogicalName) is KeyValuePair token)
{
token.value = txtTokensIn.Text;
}
else
{
settings.Token.Add(new KeyValuePair { key = tableselect.SelectedEntity.LogicalName, value = txtTokensIn.Text });
}
}

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start("https://jonasr.app/xrm-tokens/");
Expand Down
16 changes: 14 additions & 2 deletions XRMTokensRun/XRMTR.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 385d251

Please sign in to comment.