From b49477c2231641ba9eef0bcb516d6abbc642f449 Mon Sep 17 00:00:00 2001 From: Joakim Gustafsson Date: Wed, 26 Apr 2017 22:10:31 +0200 Subject: [PATCH] Moved sorting to presenter --- .../Controls/MainForm/MainFormPresenter.cs | 15 ++++++++++++++- .../Controls/MainForm/MainFormView.Designer.cs | 6 +++--- .../Controls/MainForm/MainFormView.cs | 18 +++++++++++------- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/SportidentLapCounter/Controls/MainForm/MainFormPresenter.cs b/src/SportidentLapCounter/Controls/MainForm/MainFormPresenter.cs index bd4b13b..a9164a2 100644 --- a/src/SportidentLapCounter/Controls/MainForm/MainFormPresenter.cs +++ b/src/SportidentLapCounter/Controls/MainForm/MainFormPresenter.cs @@ -1,4 +1,7 @@ -using SportidentLapCounter.Services; +using System.ComponentModel; +using System.Linq; +using SportidentLapCounter.DataTypes; +using SportidentLapCounter.Services; namespace SportidentLapCounter.Controls.MainForm { @@ -13,6 +16,16 @@ public MainFormPresenter() Model = PersistenceService.Load(); } + public void SortTeams() + { + Model.Teams = new BindingList( + Model.Teams + .OrderByDescending(x => x.Laps) + .ThenBy(x => x.LatestPunchTime) + .ThenBy(x => x.Number) + .ToList()); + } + public void PersistModel() { PersistenceService.Save(Model); diff --git a/src/SportidentLapCounter/Controls/MainForm/MainFormView.Designer.cs b/src/SportidentLapCounter/Controls/MainForm/MainFormView.Designer.cs index 549b7ef..1f6a806 100644 --- a/src/SportidentLapCounter/Controls/MainForm/MainFormView.Designer.cs +++ b/src/SportidentLapCounter/Controls/MainForm/MainFormView.Designer.cs @@ -95,9 +95,9 @@ private void InitializeComponent() this.dataGridView.Name = "dataGridView"; this.dataGridView.Size = new System.Drawing.Size(934, 464); this.dataGridView.TabIndex = 9; - this.dataGridView.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.SaveFile); - this.dataGridView.UserAddedRow += new System.Windows.Forms.DataGridViewRowEventHandler(this.SaveFile); - this.dataGridView.UserDeletedRow += new System.Windows.Forms.DataGridViewRowEventHandler(this.SaveFile); + this.dataGridView.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.CellValueChanged); + this.dataGridView.UserAddedRow += new System.Windows.Forms.DataGridViewRowEventHandler(this.RowAddedOrDeleted); + this.dataGridView.UserDeletedRow += new System.Windows.Forms.DataGridViewRowEventHandler(this.RowAddedOrDeleted); // // columnNumber // diff --git a/src/SportidentLapCounter/Controls/MainForm/MainFormView.cs b/src/SportidentLapCounter/Controls/MainForm/MainFormView.cs index 572f7db..a039b58 100644 --- a/src/SportidentLapCounter/Controls/MainForm/MainFormView.cs +++ b/src/SportidentLapCounter/Controls/MainForm/MainFormView.cs @@ -1,5 +1,4 @@ using System; -using System.ComponentModel; using System.Drawing; using System.IO; using System.Linq; @@ -26,7 +25,7 @@ public MainFormView() dataGridView.AutoGenerateColumns = false; - dataGridView.DataSource = Presenter.Model.Teams; + UpdateFromModel(); SetFontSize(Presenter.Model.FontSize); } @@ -79,16 +78,21 @@ private void ReaderOnOnlineStampRead(object sender, SportidentDataEventArgs e) x.LatestPunchTime = punchData.PunchDateTime; } - Presenter.Model.Teams = new BindingList(Presenter.Model.Teams.OrderByDescending(x => x.Laps).ThenBy(x => x.LatestPunchTime).ToList()); + Presenter.SortTeams(); - dataGridView.DataSource = null; - dataGridView.DataSource = Presenter.Model.Teams; + UpdateFromModel(); dataGridView.ClearSelection(); Presenter.PersistModel(); }); } + private void UpdateFromModel() + { + dataGridView.DataSource = null; + dataGridView.DataSource = Presenter.Model.Teams; + } + private void Connect() { var readerDeviceInfo = (ReaderDeviceInfo)(comboBox_sportidentDevices.SelectedItem); @@ -123,7 +127,7 @@ private void button_sportidentDisconnect_Click(object sender, EventArgs e) } - private void SaveFile(object sender, DataGridViewCellEventArgs e) + private void CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (Presenter.Model == null) return; @@ -131,7 +135,7 @@ private void SaveFile(object sender, DataGridViewCellEventArgs e) Presenter.PersistModel(); } - private void SaveFile(object sender, DataGridViewRowEventArgs e) + private void RowAddedOrDeleted(object sender, DataGridViewRowEventArgs e) { Presenter.PersistModel(); }