WorkLog: Save and Load.

This commit is contained in:
2020-06-07 19:18:42 +02:00
parent 523ec9b7f5
commit db7df7123f
2 changed files with 147 additions and 2 deletions

View File

@@ -29,6 +29,9 @@
private void InitializeComponent()
{
this.splitWindow = new System.Windows.Forms.SplitContainer();
this.txtName = new System.Windows.Forms.TextBox();
this.btnLoad = new System.Windows.Forms.Button();
this.btnSave = new System.Windows.Forms.Button();
this.btnNextDay = new System.Windows.Forms.Button();
this.btnPreviousDay = new System.Windows.Forms.Button();
this.dtToday = new System.Windows.Forms.DateTimePicker();
@@ -53,6 +56,9 @@
//
// splitWindow.Panel1
//
this.splitWindow.Panel1.Controls.Add(this.txtName);
this.splitWindow.Panel1.Controls.Add(this.btnLoad);
this.splitWindow.Panel1.Controls.Add(this.btnSave);
this.splitWindow.Panel1.Controls.Add(this.btnNextDay);
this.splitWindow.Panel1.Controls.Add(this.btnPreviousDay);
this.splitWindow.Panel1.Controls.Add(this.dtToday);
@@ -70,6 +76,34 @@
this.splitWindow.SplitterDistance = 442;
this.splitWindow.TabIndex = 0;
//
// txtName
//
this.txtName.ImeMode = System.Windows.Forms.ImeMode.Disable;
this.txtName.Location = new System.Drawing.Point(3, 7);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(104, 20);
this.txtName.TabIndex = 5;
//
// btnLoad
//
this.btnLoad.Location = new System.Drawing.Point(113, 7);
this.btnLoad.Name = "btnLoad";
this.btnLoad.Size = new System.Drawing.Size(53, 20);
this.btnLoad.TabIndex = 4;
this.btnLoad.Text = "Load";
this.btnLoad.UseVisualStyleBackColor = true;
this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click);
//
// btnSave
//
this.btnSave.Location = new System.Drawing.Point(172, 7);
this.btnSave.Name = "btnSave";
this.btnSave.Size = new System.Drawing.Size(57, 20);
this.btnSave.TabIndex = 3;
this.btnSave.Text = "Save";
this.btnSave.UseVisualStyleBackColor = true;
this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
//
// btnNextDay
//
this.btnNextDay.Location = new System.Drawing.Point(154, 33);
@@ -187,7 +221,9 @@
this.Controls.Add(this.splitWindow);
this.Name = "FrmWorkLog";
this.Text = "WorkLog";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FrmWorkLog_FormClosing);
this.splitWindow.Panel1.ResumeLayout(false);
this.splitWindow.Panel1.PerformLayout();
this.splitWindow.Panel2.ResumeLayout(false);
this.splitWindow.Panel2.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.splitWindow)).EndInit();
@@ -209,5 +245,8 @@
private System.Windows.Forms.DateTimePicker dtStart;
private System.Windows.Forms.Button btnNextDay;
private System.Windows.Forms.Button btnPreviousDay;
private System.Windows.Forms.Button btnSave;
private System.Windows.Forms.TextBox txtName;
private System.Windows.Forms.Button btnLoad;
}
}

View File

@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using VAR.Json;
namespace VAR.Toolbox.UI.Tools
{
@@ -21,15 +23,32 @@ namespace VAR.Toolbox.UI.Tools
public FrmWorkLog()
{
InitializeComponent();
WorkLog_LoadConfig();
WorkLog_LoadData();
}
private void FrmWorkLog_FormClosing(object sender, FormClosingEventArgs e)
{
WorkLog_SaveConfig();
}
#endregion Form life cycle
#region UI events
private bool _selecting = false;
private void btnLoad_Click(object sender, EventArgs e)
{
WorkLog_LoadData();
}
private void btnSave_Click(object sender, EventArgs e)
{
WorkLog_SaveData();
}
private void lsbWorkLog_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (_selecting) { return; }
@@ -79,6 +98,7 @@ namespace VAR.Toolbox.UI.Tools
WorkLog_Refresh();
WorkLog_SelectDate(item.DateStart);
WorkLogItem_Show(item);
WorkLog_MarkDirty();
}
private void btnDelete_Click(object sender, System.EventArgs e)
@@ -92,6 +112,7 @@ namespace VAR.Toolbox.UI.Tools
_workLog.Remove(_currentWorkLogItem);
WorkLog_Refresh();
WorkLogItem_Show(null);
WorkLog_MarkDirty();
}
private void dtStart_ValueChanged(object sender, EventArgs e)
@@ -133,13 +154,93 @@ namespace VAR.Toolbox.UI.Tools
#region Private methods
private const string ConfigFile = "WorkLog.Config.json";
private WorkLogConfig _config = null;
private void WorkLog_LoadConfig()
{
if (File.Exists(ConfigFile))
{
JsonParser jsonParser = new JsonParser();
jsonParser.KnownTypes.Add(typeof(WorkLogConfig));
string jsonConfig = File.ReadAllText(ConfigFile);
_config = jsonParser.Parse(jsonConfig) as WorkLogConfig;
}
if (_config == null)
{
_config = new WorkLogConfig();
}
txtName.Text = _config.LastName;
}
private void WorkLog_SaveConfig()
{
_config.LastName = txtName.Text;
JsonWriter jsonWriter = new JsonWriter(new JsonWriterConfiguration(indent: true));
using (StreamWriter streamWriter = new StreamWriter(ConfigFile))
{
jsonWriter.Write(_config, streamWriter);
}
}
private List<WorkLogItem> _workLog = null;
private void WorkLog_LoadData()
{
_workLog = null;
string fileName = string.Format("{0}.WorkLog.json", txtName.Text);
if (File.Exists(fileName))
{
string rawFile = File.ReadAllText(fileName);
JsonParser jsonParser = new JsonParser();
jsonParser.KnownTypes.Add(typeof(WorkLogItem));
object result = jsonParser.Parse(rawFile);
if (result is IEnumerable<object>)
{
_workLog = new List<WorkLogItem>();
foreach (object obj in (IEnumerable<object>)result)
{
WorkLogItem item = obj as WorkLogItem;
if (item == null) { continue; }
_workLog.Add(item);
}
}
}
if (_workLog == null)
{
_workLog = new List<WorkLogItem>();
}
WorkLog_Refresh();
WorkLog_SelectDate(DateTime.Now);
WorkLog_CleanDirty();
}
private void WorkLog_SaveData()
{
string fileName = string.Format("{0}.WorkLog.json", txtName.Text);
if (File.Exists(fileName))
{
File.Delete(fileName);
}
JsonWriter jsonWriter = new JsonWriter(new JsonWriterConfiguration(indent: true));
using (StreamWriter streamWriter = new StreamWriter(fileName))
{
jsonWriter.Write(_workLog, streamWriter);
}
WorkLog_CleanDirty();
}
private void WorkLog_MarkDirty()
{
btnSave.Enabled = true;
}
private void WorkLog_CleanDirty()
{
btnSave.Enabled = false;
}
private void WorkLog_SelectDate(DateTime date)
@@ -199,6 +300,7 @@ namespace VAR.Toolbox.UI.Tools
WorkLog_Refresh();
WorkLog_SelectDate(_currentWorkLogItem.DateStart);
}
WorkLog_MarkDirty();
}
public void lsbWorkLog_BindData(IEnumerable<WorkLogItem> items, int year, int month, int day, int q = 15)
@@ -239,7 +341,6 @@ namespace VAR.Toolbox.UI.Tools
}
#endregion Private methods
}
public class WorkLogRow
@@ -319,4 +420,9 @@ namespace VAR.Toolbox.UI.Tools
public string Activity { get; set; }
public string Description { get; set; }
}
public class WorkLogConfig
{
public string LastName { get; set; }
}
}