private async void CheckNotificationsCount() { if (!IsInternetAvailable()) { ShowMessage("Check your internet connection and try again", MessageType.Error); } try { while (true) { int count = 0; await Task.Delay(200); DatabaseConnectivity dc = new DatabaseConnectivity(); try { await Task.Run(() => { if (dc.Connect()) { MySqlCommand command = new MySqlCommand(); this.Dispatcher.Invoke(() => { command.Parameters.AddWithValue("@id", UserIdLB.Content); }); dc.ExecuteReader(command, "SELECT * FROM notifications WHERE AccountId=@id and IsRead=0"); MySqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { count++; } var template = ViewNotificationsB.Template; TextBlock notificationCount = (TextBlock)template.FindName("NotificationCountTBl", ViewNotificationsB); Border notificationBorder = (Border)template.FindName("NotificationBorder", ViewNotificationsB); if (count > 0) { this.Dispatcher.Invoke(() => { notificationBorder.Visibility = Visibility.Visible; notificationCount.Text = count.ToString(); }); } else { this.Dispatcher.Invoke(() => { if (notificationBorder.Visibility == Visibility.Visible) { notificationBorder.Visibility = Visibility.Collapsed; } }); } dc.Unconnect(); } else { } }); } catch { } CurrentNotificationsCount = count; } } catch { ShowMessage("Check your internet connection and try again", MessageType.Error); } }
private async void ViewNotificationsB_Click(object sender, RoutedEventArgs e) { ClearAllNotificationB.Visibility = Visibility.Collapsed; NotificationsSP.Children.Clear(); NotificationsSP.Children.Add(NotificationsLoaderCL); TranslateTransform tt = new TranslateTransform(); tt.X = 0; tt.Y = 0; NotificationsSP.RenderTransform = tt; NotificationsLoaderCL.Visibility = Visibility.Visible; NotificationsG.Visibility = Visibility.Visible; NotificationsLoaderCL.Start(); int count = 0; List <NotificationItem> notificationItems = new List <NotificationItem>(); try { await Task.Run(new Action(() => { try { DatabaseConnectivity dc = new DatabaseConnectivity(); dc.Connect(); MySqlCommand command = new MySqlCommand(); this.Dispatcher.Invoke(() => { command.Parameters.AddWithValue("@id", UserIdLB.Content); }); dc.ExecuteReader(command, "SELECT * FROM notifications WHERE AccountId=@id"); MySqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { count++; this.Dispatcher.Invoke(() => { NotificationItem ni = new NotificationItem(); ni.Padding = new Thickness(10); ni.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFF9F9F9")); ni.Notification = reader["Message"].ToString(); ni.NotificationDate = Convert.ToDateTime(reader["Date"]); ni.NotificationId = Convert.ToInt32(reader["NotificationId"]); notificationItems.Add(ni); }); DatabaseConnectivity dc1 = new DatabaseConnectivity(); dc1.Connect(); this.Dispatcher.Invoke(() => { dc1.ExecuteNonQuery("UPDATE notifications Set IsRead=1 WHERE AccountId=" + UserIdLB.Content); }); dc1.Unconnect(); } } catch (Exception) { this.Dispatcher.Invoke(() => { ShowMessage("Check your internet connection and try again", MessageType.Error); }); count = -1; } })); } catch { NotificationsLoaderCL.Visibility = Visibility.Collapsed; NotificationsLoaderCL.Stop(); ClearAllNotificationB.Visibility = Visibility.Visible; NotificationsG.Visibility = Visibility.Collapsed; ShowMessage("Check your internet connection and try again", MessageType.Error); } if (!count.Equals(-1)) { if (count.Equals(0)) { TextBlock tb = new TextBlock(); tb.Padding = new Thickness(25); tb.FontSize = 15; tb.Text = "No Notification..."; tb.VerticalAlignment = VerticalAlignment.Center; tb.HorizontalAlignment = HorizontalAlignment.Center; tb.FontFamily = new FontFamily(new Uri(@"pack:\\,,,\Resources\Fonts\Nunito-SemiBold.ttf"), "Nunito SemiBold"); NotificationsSP.Children.Add(tb); ClearAllNotificationB.Visibility = Visibility.Collapsed; } foreach (NotificationItem ni in notificationItems) { NotificationsSP.Children.Insert(0, ni); } //for (int i = 0; i < CurrentNotificationsCount; i++) //{ // ControlTemplate template = notificationItems[i].Template; // Border border = (Border)template.FindName("border", notificationItems[i]); // border.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFE0EBFF")); ; //} NotificationsLoaderCL.Visibility = Visibility.Collapsed; NotificationsLoaderCL.Stop(); if (!count.Equals(0)) { ClearAllNotificationB.Visibility = Visibility.Visible; } } else { NotificationsLoaderCL.Visibility = Visibility.Collapsed; NotificationsLoaderCL.Stop(); NotificationsG.Visibility = Visibility.Collapsed; } }