Skip to content

Commit

Permalink
Fixed Tag addition and deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
netquick committed Aug 17, 2024
1 parent 4e8613a commit 2856050
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 72 deletions.
34 changes: 17 additions & 17 deletions DeFRaG_Helper/UserControls/MapCardBig.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
using DeFRaG_Helper.ViewModels;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DeFRaG_Helper.UserControls
{
Expand Down Expand Up @@ -72,13 +62,21 @@ private void OpenTagManager(object parameter)
// Handle the Deactivated event to close the popup
window.Deactivated += (s, e) =>
{
window.Close();
// Bring the main window to the foreground
Application.Current.MainWindow.Activate();
var mainWindow = Application.Current.MainWindow;
mainWindow.Topmost = true; // Set MainWindow as topmost
mainWindow.Topmost = false; // Revert MainWindow back to normal
if (!tagManager.IsInternalClick)
{
window.Close();
// Bring the main window to the foreground
Application.Current.MainWindow.Activate();
var mainWindow = Application.Current.MainWindow;
mainWindow.Topmost = true; // Set MainWindow as topmost
mainWindow.Topmost = false; // Revert MainWindow back to normal
}
else
{
// Reset the internal click flag
tagManager.IsInternalClick = false;
}
};

// Add fade-in animation
Expand All @@ -96,6 +94,8 @@ private void OpenTagManager(object parameter)





public IEnumerable Icons
{
get { return (IEnumerable)GetValue(IconsProperty); }
Expand Down
4 changes: 2 additions & 2 deletions DeFRaG_Helper/UserControls/TagManager.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<Border Background="Transparent" Padding="10" CornerRadius="5">
<StackPanel>
<TextBlock Text="Manage Tags" FontSize="16" Margin="0,0,0,10" Foreground="White"/>
<ListBox ItemsSource="{Binding Tags}" SelectionMode="Multiple">
<ListBox x:Name="TagsListBox" ItemsSource="{Binding Tags}" SelectionMode="Single">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked, Mode=TwoWay}"
Expand All @@ -22,7 +22,7 @@
<TextBox x:Name="NewTagTextBox" Width="200" Margin="0,10,0,0" Text="{Binding NewTagName, UpdateSourceTrigger=PropertyChanged}"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,10,0,0">
<Button Command="{Binding CancelCommand}" Content="Cancel"/>
<Button Command="{Binding RemoveTagCommand}" Content="Remove Tag"/>
<Button Command="{Binding RemoveTagCommand}" Content="Delete Tag" CommandParameter="{Binding SelectedItem, ElementName=TagsListBox}"/>
<Button Command="{Binding AddTagCommand}" Content="Add Tag" CommandParameter="{Binding Text, ElementName=NewTagTextBox}"/>
</StackPanel>
</StackPanel>
Expand Down
29 changes: 2 additions & 27 deletions DeFRaG_Helper/UserControls/TagManager.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,18 @@ namespace DeFRaG_Helper.UserControls
{
public partial class TagManager : UserControl
{
private Window _associatedWindow;
private bool _isInternalClick = false;
public bool IsInternalClick { get; set; } = false;

public TagManager(TagManagerViewModel viewModel)
{
InitializeComponent();
DataContext = viewModel;

// Handle Loaded event to get the associated window
this.Loaded += OnLoaded;
// Handle LostFocus event
this.LostFocus += OnLostFocus;
}

private void OnLoaded(object sender, RoutedEventArgs e)
{
_associatedWindow = Window.GetWindow(this);
}

private void OnLostFocus(object sender, RoutedEventArgs e)
{
if (_associatedWindow != null && _associatedWindow.IsVisible && !_isInternalClick)
{
_associatedWindow.Close();
}

// Reset the internal click flag
_isInternalClick = false;

// Bring the main window to the foreground
Application.Current.MainWindow.Activate();
}

private void CheckBox_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
// Set the flag to indicate an internal click
_isInternalClick = true;
IsInternalClick = true;
}

private void CheckBox_Checked(object sender, RoutedEventArgs e)
Expand Down
24 changes: 0 additions & 24 deletions DeFRaG_Helper/ViewModels/MapViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,30 +233,6 @@ public Map SelectedMap
{
_selectedMap = value;
OnPropertyChanged(nameof(SelectedMap)); // Notify if you're implementing INotifyPropertyChanged
try
{
if (value != null)
{
Debug.WriteLine($"Selected map: {value.Name}");
}
else
{
Debug.WriteLine("Selected map is null.");
}
}
catch (HttpRequestException ex)
{
// Log the exception details
Debug.WriteLine($"Request error: {ex.Message}");
Debug.WriteLine($"Stack Trace: {ex.StackTrace}");
// Optionally, handle the exception as needed
}
catch (Exception ex)
{
// Log any other exceptions
Debug.WriteLine($"Unexpected error: {ex.Message}");
Debug.WriteLine($"Stack Trace: {ex.StackTrace}");
}
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions DeFRaG_Helper/ViewModels/TagBarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,23 @@ protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

public void AddTag(TagItem tagItem)
{
if (!Tags.Any(t => t.Name == tagItem.Name))
{
Tags.Insert(0, tagItem); // Insert at the beginning
}
}

public void RemoveTag(string tagName)
{
var tagItem = Tags.FirstOrDefault(t => t.Name == tagName);
if (tagItem != null)
{
Tags.Remove(tagItem);
}
}
}

public class TagItem
Expand Down
33 changes: 31 additions & 2 deletions DeFRaG_Helper/ViewModels/TagManagerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ private void AddTag(object parameter)
{
if (!string.IsNullOrEmpty(NewTagName) && !Tags.Any(t => t.Name == NewTagName))
{
Tags.Add(new TagTextItem { Name = NewTagName, IsChecked = true });
var newTagItem = new TagTextItem { Name = NewTagName, IsChecked = true };
Tags.Add(newTagItem);
_map.Tags.Add(NewTagName);
AddTagToDatabase(NewTagName);

// Update TagBarViewModel
TagBarViewModel.Instance.AddTag(new TagItem { Name = NewTagName });
}
}

Expand All @@ -67,10 +71,35 @@ private void RemoveTag(object parameter)
{
Tags.Remove(tagItem);
_map.Tags.Remove(tagItem.Name);
UpdateTagsInDatabase();
DeleteTagFromDatabase(tagItem.Name);

// Update TagBarViewModel
TagBarViewModel.Instance.RemoveTag(tagItem.Name);
}
}

private void DeleteTagFromDatabase(string tagName)
{
DbQueue.Instance.Enqueue(async connection =>
{
// Delete the tag from the MapTag table
using (var deleteMapTagCommand = new SqliteCommand("DELETE FROM MapTag WHERE TagID = (SELECT TagID FROM Tag WHERE Tag = @tag)", connection))
{
deleteMapTagCommand.Parameters.AddWithValue("@tag", tagName);
await deleteMapTagCommand.ExecuteNonQueryAsync();
}
// Delete the tag from the Tag table
using (var deleteTagCommand = new SqliteCommand("DELETE FROM Tag WHERE Tag = @tag", connection))
{
deleteTagCommand.Parameters.AddWithValue("@tag", tagName);
await deleteTagCommand.ExecuteNonQueryAsync();
}
});
}



private void Cancel(object parameter)
{
RequestClose?.Invoke(this, EventArgs.Empty);
Expand Down

0 comments on commit 2856050

Please sign in to comment.