Skip to content

Commit 22e60b1

Browse files
committed
added close button to notifications
1 parent ae6d86c commit 22e60b1

File tree

5 files changed

+99
-28
lines changed

5 files changed

+99
-28
lines changed

CutCode/Interfaces/INotificationManager.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,24 @@ public interface INotificationManager
1111
{
1212
event EventHandler ShowNotification;
1313
void CreateNotification(string message, int delay);
14+
void CloseNotification(NotifyObject notification);
15+
event EventHandler OnCloseNotification;
1416
}
1517

1618
public class NotificationManager : INotificationManager
1719
{
1820
public event EventHandler ShowNotification;
1921
public void CreateNotification(string message, int delay)
2022
{
21-
var notify = new Object(){ Message = message, Delay= delay};
23+
var notify = new NotifyObject(){ Message = message, Delay= delay};
2224
ShowNotification?.Invoke(notify, EventArgs.Empty);
2325
}
26+
27+
public event EventHandler OnCloseNotification;
28+
public void CloseNotification(NotifyObject notification) => OnCloseNotification?.Invoke(notification, EventArgs.Empty);
2429
}
2530

26-
public class Object : PropertyChangedBase
31+
public class NotifyObject : PropertyChangedBase
2732
{
2833
public string Message { get; set; }
2934
public int Delay { get; set; }

CutCode/ViewModels/MainViewModel.cs

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public MainViewModel(IWindowManager _windowManager,IThemeService themeService, I
3333

3434
notifyManager = _notifyManager;
3535
notifyManager.ShowNotification += showNotification;
36-
notificationList = new ObservableCollection<Object>();
36+
notifyManager.OnCloseNotification += exitNotification;
37+
notificationList = new ObservableCollection<NotifyObject>();
3738

3839
_themeService = themeService;
3940
_themeService.ThemeChanged += ThemeChanged;
@@ -102,6 +103,11 @@ public void ChangePageCommand(string selected_item)
102103

103104
sideBarBtns[ind].background = _themeService.IsLightTheme ? ColorCon.Convert("#FCFCFC") : ColorCon.Convert("#36393F");
104105
if (currentPage != Pages[ind]) pageService.Page = Pages[ind];
106+
107+
if(selected_item == "Settings")
108+
{
109+
notifyManager.CreateNotification("Simple test", 10);
110+
}
105111
}
106112
#region Color
107113
private SolidColorBrush _backgroundColor;
@@ -195,20 +201,20 @@ public string minImage
195201
}
196202

197203
#region NotificationDialogView
198-
private ObservableCollection<Object> _notificationList;
199-
public ObservableCollection<Object> notificationList
204+
private ObservableCollection<NotifyObject> _notificationList;
205+
public ObservableCollection<NotifyObject> notificationList
200206
{
201207
get => _notificationList;
202208
set => SetAndNotify(ref _notificationList, value);
203209
}
204210

205-
private List<Object> WaitingNotifications = new List<Object>();
211+
private List<NotifyObject> WaitingNotifications = new List<NotifyObject>();
206212
private void showNotification(object sender, EventArgs e)
207213
{
208-
var notification = sender as Object;
214+
var notification = sender as NotifyObject;
209215

210-
var notifcationViewModel = new NotificationDialogViewModel(_themeService, notification.Message);
211-
notification.View = (System.Object)notifcationViewModel;
216+
var notifcationViewModel = new NotificationDialogViewModel(_themeService, notifyManager, notification);
217+
notification.View = (Object)notifcationViewModel;
212218

213219
if(notificationList.Count > 2)
214220
{
@@ -227,16 +233,33 @@ private void showNotification(object sender, EventArgs e)
227233
}
228234
}
229235

236+
private void exitNotification(object sender, EventArgs e)
237+
{
238+
var notification = sender as NotifyObject;
239+
notificationList.Remove(notification);
240+
UpdateNotification();
241+
}
242+
230243
private void CloseNotification(object sender, EventArgs e)
231244
{
232-
notificationList.RemoveAt(0);
233-
if (WaitingNotifications.Count != 0)
245+
if(notificationList.Count > 0) notificationList.RemoveAt(0);
246+
UpdateNotification();
247+
(sender as DispatcherTimer).Stop();
248+
}
249+
250+
private void UpdateNotification()
251+
{
252+
if (WaitingNotifications.Count > 0)
234253
{
235-
var notification = WaitingNotifications[0];
236-
WaitingNotifications.RemoveAt(0);
237-
notifyManager.CreateNotification(notification.Message, notification.Delay);
254+
for (int i = 0; i < (3 - notificationList.Count); i++)
255+
{
256+
if (WaitingNotifications.Count == 0) break;
257+
258+
var notification = WaitingNotifications[i];
259+
WaitingNotifications.RemoveAt(i);
260+
notifyManager.CreateNotification(notification.Message, notification.Delay);
261+
}
238262
}
239-
(sender as DispatcherTimer).Stop();
240263
}
241264
#endregion
242265
}

CutCode/ViewModels/NotificationDialogViewModel.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@ namespace CutCode
1111
public class NotificationDialogViewModel : Screen
1212
{
1313
private readonly IThemeService themeService;
14-
public NotificationDialogViewModel(IThemeService _themeService, string _message)
14+
private readonly INotificationManager notifyManager;
15+
private NotifyObject notification;
16+
public NotificationDialogViewModel(IThemeService _themeService, INotificationManager _notifyManager, NotifyObject _notification)
1517
{
16-
message = _message;
18+
notifyManager = _notifyManager;
19+
20+
notification = _notification;
21+
message = notification.Message;
1722
themeService = _themeService;
1823
SetAppearance(null, null);
1924
themeService.ThemeChanged += SetAppearance;
@@ -23,6 +28,8 @@ private void SetAppearance(object sender, EventArgs e)
2328
{
2429
background = themeService.IsLightTheme ? ColorCon.Convert("#DCDCDC") : ColorCon.Convert("#26292F");
2530
textColor = themeService.IsLightTheme ? ColorCon.Convert("#060607") : ColorCon.Convert("#F0F0F0");
31+
exitImage = themeService.IsLightTheme ? "../Resources/Images/Icons/exit_black.png" : "../Resources/Images/Icons/exit_white.png";
32+
exitBtnHoverColor = themeService.IsLightTheme ? ColorCon.Convert("#D0D1D2") : ColorCon.Convert("#1F232B");
2633
}
2734

2835
private string _message;
@@ -53,5 +60,27 @@ public SolidColorBrush textColor
5360
}
5461
}
5562
}
63+
64+
private string _exitImage;
65+
public string exitImage
66+
{
67+
get => _exitImage;
68+
set
69+
{ SetAndNotify(ref _exitImage, value); }
70+
}
71+
72+
private SolidColorBrush _exitBtnHoverColor;
73+
public SolidColorBrush exitBtnHoverColor
74+
{
75+
get => _exitBtnHoverColor;
76+
set
77+
{
78+
if (value != _exitBtnHoverColor)
79+
{
80+
SetAndNotify(ref _exitBtnHoverColor, value);
81+
}
82+
}
83+
}
84+
public void ExitCommand() => notifyManager.CloseNotification(notification);
5685
}
5786
}

CutCode/ViewModels/SettingViewModel.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ public void SyncCommand(string syncType)
102102
fileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
103103
fileDialog.DefaultExt = "whl";
104104
fileDialog.Filter = "whl files (*.whl)|*.whl";
105-
fileDialog.ShowDialog();
105+
var dialogResult = fileDialog.ShowDialog();
106106

107-
if (!string.IsNullOrEmpty(fileDialog.FileName))
107+
if (dialogResult != DialogResult.Cancel && !string.IsNullOrEmpty(fileDialog.FileName))
108108
{
109109
message = database.ImportData(fileDialog.FileName);
110110
}
@@ -116,8 +116,8 @@ public void SyncCommand(string syncType)
116116
fileDialog.DefaultExt = "whl";
117117
fileDialog.FileName = $"Export_CutCode_{DateTime.Now.ToString("yyyyMMddhhmmss")}.whl";
118118
fileDialog.Filter = "whl files (*.whl)|*.whl";
119-
fileDialog.ShowDialog();
120-
if (!string.IsNullOrEmpty(fileDialog.FileName))
119+
var dialogResult = fileDialog.ShowDialog();
120+
if (dialogResult != DialogResult.Cancel)
121121
{
122122
message = database.ExportData(fileDialog.FileName);
123123
}

CutCode/Views/NotificationDialogView.xaml

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,30 @@
3636
</Style>
3737
</UserControl.Resources>
3838

39-
<Border Background="{Binding background}" CornerRadius="8"
39+
<Border Background="{Binding background}" CornerRadius="6"
4040
Height="50" MinWidth="230">
4141
<Grid>
42-
<Grid.RowDefinitions>
43-
<RowDefinition Height="30"/>
44-
<RowDefinition Height="Auto"/>
45-
</Grid.RowDefinitions>
46-
<Label Grid.Row="0" FontSize="10.5" FontFamily="{StaticResource poppins_semibold}" Content="Notification" Foreground="{Binding textColor}"/>
47-
<Label Grid.Row="1" Foreground="{Binding textColor}" Content="{Binding message}" Margin="0,-12,0,0"
42+
<Grid.ColumnDefinitions>
43+
<ColumnDefinition Width="*"/>
44+
<ColumnDefinition Width="Auto"/>
45+
</Grid.ColumnDefinitions>
46+
47+
<Grid Grid.Column="0">
48+
<Grid.RowDefinitions>
49+
<RowDefinition Height="30"/>
50+
<RowDefinition Height="Auto"/>
51+
</Grid.RowDefinitions>
52+
<Label Grid.Row="0" FontSize="10.5" FontFamily="{StaticResource poppins_semibold}" Content="Notification" Foreground="{Binding textColor}"/>
53+
<Label Grid.Row="1" Foreground="{Binding textColor}" Content="{Binding message}" Margin="0,-12,0,0"
4854
FontSize="14" FontFamily="{StaticResource poppins_regular}"/>
55+
</Grid>
56+
57+
<Button Grid.Column="1" Background="Transparent"
58+
Style="{DynamicResource ButtonStyle}" Command="{s:Action ExitCommand}" Margin="0,0,5,0"
59+
BorderBrush="{Binding exitBtnHoverColor}" VerticalAlignment="Center" Height="20" Width="20">
60+
<Image Width="15" Height="15" Source="{Binding exitImage}"
61+
RenderOptions.BitmapScalingMode="HighQuality" RenderOptions.EdgeMode="Aliased"/>
62+
</Button>
4963
</Grid>
5064
</Border>
5165
</UserControl>

0 commit comments

Comments
 (0)