-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
70 additions
and
44 deletions.
There are no files selected for viewing
16 changes: 15 additions & 1 deletion
16
src/SportidentLapCounter/Controls/MainForm/MainFormPresenter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters