kod ilk çalıştığında veritabanından verileri gösterdi , sonra database e yazdıktan sonra…
- User
kod ilk çalıştığında veritabanından verileri gösterdi , sonra database e yazdıktan sonra internetten açıkken herhangi bir şey göstermedi ,nerede hata olabilir kodu düzelt tek parça yaz -> using System; using System.Windows.Forms; using Firebase.Database; using Firebase.Database.Query; using FirebaseAdmin; using Google.Apis.Auth.OAuth2; using System.Threading.Tasks; using System.Linq; using System.Drawing; using System.Collections.Generic; using System.IO; using System.Text; using Newtonsoft.Json; // JSON işlemleri için eklendi using Firebase.Database.Streaming; // FirebaseEventType için eklendi using System.Data.SQLite; // SQLite için eklendi using System.Net.NetworkInformation; // İnternet bağlantısı kontrolü için eklendi
namespace Flashcard { public partial class MainForm : Form { // Firebase client private FirebaseClient firebaseClient;
// UI Controls private DataGridView dataGridView; private TextBox txtTitle, txtCategory, txtContent, txtBackgroundColor, txtSearch; private CheckBox chkFavorite, chkPinned; private CheckBox chkFilterFavorites, chkFilterPinned; // Filtering CheckBoxes private Button btnAdd, btnUpdate, btnDelete, btnSearch, btnClear; private StatusStrip statusStrip; private ToolStripStatusLabel statusLabel; // Yeni Eklenen Kontroller private MenuStrip menuStrip; // Menü Çubuğu private ToolStripMenuItem fileMenuItem, themeMenuItem; // Menü Öğeleri private ToolStripMenuItem saveAsMenuItem, lightModeMenuItem, darkModeMenuItem; // Alt Menü Öğeleri private ToolStripMenuItem clipboardModeMenuItem, clipboardOnMenuItem, clipboardOffMenuItem; // Clipboard Mode Menü Öğeleri private ToolStripMenuItem loadTagsMenuItem; // "Load Tags" Menü Öğesi // Custom Tag Menu Items private ToolStripMenuItem customTagMenuItem; // Custom Tag Menu Item private ToolStripMenuItem addTagMenuItem; // Alt Menü öğesi private ToolStripMenuItem manageTagsMenuItem; // Alt Menü öğesi private List<string> customTags = new List<string>(); // Custom Tag listesi private string selectedCustomTag = null; // Seçilen Custom Tag // customTags'i kaydetmek için dosya yolu private readonly string customTagsFilePath = "custom_tags.json"; // Eklendi // ContextMenuStrip for column visibility private ContextMenuStrip columnContextMenu; // Sorting state private string currentSortColumn = "timestamp"; // Varsayılan olarak timestamp'e göre sıralama private bool sortAscending = false; // En yeni veriler en üstte olacak şekilde // Data storage private List<FlashcardItem> allFlashcards = new List<FlashcardItem>(); // Current search term private string currentSearchTerm = ""; // Dictionary to store column visibility state private Dictionary<string, bool> columnVisibilityState = new Dictionary<string, bool>(); // Clipboard monitoring private Timer clipboardMonitorTimer; private string lastClipboardText; // Yeni Eklenen Değişkenler private CheckedListBox tagCheckedListBox; // Etiketlerin gösterileceği CheckedListBox private string selectedTag = "uncategorized"; // Seçilen etiket // SQLite bağlantısı için değişken private SQLiteConnection sqliteConnection; // İnternet bağlantısı durumu private bool isInternetAvailable = true; public MainForm() { InitializeComponent(); InitializeFirebase(); InitializeLocalDatabase(); // Yerel veritabanını başlat InitializeUI(); } /// <summary> /// Initializes Firebase connection using the provided JSON credentials. /// </summary> private void InitializeFirebase() { var path = "flashcard-e7f47-firebase-adminsdk-om656-e70a5506c2.json"; var credential = GoogleCredential.FromFile(path); // Check if FirebaseApp is already created to prevent duplicate initialization if (FirebaseApp.DefaultInstance == null) { FirebaseApp.Create(new AppOptions { Credential = credential }); } firebaseClient = new FirebaseClient("https://flashcard-e7f47-default-rtdb.europe-west1.firebasedatabase.app/"); // Firebase Realtime Database URL'niz } /// <summary> /// Yerel SQLite veritabanını başlatır ve gerekli tabloyu oluşturur. /// </summary> private void InitializeLocalDatabase() { string databaseFile = "local_flashcards.db"; bool databaseExists = File.Exists(databaseFile); sqliteConnection = new SQLiteConnection($"Data Source={databaseFile};Version=3;"); sqliteConnection.Open(); if (!databaseExists) { string createTableQuery = @" CREATE TABLE flashcards ( id TEXT PRIMARY KEY, category TEXT, title TEXT, content TEXT, favorite INTEGER, pinned INTEGER, timestamp INTEGER, backgroundColor TEXT, isUpdated INTEGER, isDeleted INTEGER ); "; SQLiteCommand command = new SQLiteCommand(createTableQuery, sqliteConnection); command.ExecuteNonQuery(); } } /// <summary> /// Sets up the user interface components. /// </summary> private void InitializeUI() { this.Size = new Size(1000, 800); this.Text = "Flashcard Application"; this.StartPosition = FormStartPosition.CenterScreen; // MainForm ekranın ortasında açılsın // MenuStrip Oluşturma menuStrip = new MenuStrip { Dock = DockStyle.Top // MenuStrip'i üstte konumlandır }; // File Menu fileMenuItem = new ToolStripMenuItem("File"); saveAsMenuItem = new ToolStripMenuItem("Save As"); saveAsMenuItem.Click += SaveAsMenuItem_Click; // Event Handler fileMenuItem.DropDownItems.Add(saveAsMenuItem); // Theme Menu themeMenuItem = new ToolStripMenuItem("Theme"); lightModeMenuItem = new ToolStripMenuItem("Light Mode"); darkModeMenuItem = new ToolStripMenuItem("Dark Mode"); lightModeMenuItem.Click += LightModeMenuItem_Click; // Event Handler darkModeMenuItem.Click += DarkModeMenuItem_Click; // Event Handler themeMenuItem.DropDownItems.Add(lightModeMenuItem); themeMenuItem.DropDownItems.Add(darkModeMenuItem); // Clipboard Mode Menu clipboardModeMenuItem = new ToolStripMenuItem("Clipboard Mode"); clipboardOnMenuItem = new ToolStripMenuItem("On"); clipboardOffMenuItem = new ToolStripMenuItem("Off"); // CheckOnClick özelliğini kullanarak tıklanınca işaretlenmesini sağlıyoruz clipboardOnMenuItem.CheckOnClick = true; clipboardOffMenuItem.CheckOnClick = true; clipboardOffMenuItem.Checked = true; // Başlangıçta "Off" seçili clipboardOnMenuItem.Click += ClipboardOnMenuItem_Click; clipboardOffMenuItem.Click += ClipboardOffMenuItem_Click; clipboardModeMenuItem.DropDownItems.Add(clipboardOnMenuItem); clipboardModeMenuItem.DropDownItems.Add(clipboardOffMenuItem); // Load Tags Menu loadTagsMenuItem = new ToolStripMenuItem("Load Tags"); loadTagsMenuItem.Click += LoadTagsMenuItem_Click; // Event Handler // Custom Tag Menu customTagMenuItem = new ToolStripMenuItem("Custom Tag"); addTagMenuItem = new ToolStripMenuItem("Add Tag"); addTagMenuItem.Click += AddTagMenuItem_Click; // Event Handler // Declare manageTagsMenuItem at class level and initialize manageTagsMenuItem = new ToolStripMenuItem("Manage Tags"); manageTagsMenuItem.Click += ManageTagsMenuItem_Click; // Event Handler customTagMenuItem.DropDownItems.Add(addTagMenuItem); customTagMenuItem.DropDownItems.Add(manageTagsMenuItem); // MenuStrip'e Menüleri Ekleme menuStrip.Items.Add(fileMenuItem); menuStrip.Items.Add(themeMenuItem); menuStrip.Items.Add(clipboardModeMenuItem); menuStrip.Items.Add(loadTagsMenuItem); // "Load Tags" Menü Öğesini Ekledik menuStrip.Items.Add(customTagMenuItem); // "Custom Tag" Menü Öğesini Ekledik // Form'a MenuStrip'i Ekleme this.MainMenuStrip = menuStrip; Controls.Add(menuStrip); // MenuStrip'i ilk olarak ekliyoruz // Clipboard izleme için Timer clipboardMonitorTimer = new Timer { Interval = 1000 }; // Her saniyede bir kontrol eder clipboardMonitorTimer.Tick += ClipboardMonitorTimer_Tick; // Arama Metin Kutusu ve Butonları txtSearch = CreateTextBox("", 10, menuStrip.Bottom + 6, 200); // Placeholder'ı boş bıraktık //txtSearch.PlaceholderText = "Search..."; // .NET 6 ve üzeri için btnSearch = CreateButton("Search", txtSearch.Right + 6, txtSearch.Top - 2, 80); btnClear = CreateButton("Clear", btnSearch.Right + 6, txtSearch.Top - 2, 80); // Filtreleme CheckBox'ları chkFilterFavorites = new CheckBox { Text = "Show Favorites", Top = txtSearch.Bottom + 6, Left = txtSearch.Left, Width = 120 }; chkFilterFavorites.CheckedChanged += FilterCheckBoxChanged; Controls.Add(chkFilterFavorites); chkFilterPinned = new CheckBox { Text = "Show Pinned", Top = txtSearch.Bottom + 6, Left = chkFilterFavorites.Right + 10, Width = 120 }; chkFilterPinned.CheckedChanged += FilterCheckBoxChanged; Controls.Add(chkFilterPinned); // Initialize DataGridView dataGridView = new DataGridView { Height = 400, Left = 0, Top = chkFilterFavorites.Bottom + 10, // DataGridView'i filtrelerin altına yerleştir Width = this.ClientSize.Width, // Tam pencere genişliğini kapla Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right, AllowUserToAddRows = false, AllowUserToDeleteRows = false, ReadOnly = false, SelectionMode = DataGridViewSelectionMode.FullRowSelect, MultiSelect = true, AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill, ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize }; dataGridView.SelectionChanged += DataGridView_SelectionChanged; dataGridView.CellContentClick += DataGridView_CellContentClick; dataGridView.CellFormatting += DataGridView_CellFormatting; dataGridView.ColumnHeaderMouseClick += DataGridView_ColumnHeaderMouseClick; // For sorting Controls.Add(dataGridView); // StatusStrip statusStrip = new StatusStrip(); statusLabel = new ToolStripStatusLabel(); statusStrip.Items.Add(statusLabel); statusStrip.Dock = DockStyle.Bottom; // StatusStrip'i pencerenin en altına sabitle Controls.Add(statusStrip); // Tag CheckedListBox Oluşturma tagCheckedListBox = new CheckedListBox { Width = 400, CheckOnClick = true, Left = this.ClientSize.Width - 400, // Pencerenin en sağ kenarına bitişik hizala Top = dataGridView.Bottom, // DataGridView'in hemen altına yerleştir Height = statusStrip.Top - dataGridView.Bottom + 10, // DataGridView'den StatusStrip'e kadar olan yüksekliği ayarla Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right, Visible = false // Başlangıçta gizli }; tagCheckedListBox.ItemCheck += TagCheckedListBox_ItemCheck; Controls.Add(tagCheckedListBox); // Kontroller arası standart Windows Forms arayüzündeki gibi mesafeler int labelLeft = 10; int textBoxLeft = 150; int startTop = dataGridView.Bottom + 6; // DataGridView'in hemen altından başla int textBoxWidth = 300; int textBoxHeight = 25; int verticalSpacing = 6; // Kontroller arası boşluk int currentTop = dataGridView.Bottom + 6; // Kontrolleri DataGridView'in altına yerleştir // Etiketleri ve Metin Kutularını Oluştur // Category CreateLabel("Category:", labelLeft, currentTop); txtCategory = CreateTextBox("", textBoxLeft, currentTop, textBoxWidth); // Placeholder'ı boş bıraktık currentTop += textBoxHeight + verticalSpacing; // Title CreateLabel("Title:", labelLeft, currentTop); txtTitle = CreateTextBox("", textBoxLeft, currentTop, textBoxWidth); // Placeholder'ı boş bıraktık currentTop += textBoxHeight + verticalSpacing; // Content CreateLabel("Content:", labelLeft, currentTop); txtContent = CreateTextBox("", textBoxLeft, currentTop, textBoxWidth); // Placeholder'ı boş bıraktık currentTop += textBoxHeight + verticalSpacing; // Background Color CreateLabel("Background Color:", labelLeft, currentTop); txtBackgroundColor = CreateTextBox("", textBoxLeft, currentTop, textBoxWidth); // Placeholder'ı boş bıraktık currentTop += textBoxHeight + verticalSpacing; // İşlevsel CheckBox'lar chkFavorite = new CheckBox { Text = "Favorite", Top = currentTop, Left = textBoxLeft, Width = 80 }; Controls.Add(chkFavorite); chkPinned = new CheckBox { Text = "Pinned", Top = currentTop, Left = textBoxLeft + 90, Width = 80 }; Controls.Add(chkPinned); currentTop += chkFavorite.Height + verticalSpacing; // Butonları Yatay Olarak Hizala int buttonTop = currentTop + 10; // Biraz boşluk ekle int buttonLeft = labelLeft; btnAdd = CreateButton("Add", buttonLeft, buttonTop, 100); buttonLeft += 110; btnUpdate = CreateButton("Update", buttonLeft, buttonTop, 100); buttonLeft += 110; btnDelete = CreateButton("Delete", buttonLeft, buttonTop, 100); // ContextMenuStrip for column visibility columnContextMenu = new ContextMenuStrip(); // Not: Kolon bilgisi mevcut olduktan sonra ContextMenuStrip'i dolduracağız // Assign the MouseUp event to show the ContextMenuStrip on right-click dataGridView.MouseUp += DataGridView_MouseUp; // Event Handler for Form Load this.Load += MainForm_Load; // Formun MouseDown olayını abone olma this.MouseDown += MainForm_MouseDown; // Tüm kontrollerin MouseDown olaylarını abone et RegisterMouseDownEvent(this); } private void MainForm_MouseDown(object sender, MouseEventArgs e) { // Fare imlecinin altındaki kontrolü al Control clickedControl = this.GetChildAtPoint(e.Location); // Eğer tıklanan kontrol null ise veya formun kendisiyse, metin kutularını sıfırla if (clickedControl == null || clickedControl == this) { ClearInputs(); } } private void RegisterMouseDownEvent(Control parent) { foreach (Control control in parent.Controls) { control.MouseDown += Control_MouseDown; if (control.HasChildren) { RegisterMouseDownEvent(control); } } } private void Control_MouseDown(object sender, MouseEventArgs e) { // Kontrolün MouseDown olayını formun MouseDown olayına yönlendir MainForm_MouseDown(sender, e); } // Yeni bir CreateLabel metodu ekliyoruz private Label CreateLabel(string text, int left, int top) { Label label = new Label { Text = text, Left = left, Top = top, AutoSize = true }; Controls.Add(label); return label; } /// <summary> /// Handles the Form Load event to initially load data and start real-time listener. /// </summary> private async void MainForm_Load(object sender, EventArgs e) { LoadCustomTags(); // Eklendi // Varsayılan sıralamayı ayarladık currentSortColumn = "timestamp"; sortAscending = false; // İnternet bağlantısını kontrol et isInternetAvailable = CheckInternetConnectivity(); if (isInternetAvailable) { await SyncLocalChangesToFirebase(); // Yerel değişiklikleri Firebase'e senkronize et await LoadDataFromFirebaseAsync(); // Firebase'den verileri çek StartFirebaseRealTimeListener(); // Gerçek zamanlı dinleyiciyi başlat } else { LoadDataFromLocalDatabase(); // Yerel veritabanından verileri yükle MessageBox.Show("No internet connection. Loaded data from local storage.", "Offline Mode", MessageBoxButtons.OK, MessageBoxIcon.Information); } } /// <summary> /// Creates a TextBox. /// </summary> private TextBox CreateTextBox(string text, int left, int top, int width) { TextBox textBox = new TextBox { Left = left, Top = top, Width = width, Text = text, ForeColor = Color.Black }; Controls.Add(textBox); return textBox; } /// <summary> /// Creates a Button with specified properties and assigns click event. /// </summary> private Button CreateButton(string text, int left, int top, int width) { Button button = new Button { Text = text, Left = left, Top = top, Width = width }; button.Click += (sender, e) => ButtonClick(text); Controls.Add(button); return button; } /// <summary> /// Handles button click events by identifying the button text. /// </summary> private void ButtonClick(string buttonText) { switch (buttonText) { case "Add": AddData(); break; case "Update": UpdateData(); break; case "Delete": DeleteData(); break; case "Search": SearchData(); break; case "Clear": ClearSearch(); break; } } /// <summary> /// Handles the "Save As" menu item click to save data in selected format. /// </summary> private void SaveAsMenuItem_Click(object sender, EventArgs e) { SaveFileDialog saveFileDialog = new SaveFileDialog { Filter = "CSV files (*.csv)|*.csv|TSV files (*.tsv)|*.tsv|Text files (*.txt)|*.txt", Title = "Save As" }; if (saveFileDialog.ShowDialog() == DialogResult.OK) { try { string extension = Path.GetExtension(saveFileDialog.FileName).ToLower(); // DataGridView'den verileri al var data = (List<FlashcardItem>)dataGridView.DataSource; // Verileri seçilen formata göre kaydet switch (extension) { case ".csv": SaveDataAsCsv(data, saveFileDialog.FileName, ','); break; case ".tsv": SaveDataAsCsv(data, saveFileDialog.FileName, '\t'); break; case ".txt": SaveDataAsTxt(data, saveFileDialog.FileName); break; default: MessageBox.Show("Unsupported file format.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); break; } MessageBox.Show("Data saved successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show($"Error saving data: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } /// <summary> /// Saves data as CSV or TSV file. /// </summary> private void SaveDataAsCsv(List<FlashcardItem> data, string filePath, char delimiter) { StringBuilder sb = new StringBuilder(); // Başlık satırı sb.AppendLine(string.Join(delimiter.ToString(), "Category", "Title", "Content", "Favorite", "Pinned", "Timestamp", "BackgroundColor")); foreach (var item in data) { string[] fields = new string[] { EscapeCsvField(item.category), EscapeCsvField(item.title), EscapeCsvField(item.content), item.favorite.ToString(), item.pinned.ToString(), item.timestamp.ToString(), EscapeCsvField(item.backgroundColor) }; sb.AppendLine(string.Join(delimiter.ToString(), fields)); } File.WriteAllText(filePath, sb.ToString(), Encoding.UTF8); } /// <summary> /// Escapes CSV fields containing delimiter or quotes. /// </summary> private string EscapeCsvField(string field) { if (field.Contains(",") || field.Contains("\"") || field.Contains("\r") || field.Contains("\n")) { return $"\"{field.Replace("\"", "\"\"")}\""; } else { return field; } } /// <summary> /// Saves data as TXT file. /// </summary> private void SaveDataAsTxt(List<FlashcardItem> data, string filePath) { StringBuilder sb = new StringBuilder(); foreach (var item in data) { sb.AppendLine($"Category: {item.category}"); sb.AppendLine($"Title: {item.title}"); sb.AppendLine($"Content: {item.content}"); sb.AppendLine($"Favorite: {item.favorite}"); sb.AppendLine($"Pinned: {item.pinned}"); sb.AppendLine($"Timestamp: {item.timestamp}"); sb.AppendLine($"BackgroundColor: {item.backgroundColor}"); sb.AppendLine(new string('-', 50)); } File.WriteAllText(filePath, sb.ToString(), Encoding.UTF8); } /// <summary> /// Handles the Light Mode menu item click. /// </summary> private void LightModeMenuItem_Click(object sender, EventArgs e) { SetTheme(Color.White, Color.Black, false); } /// <summary> /// Handles the Dark Mode menu item click. /// </summary> private void DarkModeMenuItem_Click(object sender, EventArgs e) { SetTheme(Color.FromArgb(45, 45, 48), Color.White, true); } /// <summary> /// Sets the application's theme colors. /// </summary> private void SetTheme(Color backColor, Color foreColor, bool isDarkMode) { this.BackColor = backColor; this.ForeColor = foreColor; foreach (Control control in this.Controls) { SetControlTheme(control, backColor, foreColor); } // DataGridView renklerini ayarla dataGridView.BackgroundColor = backColor; dataGridView.DefaultCellStyle.BackColor = backColor; dataGridView.DefaultCellStyle.ForeColor = foreColor; // Dark mode ise DataGridView kenarlarını, sütun başlıklarını ve yan seçim alanını portakal rengi yap if (isDarkMode) { dataGridView.GridColor = Color.Orange; dataGridView.EnableHeadersVisualStyles = false; dataGridView.ColumnHeadersDefaultCellStyle.BackColor = Color.Orange; dataGridView.ColumnHeadersDefaultCellStyle.ForeColor = Color.Black; dataGridView.RowHeadersDefaultCellStyle.BackColor = Color.Orange; dataGridView.RowHeadersDefaultCellStyle.ForeColor = Color.Black; // Satır yüksekliğini ve yazı boyutunu ayarla dataGridView.RowTemplate.Height = 30; // Satır yüksekliği dataGridView.DefaultCellStyle.Font = new Font("Arial", 12, FontStyle.Regular); // Yazı boyutu // Satır çizgilerini kaldır dataGridView.CellBorderStyle = DataGridViewCellBorderStyle.None; } else { dataGridView.EnableHeadersVisualStyles = true; dataGridView.GridColor = foreColor; dataGridView.ColumnHeadersDefaultCellStyle.BackColor = backColor; dataGridView.ColumnHeadersDefaultCellStyle.ForeColor = foreColor; dataGridView.RowHeadersDefaultCellStyle.BackColor = backColor; dataGridView.RowHeadersDefaultCellStyle.ForeColor = foreColor; } } /// <summary> /// Recursively sets theme colors for controls. /// </summary> private void SetControlTheme(Control control, Color backColor, Color foreColor) { control.BackColor = backColor; control.ForeColor = foreColor; if (control.HasChildren) { foreach (Control child in control.Controls) { SetControlTheme(child, backColor, foreColor); } } } /// <summary> /// Handles CellContentClick events for CheckBox columns to toggle favorite and pinned statuses. /// </summary> private void DataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex >= 0 && (dataGridView.Columns[e.ColumnIndex].Name == "favorite" || dataGridView.Columns[e.ColumnIndex].Name == "pinned")) { var flashcard = (FlashcardItem)dataGridView.Rows[e.RowIndex].DataBoundItem; bool newValue = !(bool)dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value; if (dataGridView.Columns[e.ColumnIndex].Name == "favorite") { flashcard.favorite = newValue; } else { flashcard.pinned = newValue; } if (isInternetAvailable) { _ = UpdateFlashcardInDatabase(flashcard); } else { // İnternet yoksa yerel veritabanında güncelle UpdateFlashcardInLocalDatabase(flashcard); } } } /// <summary> /// Updates a flashcard in the Firebase database. /// </summary> private async Task UpdateFlashcardInDatabase(FlashcardItem flashcard) { try { await firebaseClient .Child("flashcards") .Child(flashcard.id) .PutAsync(flashcard); // Yerel veritabanında da güncelle UpdateFlashcardInLocalDatabase(flashcard); } catch (Exception ex) { MessageBox.Show($"Error updating flashcard: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void UpdateFlashcardInLocalDatabase(FlashcardItem flashcard) { string query = @" UPDATE flashcards SET category = @category, title = @title, content = @content, favorite = @favorite, pinned = @pinned, timestamp = @timestamp, backgroundColor = @backgroundColor, isUpdated = 1 WHERE id = @id; "; SQLiteCommand command = new SQLiteCommand(query, sqliteConnection); command.Parameters.AddWithValue("@category", flashcard.category); command.Parameters.AddWithValue("@title", flashcard.title); command.Parameters.AddWithValue("@content", flashcard.content); command.Parameters.AddWithValue("@favorite", flashcard.favorite ? 1 : 0); command.Parameters.AddWithValue("@pinned", flashcard.pinned ? 1 : 0); command.Parameters.AddWithValue("@timestamp", flashcard.timestamp); command.Parameters.AddWithValue("@backgroundColor", flashcard.backgroundColor); command.Parameters.AddWithValue("@id", flashcard.id); command.ExecuteNonQuery(); // Yerel listede güncelle var existingItem = allFlashcards.FirstOrDefault(f => f.id == flashcard.id); if (existingItem != null) { int index = allFlashcards.IndexOf(existingItem); allFlashcards[index] = flashcard; } ApplyFiltersAndSort(); } private async Task LoadDataFromFirebaseAsync() { try { var data = await firebaseClient .Child("flashcards") .OnceAsync<FlashcardItem>(); if (data == null || !data.Any()) { allFlashcards = new List<FlashcardItem>(); } else { allFlashcards = data.Select(d => new FlashcardItem { id = d.Object.id ?? d.Key, title = d.Object.title, category = string.IsNullOrWhiteSpace(d.Object.category) ? "uncategorized" : d.Object.category, content = d.Object.content, favorite = d.Object.favorite, pinned = d.Object.pinned, timestamp = d.Object.timestamp, backgroundColor = d.Object.backgroundColor }).ToList(); // Firebase'den gelen verileri yerel veritabanına kaydet SaveDataToLocalDatabase(allFlashcards); } ApplyFiltersAndSort(); // After data is loaded, populate the ContextMenuStrip PopulateColumnContextMenu(); } catch (Exception ex) { MessageBox.Show($"Error loading data: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } /// <summary> /// Loads data from the local SQLite database. /// </summary> private void LoadDataFromLocalDatabase() { string query = "SELECT * FROM flashcards WHERE isDeleted = 0;"; SQLiteCommand command = new SQLiteCommand(query, sqliteConnection); SQLiteDataReader reader = command.ExecuteReader(); allFlashcards.Clear(); while (reader.Read()) { var flashcard = new FlashcardItem { id = reader["id"].ToString(), category = reader["category"].ToString(), title = reader["title"].ToString(), content = reader["content"].ToString(), favorite = Convert.ToInt32(reader["favorite"]) == 1, pinned = Convert.ToInt32(reader["pinned"]) == 1, timestamp = Convert.ToInt64(reader["timestamp"]), backgroundColor = reader["backgroundColor"].ToString() }; allFlashcards.Add(flashcard); } ApplyFiltersAndSort(); PopulateColumnContextMenu(); } /// <summary> /// Saves the list of flashcards to the local SQLite database. /// </summary> private void SaveDataToLocalDatabase(List<FlashcardItem> flashcards) { foreach (var flashcard in flashcards) { string insertQuery = @" INSERT OR REPLACE INTO flashcards (id, category, title, content, favorite, pinned, timestamp, backgroundColor, isUpdated, isDeleted) VALUES (@id, @category, @title, @content, @favorite, @pinned, @timestamp, @backgroundColor, 0, 0); "; SQLiteCommand command = new SQLiteCommand(insertQuery, sqliteConnection); command.Parameters.AddWithValue("@id", flashcard.id); command.Parameters.AddWithValue("@category", flashcard.category); command.Parameters.AddWithValue("@title", flashcard.title); command.Parameters.AddWithValue("@content", flashcard.content); command.Parameters.AddWithValue("@favorite", flashcard.favorite ? 1 : 0); command.Parameters.AddWithValue("@pinned", flashcard.pinned ? 1 : 0); command.Parameters.AddWithValue("@timestamp", flashcard.timestamp); command.Parameters.AddWithValue("@backgroundColor", flashcard.backgroundColor); command.ExecuteNonQuery(); } } private void ApplyFiltersAndSort() { // Başlangıç olarak tüm flashcard'ları al IEnumerable<FlashcardItem> filtered = allFlashcards; // Eğer veritabanı tamamen boşsa, mesaj kutusu göstermeden DataGridView'ı güncelleyin if (allFlashcards.Count == 0) { UpdateDataGridView(new List<FlashcardItem>()); return; } // Favoriler filtresini uygula if (chkFilterFavorites.Checked) { filtered = filtered.Where(f => f.favorite); } // Sabitlenmiş filtresini uygula if (chkFilterPinned.Checked) { filtered = filtered.Where(f => f.pinned); } // Arama filtresini uygula if (!string.IsNullOrWhiteSpace(currentSearchTerm)) { filtered = filtered.Where(f => f.title.ToLower().Contains(currentSearchTerm) || f.category.ToLower().Contains(currentSearchTerm) || f.content.ToLower().Contains(currentSearchTerm)); } // Listeye dönüştür List<FlashcardItem> filteredList = filtered.ToList(); if (filteredList.Count == 0) { // Filtrelerden hangisinin boş listeye neden olduğunu belirleyin bool isFavoritesFilterActive = chkFilterFavorites.Checked; bool isPinnedFilterActive = chkFilterPinned.Checked; // Filtreleri sıfırlarken olayların tetiklenmesini engelleyin chkFilterFavorites.CheckedChanged -= FilterCheckBoxChanged; chkFilterPinned.CheckedChanged -= FilterCheckBoxChanged; if (isFavoritesFilterActive) { chkFilterFavorites.Checked = false; MessageBox.Show("No favorites found. Favorite filter has been reset.", "No Results", MessageBoxButtons.OK, MessageBoxIcon.Information); } if (isPinnedFilterActive) { chkFilterPinned.Checked = false; MessageBox.Show("No pinned items found. Pinned filter has been reset.", "No Results", MessageBoxButtons.OK, MessageBoxIcon.Information); } // Olayları yeniden bağlayın chkFilterFavorites.CheckedChanged += FilterCheckBoxChanged; chkFilterPinned.CheckedChanged += FilterCheckBoxChanged; // Filtreler sıfırlandıktan sonra tekrar filtreleme işlemi yapın filtered = allFlashcards; if (!string.IsNullOrWhiteSpace(currentSearchTerm)) { filtered = filtered.Where(f => f.title.ToLower().Contains(currentSearchTerm) || f.category.ToLower().Contains(currentSearchTerm) || f.content.ToLower().Contains(currentSearchTerm)); } filteredList = filtered.ToList(); // Eğer hala sonuç yoksa ve arama terimi varsa, kullanıcıya bilgi verin if (filteredList.Count == 0 && !string.IsNullOrWhiteSpace(currentSearchTerm)) { MessageBox.Show("No flashcards match the current search term.", "No Results", MessageBoxButtons.OK, MessageBoxIcon.Information); } } // Sıralama işlemini uygula if (!string.IsNullOrEmpty(currentSortColumn)) { if (sortAscending) { switch (currentSortColumn) { case "title": filteredList = filteredList.OrderBy(f => f.title).ToList(); break; case "timestamp": filteredList = filteredList.OrderBy(f => f.timestamp).ToList(); break; } } else { switch (currentSortColumn) { case "title": filteredList = filteredList.OrderByDescending(f => f.title).ToList(); break; case "timestamp": filteredList = filteredList.OrderByDescending(f => f.timestamp).ToList(); break; } } } UpdateDataGridView(filteredList); } /// <summary> /// Updates the DataGridView with the provided list of flashcards. /// </summary> private void UpdateDataGridView(List<FlashcardItem> flashcards) { // SelectionChanged olayını geçici olarak devre dışı bırak dataGridView.SelectionChanged -= DataGridView_SelectionChanged; dataGridView.DataSource = null; dataGridView.Columns.Clear(); if (flashcards.Any()) { dataGridView.AutoGenerateColumns = false; // Define DataGridView columns // ID Column - Hidden var idColumn = new DataGridViewTextBoxColumn { DataPropertyName = "id", HeaderText = "ID", Visible = false }; dataGridView.Columns.Add(idColumn); // Category Column var categoryColumn = new DataGridViewTextBoxColumn { DataPropertyName = "category", HeaderText = "Category", Name = "category", SortMode = DataGridViewColumnSortMode.NotSortable }; dataGridView.Columns.Add(categoryColumn); // Title Column var titleColumn = new DataGridViewTextBoxColumn { DataPropertyName = "title", HeaderText = "Title", Name = "title", SortMode = DataGridViewColumnSortMode.Programmatic }; dataGridView.Columns.Add(titleColumn); // Content Column var contentColumn = new DataGridViewTextBoxColumn { DataPropertyName = "content", HeaderText = "Content", Name = "content", SortMode = DataGridViewColumnSortMode.NotSortable }; dataGridView.Columns.Add(contentColumn); // Favorite Column (Checkbox) var favoriteColumn = new DataGridViewCheckBoxColumn { DataPropertyName = "favorite", HeaderText = "Favorite", Name = "favorite", SortMode = DataGridViewColumnSortMode.Automatic }; dataGridView.Columns.Add(favoriteColumn); // Pinned Column (Checkbox) var pinnedColumn = new DataGridViewCheckBoxColumn { DataPropertyName = "pinned", HeaderText = "Pinned", Name = "pinned", SortMode = DataGridViewColumnSortMode.Automatic }; dataGridView.Columns.Add(pinnedColumn); // Timestamp Column var timestampColumn = new DataGridViewTextBoxColumn { DataPropertyName = "timestamp", HeaderText = "Timestamp", Name = "timestamp", SortMode = DataGridViewColumnSortMode.Programmatic }; dataGridView.Columns.Add(timestampColumn); // BackgroundColor Column var backgroundColorColumn = new DataGridViewTextBoxColumn { DataPropertyName = "backgroundColor", HeaderText = "Background Color", Name = "backgroundColor", SortMode = DataGridViewColumnSortMode.NotSortable }; dataGridView.Columns.Add(backgroundColorColumn); dataGridView.DataSource = flashcards; dataGridView.ClearSelection(); // Seçimi temizle } else { // If no flashcards match the filters, display a message var messageColumn = new DataGridViewTextBoxColumn { HeaderText = "Message", Name = "Message", AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill }; dataGridView.Columns.Add(messageColumn); dataGridView.Rows.Add("No flashcards found."); } // Restore column visibility state RestoreColumnVisibilityState(); // Update the status label with the count statusLabel.Text = $"Total Items: {flashcards.Count}"; // SelectionChanged olayını tekrar etkinleştir dataGridView.SelectionChanged += DataGridView_SelectionChanged; } /// <summary> /// Populates the ContextMenuStrip with the current DataGridView columns. /// Ensures no duplicate menu items are added. /// </summary> private void PopulateColumnContextMenu() { columnContextMenu.Items.Clear(); // Clear existing items to prevent duplication foreach (DataGridViewColumn column in dataGridView.Columns) { // Skip hidden or irrelevant columns if necessary // For example, you might want to skip the "ID" column if (column.Name == "ID") continue; // Create a ToolStripMenuItem for the column var menuItem = new ToolStripMenuItem(column.HeaderText) { Checked = column.Visible, CheckOnClick = true, Tag = column // Store the column in Tag for reference }; menuItem.CheckedChanged += ColumnMenuItem_CheckedChanged; // Add the menu item to the ContextMenuStrip columnContextMenu.Items.Add(menuItem); } } /// <summary> /// Handles the CheckedChanged event of ContextMenuStrip items to toggle column visibility. /// </summary> private void ColumnMenuItem_CheckedChanged(object sender, EventArgs e) { if (sender is ToolStripMenuItem menuItem && menuItem.Tag is DataGridViewColumn column) { column.Visible = menuItem.Checked; columnVisibilityState[column.Name] = column.Visible; // Update the visibility state } } /// <summary> /// Shows the ContextMenuStrip when the user right-clicks on a column header. /// </summary> private void DataGridView_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { var hitTestInfo = dataGridView.HitTest(e.X, e.Y); if (hitTestInfo.Type == DataGridViewHitTestType.ColumnHeader) { // Show the ContextMenuStrip at the mouse position columnContextMenu.Show(dataGridView, new Point(e.X, e.Y)); } } } /// <summary> /// Adds a new flashcard to the Firebase database. /// </summary> private async void AddData() { var newFlashcard = CreateFlashcardFromInputs(); if (newFlashcard != null) { if (isInternetAvailable) { try { var response = await firebaseClient .Child("flashcards") .PostAsync(newFlashcard); newFlashcard.id = response.Key; // Firebase veritabanında id alanını eklemek için güncelleme yapıyoruz await firebaseClient .Child("flashcards") .Child(response.Key) .PutAsync(newFlashcard); // Yerel veritabanına ekle SaveDataToLocalDatabase(new List<FlashcardItem> { newFlashcard }); ClearInputs(); } catch (Exception ex) { MessageBox.Show($"Error adding flashcard: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { // İnternet yoksa yerel veritabanına ekle ve senkronizasyon için işaretle AddFlashcardToLocalDatabase(newFlashcard); ClearInputs(); MessageBox.Show("No internet connection. Flashcard saved locally.", "Offline Mode", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } private void AddFlashcardToLocalDatabase(FlashcardItem flashcard) { // Yerel bir id oluştur flashcard.id = Guid.NewGuid().ToString(); string insertQuery = @" INSERT INTO flashcards (id, category, title, content, favorite, pinned, timestamp, backgroundColor, isUpdated, isDeleted) VALUES (@id, @category, @title, @content, @favorite, @pinned, @timestamp, @backgroundColor, 1, 0); "; SQLiteCommand command = new SQLiteCommand(insertQuery, sqliteConnection); command.Parameters.AddWithValue("@id", flashcard.id); command.Parameters.AddWithValue("@category", flashcard.category); command.Parameters.AddWithValue("@title", flashcard.title); command.Parameters.AddWithValue("@content", flashcard.content); command.Parameters.AddWithValue("@favorite", flashcard.favorite ? 1 : 0); command.Parameters.AddWithValue("@pinned", flashcard.pinned ? 1 : 0); command.Parameters.AddWithValue("@timestamp", flashcard.timestamp); command.Parameters.AddWithValue("@backgroundColor", flashcard.backgroundColor); command.ExecuteNonQuery(); // Yerel listeye ekle allFlashcards.Add(flashcard); ApplyFiltersAndSort(); } /// <summary> /// Updates the selected flashcard in the Firebase database. /// </summary> private async void UpdateData() { if (dataGridView.SelectedRows.Count > 0) { var selectedFlashcard = (FlashcardItem)dataGridView.SelectedRows[0].DataBoundItem; var updatedFlashcard = CreateFlashcardFromInputs(); if (updatedFlashcard != null) { updatedFlashcard.id = selectedFlashcard.id; updatedFlashcard.timestamp = selectedFlashcard.timestamp; // Zaman damgasını koru if (isInternetAvailable) { try { await firebaseClient .Child("flashcards") .Child(selectedFlashcard.id) .PutAsync(updatedFlashcard); // Yerel veritabanında da güncelle UpdateFlashcardInLocalDatabase(updatedFlashcard); ClearInputs(); } catch (Exception ex) { MessageBox.Show($"Error updating flashcard: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { // İnternet yoksa yerel veritabanında güncelle ve senkronizasyon için işaretle UpdateFlashcardInLocalDatabase(updatedFlashcard); ClearInputs(); MessageBox.Show("No internet connection. Changes saved locally.", "Offline Mode", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } else { MessageBox.Show("Please select a flashcard to update.", "No Selection", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } /// <summary> /// Deletes selected flashcards from the Firebase database. /// </summary> private async void DeleteData() { if (dataGridView.SelectedRows.Count > 0) { int selectedCount = dataGridView.SelectedRows.Count; var confirmResult = MessageBox.Show($"Are you sure you want to delete {selectedCount} flashcard(s)?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (confirmResult == DialogResult.Yes) { try { var deleteFlashcards = dataGridView.SelectedRows .Cast<DataGridViewRow>() .Select(row => (FlashcardItem)row.DataBoundItem) .ToList(); if (isInternetAvailable) { // Create a list of delete tasks var deleteTasks = deleteFlashcards .Select(flashcard => { return firebaseClient.Child("flashcards").Child(flashcard.id).DeleteAsync(); }) .ToList(); // Execute all delete tasks in parallel await Task.WhenAll(deleteTasks); // Yerel veritabanından sil foreach (var flashcard in deleteFlashcards) { DeleteFlashcardFromLocalDatabase(flashcard); } // Yerel listeden sil allFlashcards.RemoveAll(f => deleteFlashcards.Any(df => df.id == f.id)); // Clear inputs ClearInputs(); } else { // İnternet yoksa yerel veritabanında sil ve senkronizasyon için işaretle foreach (var flashcard in deleteFlashcards) { MarkFlashcardAsDeletedInLocalDatabase(flashcard); } // Yerel listeden sil allFlashcards.RemoveAll(f => deleteFlashcards.Any(df => df.id == f.id)); // Clear inputs ClearInputs(); MessageBox.Show("No internet connection. Changes saved locally.", "Offline Mode", MessageBoxButtons.OK, MessageBoxIcon.Information); } ApplyFiltersAndSort(); MessageBox.Show($"{selectedCount} flashcard(s) deleted successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show($"Error deleting flashcards: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } else { MessageBox.Show("Please select one or more flashcards to delete.", "No Selection", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } private void DeleteFlashcardFromLocalDatabase(FlashcardItem flashcard) { string query = "DELETE FROM flashcards WHERE id = @id;"; SQLiteCommand command = new SQLiteCommand(query, sqliteConnection); command.Parameters.AddWithValue("@id", flashcard.id); command.ExecuteNonQuery(); } private void MarkFlashcardAsDeletedInLocalDatabase(FlashcardItem flashcard) { string query = "UPDATE flashcards SET isDeleted = 1 WHERE id = @id;"; SQLiteCommand command = new SQLiteCommand(query, sqliteConnection); command.Parameters.AddWithValue("@id", flashcard.id); command.ExecuteNonQuery(); } /// <summary> /// Creates a FlashcardItem object from the input fields. /// </summary> private FlashcardItem CreateFlashcardFromInputs() { string title = txtTitle.Text.Trim(); string content = txtContent.Text.Trim(); string backgroundColor = txtBackgroundColor.Text.Trim(); // Kategori belirleme string category; if (!string.IsNullOrWhiteSpace(selectedCustomTag)) { category = selectedCustomTag; } else if (!string.IsNullOrWhiteSpace(txtCategory.Text)) { category = txtCategory.Text.Trim(); } else { category = "uncategorized"; } // Title alanı boş olsa bile veri eklemeye izin veriyoruz return new FlashcardItem { title = title, category = category, content = content, favorite = chkFavorite.Checked, pinned = chkPinned.Checked, timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), backgroundColor = backgroundColor }; } /// <summary> /// Clears the input fields. /// </summary> private void ClearInputs() { txtTitle.Text = ""; txtCategory.Text = ""; txtContent.Text = ""; txtBackgroundColor.Text = ""; chkFavorite.Checked = false; chkPinned.Checked = false; } /// <summary> /// Handles the selection change in the DataGridView to populate the input fields. /// </summary> private void DataGridView_SelectionChanged(object sender, EventArgs e) { if (dataGridView.SelectedRows.Count > 0) { var selectedFlashcard = (FlashcardItem)dataGridView.SelectedRows[0].DataBoundItem; txtTitle.Text = selectedFlashcard.title; txtTitle.ForeColor = Color.Black; txtCategory.Text = selectedFlashcard.category; txtCategory.ForeColor = Color.Black; txtContent.Text = selectedFlashcard.content; txtContent.ForeColor = Color.Black; txtBackgroundColor.Text = selectedFlashcard.backgroundColor; txtBackgroundColor.ForeColor = Color.Black; chkFavorite.Checked = selectedFlashcard.favorite; chkPinned.Checked = selectedFlashcard.pinned; } else { // Seçim yoksa metin kutularını temizle ClearInputs(); } } /// <summary> /// Sets up a real-time listener to Firebase to update data upon changes. /// </summary> private void StartFirebaseRealTimeListener() { firebaseClient .Child("flashcards") .AsObservable<FlashcardItem>() .Subscribe(async d => { if (d.Object == null) return; var flashcard = d.Object; flashcard.id = d.Key; if (d.EventType == FirebaseEventType.InsertOrUpdate) { var existingItem = allFlashcards.FirstOrDefault(f => f.id == flashcard.id); if (existingItem != null) { // Mevcut öğeyi güncelle int index = allFlashcards.IndexOf(existingItem); allFlashcards[index] = flashcard; } else { // Yeni öğe ekle allFlashcards.Add(flashcard); } // Yerel veritabanına kaydet SaveDataToLocalDatabase(new List<FlashcardItem> { flashcard }); } else if (d.EventType == FirebaseEventType.Delete) { var itemToRemove = allFlashcards.FirstOrDefault(f => f.id == d.Key); if (itemToRemove != null) { allFlashcards.Remove(itemToRemove); } // Yerel veritabanından sil DeleteFlashcardFromLocalDatabase(flashcard); } // Arayüzü güncelle Invoke((Action)(() => { ApplyFiltersAndSort(); })); }); } /// <summary> /// Handles the Search button click to apply search criteria. /// </summary> private void SearchData() { string searchText = txtSearch.Text; if (string.IsNullOrWhiteSpace(searchText)) { currentSearchTerm = ""; } else { currentSearchTerm = searchText.ToLower(); } ApplyFiltersAndSort(); } /// <summary> /// Clears the search input and resets search filter. /// </summary> private void ClearSearch() { txtSearch.Text = ""; currentSearchTerm = ""; ApplyFiltersAndSort(); } /// <summary> /// Formats the timestamp column to display human-readable dates. /// </summary> private void DataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (dataGridView.Columns[e.ColumnIndex].Name == "timestamp") { if (e.Value != null && long.TryParse(e.Value.ToString(), out long timestamp)) { DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeMilliseconds(timestamp); DateTime dateTime = dateTimeOffset.ToLocalTime().DateTime; e.Value = dateTime.ToString("yyyy-MM-dd HH:mm:ss"); e.FormattingApplied = true; } } } /// <summary> /// Handles the column header click event to apply sorting. /// </summary> private void DataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) { string columnName = dataGridView.Columns[e.ColumnIndex].Name; // Only allow sorting on 'title' and 'timestamp' columns if (columnName == "title" || columnName == "timestamp") { // Toggle sort direction if the same column is clicked if (currentSortColumn == columnName) { sortAscending = !sortAscending; } else { // Default to ascending if a new column is clicked currentSortColumn = columnName; sortAscending = true; } // Apply sorting and refresh DataGridView ApplyFiltersAndSort(); // Reset sort glyphs on all columns foreach (DataGridViewColumn column in dataGridView.Columns) { column.HeaderCell.SortGlyphDirection = SortOrder.None; } // Set sort glyph on the sorted column dataGridView.Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection = sortAscending ? SortOrder.Ascending : SortOrder.Descending; } } /// <summary> /// Handles CheckedChanged events for filtering CheckBoxes. /// </summary> private void FilterCheckBoxChanged(object sender, EventArgs e) { ApplyFiltersAndSort(); } /// <summary> /// Restores the column visibility state from the stored dictionary. /// </summary> private void RestoreColumnVisibilityState() { foreach (DataGridViewColumn column in dataGridView.Columns) { if (columnVisibilityState.TryGetValue(column.Name, out bool isVisible)) { column.Visible = isVisible; } } } /// <summary> /// Handles MouseDown event on the background panel to clear inputs. /// </summary> private void BackgroundPanel_MouseDown(object sender, MouseEventArgs e) { ClearInputs(); } // Yeni Eklenen Metodlar ve Event Handler'lar private void ClipboardOnMenuItem_Click(object sender, EventArgs e) { StartClipboardMonitoring(); tagCheckedListBox.Visible = true; // Clipboard modu açılırken görünür yap } private void ClipboardOffMenuItem_Click(object sender, EventArgs e) { StopClipboardMonitoring(); tagCheckedListBox.Visible = false; // Clipboard modu kapanırken gizle } private void StartClipboardMonitoring() { clipboardOnMenuItem.Checked = true; clipboardOffMenuItem.Checked = false; // Panodaki mevcut metni alarak lastClipboardText'e atıyoruz if (Clipboard.ContainsText()) { lastClipboardText = Clipboard.GetText(); } else { lastClipboardText = null; } clipboardMonitorTimer.Start(); } private void StopClipboardMonitoring() { clipboardOnMenuItem.Checked = false; clipboardOffMenuItem.Checked = true; clipboardMonitorTimer.Stop(); } private async void ClipboardMonitorTimer_Tick(object sender, EventArgs e) { await CheckClipboard(); } private async Task CheckClipboard() { try { if (Clipboard.ContainsText()) { string clipboardText = Clipboard.GetText(); if (clipboardText != lastClipboardText) { lastClipboardText = clipboardText; // Metni tek satıra dönüştür string singleLineText = clipboardText.Replace(Environment.NewLine, " "); // Kategori belirleme string category; if (!string.IsNullOrWhiteSpace(selectedCustomTag)) { // Eğer Custom Tag altında bir etiket seçildiyse category = selectedCustomTag; } else if (tagCheckedListBox.CheckedItems.Count > 0) { // Eğer tagCheckedListBox'dan bir etiket seçildiyse category = tagCheckedListBox.CheckedItems[0].ToString(); } else { // Hiçbir etiket seçili değilse varsayılan olarak "uncategorized" kullan category = "uncategorized"; } // Yeni FlashcardItem oluştur var newFlashcard = new FlashcardItem { title = "", // Başlık boş category = category, // Belirlenen kategoriyi kullan content = singleLineText, favorite = false, pinned = false, timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), backgroundColor = "" }; if (isInternetAvailable) { // Veritabanına kaydet await SaveFlashcardToFirebase(newFlashcard); } else { // Yerel veritabanına kaydet AddFlashcardToLocalDatabase(newFlashcard); } } } } catch (Exception ex) { // Pano erişiminde hata olursa burası çalışacak // İsterseniz hata mesajı gösterebilirsiniz } } private async Task SaveFlashcardToFirebase(FlashcardItem flashcard) { try { var response = await firebaseClient .Child("flashcards") .PostAsync(flashcard); flashcard.id = response.Key; // Firebase veritabanında id alanını eklemek için güncelleme yapıyoruz await firebaseClient .Child("flashcards") .Child(response.Key) .PutAsync(flashcard); // Yerel veritabanına ekle SaveDataToLocalDatabase(new List<FlashcardItem> { flashcard }); } catch (Exception ex) { // Hata yönetimi } } private void LoadTagsMenuItem_Click(object sender, EventArgs e) { using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog()) { folderBrowserDialog.Description = "Klasör Seçin"; if (folderBrowserDialog.ShowDialog() == DialogResult.OK) { string selectedFolder = folderBrowserDialog.SelectedPath; // Klasördeki ve alt klasörlerdeki PDF dosyalarını al string[] pdfFiles = Directory.GetFiles(selectedFolder, "*.pdf", SearchOption.AllDirectories); // Dosya isimlerini al (uzantısız) List<string> tags = pdfFiles.Select(file => Path.GetFileNameWithoutExtension(file)).ToList(); // CheckedListBox'ı güncelle tagCheckedListBox.Items.Clear(); foreach (var tag in tags) { tagCheckedListBox.Items.Add(tag); } } } } private void TagCheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e) { // İşaretlenen diğer öğeleri kaldır, sadece bir tanesi seçili olsun if (e.NewValue == CheckState.Checked) { for (int i = 0; i < tagCheckedListBox.Items.Count; i++) { if (i != e.Index) { tagCheckedListBox.SetItemChecked(i, false); } } selectedTag = tagCheckedListBox.Items[e.Index].ToString(); // Custom Tag seçimini kaldır foreach (ToolStripMenuItem item in customTagMenuItem.DropDownItems) { if (item != addTagMenuItem && item != manageTagsMenuItem) { item.Checked = false; } } selectedCustomTag = null; } else { selectedTag = "uncategorized"; } } private void AddTagMenuItem_Click(object sender, EventArgs e) { using (Form inputDialog = new Form()) { inputDialog.Width = 300; inputDialog.Height = 150; inputDialog.Text = "Add Custom Tag"; inputDialog.FormBorderStyle = FormBorderStyle.FixedDialog; inputDialog.StartPosition = FormStartPosition.CenterParent; // Ekranın ortasında açılsın inputDialog.MinimizeBox = false; inputDialog.MaximizeBox = false; inputDialog.AcceptButton = null; // We'll set the accept button later Label lblTag = new Label() { Left = 20, Top = 20, Text = "Tag:", AutoSize = false, // Otomatik boyutlandırmayı kapatıyoruz Width = 40, // Daha dar bir genişlik veriyoruz TextAlign = ContentAlignment.TopCenter // Metni sola hizalıyoruz }; TextBox txtTag = new TextBox() { Left = lblTag.Left + lblTag.Width + 5, Top = lblTag.Top - 3, Width = 150 }; // Metin kutusunu etikete yaklaştırıyoruz Button btnOK = new Button() { Text = "OK", Left = 50, Width = 100, Top = txtTag.Top + txtTag.Height + 20 }; Button btnCancel = new Button() { Text = "Cancel", Left = 150, Width = 100, Top = txtTag.Top + txtTag.Height + 20 }; btnOK.Click += (s, args) => { string newTag = txtTag.Text.Trim(); if (!string.IsNullOrEmpty(newTag)) { // Add tag to customTags list customTags.Add(newTag); // Save the custom tags to the file SaveCustomTags(); // Eklendi // Add tag as submenu item under customTagMenuItem AddCustomTagMenuItem(newTag); // Close the dialog inputDialog.DialogResult = DialogResult.OK; inputDialog.Close(); } else { MessageBox.Show("Tag cannot be empty.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }; btnCancel.Click += (s, args) => { inputDialog.DialogResult = DialogResult.Cancel; inputDialog.Close(); }; inputDialog.Controls.Add(lblTag); inputDialog.Controls.Add(txtTag); inputDialog.Controls.Add(btnOK); inputDialog.Controls.Add(btnCancel); inputDialog.AcceptButton = btnOK; inputDialog.ShowDialog(); // Show as modal dialog } } private void AddCustomTagMenuItem(string tag) { ToolStripMenuItem tagMenuItem = new ToolStripMenuItem(tag); tagMenuItem.Checked = false; tagMenuItem.CheckOnClick = true; tagMenuItem.Click += TagMenuItem_Click; // Insert the tag menu item after the "Manage Tags" menu item int insertIndex = customTagMenuItem.DropDownItems.IndexOf(manageTagsMenuItem) + 1; customTagMenuItem.DropDownItems.Insert(insertIndex, tagMenuItem); } private void TagMenuItem_Click(object sender, EventArgs e) { // When a tag is clicked, uncheck other tags if (sender is ToolStripMenuItem clickedItem) { foreach (ToolStripMenuItem item in customTagMenuItem.DropDownItems) { if (item != clickedItem && item != addTagMenuItem && item != manageTagsMenuItem) { item.Checked = false; } } // Update the selectedCustomTag if (clickedItem.Checked) { selectedCustomTag = clickedItem.Text; // CheckedListBox'daki tüm seçimleri kaldır tagCheckedListBox.ItemCheck -= TagCheckedListBox_ItemCheck; // Event geçici olarak devre dışı for (int i = 0; i < tagCheckedListBox.Items.Count; i++) { tagCheckedListBox.SetItemChecked(i, false); } tagCheckedListBox.ItemCheck += TagCheckedListBox_ItemCheck; // Event tekrar aktif selectedTag = "uncategorized"; } else { selectedCustomTag = null; } } } // Event handler for "Manage Tags" menu item click private void ManageTagsMenuItem_Click(object sender, EventArgs e) { // Open a dialog to manage tags using (Form manageTagsForm = new Form()) { manageTagsForm.Width = 300; manageTagsForm.Height = 400; manageTagsForm.Text = "Manage Tags"; manageTagsForm.FormBorderStyle = FormBorderStyle.FixedDialog; manageTagsForm.StartPosition = FormStartPosition.CenterParent; // Ekranın ortasında açılsın manageTagsForm.MinimizeBox = false; manageTagsForm.MaximizeBox = false; ListBox lstTags = new ListBox() { Left = 10, Top = 10, Width = 260, Height = 300 }; lstTags.DataSource = null; lstTags.DataSource = new List<string>(customTags); Button btnEdit = new Button() { Text = "Edit", Left = 10, Width = 80, Top = lstTags.Bottom + 10 }; Button btnDelete = new Button() { Text = "Delete", Left = 100, Width = 80, Top = lstTags.Bottom + 10 }; Button btnClose = new Button() { Text = "Close", Left = 190, Width = 80, Top = lstTags.Bottom + 10 }; btnEdit.Click += (s, args) => { if (lstTags.SelectedItem != null) { string selectedTag = lstTags.SelectedItem.ToString(); using (Form editTagForm = new Form()) { editTagForm.Width = 300; editTagForm.Height = 150; editTagForm.Text = "Edit Tag"; editTagForm.FormBorderStyle = FormBorderStyle.FixedDialog; editTagForm.StartPosition = FormStartPosition.CenterParent; // Ekranın ortasında açılsın editTagForm.MinimizeBox = false; editTagForm.MaximizeBox = false; Label lblTag = new Label() { Left = 10, Top = 20, Text = "Tag:", AutoSize = false, Width = 40, // Daha küçük bir genişlik veriyoruz TextAlign = ContentAlignment.TopCenter }; TextBox txtTag = new TextBox() { Left = lblTag.Left + lblTag.Width + 5, Top = lblTag.Top - 3, Width = 150 }; // Metin kutusunu etikete yaklaştırıyoruz Button btnOK = new Button() { Text = "OK", Left = 50, Width = 100, Top = txtTag.Top + txtTag.Height + 20 }; Button btnCancel = new Button() { Text = "Cancel", Left = 150, Width = 100, Top = txtTag.Top + txtTag.Height + 20 }; btnOK.Click += (s2, args2) => { string newTag = txtTag.Text.Trim(); if (!string.IsNullOrEmpty(newTag)) { // Update the tag in customTags list int index = customTags.IndexOf(selectedTag); customTags[index] = newTag; // Save the custom tags to the file SaveCustomTags(); // Eklendi // Update the menu item text foreach (ToolStripMenuItem item in customTagMenuItem.DropDownItems) { if (item.Text == selectedTag) { item.Text = newTag; break; } } // Update selectedCustomTag if this tag was the selected one if (selectedCustomTag == selectedTag) { selectedCustomTag = newTag; } // Update the listbox lstTags.DataSource = null; lstTags.DataSource = new List<string>(customTags); // Close the edit form editTagForm.DialogResult = DialogResult.OK; editTagForm.Close(); } else { MessageBox.Show("Tag cannot be empty.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }; btnCancel.Click += (s2, args2) => { editTagForm.DialogResult = DialogResult.Cancel; editTagForm.Close(); }; editTagForm.Controls.Add(lblTag); editTagForm.Controls.Add(txtTag); editTagForm.Controls.Add(btnOK); editTagForm.Controls.Add(btnCancel); editTagForm.AcceptButton = btnOK; editTagForm.ShowDialog(); } } else { MessageBox.Show("Please select a tag to edit.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } }; btnDelete.Click += (s, args) => { if (lstTags.SelectedItem != null) { string selectedTag = lstTags.SelectedItem.ToString(); var confirmResult = MessageBox.Show($"Are you sure you want to delete tag '{selectedTag}'?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (confirmResult == DialogResult.Yes) { // Remove from customTags list customTags.Remove(selectedTag); // Save the custom tags to the file SaveCustomTags(); // Eklendi // Remove from menu items ToolStripMenuItem itemToRemove = null; foreach (ToolStripMenuItem item in customTagMenuItem.DropDownItems) { if (item.Text == selectedTag) { itemToRemove = item; break; } } if (itemToRemove != null) { customTagMenuItem.DropDownItems.Remove(itemToRemove); } // Clear selectedCustomTag if it was deleted if (selectedCustomTag == selectedTag) { selectedCustomTag = null; } // Update the listbox lstTags.DataSource = null; lstTags.DataSource = new List<string>(customTags); } } else { MessageBox.Show("Please select a tag to delete.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } }; btnClose.Click += (s, args) => { manageTagsForm.Close(); }; manageTagsForm.Controls.Add(lstTags); manageTagsForm.Controls.Add(btnEdit); manageTagsForm.Controls.Add(btnDelete); manageTagsForm.Controls.Add(btnClose); manageTagsForm.ShowDialog(); } } // Custom Tags'i kaydetme metodu private void SaveCustomTags() { try { string json = JsonConvert.SerializeObject(customTags); File.WriteAllText(customTagsFilePath, json); } catch (Exception ex) { MessageBox.Show($"Error saving custom tags: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } // Custom Tags'i yükleme metodu private void LoadCustomTags() { if (File.Exists(customTagsFilePath)) { try { string json = File.ReadAllText(customTagsFilePath); customTags = JsonConvert.DeserializeObject<List<string>>(json); // customTagMenuItem içindeki mevcut özel etiketleri temizleyelim (Add Tag ve Manage Tags hariç) for (int i = customTagMenuItem.DropDownItems.Count - 1; i >= 0; i--) { var item = customTagMenuItem.DropDownItems[i]; if (item != addTagMenuItem && item != manageTagsMenuItem) { customTagMenuItem.DropDownItems.RemoveAt(i); } } // customTags listesindeki etiketleri menüye ekleyelim foreach (var tag in customTags) { AddCustomTagMenuItem(tag); } } catch (Exception ex) { MessageBox.Show($"Error loading custom tags: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } /// <summary> /// İnternet bağlantısının olup olmadığını kontrol eder. /// </summary> private bool CheckInternetConnectivity() { try { using (var client = new System.Net.WebClient()) using (client.OpenRead("http://google.com/generate_204")) return true; } catch { return false; } } /// <summary> /// Yerel veritabanındaki değişiklikleri Firebase'e senkronize eder. /// </summary> private async Task SyncLocalChangesToFirebase() { // Güncellenmiş kayıtları al string query = "SELECT * FROM flashcards WHERE isUpdated = 1 AND isDeleted = 0;"; SQLiteCommand command = new SQLiteCommand(query, sqliteConnection); SQLiteDataReader reader = command.ExecuteReader(); var updatedFlashcards = new List<FlashcardItem>(); while (reader.Read()) { var flashcard = new FlashcardItem { id = reader["id"].ToString(), category = reader["category"].ToString(), title = reader["title"].ToString(), content = reader["content"].ToString(), favorite = Convert.ToInt32(reader["favorite"]) == 1, pinned = Convert.ToInt32(reader["pinned"]) == 1, timestamp = Convert.ToInt64(reader["timestamp"]), backgroundColor = reader["backgroundColor"].ToString() }; updatedFlashcards.Add(flashcard); } // Güncellenmiş kayıtları Firebase'e gönder foreach (var flashcard in updatedFlashcards) { if (flashcard.id.StartsWith("local_")) { // Yerel olarak oluşturulmuş bir kayıt, yeni ekle var newFlashcard = new FlashcardItem { category = flashcard.category, title = flashcard.title, content = flashcard.content, favorite = flashcard.favorite, pinned = flashcard.pinned, timestamp = flashcard.timestamp, backgroundColor = flashcard.backgroundColor }; var response = await firebaseClient .Child("flashcards") .PostAsync(newFlashcard); newFlashcard.id = response.Key; // Firebase id ile yerel veritabanını güncelle string updateQuery = @" UPDATE flashcards SET id = @newId, isUpdated = 0 WHERE id = @oldId; "; SQLiteCommand updateCommand = new SQLiteCommand(updateQuery, sqliteConnection); updateCommand.Parameters.AddWithValue("@newId", newFlashcard.id); updateCommand.Parameters.AddWithValue("@oldId", flashcard.id); updateCommand.ExecuteNonQuery(); } else { // Mevcut bir kaydı güncelle await firebaseClient .Child("flashcards") .Child(flashcard.id) .PutAsync(flashcard); // isUpdated flag'ini sıfırla string updateQuery = "UPDATE flashcards SET isUpdated = 0 WHERE id = @id;"; SQLiteCommand updateCommand = new SQLiteCommand(updateQuery, sqliteConnection); updateCommand.Parameters.AddWithValue("@id", flashcard.id); updateCommand.ExecuteNonQuery(); } } // Silinmiş kayıtları al query = "SELECT * FROM flashcards WHERE isDeleted = 1;"; command = new SQLiteCommand(query, sqliteConnection); reader = command.ExecuteReader(); var deletedFlashcards = new List<FlashcardItem>(); while (reader.Read()) { var flashcard = new FlashcardItem { id = reader["id"].ToString() }; deletedFlashcards.Add(flashcard); } // Silinmiş kayıtları Firebase'den sil foreach (var flashcard in deletedFlashcards) { await firebaseClient .Child("flashcards") .Child(flashcard.id) .DeleteAsync(); // Yerel veritabanından tamamen sil DeleteFlashcardFromLocalDatabase(flashcard); } } } /// <summary> /// Represents a Flashcard item. /// </summary> public class FlashcardItem { public string id { get; set; } public string category { get; set; } public string title { get; set; } public string content { get; set; } public bool favorite { get; set; } public bool pinned { get; set; } public long timestamp { get; set; } public string backgroundColor { get; set; } }}
- Assistant
Sorununuz muhtemelen
FlashcardItemsınıfındaisDeletedözelliğinin eksik olmasından kaynaklanmaktadır. Firebase'den verileri alırken, silinen kayıtları doğru şekilde filtreleyemediğiniz için veriler görüntülenmiyor olabilir. Bu durumu düzeltmek için aşağıdaki adımları izleyebilirsiniz:-
FlashcardItemSınıfınaisDeletedÖzelliği Ekleyin:Firebase'de
isDeletedözelliğini kullanarak silinen kayıtları işaretliyorsunuz. Bu özelliğiFlashcardItemsınıfınıza ekleyerek bu kayıtları doğru şekilde filtreleyebilirsiniz. -
LoadDataFromFirebaseAsyncMetodunu Güncelleyin:Firebase'den verileri çekerken
isDeletedözelliğini kontrol edin ve sadece silinmemiş kayıtlarıallFlashcardslistesine ekleyin.
Aşağıda, bu düzeltmeleri içeren tam kod parçasını bulabilirsiniz:
using System; using System.Windows.Forms; using Firebase.Database; using Firebase.Database.Query; using FirebaseAdmin; using Google.Apis.Auth.OAuth2; using System.Threading.Tasks; using System.Linq; using System.Drawing; using System.Collections.Generic; using System.IO; using System.Text; using Newtonsoft.Json; // JSON işlemleri için eklendi using Firebase.Database.Streaming; // FirebaseEventType için eklendi using System.Data.SQLite; // SQLite için eklendi using System.Net.NetworkInformation; // İnternet bağlantısı kontrolü için eklendi namespace Flashcard { public partial class MainForm : Form { // Firebase client private FirebaseClient firebaseClient; // UI Controls private DataGridView dataGridView; private TextBox txtTitle, txtCategory, txtContent, txtBackgroundColor, txtSearch; private CheckBox chkFavorite, chkPinned; private CheckBox chkFilterFavorites, chkFilterPinned; // Filtering CheckBoxes private Button btnAdd, btnUpdate, btnDelete, btnSearch, btnClear; private StatusStrip statusStrip; private ToolStripStatusLabel statusLabel; // Yeni Eklenen Kontroller private MenuStrip menuStrip; // Menü Çubuğu private ToolStripMenuItem fileMenuItem, themeMenuItem; // Menü Öğeleri private ToolStripMenuItem saveAsMenuItem, lightModeMenuItem, darkModeMenuItem; // Alt Menü Öğeleri private ToolStripMenuItem clipboardModeMenuItem, clipboardOnMenuItem, clipboardOffMenuItem; // Clipboard Mode Menü Öğeleri private ToolStripMenuItem loadTagsMenuItem; // "Load Tags" Menü Öğesi // Custom Tag Menu Items private ToolStripMenuItem customTagMenuItem; // Custom Tag Menu Item private ToolStripMenuItem addTagMenuItem; // Alt Menü öğesi private ToolStripMenuItem manageTagsMenuItem; // Alt Menü öğesi private List<string> customTags = new List<string>(); // Custom Tag listesi private string selectedCustomTag = null; // Seçilen Custom Tag // customTags'i kaydetmek için dosya yolu private readonly string customTagsFilePath = "custom_tags.json"; // Eklendi // ContextMenuStrip for column visibility private ContextMenuStrip columnContextMenu; // Sorting state private string currentSortColumn = "timestamp"; // Varsayılan olarak timestamp'e göre sıralama private bool sortAscending = false; // En yeni veriler en üstte olacak şekilde // Data storage private List<FlashcardItem> allFlashcards = new List<FlashcardItem>(); // Current search term private string currentSearchTerm = ""; // Dictionary to store column visibility state private Dictionary<string, bool> columnVisibilityState = new Dictionary<string, bool>(); // Clipboard monitoring private Timer clipboardMonitorTimer; private string lastClipboardText; // Yeni Eklenen Değişkenler private CheckedListBox tagCheckedListBox; // Etiketlerin gösterileceği CheckedListBox private string selectedTag = "uncategorized"; // Seçilen etiket // SQLite bağlantısı için değişken private SQLiteConnection sqliteConnection; // İnternet bağlantısı durumu private bool isInternetAvailable = true; public MainForm() { InitializeComponent(); InitializeFirebase(); InitializeLocalDatabase(); // Yerel veritabanını başlat InitializeUI(); } /// <summary> /// Initializes Firebase connection using the provided JSON credentials. /// </summary> private void InitializeFirebase() { var path = "flashcard-e7f47-firebase-adminsdk-om656-e70a5506c2.json"; var credential = GoogleCredential.FromFile(path); // Check if FirebaseApp is already created to prevent duplicate initialization if (FirebaseApp.DefaultInstance == null) { FirebaseApp.Create(new AppOptions { Credential = credential }); } firebaseClient = new FirebaseClient("https://flashcard-e7f47-default-rtdb.europe-west1.firebasedatabase.app/"); // Firebase Realtime Database URL'niz } /// <summary> /// Yerel SQLite veritabanını başlatır ve gerekli tabloyu oluşturur. /// </summary> private void InitializeLocalDatabase() { string databaseFile = "local_flashcards.db"; bool databaseExists = File.Exists(databaseFile); sqliteConnection = new SQLiteConnection($"Data Source={databaseFile};Version=3;"); sqliteConnection.Open(); if (!databaseExists) { string createTableQuery = @" CREATE TABLE flashcards ( id TEXT PRIMARY KEY, category TEXT, title TEXT, content TEXT, favorite INTEGER, pinned INTEGER, timestamp INTEGER, backgroundColor TEXT, isUpdated INTEGER, isDeleted INTEGER ); "; SQLiteCommand command = new SQLiteCommand(createTableQuery, sqliteConnection); command.ExecuteNonQuery(); } } /// <summary> /// Sets up the user interface components. /// </summary> private void InitializeUI() { this.Size = new Size(1000, 800); this.Text = "Flashcard Application"; this.StartPosition = FormStartPosition.CenterScreen; // MainForm ekranın ortasında açılsın // MenuStrip Oluşturma menuStrip = new MenuStrip { Dock = DockStyle.Top // MenuStrip'i üstte konumlandır }; // File Menu fileMenuItem = new ToolStripMenuItem("File"); saveAsMenuItem = new ToolStripMenuItem("Save As"); saveAsMenuItem.Click += SaveAsMenuItem_Click; // Event Handler fileMenuItem.DropDownItems.Add(saveAsMenuItem); // Theme Menu themeMenuItem = new ToolStripMenuItem("Theme"); lightModeMenuItem = new ToolStripMenuItem("Light Mode"); darkModeMenuItem = new ToolStripMenuItem("Dark Mode"); lightModeMenuItem.Click += LightModeMenuItem_Click; // Event Handler darkModeMenuItem.Click += DarkModeMenuItem_Click; // Event Handler themeMenuItem.DropDownItems.Add(lightModeMenuItem); themeMenuItem.DropDownItems.Add(darkModeMenuItem); // Clipboard Mode Menu clipboardModeMenuItem = new ToolStripMenuItem("Clipboard Mode"); clipboardOnMenuItem = new ToolStripMenuItem("On"); clipboardOffMenuItem = new ToolStripMenuItem("Off"); // CheckOnClick özelliğini kullanarak tıklanınca işaretlenmesini sağlıyoruz clipboardOnMenuItem.CheckOnClick = true; clipboardOffMenuItem.CheckOnClick = true; clipboardOffMenuItem.Checked = true; // Başlangıçta "Off" seçili clipboardOnMenuItem.Click += ClipboardOnMenuItem_Click; clipboardOffMenuItem.Click += ClipboardOffMenuItem_Click; clipboardModeMenuItem.DropDownItems.Add(clipboardOnMenuItem); clipboardModeMenuItem.DropDownItems.Add(clipboardOffMenuItem); // Load Tags Menu loadTagsMenuItem = new ToolStripMenuItem("Load Tags"); loadTagsMenuItem.Click += LoadTagsMenuItem_Click; // Event Handler // Custom Tag Menu customTagMenuItem = new ToolStripMenuItem("Custom Tag"); addTagMenuItem = new ToolStripMenuItem("Add Tag"); addTagMenuItem.Click += AddTagMenuItem_Click; // Event Handler // Declare manageTagsMenuItem at class level and initialize manageTagsMenuItem = new ToolStripMenuItem("Manage Tags"); manageTagsMenuItem.Click += ManageTagsMenuItem_Click; // Event Handler customTagMenuItem.DropDownItems.Add(addTagMenuItem); customTagMenuItem.DropDownItems.Add(manageTagsMenuItem); // MenuStrip'e Menüleri Ekleme menuStrip.Items.Add(fileMenuItem); menuStrip.Items.Add(themeMenuItem); menuStrip.Items.Add(clipboardModeMenuItem); menuStrip.Items.Add(loadTagsMenuItem); // "Load Tags" Menü Öğesini Ekledik menuStrip.Items.Add(customTagMenuItem); // "Custom Tag" Menü Öğesini Ekledik // Form'a MenuStrip'i Ekleme this.MainMenuStrip = menuStrip; Controls.Add(menuStrip); // MenuStrip'i ilk olarak ekliyoruz // Clipboard izleme için Timer clipboardMonitorTimer = new Timer { Interval = 1000 }; // Her saniyede bir kontrol eder clipboardMonitorTimer.Tick += ClipboardMonitorTimer_Tick; // Arama Metin Kutusu ve Butonları txtSearch = CreateTextBox("", 10, menuStrip.Bottom + 6, 200); // Placeholder'ı boş bıraktık //txtSearch.PlaceholderText = "Search..."; // .NET 6 ve üzeri için btnSearch = CreateButton("Search", txtSearch.Right + 6, txtSearch.Top - 2, 80); btnClear = CreateButton("Clear", btnSearch.Right + 6, txtSearch.Top - 2, 80); // Filtreleme CheckBox'ları chkFilterFavorites = new CheckBox { Text = "Show Favorites", Top = txtSearch.Bottom + 6, Left = txtSearch.Left, Width = 120 }; chkFilterFavorites.CheckedChanged += FilterCheckBoxChanged; Controls.Add(chkFilterFavorites); chkFilterPinned = new CheckBox { Text = "Show Pinned", Top = txtSearch.Bottom + 6, Left = chkFilterFavorites.Right + 10, Width = 120 }; chkFilterPinned.CheckedChanged += FilterCheckBoxChanged; Controls.Add(chkFilterPinned); // Initialize DataGridView dataGridView = new DataGridView { Height = 400, Left = 0, Top = chkFilterFavorites.Bottom + 10, // DataGridView'i filtrelerin altına yerleştir Width = this.ClientSize.Width, // Tam pencere genişliğini kapla Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right, AllowUserToAddRows = false, AllowUserToDeleteRows = false, ReadOnly = false, SelectionMode = DataGridViewSelectionMode.FullRowSelect, MultiSelect = true, AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill, ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize }; dataGridView.SelectionChanged += DataGridView_SelectionChanged; dataGridView.CellContentClick += DataGridView_CellContentClick; dataGridView.CellFormatting += DataGridView_CellFormatting; dataGridView.ColumnHeaderMouseClick += DataGridView_ColumnHeaderMouseClick; // For sorting Controls.Add(dataGridView); // StatusStrip statusStrip = new StatusStrip(); statusLabel = new ToolStripStatusLabel(); statusStrip.Items.Add(statusLabel); statusStrip.Dock = DockStyle.Bottom; // StatusStrip'i pencerenin en altına sabitle Controls.Add(statusStrip); // Tag CheckedListBox Oluşturma tagCheckedListBox = new CheckedListBox { Width = 400, CheckOnClick = true, Left = this.ClientSize.Width - 400, // Pencerenin en sağ kenarına bitişik hizala Top = dataGridView.Bottom, // DataGridView'in hemen altına yerleştir Height = statusStrip.Top - dataGridView.Bottom + 10, // DataGridView'den StatusStrip'e kadar olan yüksekliği ayarla Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right, Visible = false // Başlangıçta gizli }; tagCheckedListBox.ItemCheck += TagCheckedListBox_ItemCheck; Controls.Add(tagCheckedListBox); // Kontroller arası standart Windows Forms arayüzündeki gibi mesafeler int labelLeft = 10; int textBoxLeft = 150; int startTop = dataGridView.Bottom + 6; // DataGridView'in hemen altından başla int textBoxWidth = 300; int textBoxHeight = 25; int verticalSpacing = 6; // Kontroller arası boşluk int currentTop = dataGridView.Bottom + 6; // Kontrolleri DataGridView'in altına yerleştir // Etiketleri ve Metin Kutularını Oluştur // Category CreateLabel("Category:", labelLeft, currentTop); txtCategory = CreateTextBox("", textBoxLeft, currentTop, textBoxWidth); // Placeholder'ı boş bıraktık currentTop += textBoxHeight + verticalSpacing; // Title CreateLabel("Title:", labelLeft, currentTop); txtTitle = CreateTextBox("", textBoxLeft, currentTop, textBoxWidth); // Placeholder'ı boş bıraktık currentTop += textBoxHeight + verticalSpacing; // Content CreateLabel("Content:", labelLeft, currentTop); txtContent = CreateTextBox("", textBoxLeft, currentTop, textBoxWidth); // Placeholder'ı boş bıraktık currentTop += textBoxHeight + verticalSpacing; // Background Color CreateLabel("Background Color:", labelLeft, currentTop); txtBackgroundColor = CreateTextBox("", textBoxLeft, currentTop, textBoxWidth); // Placeholder'ı boş bıraktık currentTop += textBoxHeight + verticalSpacing; // İşlevsel CheckBox'lar chkFavorite = new CheckBox { Text = "Favorite", Top = currentTop, Left = textBoxLeft, Width = 80 }; Controls.Add(chkFavorite); chkPinned = new CheckBox { Text = "Pinned", Top = currentTop, Left = textBoxLeft + 90, Width = 80 }; Controls.Add(chkPinned); currentTop += chkFavorite.Height + verticalSpacing; // Butonları Yatay Olarak Hizala int buttonTop = currentTop + 10; // Biraz boşluk ekle int buttonLeft = labelLeft; btnAdd = CreateButton("Add", buttonLeft, buttonTop, 100); buttonLeft += 110; btnUpdate = CreateButton("Update", buttonLeft, buttonTop, 100); buttonLeft += 110; btnDelete = CreateButton("Delete", buttonLeft, buttonTop, 100); // ContextMenuStrip for column visibility columnContextMenu = new ContextMenuStrip(); // Not: Kolon bilgisi mevcut olduktan sonra ContextMenuStrip'i dolduracağız // Assign the MouseUp event to show the ContextMenuStrip on right-click dataGridView.MouseUp += DataGridView_MouseUp; // Event Handler for Form Load this.Load += MainForm_Load; // Formun MouseDown olayını abone olma this.MouseDown += MainForm_MouseDown; // Tüm kontrollerin MouseDown olaylarını abone et RegisterMouseDownEvent(this); } private void MainForm_MouseDown(object sender, MouseEventArgs e) { // Fare imlecinin altındaki kontrolü al Control clickedControl = this.GetChildAtPoint(e.Location); // Eğer tıklanan kontrol null ise veya formun kendisiyse, metin kutularını sıfırla if (clickedControl == null || clickedControl == this) { ClearInputs(); } } private void RegisterMouseDownEvent(Control parent) { foreach (Control control in parent.Controls) { control.MouseDown += Control_MouseDown; if (control.HasChildren) { RegisterMouseDownEvent(control); } } } private void Control_MouseDown(object sender, MouseEventArgs e) { // Kontrolün MouseDown olayını formun MouseDown olayına yönlendir MainForm_MouseDown(sender, e); } // Yeni bir CreateLabel metodu ekliyoruz private Label CreateLabel(string text, int left, int top) { Label label = new Label { Text = text, Left = left, Top = top, AutoSize = true }; Controls.Add(label); return label; } /// <summary> /// Handles the Form Load event to initially load data and start real-time listener. /// </summary> private async void MainForm_Load(object sender, EventArgs e) { LoadCustomTags(); // Eklendi // Varsayılan sıralamayı ayarladık currentSortColumn = "timestamp"; sortAscending = false; // İnternet bağlantısını kontrol et isInternetAvailable = CheckInternetConnectivity(); if (isInternetAvailable) { await SyncLocalChangesToFirebase(); // Yerel değişiklikleri Firebase'e senkronize et await LoadDataFromFirebaseAsync(); // Firebase'den verileri çek StartFirebaseRealTimeListener(); // Gerçek zamanlı dinleyiciyi başlat } else { LoadDataFromLocalDatabase(); // Yerel veritabanından verileri yükle MessageBox.Show("No internet connection. Loaded data from local storage.", "Offline Mode", MessageBoxButtons.OK, MessageBoxIcon.Information); } } /// <summary> /// Creates a TextBox. /// </summary> private TextBox CreateTextBox(string text, int left, int top, int width) { TextBox textBox = new TextBox { Left = left, Top = top, Width = width, Text = text, ForeColor = Color.Black }; Controls.Add(textBox); return textBox; } /// <summary> /// Creates a Button with specified properties and assigns click event. /// </summary> private Button CreateButton(string text, int left, int top, int width) { Button button = new Button { Text = text, Left = left, Top = top, Width = width }; button.Click += (sender, e) => ButtonClick(text); Controls.Add(button); return button; } /// <summary> /// Handles button click events by identifying the button text. /// </summary> private void ButtonClick(string buttonText) { switch (buttonText) { case "Add": AddData(); break; case "Update": UpdateData(); break; case "Delete": DeleteData(); break; case "Search": SearchData(); break; case "Clear": ClearSearch(); break; } } /// <summary> /// Handles the "Save As" menu item click to save data in selected format. /// </summary> private void SaveAsMenuItem_Click(object sender, EventArgs e) { SaveFileDialog saveFileDialog = new SaveFileDialog { Filter = "CSV files (*.csv)|*.csv|TSV files (*.tsv)|*.tsv|Text files (*.txt)|*.txt", Title = "Save As" }; if (saveFileDialog.ShowDialog() == DialogResult.OK) { try { string extension = Path.GetExtension(saveFileDialog.FileName).ToLower(); // DataGridView'den verileri al var data = (List<FlashcardItem>)dataGridView.DataSource; // Verileri seçilen formata göre kaydet switch (extension) { case ".csv": SaveDataAsCsv(data, saveFileDialog.FileName, ','); break; case ".tsv": SaveDataAsCsv(data, saveFileDialog.FileName, '\t'); break; case ".txt": SaveDataAsTxt(data, saveFileDialog.FileName); break; default: MessageBox.Show("Unsupported file format.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); break; } MessageBox.Show("Data saved successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show($"Error saving data: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } /// <summary> /// Saves data as CSV or TSV file. /// </summary> private void SaveDataAsCsv(List<FlashcardItem> data, string filePath, char delimiter) { StringBuilder sb = new StringBuilder(); // Başlık satırı sb.AppendLine(string.Join(delimiter.ToString(), "Category", "Title", "Content", "Favorite", "Pinned", "Timestamp", "BackgroundColor")); foreach (var item in data) { string[] fields = new string[] { EscapeCsvField(item.category), EscapeCsvField(item.title), EscapeCsvField(item.content), item.favorite.ToString(), item.pinned.ToString(), item.timestamp.ToString(), EscapeCsvField(item.backgroundColor) }; sb.AppendLine(string.Join(delimiter.ToString(), fields)); } File.WriteAllText(filePath, sb.ToString(), Encoding.UTF8); } /// <summary> /// Escapes CSV fields containing delimiter or quotes. /// </summary> private string EscapeCsvField(string field) { if (field.Contains(",") || field.Contains("\"") || field.Contains("\r") || field.Contains("\n")) { return $"\"{field.Replace("\"", "\"\"")}\""; } else { return field; } } /// <summary> /// Saves data as TXT file. /// </summary> private void SaveDataAsTxt(List<FlashcardItem> data, string filePath) { StringBuilder sb = new StringBuilder(); foreach (var item in data) { sb.AppendLine($"Category: {item.category}"); sb.AppendLine($"Title: {item.title}"); sb.AppendLine($"Content: {item.content}"); sb.AppendLine($"Favorite: {item.favorite}"); sb.AppendLine($"Pinned: {item.pinned}"); sb.AppendLine($"Timestamp: {item.timestamp}"); sb.AppendLine($"BackgroundColor: {item.backgroundColor}"); sb.AppendLine(new string('-', 50)); } File.WriteAllText(filePath, sb.ToString(), Encoding.UTF8); } /// <summary> /// Handles the Light Mode menu item click. /// </summary> private void LightModeMenuItem_Click(object sender, EventArgs e) { SetTheme(Color.White, Color.Black, false); } /// <summary> /// Handles the Dark Mode menu item click. /// </summary> private void DarkModeMenuItem_Click(object sender, EventArgs e) { SetTheme(Color.FromArgb(45, 45, 48), Color.White, true); } /// <summary> /// Sets the application's theme colors. /// </summary> private void SetTheme(Color backColor, Color foreColor, bool isDarkMode) { this.BackColor = backColor; this.ForeColor = foreColor; foreach (Control control in this.Controls) { SetControlTheme(control, backColor, foreColor); } // DataGridView renklerini ayarla dataGridView.BackgroundColor = backColor; dataGridView.DefaultCellStyle.BackColor = backColor; dataGridView.DefaultCellStyle.ForeColor = foreColor; // Dark mode ise DataGridView kenarlarını, sütun başlıklarını ve yan seçim alanını portakal rengi yap if (isDarkMode) { dataGridView.GridColor = Color.Orange; dataGridView.EnableHeadersVisualStyles = false; dataGridView.ColumnHeadersDefaultCellStyle.BackColor = Color.Orange; dataGridView.ColumnHeadersDefaultCellStyle.ForeColor = Color.Black; dataGridView.RowHeadersDefaultCellStyle.BackColor = Color.Orange; dataGridView.RowHeadersDefaultCellStyle.ForeColor = Color.Black; // Satır yüksekliğini ve yazı boyutunu ayarla dataGridView.RowTemplate.Height = 30; // Satır yüksekliği dataGridView.DefaultCellStyle.Font = new Font("Arial", 12, FontStyle.Regular); // Yazı boyutu // Satır çizgilerini kaldır dataGridView.CellBorderStyle = DataGridViewCellBorderStyle.None; } else { dataGridView.EnableHeadersVisualStyles = true; dataGridView.GridColor = foreColor; dataGridView.ColumnHeadersDefaultCellStyle.BackColor = backColor; dataGridView.ColumnHeadersDefaultCellStyle.ForeColor = foreColor; dataGridView.RowHeadersDefaultCellStyle.BackColor = backColor; dataGridView.RowHeadersDefaultCellStyle.ForeColor = foreColor; } } /// <summary> /// Recursively sets theme colors for controls. /// </summary> private void SetControlTheme(Control control, Color backColor, Color foreColor) { control.BackColor = backColor; control.ForeColor = foreColor; if (control.HasChildren) { foreach (Control child in control.Controls) { SetControlTheme(child, backColor, foreColor); } } } /// <summary> /// Handles CellContentClick events for CheckBox columns to toggle favorite and pinned statuses. /// </summary> private void DataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex >= 0 && (dataGridView.Columns[e.ColumnIndex].Name == "favorite" || dataGridView.Columns[e.ColumnIndex].Name == "pinned")) { var flashcard = (FlashcardItem)dataGridView.Rows[e.RowIndex].DataBoundItem; bool newValue = !(bool)dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value; if (dataGridView.Columns[e.ColumnIndex].Name == "favorite") { flashcard.favorite = newValue; } else { flashcard.pinned = newValue; } if (isInternetAvailable) { _ = UpdateFlashcardInDatabase(flashcard); } else { // İnternet yoksa yerel veritabanında güncelle UpdateFlashcardInLocalDatabase(flashcard); } } } /// <summary> /// Updates a flashcard in the Firebase database. /// </summary> private async Task UpdateFlashcardInDatabase(FlashcardItem flashcard) { try { await firebaseClient .Child("flashcards") .Child(flashcard.id) .PutAsync(flashcard); // Yerel veritabanında da güncelle UpdateFlashcardInLocalDatabase(flashcard); } catch (Exception ex) { MessageBox.Show($"Error updating flashcard: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void UpdateFlashcardInLocalDatabase(FlashcardItem flashcard) { string query = @" UPDATE flashcards SET category = @category, title = @title, content = @content, favorite = @favorite, pinned = @pinned, timestamp = @timestamp, backgroundColor = @backgroundColor, isUpdated = 1 WHERE id = @id; "; SQLiteCommand command = new SQLiteCommand(query, sqliteConnection); command.Parameters.AddWithValue("@category", flashcard.category); command.Parameters.AddWithValue("@title", flashcard.title); command.Parameters.AddWithValue("@content", flashcard.content); command.Parameters.AddWithValue("@favorite", flashcard.favorite ? 1 : 0); command.Parameters.AddWithValue("@pinned", flashcard.pinned ? 1 : 0); command.Parameters.AddWithValue("@timestamp", flashcard.timestamp); command.Parameters.AddWithValue("@backgroundColor", flashcard.backgroundColor); command.Parameters.AddWithValue("@id", flashcard.id); command.ExecuteNonQuery(); // Yerel listede güncelle var existingItem = allFlashcards.FirstOrDefault(f => f.id == flashcard.id); if (existingItem != null) { int index = allFlashcards.IndexOf(existingItem); allFlashcards[index] = flashcard; } ApplyFiltersAndSort(); } private async Task LoadDataFromFirebaseAsync() { try { var data = await firebaseClient .Child("flashcards") .OnceAsync<FlashcardItem>(); if (data == null || !data.Any()) { allFlashcards = new List<FlashcardItem>(); } else { allFlashcards = data .Where(d => d.Object != null && !d.Object.isDeleted) // Sadece silinmemiş kayıtları al .Select(d => new FlashcardItem { id = d.Key, // Doğrudan key'i kullan title = d.Object.title, category = string.IsNullOrWhiteSpace(d.Object.category) ? "uncategorized" : d.Object.category, content = d.Object.content, favorite = d.Object.favorite, pinned = d.Object.pinned, timestamp = d.Object.timestamp, backgroundColor = d.Object.backgroundColor, isDeleted = d.Object.isDeleted // Yeni eklenen }).ToList(); // Firebase'den gelen verileri yerel veritabanına kaydet SaveDataToLocalDatabase(allFlashcards); } ApplyFiltersAndSort(); // After data is loaded, populate the ContextMenuStrip PopulateColumnContextMenu(); } catch (Exception ex) { MessageBox.Show($"Error loading data: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } /// <summary> /// Loads data from the local SQLite database. /// </summary> private void LoadDataFromLocalDatabase() { string query = "SELECT * FROM flashcards WHERE isDeleted = 0;"; SQLiteCommand command = new SQLiteCommand(query, sqliteConnection); SQLiteDataReader reader = command.ExecuteReader(); allFlashcards.Clear(); while (reader.Read()) { var flashcard = new FlashcardItem { id = reader["id"].ToString(), category = reader["category"].ToString(), title = reader["title"].ToString(), content = reader["content"].ToString(), favorite = Convert.ToInt32(reader["favorite"]) == 1, pinned = Convert.ToInt32(reader["pinned"]) == 1, timestamp = Convert.ToInt64(reader["timestamp"]), backgroundColor = reader["backgroundColor"].ToString(), isDeleted = Convert.ToInt32(reader["isDeleted"]) == 1 // Yeni eklenen }; allFlashcards.Add(flashcard); } ApplyFiltersAndSort(); PopulateColumnContextMenu(); } /// <summary> /// Saves the list of flashcards to the local SQLite database. /// </summary> private void SaveDataToLocalDatabase(List<FlashcardItem> flashcards) { foreach (var flashcard in flashcards) { string insertQuery = @" INSERT OR REPLACE INTO flashcards (id, category, title, content, favorite, pinned, timestamp, backgroundColor, isUpdated, isDeleted) VALUES (@id, @category, @title, @content, @favorite, @pinned, @timestamp, @backgroundColor, 0, @isDeleted); "; SQLiteCommand command = new SQLiteCommand(insertQuery, sqliteConnection); command.Parameters.AddWithValue("@id", flashcard.id); command.Parameters.AddWithValue("@category", flashcard.category); command.Parameters.AddWithValue("@title", flashcard.title); command.Parameters.AddWithValue("@content", flashcard.content); command.Parameters.AddWithValue("@favorite", flashcard.favorite ? 1 : 0); command.Parameters.AddWithValue("@pinned", flashcard.pinned ? 1 : 0); command.Parameters.AddWithValue("@timestamp", flashcard.timestamp); command.Parameters.AddWithValue("@backgroundColor", flashcard.backgroundColor); command.Parameters.AddWithValue("@isDeleted", flashcard.isDeleted ? 1 : 0); // Yeni eklenen command.ExecuteNonQuery(); } } private void ApplyFiltersAndSort() { // Başlangıç olarak tüm flashcard'ları al IEnumerable<FlashcardItem> filtered = allFlashcards.Where(f => !f.isDeleted); // Silinmişleri filtrele // Eğer veritabanı tamamen boşsa, mesaj kutusu göstermeden DataGridView'ı güncelleyin if (allFlashcards.Count == 0) { UpdateDataGridView(new List<FlashcardItem>()); return; } // Favoriler filtresini uygula if (chkFilterFavorites.Checked) { filtered = filtered.Where(f => f.favorite); } // Sabitlenmiş filtresini uygula if (chkFilterPinned.Checked) { filtered = filtered.Where(f => f.pinned); } // Arama filtresini uygula if (!string.IsNullOrWhiteSpace(currentSearchTerm)) { filtered = filtered.Where(f => f.title.ToLower().Contains(currentSearchTerm) || f.category.ToLower().Contains(currentSearchTerm) || f.content.ToLower().Contains(currentSearchTerm)); } // Listeye dönüştür List<FlashcardItem> filteredList = filtered.ToList(); if (filteredList.Count == 0) { // Filtrelerden hangisinin boş listeye neden olduğunu belirleyin bool isFavoritesFilterActive = chkFilterFavorites.Checked; bool isPinnedFilterActive = chkFilterPinned.Checked; // Filtreleri sıfırlarken olayların tetiklenmesini engelleyin chkFilterFavorites.CheckedChanged -= FilterCheckBoxChanged; chkFilterPinned.CheckedChanged -= FilterCheckBoxChanged; if (isFavoritesFilterActive) { chkFilterFavorites.Checked = false; MessageBox.Show("No favorites found. Favorite filter has been reset.", "No Results", MessageBoxButtons.OK, MessageBoxIcon.Information); } if (isPinnedFilterActive) { chkFilterPinned.Checked = false; MessageBox.Show("No pinned items found. Pinned filter has been reset.", "No Results", MessageBoxButtons.OK, MessageBoxIcon.Information); } // Olayları yeniden bağlayın chkFilterFavorites.CheckedChanged += FilterCheckBoxChanged; chkFilterPinned.CheckedChanged += FilterCheckBoxChanged; // Filtreler sıfırlandıktan sonra tekrar filtreleme işlemi yapın filtered = allFlashcards.Where(f => !f.isDeleted); if (chkFilterFavorites.Checked) { filtered = filtered.Where(f => f.favorite); } if (chkFilterPinned.Checked) { filtered = filtered.Where(f => f.pinned); } if (!string.IsNullOrWhiteSpace(currentSearchTerm)) { filtered = filtered.Where(f => f.title.ToLower().Contains(currentSearchTerm) || f.category.ToLower().Contains(currentSearchTerm) || f.content.ToLower().Contains(currentSearchTerm)); } filteredList = filtered.ToList(); // Eğer hala sonuç yoksa ve arama terimi varsa, kullanıcıya bilgi verin if (filteredList.Count == 0 && !string.IsNullOrWhiteSpace(currentSearchTerm)) { MessageBox.Show("No flashcards match the current search term.", "No Results", MessageBoxButtons.OK, MessageBoxIcon.Information); } } // Sıralama işlemini uygula if (!string.IsNullOrEmpty(currentSortColumn)) { if (sortAscending) { switch (currentSortColumn) { case "title": filteredList = filteredList.OrderBy(f => f.title).ToList(); break; case "timestamp": filteredList = filteredList.OrderBy(f => f.timestamp).ToList(); break; } } else { switch (currentSortColumn) { case "title": filteredList = filteredList.OrderByDescending(f => f.title).ToList(); break; case "timestamp": filteredList = filteredList.OrderByDescending(f => f.timestamp).ToList(); break; } } } UpdateDataGridView(filteredList); } /// <summary> /// Updates the DataGridView with the provided list of flashcards. /// </summary> private void UpdateDataGridView(List<FlashcardItem> flashcards) { // SelectionChanged olayını geçici olarak devre dışı bırak dataGridView.SelectionChanged -= DataGridView_SelectionChanged; dataGridView.DataSource = null; dataGridView.Columns.Clear(); if (flashcards.Any()) { dataGridView.AutoGenerateColumns = false; // Define DataGridView columns // ID Column - Hidden var idColumn = new DataGridViewTextBoxColumn { DataPropertyName = "id", HeaderText = "ID", Visible = false }; dataGridView.Columns.Add(idColumn); // Category Column var categoryColumn = new DataGridViewTextBoxColumn { DataPropertyName = "category", HeaderText = "Category", Name = "category", SortMode = DataGridViewColumnSortMode.NotSortable }; dataGridView.Columns.Add(categoryColumn); // Title Column var titleColumn = new DataGridViewTextBoxColumn { DataPropertyName = "title", HeaderText = "Title", Name = "title", SortMode = DataGridViewColumnSortMode.Programmatic }; dataGridView.Columns.Add(titleColumn); // Content Column var contentColumn = new DataGridViewTextBoxColumn { DataPropertyName = "content", HeaderText = "Content", Name = "content", SortMode = DataGridViewColumnSortMode.NotSortable }; dataGridView.Columns.Add(contentColumn); // Favorite Column (Checkbox) var favoriteColumn = new DataGridViewCheckBoxColumn { DataPropertyName = "favorite", HeaderText = "Favorite", Name = "favorite", SortMode = DataGridViewColumnSortMode.Automatic }; dataGridView.Columns.Add(favoriteColumn); // Pinned Column (Checkbox) var pinnedColumn = new DataGridViewCheckBoxColumn { DataPropertyName = "pinned", HeaderText = "Pinned", Name = "pinned", SortMode = DataGridViewColumnSortMode.Automatic }; dataGridView.Columns.Add(pinnedColumn); // Timestamp Column var timestampColumn = new DataGridViewTextBoxColumn { DataPropertyName = "timestamp", HeaderText = "Timestamp", Name = "timestamp", SortMode = DataGridViewColumnSortMode.Programmatic }; dataGridView.Columns.Add(timestampColumn); // BackgroundColor Column var backgroundColorColumn = new DataGridViewTextBoxColumn { DataPropertyName = "backgroundColor", HeaderText = "Background Color", Name = "backgroundColor", SortMode = DataGridViewColumnSortMode.NotSortable }; dataGridView.Columns.Add(backgroundColorColumn); dataGridView.DataSource = flashcards; dataGridView.ClearSelection(); // Seçimi temizle } else { // If no flashcards match the filters, display a message var messageColumn = new DataGridViewTextBoxColumn { HeaderText = "Message", Name = "Message", AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill }; dataGridView.Columns.Add(messageColumn); dataGridView.Rows.Add("No flashcards found."); } // Restore column visibility state RestoreColumnVisibilityState(); // Update the status label with the count statusLabel.Text = $"Total Items: {flashcards.Count}"; // SelectionChanged olayını tekrar etkinleştir dataGridView.SelectionChanged += DataGridView_SelectionChanged; } /// <summary> /// Populates the ContextMenuStrip with the current DataGridView columns. /// Ensures no duplicate menu items are added. /// </summary> private void PopulateColumnContextMenu() { columnContextMenu.Items.Clear(); // Clear existing items to prevent duplication foreach (DataGridViewColumn column in dataGridView.Columns) { // Skip hidden or irrelevant columns if necessary // For example, you might want to skip the "ID" column if (column.Name == "ID") continue; // Create a ToolStripMenuItem for the column var menuItem = new ToolStripMenuItem(column.HeaderText) { Checked = column.Visible, CheckOnClick = true, Tag = column // Store the column in Tag for reference }; menuItem.CheckedChanged += ColumnMenuItem_CheckedChanged; // Add the menu item to the ContextMenuStrip columnContextMenu.Items.Add(menuItem); } } /// <summary> /// Handles the CheckedChanged event of ContextMenuStrip items to toggle column visibility. /// </summary> private void ColumnMenuItem_CheckedChanged(object sender, EventArgs e) { if (sender is ToolStripMenuItem menuItem && menuItem.Tag is DataGridViewColumn column) { column.Visible = menuItem.Checked; columnVisibilityState[column.Name] = column.Visible; // Update the visibility state } } /// <summary> /// Shows the ContextMenuStrip when the user right-clicks on a column header. /// </summary> private void DataGridView_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { var hitTestInfo = dataGridView.HitTest(e.X, e.Y); if (hitTestInfo.Type == DataGridViewHitTestType.ColumnHeader) { // Show the ContextMenuStrip at the mouse position columnContextMenu.Show(dataGridView, new Point(e.X, e.Y)); } } } /// <summary> /// Adds a new flashcard to the Firebase database. /// </summary> private async void AddData() { var newFlashcard = CreateFlashcardFromInputs(); if (newFlashcard != null) { if (isInternetAvailable) { try { var response = await firebaseClient .Child("flashcards") .PostAsync(newFlashcard); newFlashcard.id = response.Key; // Firebase veritabanında id alanını eklemek için güncelleme yapıyoruz await firebaseClient .Child("flashcards") .Child(response.Key) .PutAsync(newFlashcard); // Yerel veritabanına ekle SaveDataToLocalDatabase(new List<FlashcardItem> { newFlashcard }); ClearInputs(); } catch (Exception ex) { MessageBox.Show($"Error adding flashcard: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { // İnternet yoksa yerel veritabanına ekle ve senkronizasyon için işaretle AddFlashcardToLocalDatabase(newFlashcard); ClearInputs(); MessageBox.Show("No internet connection. Flashcard saved locally.", "Offline Mode", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } private void AddFlashcardToLocalDatabase(FlashcardItem flashcard) { // Yerel bir id oluştur flashcard.id = Guid.NewGuid().ToString(); string insertQuery = @" INSERT INTO flashcards (id, category, title, content, favorite, pinned, timestamp, backgroundColor, isUpdated, isDeleted) VALUES (@id, @category, @title, @content, @favorite, @pinned, @timestamp, @backgroundColor, 1, 0); "; SQLiteCommand command = new SQLiteCommand(insertQuery, sqliteConnection); command.Parameters.AddWithValue("@id", flashcard.id); command.Parameters.AddWithValue("@category", flashcard.category); command.Parameters.AddWithValue("@title", flashcard.title); command.Parameters.AddWithValue("@content", flashcard.content); command.Parameters.AddWithValue("@favorite", flashcard.favorite ? 1 : 0); command.Parameters.AddWithValue("@pinned", flashcard.pinned ? 1 : 0); command.Parameters.AddWithValue("@timestamp", flashcard.timestamp); command.Parameters.AddWithValue("@backgroundColor", flashcard.backgroundColor); command.ExecuteNonQuery(); // Yerel listeye ekle allFlashcards.Add(flashcard); ApplyFiltersAndSort(); } /// <summary> /// Updates the selected flashcard in the Firebase database. /// </summary> private async void UpdateData() { if (dataGridView.SelectedRows.Count > 0) { var selectedFlashcard = (FlashcardItem)dataGridView.SelectedRows[0].DataBoundItem; var updatedFlashcard = CreateFlashcardFromInputs(); if (updatedFlashcard != null) { updatedFlashcard.id = selectedFlashcard.id; updatedFlashcard.timestamp = selectedFlashcard.timestamp; // Zaman damgasını koru updatedFlashcard.isDeleted = selectedFlashcard.isDeleted; // Yeni eklenen if (isInternetAvailable) { try { await firebaseClient .Child("flashcards") .Child(selectedFlashcard.id) .PutAsync(updatedFlashcard); // Yerel veritabanında da güncelle UpdateFlashcardInLocalDatabase(updatedFlashcard); ClearInputs(); } catch (Exception ex) { MessageBox.Show($"Error updating flashcard: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { // İnternet yoksa yerel veritabanında güncelle ve senkronizasyon için işaretle UpdateFlashcardInLocalDatabase(updatedFlashcard); ClearInputs(); MessageBox.Show("No internet connection. Changes saved locally.", "Offline Mode", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } else { MessageBox.Show("Please select a flashcard to update.", "No Selection", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } /// <summary> /// Deletes selected flashcards from the Firebase database. /// </summary> private async void DeleteData() { if (dataGridView.SelectedRows.Count > 0) { int selectedCount = dataGridView.SelectedRows.Count; var confirmResult = MessageBox.Show($"Are you sure you want to delete {selectedCount} flashcard(s)?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (confirmResult == DialogResult.Yes) { try { var deleteFlashcards = dataGridView.SelectedRows .Cast<DataGridViewRow>() .Select(row => (FlashcardItem)row.DataBoundItem) .ToList(); if (isInternetAvailable) { // Create a list of delete tasks var deleteTasks = deleteFlashcards .Select(flashcard => { return firebaseClient.Child("flashcards").Child(flashcard.id).DeleteAsync(); }) .ToList(); // Execute all delete tasks in parallel await Task.WhenAll(deleteTasks); // Yerel veritabanından sil foreach (var flashcard in deleteFlashcards) { DeleteFlashcardFromLocalDatabase(flashcard); } // Yerel listeden sil allFlashcards.RemoveAll(f => deleteFlashcards.Any(df => df.id == f.id)); // Clear inputs ClearInputs(); } else { // İnternet yoksa yerel veritabanında sil ve senkronizasyon için işaretle foreach (var flashcard in deleteFlashcards) { MarkFlashcardAsDeletedInLocalDatabase(flashcard); } // Yerel listeden sil allFlashcards.RemoveAll(f => deleteFlashcards.Any(df => df.id == f.id)); // Clear inputs ClearInputs(); MessageBox.Show("No internet connection. Changes saved locally.", "Offline Mode", MessageBoxButtons.OK, MessageBoxIcon.Information); } ApplyFiltersAndSort(); MessageBox.Show($"{selectedCount} flashcard(s) deleted successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show($"Error deleting flashcards: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } else { MessageBox.Show("Please select one or more flashcards to delete.", "No Selection", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } private void DeleteFlashcardFromLocalDatabase(FlashcardItem flashcard) { string query = "DELETE FROM flashcards WHERE id = @id;"; SQLiteCommand command = new SQLiteCommand(query, sqliteConnection); command.Parameters.AddWithValue("@id", flashcard.id); command.ExecuteNonQuery(); } private void MarkFlashcardAsDeletedInLocalDatabase(FlashcardItem flashcard) { string query = "UPDATE flashcards SET isDeleted = 1 WHERE id = @id;"; SQLiteCommand command = new SQLiteCommand(query, sqliteConnection); command.Parameters.AddWithValue("@id", flashcard.id); command.ExecuteNonQuery(); } /// <summary> /// Creates a FlashcardItem object from the input fields. /// </summary> private FlashcardItem CreateFlashcardFromInputs() { string title = txtTitle.Text.Trim(); string content = txtContent.Text.Trim(); string backgroundColor = txtBackgroundColor.Text.Trim(); // Kategori belirleme string category; if (!string.IsNullOrWhiteSpace(selectedCustomTag)) { category = selectedCustomTag; } else if (!string.IsNullOrWhiteSpace(txtCategory.Text)) { category = txtCategory.Text.Trim(); } else { category = "uncategorized"; } // Title alanı boş olsa bile veri eklemeye izin veriyoruz return new FlashcardItem { title = title, category = category, content = content, favorite = chkFavorite.Checked, pinned = chkPinned.Checked, timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), backgroundColor = backgroundColor, isDeleted = false // Yeni eklenen }; } /// <summary> /// Clears the input fields. /// </summary> private void ClearInputs() { txtTitle.Text = ""; txtCategory.Text = ""; txtContent.Text = ""; txtBackgroundColor.Text = ""; chkFavorite.Checked = false; chkPinned.Checked = false; } /// <summary> /// Handles the selection change in the DataGridView to populate the input fields. /// </summary> private void DataGridView_SelectionChanged(object sender, EventArgs e) { if (dataGridView.SelectedRows.Count > 0) { var selectedFlashcard = (FlashcardItem)dataGridView.SelectedRows[0].DataBoundItem; txtTitle.Text = selectedFlashcard.title; txtTitle.ForeColor = Color.Black; txtCategory.Text = selectedFlashcard.category; txtCategory.ForeColor = Color.Black; txtContent.Text = selectedFlashcard.content; txtContent.ForeColor = Color.Black; txtBackgroundColor.Text = selectedFlashcard.backgroundColor; txtBackgroundColor.ForeColor = Color.Black; chkFavorite.Checked = selectedFlashcard.favorite; chkPinned.Checked = selectedFlashcard.pinned; } else { // Seçim yoksa metin kutularını temizle ClearInputs(); } } /// <summary> /// Sets up a real-time listener to Firebase to update data upon changes. /// </summary> private void StartFirebaseRealTimeListener() { firebaseClient .Child("flashcards") .AsObservable<FlashcardItem>() .Subscribe(async d => { if (d.Object == null) return; var flashcard = d.Object; flashcard.id = d.Key; if (!flashcard.isDeleted) { var existingItem = allFlashcards.FirstOrDefault(f => f.id == flashcard.id); if (existingItem != null) { // Mevcut öğeyi güncelle int index = allFlashcards.IndexOf(existingItem); allFlashcards[index] = flashcard; } else { // Yeni öğe ekle allFlashcards.Add(flashcard); } // Yerel veritabanına kaydet SaveDataToLocalDatabase(new List<FlashcardItem> { flashcard }); } else { var itemToRemove = allFlashcards.FirstOrDefault(f => f.id == d.Key); if (itemToRemove != null) { allFlashcards.Remove(itemToRemove); } // Yerel veritabanından sil DeleteFlashcardFromLocalDatabase(flashcard); } // Arayüzü güncelle Invoke((Action)(() => { ApplyFiltersAndSort(); })); }); } /// <summary> /// Handles the Search button click to apply search criteria. /// </summary> private void SearchData() { string searchText = txtSearch.Text; if (string.IsNullOrWhiteSpace(searchText)) { currentSearchTerm = ""; } else { currentSearchTerm = searchText.ToLower(); } ApplyFiltersAndSort(); } /// <summary> /// Clears the search input and resets search filter. /// </summary> private void ClearSearch() { txtSearch.Text = ""; currentSearchTerm = ""; ApplyFiltersAndSort(); } /// <summary> /// Formats the timestamp column to display human-readable dates. /// </summary> private void DataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (dataGridView.Columns[e.ColumnIndex].Name == "timestamp") { if (e.Value != null && long.TryParse(e.Value.ToString(), out long timestamp)) { DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeMilliseconds(timestamp); DateTime dateTime = dateTimeOffset.ToLocalTime().DateTime; e.Value = dateTime.ToString("yyyy-MM-dd HH:mm:ss"); e.FormattingApplied = true; } } } /// <summary> /// Handles the column header click event to apply sorting. /// </summary> private void DataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) { string columnName = dataGridView.Columns[e.ColumnIndex].Name; // Only allow sorting on 'title' and 'timestamp' columns if (columnName == "title" || columnName == "timestamp") { // Toggle sort direction if the same column is clicked if (currentSortColumn == columnName) { sortAscending = !sortAscending; } else { // Default to ascending if a new column is clicked currentSortColumn = columnName; sortAscending = true; } // Apply sorting and refresh DataGridView ApplyFiltersAndSort(); // Reset sort glyphs on all columns foreach (DataGridViewColumn column in dataGridView.Columns) { column.HeaderCell.SortGlyphDirection = SortOrder.None; } // Set sort glyph on the sorted column dataGridView.Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection = sortAscending ? SortOrder.Ascending : SortOrder.Descending; } } /// <summary> /// Handles CheckedChanged events for filtering CheckBoxes. /// </summary> private void FilterCheckBoxChanged(object sender, EventArgs e) { ApplyFiltersAndSort(); } /// <summary> /// Restores the column visibility state from the stored dictionary. /// </summary> private void RestoreColumnVisibilityState() { foreach (DataGridViewColumn column in dataGridView.Columns) { if (columnVisibilityState.TryGetValue(column.Name, out bool isVisible)) { column.Visible = isVisible; } } } /// <summary> /// Handles MouseDown event on the background panel to clear inputs. /// </summary> private void BackgroundPanel_MouseDown(object sender, MouseEventArgs e) { ClearInputs(); } // Yeni Eklenen Metodlar ve Event Handler'lar private void ClipboardOnMenuItem_Click(object sender, EventArgs e) { StartClipboardMonitoring(); tagCheckedListBox.Visible = true; // Clipboard modu açılırken görünür yap } private void ClipboardOffMenuItem_Click(object sender, EventArgs e) { StopClipboardMonitoring(); tagCheckedListBox.Visible = false; // Clipboard modu kapanırken gizle } private void StartClipboardMonitoring() { clipboardOnMenuItem.Checked = true; clipboardOffMenuItem.Checked = false; // Panodaki mevcut metni alarak lastClipboardText'e atıyoruz if (Clipboard.ContainsText()) { lastClipboardText = Clipboard.GetText(); } else { lastClipboardText = null; } clipboardMonitorTimer.Start(); } private void StopClipboardMonitoring() { clipboardOnMenuItem.Checked = false; clipboardOffMenuItem.Checked = true; clipboardMonitorTimer.Stop(); } private async void ClipboardMonitorTimer_Tick(object sender, EventArgs e) { await CheckClipboard(); } private async Task CheckClipboard() { try { if (Clipboard.ContainsText()) { string clipboardText = Clipboard.GetText(); if (clipboardText != lastClipboardText) { lastClipboardText = clipboardText; // Metni tek satıra dönüştür string singleLineText = clipboardText.Replace(Environment.NewLine, " "); // Kategori belirleme string category; if (!string.IsNullOrWhiteSpace(selectedCustomTag)) { // Eğer Custom Tag altında bir etiket seçildiyse category = selectedCustomTag; } else if (tagCheckedListBox.CheckedItems.Count > 0) { // Eğer tagCheckedListBox'dan bir etiket seçildiyse category = tagCheckedListBox.CheckedItems[0].ToString(); } else { // Hiçbir etiket seçili değilse varsayılan olarak "uncategorized" kullan category = "uncategorized"; } // Yeni FlashcardItem oluştur var newFlashcard = new FlashcardItem { title = "", // Başlık boş category = category, // Belirlenen kategoriyi kullan content = singleLineText, favorite = false, pinned = false, timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), backgroundColor = "", isDeleted = false // Yeni eklenen }; if (isInternetAvailable) { // Veritabanına kaydet await SaveFlashcardToFirebase(newFlashcard); } else { // Yerel veritabanına kaydet AddFlashcardToLocalDatabase(newFlashcard); } } } } catch (Exception ex) { // Pano erişiminde hata olursa burası çalışacak // İsterseniz hata mesajı gösterebilirsiniz } } private async Task SaveFlashcardToFirebase(FlashcardItem flashcard) { try { var response = await firebaseClient .Child("flashcards") .PostAsync(flashcard); flashcard.id = response.Key; // Firebase veritabanında id alanını eklemek için güncelleme yapıyoruz await firebaseClient .Child("flashcards") .Child(response.Key) .PutAsync(flashcard); // Yerel veritabanına ekle SaveDataToLocalDatabase(new List<FlashcardItem> { flashcard }); } catch (Exception ex) { // Hata yönetimi } } private void LoadTagsMenuItem_Click(object sender, EventArgs e) { using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog()) { folderBrowserDialog.Description = "Klasör Seçin"; if (folderBrowserDialog.ShowDialog() == DialogResult.OK) { string selectedFolder = folderBrowserDialog.SelectedPath; // Klasördeki ve alt klasörlerdeki PDF dosyalarını al string[] pdfFiles = Directory.GetFiles(selectedFolder, "*.pdf", SearchOption.AllDirectories); // Dosya isimlerini al (uzantısız) List<string> tags = pdfFiles.Select(file => Path.GetFileNameWithoutExtension(file)).ToList(); // CheckedListBox'ı güncelle tagCheckedListBox.Items.Clear(); foreach (var tag in tags) { tagCheckedListBox.Items.Add(tag); } } } } private void TagCheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e) { // İşaretlenen diğer öğeleri kaldır, sadece bir tanesi seçili olsun if (e.NewValue == CheckState.Checked) { for (int i = 0; i < tagCheckedListBox.Items.Count; i++) { if (i != e.Index) { tagCheckedListBox.SetItemChecked(i, false); } } selectedTag = tagCheckedListBox.Items[e.Index].ToString(); // Custom Tag seçimini kaldır foreach (ToolStripMenuItem item in customTagMenuItem.DropDownItems) { if (item != addTagMenuItem && item != manageTagsMenuItem) { item.Checked = false; } } selectedCustomTag = null; } else { selectedTag = "uncategorized"; } } private void AddTagMenuItem_Click(object sender, EventArgs e) { using (Form inputDialog = new Form()) { inputDialog.Width = 300; inputDialog.Height = 150; inputDialog.Text = "Add Custom Tag"; inputDialog.FormBorderStyle = FormBorderStyle.FixedDialog; inputDialog.StartPosition = FormStartPosition.CenterParent; // Ekranın ortasında açılsın inputDialog.MinimizeBox = false; inputDialog.MaximizeBox = false; inputDialog.AcceptButton = null; // We'll set the accept button later Label lblTag = new Label() { Left = 20, Top = 20, Text = "Tag:", AutoSize = false, // Otomatik boyutlandırmayı kapatıyoruz Width = 40, // Daha dar bir genişlik veriyoruz TextAlign = ContentAlignment.TopCenter // Metni sola hizalıyoruz }; TextBox txtTag = new TextBox() { Left = lblTag.Left + lblTag.Width + 5, Top = lblTag.Top - 3, Width = 150 }; // Metin kutusunu etikete yaklaştırıyoruz Button btnOK = new Button() { Text = "OK", Left = 50, Width = 100, Top = txtTag.Top + txtTag.Height + 20 }; Button btnCancel = new Button() { Text = "Cancel", Left = 150, Width = 100, Top = txtTag.Top + txtTag.Height + 20 }; btnOK.Click += (s, args) => { string newTag = txtTag.Text.Trim(); if (!string.IsNullOrEmpty(newTag)) { // Add tag to customTags list customTags.Add(newTag); // Save the custom tags to the file SaveCustomTags(); // Eklendi // Add tag as submenu item under customTagMenuItem AddCustomTagMenuItem(newTag); // Close the dialog inputDialog.DialogResult = DialogResult.OK; inputDialog.Close(); } else { MessageBox.Show("Tag cannot be empty.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }; btnCancel.Click += (s, args) => { inputDialog.DialogResult = DialogResult.Cancel; inputDialog.Close(); }; inputDialog.Controls.Add(lblTag); inputDialog.Controls.Add(txtTag); inputDialog.Controls.Add(btnOK); inputDialog.Controls.Add(btnCancel); inputDialog.AcceptButton = btnOK; inputDialog.ShowDialog(); // Show as modal dialog } } private void AddCustomTagMenuItem(string tag) { ToolStripMenuItem tagMenuItem = new ToolStripMenuItem(tag); tagMenuItem.Checked = false; tagMenuItem.CheckOnClick = true; tagMenuItem.Click += TagMenuItem_Click; // Insert the tag menu item after the "Manage Tags" menu item int insertIndex = customTagMenuItem.DropDownItems.IndexOf(manageTagsMenuItem) + 1; customTagMenuItem.DropDownItems.Insert(insertIndex, tagMenuItem); } private void TagMenuItem_Click(object sender, EventArgs e) { // When a tag is clicked, uncheck other tags if (sender is ToolStripMenuItem clickedItem) { foreach (ToolStripMenuItem item in customTagMenuItem.DropDownItems) { if (item != clickedItem && item != addTagMenuItem && item != manageTagsMenuItem) { item.Checked = false; } } // Update the selectedCustomTag if (clickedItem.Checked) { selectedCustomTag = clickedItem.Text; // CheckedListBox'daki tüm seçimleri kaldır tagCheckedListBox.ItemCheck -= TagCheckedListBox_ItemCheck; // Event geçici olarak devre dışı for (int i = 0; i < tagCheckedListBox.Items.Count; i++) { tagCheckedListBox.SetItemChecked(i, false); } tagCheckedListBox.ItemCheck += TagCheckedListBox_ItemCheck; // Event tekrar aktif selectedTag = "uncategorized"; } else { selectedCustomTag = null; } } } // Event handler for "Manage Tags" menu item click private void ManageTagsMenuItem_Click(object sender, EventArgs e) { // Open a dialog to manage tags using (Form manageTagsForm = new Form()) { manageTagsForm.Width = 300; manageTagsForm.Height = 400; manageTagsForm.Text = "Manage Tags"; manageTagsForm.FormBorderStyle = FormBorderStyle.FixedDialog; manageTagsForm.StartPosition = FormStartPosition.CenterParent; // Ekranın ortasında açılsın manageTagsForm.MinimizeBox = false; manageTagsForm.MaximizeBox = false; ListBox lstTags = new ListBox() { Left = 10, Top = 10, Width = 260, Height = 300 }; lstTags.DataSource = null; lstTags.DataSource = new List<string>(customTags); Button btnEdit = new Button() { Text = "Edit", Left = 10, Width = 80, Top = lstTags.Bottom + 10 }; Button btnDelete = new Button() { Text = "Delete", Left = 100, Width = 80, Top = lstTags.Bottom + 10 }; Button btnClose = new Button() { Text = "Close", Left = 190, Width = 80, Top = lstTags.Bottom + 10 }; btnEdit.Click += (s, args) => { if (lstTags.SelectedItem != null) { string selectedTag = lstTags.SelectedItem.ToString(); using (Form editTagForm = new Form()) { editTagForm.Width = 300; editTagForm.Height = 150; editTagForm.Text = "Edit Tag"; editTagForm.FormBorderStyle = FormBorderStyle.FixedDialog; editTagForm.StartPosition = FormStartPosition.CenterParent; // Ekranın ortasında açılsın editTagForm.MinimizeBox = false; editTagForm.MaximizeBox = false; Label lblTag = new Label() { Left = 10, Top = 20, Text = "Tag:", AutoSize = false, Width = 40, // Daha küçük bir genişlik veriyoruz TextAlign = ContentAlignment.TopCenter }; TextBox txtTag = new TextBox() { Left = lblTag.Left + lblTag.Width + 5, Top = lblTag.Top - 3, Width = 150 }; // Metin kutusunu etikete yaklaştırıyoruz Button btnOK = new Button() { Text = "OK", Left = 50, Width = 100, Top = txtTag.Top + txtTag.Height + 20 }; Button btnCancel = new Button() { Text = "Cancel", Left = 150, Width = 100, Top = txtTag.Top + txtTag.Height + 20 }; btnOK.Click += (s2, args2) => { string newTag = txtTag.Text.Trim(); if (!string.IsNullOrEmpty(newTag)) { // Update the tag in customTags list int index = customTags.IndexOf(selectedTag); customTags[index] = newTag; // Save the custom tags to the file SaveCustomTags(); // Eklendi // Update the menu item text foreach (ToolStripMenuItem item in customTagMenuItem.DropDownItems) { if (item.Text == selectedTag) { item.Text = newTag; break; } } // Update selectedCustomTag if this tag was the selected one if (selectedCustomTag == selectedTag) { selectedCustomTag = newTag; } // Update the listbox lstTags.DataSource = null; lstTags.DataSource = new List<string>(customTags); // Close the edit form editTagForm.DialogResult = DialogResult.OK; editTagForm.Close(); } else { MessageBox.Show("Tag cannot be empty.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }; btnCancel.Click += (s2, args2) => { editTagForm.DialogResult = DialogResult.Cancel; editTagForm.Close(); }; editTagForm.Controls.Add(lblTag); editTagForm.Controls.Add(txtTag); editTagForm.Controls.Add(btnOK); editTagForm.Controls.Add(btnCancel); editTagForm.AcceptButton = btnOK; editTagForm.ShowDialog(); } } else { MessageBox.Show("Please select a tag to edit.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } }; btnDelete.Click += (s, args) => { if (lstTags.SelectedItem != null) { string selectedTag = lstTags.SelectedItem.ToString(); var confirmResult = MessageBox.Show($"Are you sure you want to delete tag '{selectedTag}'?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (confirmResult == DialogResult.Yes) { // Remove from customTags list customTags.Remove(selectedTag); // Save the custom tags to the file SaveCustomTags(); // Eklendi // Remove from menu items ToolStripMenuItem itemToRemove = null; foreach (ToolStripMenuItem item in customTagMenuItem.DropDownItems) { if (item.Text == selectedTag) { itemToRemove = item; break; } } if (itemToRemove != null) { customTagMenuItem.DropDownItems.Remove(itemToRemove); } // Clear selectedCustomTag if it was deleted if (selectedCustomTag == selectedTag) { selectedCustomTag = null; } // Update the listbox lstTags.DataSource = null; lstTags.DataSource = new List<string>(customTags); } } else { MessageBox.Show("Please select a tag to delete.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } }; btnClose.Click += (s, args) => { manageTagsForm.Close(); }; manageTagsForm.Controls.Add(lstTags); manageTagsForm.Controls.Add(btnEdit); manageTagsForm.Controls.Add(btnDelete); manageTagsForm.Controls.Add(btnClose); manageTagsForm.ShowDialog(); } } // Custom Tags'i kaydetme metodu private void SaveCustomTags() { try { string json = JsonConvert.SerializeObject(customTags); File.WriteAllText(customTagsFilePath, json); } catch (Exception ex) { MessageBox.Show($"Error saving custom tags: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } // Custom Tags'i yükleme metodu private void LoadCustomTags() { if (File.Exists(customTagsFilePath)) { try { string json = File.ReadAllText(customTagsFilePath); customTags = JsonConvert.DeserializeObject<List<string>>(json); // customTagMenuItem içindeki mevcut özel etiketleri temizleyelim (Add Tag ve Manage Tags hariç) for (int i = customTagMenuItem.DropDownItems.Count - 1; i >= 0; i--) { var item = customTagMenuItem.DropDownItems[i]; if (item != addTagMenuItem && item != manageTagsMenuItem) { customTagMenuItem.DropDownItems.RemoveAt(i); } } // customTags listesindeki etiketleri menüye ekleyelim foreach (var tag in customTags) { AddCustomTagMenuItem(tag); } } catch (Exception ex) { MessageBox.Show($"Error loading custom tags: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } /// <summary> /// İnternet bağlantısının olup olmadığını kontrol eder. /// </summary> private bool CheckInternetConnectivity() { try { using (var client = new System.Net.WebClient()) using (client.OpenRead("http://google.com/generate_204")) return true; } catch { return false; } } /// <summary> /// Yerel veritabanındaki değişiklikleri Firebase'e senkronize eder. /// </summary> private async Task SyncLocalChangesToFirebase() { // Güncellenmiş kayıtları al string query = "SELECT * FROM flashcards WHERE isUpdated = 1 AND isDeleted = 0;"; SQLiteCommand command = new SQLiteCommand(query, sqliteConnection); SQLiteDataReader reader = command.ExecuteReader(); var updatedFlashcards = new List<FlashcardItem>(); while (reader.Read()) { var flashcard = new FlashcardItem { id = reader["id"].ToString(), category = reader["category"].ToString(), title = reader["title"].ToString(), content = reader["content"].ToString(), favorite = Convert.ToInt32(reader["favorite"]) == 1, pinned = Convert.ToInt32(reader["pinned"]) == 1, timestamp = Convert.ToInt64(reader["timestamp"]), backgroundColor = reader["backgroundColor"].ToString(), isDeleted = Convert.ToInt32(reader["isDeleted"]) == 1 // Yeni eklenen }; updatedFlashcards.Add(flashcard); } // Güncellenmiş kayıtları Firebase'e gönder foreach (var flashcard in updatedFlashcards) { if (flashcard.id.StartsWith("local_")) { // Yerel olarak oluşturulmuş bir kayıt, yeni ekle var newFlashcard = new FlashcardItem { category = flashcard.category, title = flashcard.title, content = flashcard.content, favorite = flashcard.favorite, pinned = flashcard.pinned, timestamp = flashcard.timestamp, backgroundColor = flashcard.backgroundColor, isDeleted = flashcard.isDeleted }; var response = await firebaseClient .Child("flashcards") .PostAsync(newFlashcard); newFlashcard.id = response.Key; // Firebase id ile yerel veritabanını güncelle string updateQuery = @" UPDATE flashcards SET id = @newId, isUpdated = 0 WHERE id = @oldId; "; SQLiteCommand updateCommand = new SQLiteCommand(updateQuery, sqliteConnection); updateCommand.Parameters.AddWithValue("@newId", newFlashcard.id); updateCommand.Parameters.AddWithValue("@oldId", flashcard.id); updateCommand.ExecuteNonQuery(); } else { // Mevcut bir kaydı güncelle await firebaseClient .Child("flashcards") .Child(flashcard.id) .PutAsync(flashcard); // isUpdated flag'ini sıfırla string updateQuery = "UPDATE flashcards SET isUpdated = 0 WHERE id = @id;"; SQLiteCommand updateCommand = new SQLiteCommand(updateQuery, sqliteConnection); updateCommand.Parameters.AddWithValue("@id", flashcard.id); updateCommand.ExecuteNonQuery(); } } // Silinmiş kayıtları al query = "SELECT * FROM flashcards WHERE isDeleted = 1;"; command = new SQLiteCommand(query, sqliteConnection); reader = command.ExecuteReader(); var deletedFlashcards = new List<FlashcardItem>(); while (reader.Read()) { var flashcard = new FlashcardItem { id = reader["id"].ToString() }; deletedFlashcards.Add(flashcard); } // Silinmiş kayıtları Firebase'den sil foreach (var flashcard in deletedFlashcards) { await firebaseClient .Child("flashcards") .Child(flashcard.id) .DeleteAsync(); // Yerel veritabanından tamamen sil DeleteFlashcardFromLocalDatabase(flashcard); } } } /// <summary> /// Represents a Flashcard item. /// </summary> public class FlashcardItem { public string id { get; set; } public string category { get; set; } public string title { get; set; } public string content { get; set; } public bool favorite { get; set; } public bool pinned { get; set; } public long timestamp { get; set; } public string backgroundColor { get; set; } public bool isDeleted { get; set; } // Yeni eklenen } }Yapılan Değişiklikler:
-
FlashcardItemSınıfınaisDeletedEklenmesi:public bool isDeleted { get; set; } // Yeni eklenen -
LoadDataFromFirebaseAsyncMetodunun Güncellenmesi:Firebase'den verileri çekerken sadece silinmemiş kayıtların alınmasını sağladık.
allFlashcards = data .Where(d => d.Object != null && !d.Object.isDeleted) // Sadece silinmemiş kayıtları al .Select(d => new FlashcardItem { id = d.Key, // Doğrudan key'i kullan title = d.Object.title, category = string.IsNullOrWhiteSpace(d.Object.category) ? "uncategorized" : d.Object.category, content = d.Object.content, favorite = d.Object.favorite, pinned = d.Object.pinned, timestamp = d.Object.timestamp, backgroundColor = d.Object.backgroundColor, isDeleted = d.Object.isDeleted // Yeni eklenen }).ToList(); -
ApplyFiltersAndSortMetodunun Güncellenmesi:Filtreleme aşamasında silinmiş kayıtların dahil edilmemesini sağladık.
IEnumerable<FlashcardItem> filtered = allFlashcards.Where(f => !f.isDeleted); // Silinmişleri filtrele -
Veritabanı İşlemlerinde
isDeleted'ın Yönetilmesi:Yerel veritabanına veri eklerken ve güncellerken
isDeletedözelliğini dikkate aldık.command.Parameters.AddWithValue("@isDeleted", flashcard.isDeleted ? 1 : 0); // Yeni eklenen -
Gerçek Zamanlı Dinleyicide
isDeletedKontrolü:Firebase'den gelen verilerde
isDeletedözelliğini kontrol ederek, silinmiş kayıtları liste dışı bıraktık.if (!flashcard.isDeleted) { var existingItem = allFlashcards.FirstOrDefault(f => f.id == flashcard.id); if (existingItem != null) { // Mevcut öğeyi güncelle int index = allFlashcards.IndexOf(existingItem); allFlashcards[index] = flashcard; } else { // Yeni öğe ekle allFlashcards.Add(flashcard); } // Yerel veritabanına kaydet SaveDataToLocalDatabase(new List<FlashcardItem> { flashcard }); } else { var itemToRemove = allFlashcards.FirstOrDefault(f => f.id == d.Key); if (itemToRemove != null) { allFlashcards.Remove(itemToRemove); } // Yerel veritabanından sil DeleteFlashcardFromLocalDatabase(flashcard); }
Açıklamalar:
-
isDeletedÖzelliği: Bu özellik, bir flashcard'ın silinip silinmediğini belirtmek için kullanılır. Firebase ve yerel veritabanında bu özelliği kullanarak silinmiş kayıtları yönetebilirsiniz. -
Filtreleme ve Sıralama:
ApplyFiltersAndSortmetodunda,isDeleteddeğerifalseolan kayıtlar filtrelenerek DataGridView'da görüntülenir. -
Gerçek Zamanlı Dinleyici: Firebase'den anlık verileri alırken, silinmiş kayıtları doğru şekilde yönetmek için
isDeletedkontrolü ekledik.
Bu düzeltmelerle birlikte, uygulamanız internet bağlantısı olduğunda Firebase'den doğru verileri almalı ve DataGridView'da görüntülemelidir. Ayrıca, silinmiş kayıtlar veri tabanında işaretlenmiş olsa bile, kullanıcı arayüzünde görünmezler.
-