From 5dd829fce969a8e386a7532b8b03b9cdde758bc7 Mon Sep 17 00:00:00 2001 From: Laurence Luo Date: Thu, 12 Sep 2024 23:22:00 +0800 Subject: [PATCH] Improve search function --- KindleMate2/FrmMain.Designer.cs | 68 +++++++++++------ KindleMate2/FrmMain.cs | 88 +++++++++++++++++----- KindleMate2/FrmMain.resx | 4 +- KindleMate2/StaticData.cs | 125 ++++++++++++++++++++++++++++---- 4 files changed, 228 insertions(+), 57 deletions(-) diff --git a/KindleMate2/FrmMain.Designer.cs b/KindleMate2/FrmMain.Designer.cs index 3aa8bad..4d48d4d 100644 --- a/KindleMate2/FrmMain.Designer.cs +++ b/KindleMate2/FrmMain.Designer.cs @@ -27,7 +27,7 @@ protected override void Dispose(bool disposing) { private void InitializeComponent() { components = new System.ComponentModel.Container(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmMain)); - DataGridViewCellStyle dataGridViewCellStyle2 = new DataGridViewCellStyle(); + DataGridViewCellStyle dataGridViewCellStyle1 = new DataGridViewCellStyle(); menuStrip = new MenuStrip(); menuFile = new ToolStripMenuItem(); menuRefresh = new ToolStripMenuItem(); @@ -73,6 +73,7 @@ private void InitializeComponent() { imageListWords = new ImageList(components); panel = new Panel(); txtSearch = new TextBox(); + cmbSearch = new ComboBox(); picSearch = new PictureBox(); splitContainerDetail = new SplitContainer(); dataGridView = new DataGridView(); @@ -374,7 +375,7 @@ private void InitializeComponent() { tableLeft.Margin = new Padding(0); tableLeft.Name = "tableLeft"; tableLeft.RowCount = 2; - tableLeft.RowStyles.Add(new RowStyle(SizeType.Absolute, 25F)); + tableLeft.RowStyles.Add(new RowStyle(SizeType.Absolute, 30F)); tableLeft.RowStyles.Add(new RowStyle(SizeType.Percent, 100F)); tableLeft.Size = new Size(286, 611); tableLeft.TabIndex = 1; @@ -385,11 +386,11 @@ private void InitializeComponent() { tabControl.Controls.Add(tabPageBooks); tabControl.Controls.Add(tabPageWords); tabControl.Dock = DockStyle.Fill; - tabControl.Location = new Point(0, 25); + tabControl.Location = new Point(0, 30); tabControl.Margin = new Padding(0); tabControl.Name = "tabControl"; tabControl.SelectedIndex = 0; - tabControl.Size = new Size(286, 586); + tabControl.Size = new Size(286, 581); tabControl.SizeMode = TabSizeMode.FillToRight; tabControl.TabIndex = 0; tabControl.SelectedIndexChanged += TabControl_SelectedIndexChanged; @@ -401,7 +402,7 @@ private void InitializeComponent() { tabPageBooks.Location = new Point(4, 4); tabPageBooks.Margin = new Padding(0); tabPageBooks.Name = "tabPageBooks"; - tabPageBooks.Size = new Size(278, 545); + tabPageBooks.Size = new Size(278, 540); tabPageBooks.TabIndex = 0; tabPageBooks.Text = Strings.Clippings; // @@ -422,7 +423,7 @@ private void InitializeComponent() { treeViewBooks.ShowNodeToolTips = true; treeViewBooks.ShowPlusMinus = false; treeViewBooks.ShowRootLines = false; - treeViewBooks.Size = new Size(278, 545); + treeViewBooks.Size = new Size(278, 540); treeViewBooks.StateImageList = imageListBooks; treeViewBooks.TabIndex = 0; treeViewBooks.NodeMouseClick += TreeViewBooks_NodeMouseClick; @@ -479,7 +480,7 @@ private void InitializeComponent() { tabPageWords.Location = new Point(4, 4); tabPageWords.Margin = new Padding(0); tabPageWords.Name = "tabPageWords"; - tabPageWords.Size = new Size(278, 545); + tabPageWords.Size = new Size(278, 540); tabPageWords.TabIndex = 1; tabPageWords.Text = Strings.Vocabulary_List; // @@ -498,7 +499,7 @@ private void InitializeComponent() { treeViewWords.ShowLines = false; treeViewWords.ShowPlusMinus = false; treeViewWords.ShowRootLines = false; - treeViewWords.Size = new Size(278, 545); + treeViewWords.Size = new Size(278, 540); treeViewWords.TabIndex = 0; treeViewWords.NodeMouseClick += TreeViewWords_NodeMouseClick; treeViewWords.MouseDown += TreeViewWords_MouseDown; @@ -550,24 +551,41 @@ private void InitializeComponent() { // panel // panel.Controls.Add(txtSearch); + panel.Controls.Add(cmbSearch); panel.Controls.Add(picSearch); panel.Dock = DockStyle.Fill; panel.Location = new Point(0, 0); panel.Margin = new Padding(0); panel.Name = "panel"; - panel.Padding = new Padding(0, 5, 0, 0); - panel.Size = new Size(286, 25); + panel.Padding = new Padding(5, 5, 0, 0); + panel.Size = new Size(286, 30); panel.TabIndex = 1; // // txtSearch // + txtSearch.AutoCompleteMode = AutoCompleteMode.Suggest; txtSearch.Dock = DockStyle.Fill; - txtSearch.Location = new Point(0, 5); - txtSearch.Margin = new Padding(0); + txtSearch.Location = new Point(65, 5); + txtSearch.Margin = new Padding(5, 0, 0, 0); txtSearch.Multiline = true; txtSearch.Name = "txtSearch"; - txtSearch.Size = new Size(251, 20); - txtSearch.TabIndex = 1; + txtSearch.Size = new Size(186, 25); + txtSearch.TabIndex = 2; + txtSearch.WordWrap = false; + txtSearch.KeyPress += txtSearch_KeyPress; + // + // cmbSearch + // + cmbSearch.AutoCompleteSource = AutoCompleteSource.CustomSource; + cmbSearch.Dock = DockStyle.Left; + cmbSearch.DropDownStyle = ComboBoxStyle.DropDownList; + cmbSearch.FormattingEnabled = true; + cmbSearch.Location = new Point(5, 5); + cmbSearch.Margin = new Padding(0); + cmbSearch.Name = "cmbSearch"; + cmbSearch.Size = new Size(60, 36); + cmbSearch.TabIndex = 1; + cmbSearch.SelectedIndexChanged += cmbSearch_SelectedIndexChanged; // // picSearch // @@ -578,7 +596,7 @@ private void InitializeComponent() { picSearch.Margin = new Padding(0); picSearch.Name = "picSearch"; picSearch.Padding = new Padding(10, 0, 0, 0); - picSearch.Size = new Size(35, 20); + picSearch.Size = new Size(35, 25); picSearch.SizeMode = PictureBoxSizeMode.Zoom; picSearch.TabIndex = 0; picSearch.TabStop = false; @@ -614,14 +632,14 @@ private void InitializeComponent() { dataGridView.BorderStyle = BorderStyle.None; dataGridView.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText; dataGridView.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single; - dataGridViewCellStyle2.Alignment = DataGridViewContentAlignment.MiddleCenter; - dataGridViewCellStyle2.BackColor = SystemColors.Window; - dataGridViewCellStyle2.Font = new Font("微软雅黑", 9F, FontStyle.Regular, GraphicsUnit.Point, 134); - dataGridViewCellStyle2.ForeColor = SystemColors.WindowText; - dataGridViewCellStyle2.SelectionBackColor = SystemColors.Highlight; - dataGridViewCellStyle2.SelectionForeColor = SystemColors.HighlightText; - dataGridViewCellStyle2.WrapMode = DataGridViewTriState.False; - dataGridView.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle2; + dataGridViewCellStyle1.Alignment = DataGridViewContentAlignment.MiddleCenter; + dataGridViewCellStyle1.BackColor = SystemColors.Window; + dataGridViewCellStyle1.Font = new Font("微软雅黑", 9F, FontStyle.Regular, GraphicsUnit.Point, 134); + dataGridViewCellStyle1.ForeColor = SystemColors.WindowText; + dataGridViewCellStyle1.SelectionBackColor = SystemColors.Highlight; + dataGridViewCellStyle1.SelectionForeColor = SystemColors.HighlightText; + dataGridViewCellStyle1.WrapMode = DataGridViewTriState.False; + dataGridView.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1; dataGridView.ColumnHeadersHeight = 46; dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing; dataGridView.Dock = DockStyle.Fill; @@ -642,6 +660,7 @@ private void InitializeComponent() { dataGridView.ShowRowErrors = false; dataGridView.Size = new Size(806, 308); dataGridView.TabIndex = 0; + dataGridView.DataSourceChanged += DataGridView_SelectionChanged; dataGridView.CellDoubleClick += DataGridView_CellDoubleClick; dataGridView.CellMouseDown += DataGridView_CellMouseDown; dataGridView.SelectionChanged += DataGridView_SelectionChanged; @@ -944,7 +963,8 @@ private void InitializeComponent() { private TableLayoutPanel tableContent; private TabControl tabControl; private Panel panel; - private TextBox txtSearch; private PictureBox picSearch; + private ComboBox cmbSearch; + private TextBox txtSearch; } } diff --git a/KindleMate2/FrmMain.cs b/KindleMate2/FrmMain.cs index 85be01c..aad7597 100644 --- a/KindleMate2/FrmMain.cs +++ b/KindleMate2/FrmMain.cs @@ -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; } @@ -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) { @@ -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(); @@ -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(); @@ -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; } } @@ -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; } @@ -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("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; @@ -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; } @@ -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; } @@ -1608,9 +1629,6 @@ private void SelectRow() { } else { lblAuthor.Text = ""; } - - lblLocation.Text = clippinglocation; - lblContent.Text = content; break; case 1: if (_selectedIndex < 0) { @@ -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 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); + } } } \ No newline at end of file diff --git a/KindleMate2/FrmMain.resx b/KindleMate2/FrmMain.resx index b1adb18..5b19f3d 100644 --- a/KindleMate2/FrmMain.resx +++ b/KindleMate2/FrmMain.resx @@ -135,7 +135,7 @@ AAEAAAD/////AQAAAAAAAAAMAgAAAEZTeXN0ZW0uV2luZG93cy5Gb3JtcywgQ3VsdHVyZT1uZXV0cmFs LCBQdWJsaWNLZXlUb2tlbj1iNzdhNWM1NjE5MzRlMDg5BQEAAAAmU3lzdGVtLldpbmRvd3MuRm9ybXMu SW1hZ2VMaXN0U3RyZWFtZXIBAAAABERhdGEHAgIAAAAJAwAAAA8DAAAAKh8AAAJNU0Z0AUkBTAIBAQMB - AAGoAQsBqAELARQBAAEUAQAD/wEAASEBAAj/AUIBTQE2BwABNgMAASgDAAFQAwABFAMAAQEBAAEgBgAB + AAHQAQwB0AEMARQBAAEUAQAD/wEAASEBAAj/AUIBTQE2BwABNgMAASgDAAFQAwABFAMAAQEBAAEgBgAB GRIAAxEBFgNKAYkCWAFaAb0DXQHMA10BzANdAcwDXQHMA10BzANdAcwDXQHMA10BzANdAcwDXQHMA10B zAJcAV8ByANYAbgDMQFMAw8BFGAAAzYBWQFCAUEBQgFyAUIBQQFCAXIDQgFyA0IBcwNCAXIDQgFyA0IB cgNCAXIDQgFyA0IBcgNCAXIDQgFyA0IBcgNCAXIDQgFyA0EBcQMYASFQAAM2AVkCXQFgAc4CUQFvAfcB @@ -281,7 +281,7 @@ AAEAAAD/////AQAAAAAAAAAMAgAAAEZTeXN0ZW0uV2luZG93cy5Gb3JtcywgQ3VsdHVyZT1uZXV0cmFs LCBQdWJsaWNLZXlUb2tlbj1iNzdhNWM1NjE5MzRlMDg5BQEAAAAmU3lzdGVtLldpbmRvd3MuRm9ybXMu SW1hZ2VMaXN0U3RyZWFtZXIBAAAABERhdGEHAgIAAAAJAwAAAA8DAAAAxiIAAAJNU0Z0AUkBTAIBAQMB - AAGoAQsBqAELARQBAAEUAQAD/wEAASEBAAj/AUIBTQE2BwABNgMAASgDAAFQAwABFAMAAQEBAAEgBgAB + AAHQAQwB0AEMARQBAAEUAQAD/wEAASEBAAj/AUIBTQE2BwABNgMAASgDAAFQAwABFAMAAQEBAAEgBgAB GRIAAycBOgJjAV8B2gHDAYgBOwH/AcMBiAE7Af8BwwGIATsB/wHDAYgBOwH/AcMBiAE7Af8BwwGIATsB /wHDAYgBOwH/AcMBiAE7Af8BwwGIATsB/wHDAYgBOwH/AcMBiAE7Af8BwwGIATsB/wHDAYgBOwH/AcMB iAE7Af8BwwGIATsB/wHDAYgBOwH/AmMBXwHaAycBOgMnAToCYwFfAdoBwwGIATsB/wHDAYgBOwH/AcMB diff --git a/KindleMate2/StaticData.cs b/KindleMate2/StaticData.cs index b5a3856..fbec9fe 100644 --- a/KindleMate2/StaticData.cs +++ b/KindleMate2/StaticData.cs @@ -1,7 +1,6 @@ using System.Data; using System.Data.SQLite; using System.Reflection; -using System.Windows.Forms; using DarkModeForms; using KindleMate2.Entities; @@ -20,7 +19,7 @@ public StaticData() { command.ExecuteNonQuery(); } - public void OpenConnection() { + private void OpenConnection() { _connection.Open(); } @@ -57,10 +56,20 @@ public DataTable GetClipingsDataTable() { return dataTable; } - public DataTable GetClipingsDataTableFuzzySearch(string strSearch) { + public DataTable GetClipingsDataTableFuzzySearch(string strSearch, string type) { var dataTable = new DataTable(); - const string queryClippings = "SELECT DISTINCT * FROM clippings WHERE content LIKE '%' || @strSearch || '%' OR bookname LIKE '%' || @strSearch || '%' OR authorname LIKE '%' || @strSearch || '%'"; + var sql = string.Empty; + if (type.Equals(Strings.Book_Title)) { + sql = "WHERE bookname LIKE '%' || @strSearch || '%'"; + } else if (type.Equals(Strings.Author)) { + sql = "WHERE authorname LIKE '%' || @strSearch || '%'"; + } else if (type.Equals(Strings.Content)) { + sql = "WHERE content LIKE '%' || @strSearch || '%'"; + } else if (type.Equals(Strings.Select_All)) { + sql = "WHERE content LIKE '%' || @strSearch || '%' OR bookname LIKE '%' || @strSearch || '%' OR authorname LIKE '%' || @strSearch || '%'"; + } + var queryClippings = "SELECT DISTINCT * FROM clippings " + sql; using var command = new SQLiteCommand(queryClippings, _connection); command.Parameters.AddWithValue("@strSearch", strSearch); using var adapter = new SQLiteDataAdapter(command); @@ -70,6 +79,36 @@ public DataTable GetClipingsDataTableFuzzySearch(string strSearch) { return dataTable; } + public List GetClippingsBookTitleList() { + var list = new List(); + DataTable dt = GetClipingsDataTable(); + if (dt.Rows.Count <= 0) { + return list; + } + foreach (DataRow row in dt.Rows) { + var bookTitle = row["bookname"].ToString() ?? string.Empty; + if (!string.IsNullOrEmpty(bookTitle) && !list.Contains(bookTitle)) { + list.Add(bookTitle); + } + } + return list; + } + + public List GetClippingsAuthorList() { + var list = new List(); + DataTable dt = GetClipingsDataTable(); + if (dt.Rows.Count <= 0) { + return list; + } + foreach (DataRow row in dt.Rows) { + var bookTitle = row["authorname"].ToString() ?? string.Empty; + if (!string.IsNullOrEmpty(bookTitle) && !list.Contains(bookTitle)) { + list.Add(bookTitle); + } + } + return list; + } + public bool IsExistOriginalClippings(string? key) { switch (key) { case null: @@ -146,10 +185,18 @@ public int GetOriginClippingsCount() { } */ - public DataTable GetOriginClippingsDataTableFuzzySearch(string strSearch) { + public DataTable GetOriginClippingsDataTableFuzzySearch(string strSearch, string type) { var dataTable = new DataTable(); - const string queryClippings = "SELECT DISTINCT * FROM original_clipping_lines WHERE line1 LIKE '%' || @strSearch || '%' OR line4 LIKE '%' || @strSearch || '%'"; + var sql = string.Empty; + if (type.Equals(Strings.Book_Title) || type.Equals(Strings.Author)) { + sql = "WHERE line1 LIKE '%' || @strSearch || '%'"; + } else if (type.Equals(Strings.Content)) { + sql = "WHERE line4 LIKE '%' || @strSearch || '%'"; + } else if (type.Equals(Strings.Select_All)) { + sql = "WHERE line1 LIKE '%' || @strSearch || '%' OR line4 LIKE '%' || @strSearch || '%'"; + } + var queryClippings = "SELECT DISTINCT * FROM original_clipping_lines " + sql; using var command = new SQLiteCommand(queryClippings, _connection); command.Parameters.AddWithValue("@strSearch", strSearch); using var adapter = new SQLiteDataAdapter(command); @@ -466,10 +513,18 @@ public DataTable GetVocabDataTable() { return dataTable; } - public DataTable GetVocabDataTableFuzzySearch(string strSearch) { + public DataTable GetVocabDataTableFuzzySearch(string strSearch, string type) { var dataTable = new DataTable(); - const string query = "SELECT DISTINCT * FROM vocab WHERE word LIKE '%' || @strSearch || '%' OR stem LIKE '%' || @strSearch || '%'"; + var sql = string.Empty; + if (type.Equals(Strings.Vocabulary)) { + sql = "WHERE word LIKE '%' || @strSearch || '%'"; + } else if (type.Equals(Strings.Stem)) { + sql = "WHERE stem LIKE '%' || @strSearch || '%'"; + } else if (type.Equals(Strings.Select_All)) { + sql = "WHERE word LIKE '%' || @strSearch || '%' OR stem LIKE '%' || @strSearch || '%'"; + } + var query = "SELECT DISTINCT * FROM vocab " + sql; using var command = new SQLiteCommand(query, _connection); command.Parameters.AddWithValue("@strSearch", strSearch); using var adapter = new SQLiteDataAdapter(command); @@ -479,6 +534,38 @@ public DataTable GetVocabDataTableFuzzySearch(string strSearch) { return dataTable; } + + + public List GetVocabWordList() { + var list = new List(); + DataTable dt = GetVocabDataTable(); + if (dt.Rows.Count <= 0) { + return list; + } + foreach (DataRow row in dt.Rows) { + var bookTitle = row["word"].ToString() ?? string.Empty; + if (!string.IsNullOrEmpty(bookTitle) && !list.Contains(bookTitle)) { + list.Add(bookTitle); + } + } + return list; + } + + public List GetVocabStemList() { + var list = new List(); + DataTable dt = GetVocabDataTable(); + if (dt.Rows.Count <= 0) { + return list; + } + foreach (DataRow row in dt.Rows) { + var bookTitle = row["stem"].ToString() ?? string.Empty; + if (!string.IsNullOrEmpty(bookTitle) && !list.Contains(bookTitle)) { + list.Add(bookTitle); + } + } + return list; + } + public DataTable GetLookupsDataTable() { var dataTable = new DataTable(); @@ -491,10 +578,22 @@ public DataTable GetLookupsDataTable() { return dataTable; } - public DataTable GetLookupsDataTableFuzzySearch(string strSearch) { + public DataTable GetLookupsDataTableFuzzySearch(string strSearch, string type) { var dataTable = new DataTable(); - const string query = "SELECT DISTINCT * FROM lookups WHERE word_key LIKE '%' || @strSearch || '%' OR usage LIKE '%' || @strSearch || '%' OR title LIKE '%' || @strSearch || '%' OR authors LIKE '%' || @strSearch || '%'"; + var sql = string.Empty; + if (type.Equals(Strings.Book_Title)) { + sql = "WHERE title LIKE '%' || @strSearch || '%'"; + } else if (type.Equals(Strings.Author)) { + sql = "WHERE authors LIKE '%' || @strSearch || '%'"; + } else if (type.Equals(Strings.Content)) { + sql = "WHERE usage LIKE '%' || @strSearch || '%'"; + } else if (type.Equals(Strings.Vocabulary) || type.Equals(Strings.Stem)) { + sql = "WHERE word_key LIKE '%' || @strSearch || '%'"; + } else if (type.Equals(Strings.Select_All)) { + sql = "WHERE word_key LIKE '%' || @strSearch || '%' OR usage LIKE '%' || @strSearch || '%' OR title LIKE '%' || @strSearch || '%' OR authors LIKE '%' || @strSearch || '%'"; + } + var query = "SELECT DISTINCT * FROM lookups " + sql; using var command = new SQLiteCommand(query, _connection); command.Parameters.AddWithValue("@strSearch", strSearch); using var adapter = new SQLiteDataAdapter(command); @@ -620,7 +719,7 @@ private void SetSettings(string name, string value) { command.ExecuteNonQuery(); } - public string GetTheme() { + private string GetTheme() { return GetSettings("theme"); } @@ -642,7 +741,7 @@ public bool IsDarkTheme() { return isWindowsDarkTheme; } - public bool IsWindowsDarkTheme() { + private bool IsWindowsDarkTheme() { var isWindowsDarkTheme = DarkModeCS.GetWindowsColorMode() <= 0; return isWindowsDarkTheme; } @@ -651,7 +750,7 @@ public void SetTheme(string value) { SetSettings("theme", value); } - public void SetTheme(bool isDarkTheme) { + private void SetTheme(bool isDarkTheme) { SetTheme(isDarkTheme ? "dark" : "light"); }