Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
1. Fix FrmStatistic's exception when database is empty
2. Change FrmStatistic's TabControl to FlatTabControl
3. Prompt for confirmation to rebuild database
  • Loading branch information
lzcapp committed Oct 10, 2024
1 parent 6fddda4 commit 67b719c
Show file tree
Hide file tree
Showing 10 changed files with 294 additions and 251 deletions.
1 change: 1 addition & 0 deletions KindleMate2/FrmMain.Designer.cs

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

51 changes: 31 additions & 20 deletions KindleMate2/FrmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,11 @@ public FrmMain() {
StaticData.CreateDatabase();
}
} catch (Exception e) {
Messenger.MessageBox(e.Message, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox(e.Message, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
Close();
}
_staticData = new StaticData();

tabControl.ShowTabCloseButton = false;

SetTheme();

treeViewBooks.ContextMenuStrip = menu;
Expand Down Expand Up @@ -1200,21 +1198,22 @@ private void ShowContentEditDialog() {
return;
}

if (Messenger.InputBox(Strings.Edit_Clippings + Strings.Space + bookName, "", ref fields, MsgIcon.Edit, MessageBoxButtons.OKCancel, _isDarkTheme) == DialogResult.OK) {
var dialogContent = fields[0].Value.Trim();
if (string.IsNullOrWhiteSpace(dialogContent)) {
return;
}
if (dialogContent.Equals(content)) {
return;
}
if (!_staticData.UpdateClippings(key, dialogContent, string.Empty)) {
MessageBox(Strings.Clippings_Revised_Failed, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
MessageBox(Strings.Clippings_Revised, Strings.Successful, MessageBoxButtons.OK, MessageBoxIcon.Information);
RefreshData();
if (Messenger.InputBox(Strings.Edit_Clippings + Strings.Space + bookName, "", ref fields, MsgIcon.Edit, MessageBoxButtons.OKCancel, _isDarkTheme) != DialogResult.OK) {
return;
}
var dialogContent = fields[0].Value.Trim();
if (string.IsNullOrWhiteSpace(dialogContent)) {
return;
}
if (dialogContent.Equals(content)) {
return;
}
if (!_staticData.UpdateClippings(key, dialogContent, string.Empty)) {
MessageBox(Strings.Clippings_Revised_Failed, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
MessageBox(Strings.Clippings_Revised, Strings.Successful, MessageBoxButtons.OK, MessageBoxIcon.Information);
RefreshData();
}

private void DataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e) {
Expand Down Expand Up @@ -1807,7 +1806,7 @@ private void MenuClear_Click(object sender, EventArgs e) {
return;
}

DialogResult result = MessageBox(Strings.Confirm_Clear_All_Data, Strings.Confirm, MessageBoxButtons.YesNo, MsgIcon.Warning);
DialogResult result = MessageBox(Strings.Confirm_Clear_All_Data, Strings.Confirm, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result != DialogResult.Yes) {
return;
}
Expand Down Expand Up @@ -1961,6 +1960,10 @@ private void MenuRestart_Click(object sender, EventArgs e) {
}

private void MenuRebuild_Click(object sender, EventArgs e) {
DialogResult result = MessageBox(Strings.Confirm_Rebuild_Database, Strings.Confirm, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result != DialogResult.Yes) {
return;
}
SetProgressBar(true);
var bw = new BackgroundWorker();
bw.DoWork += (_, doWorkEventArgs) => {
Expand All @@ -1970,7 +1973,7 @@ private void MenuRebuild_Click(object sender, EventArgs e) {
if (runWorkerCompletedEventArgs.Result != null && !string.IsNullOrWhiteSpace(runWorkerCompletedEventArgs.Result.ToString())) {
MessageBox(runWorkerCompletedEventArgs.Result.ToString() ?? string.Empty, Strings.Rebuild_Database, MessageBoxButtons.OK, MessageBoxIcon.Information);
} else {
MessageBox(Strings.Failed, Strings.Rebuild_Database, MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox(Strings.Rebuild_Database + Strings.Failed, Strings.Rebuild_Database, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
SetProgressBar(false);
RefreshData();
Expand All @@ -1980,8 +1983,12 @@ private void MenuRebuild_Click(object sender, EventArgs e) {
}

private string RebuildDatabase() {
_staticData.EmptyTables("clippings");
DataTable origin = _staticData.GetOriginClippingsDataTable();
if (origin.Rows.Count <= 0) {
return Strings.No_Data_To_Clear;
}

_staticData.EmptyTables("clippings");
var insertedCount = 0;
foreach (DataRow row in origin.Rows) {
var entityClipping = new Clipping();
Expand Down Expand Up @@ -2351,6 +2358,10 @@ private void MenuExportMd_Click(object sender, EventArgs e) {
}

private void MenuStatistic_Click(object sender, EventArgs e) {
if (_clippingsDataTable.Rows.Count <= 0) {
MessageBox(Strings.No_Data_To_Clear, Strings.Prompt, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
using var dialog = new FrmStatistics();
dialog.ShowDialog();
}
Expand Down
9 changes: 6 additions & 3 deletions KindleMate2/FrmStatistics.Designer.cs

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

166 changes: 85 additions & 81 deletions KindleMate2/FrmStatistics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,99 +32,103 @@ public FrmStatistics() {
}

private void FrmStatistic_Load(object sender, EventArgs e) {
Text = Strings.Statistics;
tabPageBooks.Text = Strings.Clippings;
tabPageVocabs.Text = Strings.Vocabulary_List;

tabPageBooks.Text = Strings.Clippings;
tabPageVocabs.Text = Strings.Vocabulary_List;

_clippingsDataTable = _staticData.GetClipingsDataTable();
_staticData.GetOriginClippingsDataTable();
_vocabDataTable = _staticData.GetVocabDataTable();
_staticData.GetLookupsDataTable();

var enumBooks = _clippingsDataTable.AsEnumerable()
.GroupBy(row => {
DateTime date = DateTime.Parse(row.Field<string>("clippingdate") ?? string.Empty);
return new {
date.Year, date.Month, date.Day
};
})
.Select(group => new {
group.Key.Year,
group.Key.Month,
group.Key.Day,
Count = group.Count()
try {
Text = Strings.Statistics;
tabPageBooks.Text = Strings.Clippings;
tabPageVocabs.Text = Strings.Vocabulary_List;

tabPageBooks.Text = Strings.Clippings;
tabPageVocabs.Text = Strings.Vocabulary_List;

_clippingsDataTable = _staticData.GetClipingsDataTable();
_staticData.GetOriginClippingsDataTable();
_vocabDataTable = _staticData.GetVocabDataTable();
_staticData.GetLookupsDataTable();

var enumBooks = _clippingsDataTable.AsEnumerable()
.GroupBy(row => {
DateTime date = DateTime.Parse(row.Field<string>("clippingdate") ?? string.Empty);
return new {
date.Year, date.Month, date.Day
};
})
.Select(group => new {
group.Key.Year,
group.Key.Month,
group.Key.Day,
Count = group.Count()
});
var listBooks = enumBooks
.OrderBy(x => x.Year)
.ThenBy(x => x.Month)
.ThenBy(x => x.Day)
.ToList();
foreach (var dataPoint in listBooks) {
chartBooksHistory.Series[0].Points.AddXY(dataPoint.Year + "." + dataPoint.Month + "." + dataPoint.Day, dataPoint.Count);
}

var enumBooksTime = _clippingsDataTable.AsEnumerable().GroupBy(row => DateTime.Parse(row.Field<string>("clippingdate") ?? string.Empty).TimeOfDay.Hours).Select(g => new {
ClippingHour = g.Key, ClippingCount = g.Count()
});
var listBooks = enumBooks
.OrderBy(x => x.Year)
.ThenBy(x => x.Month)
.ThenBy(x => x.Day)
.ToList();
foreach (var dataPoint in listBooks) {
chartBooksHistory.Series[0].Points.AddXY(dataPoint.Year + "." + dataPoint.Month + "." + dataPoint.Day, dataPoint.Count);
}

var enumBooksTime = _clippingsDataTable.AsEnumerable().GroupBy(row => DateTime.Parse(row.Field<string>("clippingdate") ?? string.Empty).TimeOfDay.Hours).Select(g => new {
ClippingHour = g.Key, ClippingCount = g.Count()
});
var listBooksTime = enumBooksTime.ToList();
foreach (var dataPoint in listBooksTime) {
chartBooksTime.Series[0].Points.AddXY(dataPoint.ClippingHour, dataPoint.ClippingCount);
}

var listBooksTime = enumBooksTime.ToList();
foreach (var dataPoint in listBooksTime) {
chartBooksTime.Series[0].Points.AddXY(dataPoint.ClippingHour, dataPoint.ClippingCount);
}
var enumBooksWeek = _clippingsDataTable.AsEnumerable().GroupBy(row => (int)DateTime.Parse(row.Field<string>("clippingdate") ?? string.Empty).DayOfWeek).Select(g => new {
Weekday = g.Key, ClippingCount = g.Count()
});

var enumBooksWeek = _clippingsDataTable.AsEnumerable().GroupBy(row => (int)DateTime.Parse(row.Field<string>("clippingdate") ?? string.Empty).DayOfWeek).Select(g => new {
Weekday = g.Key, ClippingCount = g.Count()
});
var listBooksWeek = enumBooksWeek.ToList().OrderBy(x => x.Weekday);
foreach (var dataPoint in listBooksWeek) {
chartBooksWeek.Series[0].Points.AddXY(DateTimeFormatInfo.CurrentInfo.AbbreviatedDayNames[dataPoint.Weekday], dataPoint.ClippingCount);
}

var listBooksWeek = enumBooksWeek.ToList().OrderBy(x => x.Weekday);
foreach (var dataPoint in listBooksWeek) {
chartBooksWeek.Series[0].Points.AddXY(DateTimeFormatInfo.CurrentInfo.AbbreviatedDayNames[dataPoint.Weekday], dataPoint.ClippingCount);
}
var enumVocabs = _vocabDataTable.AsEnumerable()
.GroupBy(row => {
DateTime date = DateTime.Parse(row.Field<string>("timestamp") ?? string.Empty);
return new {
date.Year, date.Month, date.Day
};
})
.Select(group => new {
group.Key.Year,
group.Key.Month,
group.Key.Day,
Count = group.Count()
});
var listVocabs = enumVocabs
.OrderBy(x => x.Year)
.ThenBy(x => x.Month)
.ThenBy(x => x.Day)
.ToList();
foreach (var dataPoint in listVocabs) {
chartVocabsHistory.Series[0].Points.AddXY(dataPoint.Year + "." + dataPoint.Month + "." + dataPoint.Day, dataPoint.Count);
}

var enumVocabs = _vocabDataTable.AsEnumerable()
.GroupBy(row => {
DateTime date = DateTime.Parse(row.Field<string>("timestamp") ?? string.Empty);
return new {
date.Year, date.Month, date.Day
};
})
.Select(group => new {
group.Key.Year,
group.Key.Month,
group.Key.Day,
Count = group.Count()
var enumVocabsTime = _vocabDataTable.AsEnumerable().GroupBy(row => DateTime.Parse(row.Field<string>("timestamp") ?? string.Empty).TimeOfDay.Hours).Select(g => new {
ClippingHour = g.Key, ClippingCount = g.Count()
});
var listVocabs = enumVocabs
.OrderBy(x => x.Year)
.ThenBy(x => x.Month)
.ThenBy(x => x.Day)
.ToList();
foreach (var dataPoint in listVocabs) {
chartVocabsHistory.Series[0].Points.AddXY(dataPoint.Year + "." + dataPoint.Month + "." + dataPoint.Day, dataPoint.Count);
}

var enumVocabsTime = _vocabDataTable.AsEnumerable().GroupBy(row => DateTime.Parse(row.Field<string>("timestamp") ?? string.Empty).TimeOfDay.Hours).Select(g => new {
ClippingHour = g.Key, ClippingCount = g.Count()
});
var listVocabsTime = enumVocabsTime.ToList();
foreach (var dataPoint in listVocabsTime) {
chartVocabsTime.Series[0].Points.AddXY(dataPoint.ClippingHour, dataPoint.ClippingCount);
}

var listVocabsTime = enumVocabsTime.ToList();
foreach (var dataPoint in listVocabsTime) {
chartVocabsTime.Series[0].Points.AddXY(dataPoint.ClippingHour, dataPoint.ClippingCount);
}
var enumVocabsWeek = _vocabDataTable.AsEnumerable().GroupBy(row => (int)DateTime.Parse(row.Field<string>("timestamp") ?? string.Empty).DayOfWeek).Select(g => new {
Weekday = g.Key, ClippingCount = g.Count()
});

var enumVocabsWeek = _vocabDataTable.AsEnumerable().GroupBy(row => (int)DateTime.Parse(row.Field<string>("timestamp") ?? string.Empty).DayOfWeek).Select(g => new {
Weekday = g.Key, ClippingCount = g.Count()
});
var listVocabsWeek = enumVocabsWeek.ToList().OrderBy(x => x.Weekday);
foreach (var dataPoint in listVocabsWeek) {
chartVocabsWeek.Series[0].Points.AddXY(DateTimeFormatInfo.CurrentInfo.AbbreviatedDayNames[dataPoint.Weekday], dataPoint.ClippingCount);
}

var listVocabsWeek = enumVocabsWeek.ToList().OrderBy(x => x.Weekday);
foreach (var dataPoint in listVocabsWeek) {
chartVocabsWeek.Series[0].Points.AddXY(DateTimeFormatInfo.CurrentInfo.AbbreviatedDayNames[dataPoint.Weekday], dataPoint.ClippingCount);
SetLblStatistics();
} catch (Exception exception) {
Messenger.MessageBox(exception.Message, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
}

SetLblStatistics();
}

private void BtnSave_Click(object sender, EventArgs e) {
Expand Down
Loading

0 comments on commit 67b719c

Please sign in to comment.