Skip to content

Commit

Permalink
Improve search function
Browse files Browse the repository at this point in the history
  • Loading branch information
lzcapp committed Sep 12, 2024
1 parent 2a4582c commit 5dd829f
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 57 deletions.
68 changes: 44 additions & 24 deletions KindleMate2/FrmMain.Designer.cs

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

88 changes: 70 additions & 18 deletions KindleMate2/FrmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ public FrmMain() {
menuClippingsCopy.Text = Strings.Copy;
menuClippingsDelete.Text = Strings.Delete;

cmbSearch.Items.Add(Strings.Select_All);
cmbSearch.Items.Add(Strings.Book_Title);
cmbSearch.Items.Add(Strings.Author);
cmbSearch.Items.Add(Strings.Vocabulary);
cmbSearch.Items.Add(Strings.Stem);
cmbSearch.Items.Add(Strings.Content);
cmbSearch.SelectedIndex = 0;

dataGridView.ColumnHeadersHeight = 23;
}

Expand Down Expand Up @@ -172,6 +180,8 @@ private void FrmMain_Load(object? sender, EventArgs e) {
RefreshData();

treeViewBooks.Focus();

cmbSearch_SelectedIndexChanged(this, e);
}

private DialogResult Dialog(string message, string title, MessageBoxButtons buttons, MessageBoxIcon icon) {
Expand Down Expand Up @@ -322,8 +332,8 @@ private void RefreshData() {
try {
_staticData.CommitTransaction();

if (dataGridView.CurrentRow is not null) {
_selectedIndex = dataGridView.CurrentRow.Index;
if (dataGridView.SelectedRows.Count > 0) {
_selectedIndex = dataGridView.SelectedRows[0].Index;
}

DisplayData();
Expand All @@ -335,11 +345,18 @@ private void RefreshData() {
}

private void DisplayData() {
lblBook.Text = string.Empty;
lblAuthor.Text = string.Empty;
lblLocation.Text = string.Empty;
lblContent.Text = string.Empty;


if (!string.IsNullOrWhiteSpace(_searchText)) {
_clippingsDataTable = _staticData.GetClipingsDataTableFuzzySearch(_searchText);
_originClippingsDataTable = _staticData.GetOriginClippingsDataTableFuzzySearch(_searchText);
_vocabDataTable = _staticData.GetVocabDataTableFuzzySearch(_searchText);
_lookupsDataTable = _staticData.GetLookupsDataTableFuzzySearch(_searchText);
var type = cmbSearch.SelectedItem?.ToString() ?? string.Empty;
_clippingsDataTable = _staticData.GetClipingsDataTableFuzzySearch(_searchText, type);
_originClippingsDataTable = _staticData.GetOriginClippingsDataTableFuzzySearch(_searchText, type);
_vocabDataTable = _staticData.GetVocabDataTableFuzzySearch(_searchText, type);
_lookupsDataTable = _staticData.GetLookupsDataTableFuzzySearch(_searchText, type);
} else {
_clippingsDataTable = _staticData.GetClipingsDataTable();
_originClippingsDataTable = _staticData.GetOriginClippingsDataTable();
Expand Down Expand Up @@ -616,13 +633,13 @@ private void CountRows() {
var clippingsCount = _clippingsDataTable.Rows.Count;
var originClippingsCount = _originClippingsDataTable.Rows.Count;
var diff = Math.Abs(originClippingsCount - clippingsCount);
lblCount.Text = Strings.Totally + Strings.Space + booksCount + Strings.Space + Strings.X_Books + Strings.Symbol_Comma + clippingsCount + Strings.Space + Strings.X_Clippings + Strings.Symbol_Comma + Strings.Deleted_X + Strings.Space + diff + Strings.Space + Strings.X_Rows;
lblCount.Text = Strings.Space + Strings.Totally + Strings.Space + booksCount + Strings.Space + Strings.X_Books + Strings.Symbol_Comma + clippingsCount + Strings.Space + Strings.X_Clippings + Strings.Symbol_Comma + Strings.Deleted_X + Strings.Space + diff + Strings.Space + Strings.X_Rows;

break;
case 1:
var vocabCount = _vocabDataTable.Rows.Count;
var lookupsCount = _lookupsDataTable.Rows.Count;
lblCount.Text = Strings.Totally + Strings.Space + vocabCount + Strings.Space + Strings.X_Vocabs + Strings.Symbol_Comma + Strings.Quried_X + Strings.Space + lookupsCount + Strings.Space + Strings.X_Times;
lblCount.Text = Strings.Space + Strings.Totally + Strings.Space + vocabCount + Strings.Space + Strings.X_Vocabs + Strings.Symbol_Comma + Strings.Quried_X + Strings.Space + lookupsCount + Strings.Space + Strings.X_Times;
break;
}
}
Expand Down Expand Up @@ -935,8 +952,8 @@ private void DataGridView_SelectionChanged(object sender, EventArgs e) {
try {
DataGridViewRow selectedRow;

if (dataGridView.CurrentRow is not null) {
selectedRow = dataGridView.CurrentRow;
if (dataGridView.SelectedRows.Count > 0) {
selectedRow = dataGridView.SelectedRows[0];
} else {
return;
}
Expand Down Expand Up @@ -1055,7 +1072,7 @@ private void TreeViewBooks_NodeMouseClick(object sender, TreeNodeMouseClickEvent
var selectedBookName = e.Node.Text;
_selectedBook = selectedBookName;
DataTable filteredBooks = _clippingsDataTable.AsEnumerable().Where(row => row.Field<string>("bookname") == selectedBookName).CopyToDataTable();
lblBookCount.Text = Strings.Total_Clippings + Strings.Space + filteredBooks.Rows.Count + Strings.Space + Strings.X_Clippings;
lblBookCount.Text = Strings.Space + Strings.Total_Clippings + Strings.Space + filteredBooks.Rows.Count + Strings.Space + Strings.X_Clippings;
lblBookCount.Image = Properties.Resources.open_book;
lblBookCount.Visible = true;
dataGridView.DataSource = filteredBooks;
Expand Down Expand Up @@ -1098,7 +1115,11 @@ private void ShowContentEditDialog() {
return;
}

var key = dataGridView.CurrentRow?.Cells["key"].Value.ToString() ?? string.Empty;
if (dataGridView.SelectedRows.Count <= 0) {
return;
}

var key = dataGridView.SelectedRows[0].Cells["key"].Value.ToString() ?? string.Empty;
if (string.IsNullOrWhiteSpace(key)) {
return;
}
Expand Down Expand Up @@ -1236,18 +1257,18 @@ private void ClippingMenuDelete_Click(object sender, EventArgs e) {
}

private void ClippingMenuCopy_Click(object sender, EventArgs e) {
if (dataGridView.CurrentRow is null) {
if (dataGridView.SelectedRows.Count <= 0) {
return;
}

var index = tabControl.SelectedIndex;
switch (index) {
case 0:
var content = dataGridView.CurrentRow.Cells["content"].Value.ToString() ?? string.Empty;
var content = dataGridView.SelectedRows[0].Cells["content"].Value.ToString() ?? string.Empty;
Clipboard.SetText(content != string.Empty ? content : lblContent.Text);
break;
case 1:
var usage = dataGridView.CurrentRow.Cells["usage"].Value.ToString() ?? string.Empty;
var usage = dataGridView.SelectedRows[0].Cells["usage"].Value.ToString() ?? string.Empty;
Clipboard.SetText(usage != string.Empty ? usage : lblBook.Text);
break;
}
Expand Down Expand Up @@ -1608,9 +1629,6 @@ private void SelectRow() {
} else {
lblAuthor.Text = "";
}

lblLocation.Text = clippinglocation;
lblContent.Text = content;
break;
case 1:
if (_selectedIndex < 0) {
Expand Down Expand Up @@ -2200,5 +2218,39 @@ private string getSearchText() {
}
return txtSearch.Text;
}

private void cmbSearch_SelectedIndexChanged(object sender, EventArgs e) {
var selected = cmbSearch.SelectedItem?.ToString() ?? string.Empty;
if (string.IsNullOrWhiteSpace(selected)) {
return;
}
List<string> list = [];
if (selected.Equals(Strings.Book_Title)) {
list.AddRange(_staticData.GetClippingsBookTitleList());
} else if (selected.Equals(Strings.Author)) {
list.AddRange(_staticData.GetClippingsAuthorList());
} else if (selected.Equals(Strings.Vocabulary)) {
list.AddRange(_staticData.GetVocabWordList());
} else if (selected.Equals(Strings.Stem)) {
list.AddRange(_staticData.GetVocabStemList());
} else {
list.AddRange(_staticData.GetClippingsBookTitleList());
list.AddRange(_staticData.GetClippingsAuthorList());
list.AddRange(_staticData.GetVocabWordList());
list.AddRange(_staticData.GetVocabStemList());
}
var autoCompleteStringCollection = new AutoCompleteStringCollection();
autoCompleteStringCollection.AddRange(list.ToArray());
txtSearch.AutoCompleteSource = AutoCompleteSource.CustomSource;
txtSearch.AutoCompleteCustomSource = autoCompleteStringCollection;
}

private void txtSearch_KeyPress(object sender, KeyPressEventArgs e) {
if (e.KeyChar != (char)Keys.Enter) {
return;
}
e.Handled = true;
picSearch_Click(this, e);
}
}
}
4 changes: 2 additions & 2 deletions KindleMate2/FrmMain.resx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
AAEAAAD/////AQAAAAAAAAAMAgAAAEZTeXN0ZW0uV2luZG93cy5Gb3JtcywgQ3VsdHVyZT1uZXV0cmFs
LCBQdWJsaWNLZXlUb2tlbj1iNzdhNWM1NjE5MzRlMDg5BQEAAAAmU3lzdGVtLldpbmRvd3MuR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