Skip to content

Commit

Permalink
XML handling to PersistenceService
Browse files Browse the repository at this point in the history
  • Loading branch information
Jocke-G committed Apr 26, 2017
1 parent 23d9355 commit 56751ef
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 44 deletions.
16 changes: 15 additions & 1 deletion src/SportidentLapCounter/Controls/MainForm/MainFormPresenter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
namespace SportidentLapCounter.Controls.MainForm
using SportidentLapCounter.Services;

namespace SportidentLapCounter.Controls.MainForm
{
public class MainFormPresenter
{
public MainFormModel Model;
private PersistenceService _persistenceService;
private PersistenceService PersistenceService => _persistenceService ?? (_persistenceService = new PersistenceService());

public MainFormPresenter()
{
Model = PersistenceService.Load();
}

public void PersistModel()
{
PersistenceService.Save(Model);
}
}
}
48 changes: 5 additions & 43 deletions src/SportidentLapCounter/Controls/MainForm/MainFormView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Serialization;
using SportidentLapCounter.DataTypes;
using SPORTident;
using SPORTident.Common;
Expand All @@ -17,7 +16,6 @@ public partial class MainFormView : Form

private MainFormPresenter Presenter => _presenter ?? (_presenter = new MainFormPresenter());

private const string XmlPath = "database.xml";

private Reader _reader;

Expand All @@ -28,7 +26,6 @@ public MainFormView()

dataGridView.AutoGenerateColumns = false;

Presenter.Model = LoadXmlFile();
dataGridView.DataSource = Presenter.Model.Teams;
SetFontSize(Presenter.Model.FontSize);
}
Expand All @@ -46,31 +43,6 @@ private void InitializeSportidentReader()
GetSiDevices();
}

private MainFormModel LoadXmlFile()
{
if (!File.Exists(XmlPath))
return new MainFormModel();

try
{
using (var file = new StreamReader(XmlPath))
{
var serializer = new XmlSerializer(typeof(MainFormModel));
var obj = serializer.Deserialize(file);
var model = obj as MainFormModel;
if (model == null)
return new MainFormModel();

file.Close();
return model;
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
return new MainFormModel();
}
}

private void GetSiDevices()
{
Expand Down Expand Up @@ -113,7 +85,7 @@ private void ReaderOnOnlineStampRead(object sender, SportidentDataEventArgs e)
dataGridView.DataSource = Presenter.Model.Teams;
dataGridView.ClearSelection();

SaveFile();
Presenter.PersistModel();
});
}

Expand Down Expand Up @@ -150,27 +122,18 @@ private void button_sportidentDisconnect_Click(object sender, EventArgs e)
button_sportidentDisconnect.Enabled = false;
}

private void SaveFile()
{
using (var streamWriter = new StreamWriter(XmlPath))
{
var xmlSerializer = new XmlSerializer(typeof(MainFormModel));
xmlSerializer.Serialize(streamWriter, Presenter.Model);
streamWriter.Close();
}
}

private void SaveFile(object sender, DataGridViewCellEventArgs e)
{
if (Presenter.Model == null)
return;

SaveFile();
Presenter.PersistModel();
}

private void SaveFile(object sender, DataGridViewRowEventArgs e)
{
SaveFile();
Presenter.PersistModel();
}

private void buttonBigger_Click(object sender, EventArgs e)
Expand All @@ -182,7 +145,7 @@ private void Bigger()
{
var fontSize = dataGridView.DefaultCellStyle.Font.Size + 1;
SetFontSize(fontSize);
SaveFile();
Presenter.PersistModel();
}

private void buttonSmaller_Click(object sender, EventArgs e)
Expand All @@ -194,7 +157,7 @@ private void Smaller()
{
var fontSize = dataGridView.DefaultCellStyle.Font.Size - 1;
SetFontSize(fontSize);
SaveFile();
Presenter.PersistModel();
}

private void SetFontSize(float fontSize)
Expand Down Expand Up @@ -265,6 +228,5 @@ private void ToggleFullscreen()
dataGridView.ClearSelection();
}
}

}
}
49 changes: 49 additions & 0 deletions src/SportidentLapCounter/Services/PersistenceService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;
using SportidentLapCounter.Controls.MainForm;

namespace SportidentLapCounter.Services
{
public class PersistenceService
{
private const string XmlPath = "database.xml";

public MainFormModel Load()
{
if (!File.Exists(XmlPath))
return new MainFormModel();

try
{
using (var file = new StreamReader(XmlPath))
{
var serializer = new XmlSerializer(typeof(MainFormModel));
var obj = serializer.Deserialize(file);
var model = obj as MainFormModel;
if (model == null)
return new MainFormModel();

file.Close();
return model;
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
return new MainFormModel();
}
}

public void Save(MainFormModel model)
{
using (var streamWriter = new StreamWriter(XmlPath))
{
var xmlSerializer = new XmlSerializer(typeof(MainFormModel));
xmlSerializer.Serialize(streamWriter, model);
streamWriter.Close();
}
}
}
}
1 change: 1 addition & 0 deletions src/SportidentLapCounter/SportidentLapCounter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="DataTypes\Team.cs" />
<Compile Include="Services\PersistenceService.cs" />
<EmbeddedResource Include="Controls\MainForm\MainFormView.resx">
<DependentUpon>MainFormView.cs</DependentUpon>
</EmbeddedResource>
Expand Down

0 comments on commit 56751ef

Please sign in to comment.