Skip to content

Commit

Permalink
Cherry pick update notifications number (#13246)
Browse files Browse the repository at this point in the history
* DYN-5189 update notifications number (#13232)

* fetching the notifications and counting the number

* setting shortcut viewmodel to internal

* setting shortcut viewmodel to internal
  • Loading branch information
filipeotero authored Aug 25, 2022
1 parent 7240196 commit e1e7555
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/DynamoCoreWpf/Controls/ShortcutToolbar.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,13 @@
Width="14"
VerticalAlignment="Center"
HorizontalAlignment="Center">
<!--TODO insert data bind for the notifications -->
<TextBlock Name="NotificationCount"
FontFamily="{StaticResource ArtifaktElementRegular}"
Foreground="White"
FontWeight="DemiBold"
TextAlignment="Center"
FontSize="9">8
Text="{Binding NotificationsNumber}"
FontSize="9">
</TextBlock>
</Border>
</DockPanel>
Expand Down
4 changes: 4 additions & 0 deletions src/DynamoCoreWpf/Controls/ShortcutToolbar.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Dynamo.UI.Commands;
using Dynamo.Updates;
using Dynamo.ViewModels;
using Dynamo.Wpf.ViewModels.Core;
using Microsoft.Practices.Prism.ViewModel;

namespace Dynamo.UI.Controls
Expand Down Expand Up @@ -45,6 +46,9 @@ public ShortcutToolbar(IUpdateManager updateManager)

InitializeComponent();
UpdateControl.DataContext = updateManager;

var shortcutToolbar = new ShortcutToolbarViewModel();
DataContext = shortcutToolbar;
}

private void exportMenu_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
Expand Down
1 change: 1 addition & 0 deletions src/DynamoCoreWpf/DynamoCoreWpf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@
<Compile Include="ViewModels\Core\InPortViewModel.cs" />
<Compile Include="ViewModels\Core\OutPortViewModel.cs" />
<Compile Include="ViewModels\Core\SerializationExtensions.cs" />
<Compile Include="ViewModels\Core\ShortcutToolbarViewModel.cs" />
<Compile Include="ViewModels\FileTrust\FileTrustWarningViewModel.cs" />
<Compile Include="ViewModels\GuidedTour\ExitGuideWindowViewModel.cs" />
<Compile Include="ViewModels\GuidedTour\PopupWindowViewModel.cs" />
Expand Down
17 changes: 17 additions & 0 deletions src/DynamoCoreWpf/ViewModels/Core/ShortcutToolbarViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Dynamo.ViewModels;

namespace Dynamo.Wpf.ViewModels.Core
{
internal class ShortcutToolbarViewModel : ViewModelBase
{
public ShortcutToolbarViewModel()
{
NotificationsNumber = 0;
}

/// <summary>
/// This property represents the number of new notifications
/// </summary>
public int NotificationsNumber { get; set; }
}
}
50 changes: 48 additions & 2 deletions src/Notifications/NotificationCenterController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
Expand All @@ -9,6 +13,8 @@
using Dynamo.Logging;
using Dynamo.Notifications.View;
using Dynamo.ViewModels;
using Dynamo.Wpf.ViewModels.Core;
using Newtonsoft.Json;

namespace Dynamo.Notifications
{
Expand All @@ -26,7 +32,11 @@ public class NotificationCenterController
private static readonly string jsEmbeddedFile = "Dynamo.Notifications.node_modules._dynamods.notifications_center.build.index.bundle.js";
private static readonly string NotificationCenterButtonName = "notificationsButton";

private readonly DynamoLogger logger;
private DynamoLogger logger;
private static readonly DateTime notificationsCenterCreatedTime = DateTime.UtcNow;
private static System.Timers.Timer timer;
private string jsonStringFile;
private NotificationsModel notificationsModel;

internal NotificationCenterController(DynamoView view, DynamoLogger dynLogger)
{
Expand All @@ -46,9 +56,45 @@ internal NotificationCenterController(DynamoView view, DynamoLogger dynLogger)
HorizontalOffset = notificationPopupHorizontalOffset,
VerticalOffset = notificationPopupVerticalOffset
};

notificationUIPopup.webView.EnsureCoreWebView2Async();
notificationUIPopup.webView.CoreWebView2InitializationCompleted += WebView_CoreWebView2InitializationCompleted;
logger = dynLogger;

RequestNotifications();
}

private void WebView_NavigationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs e)
{
AddNotifications(notificationsModel.Notifications);
}

private void AddNotifications(List<NotificationItemModel> notifications)
{
var notificationsList = JsonConvert.SerializeObject(notifications);
InvokeJS($"window.setNotifications({notificationsList});");
}

private void RequestNotifications()
{
var uri = DynamoUtilities.PathHelper.getServiceBackendAddress(this, "notificationAddress");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
jsonStringFile = reader.ReadToEnd();
notificationsModel = JsonConvert.DeserializeObject<NotificationsModel>(jsonStringFile);

var notificationsNumber = notificationsModel.Notifications.Count();

var shortcutToolbarViewModel = (ShortcutToolbarViewModel)dynamoView.ShortcutBar.DataContext;
shortcutToolbarViewModel.NotificationsNumber = notificationsNumber;
}

notificationUIPopup.webView.NavigationCompleted += WebView_NavigationCompleted;
}

// Handler for new Webview2 tab window request
Expand Down Expand Up @@ -84,7 +130,6 @@ private void WebView_CoreWebView2InitializationCompleted(object sender, Microsof
// Opening hyper-links using default system browser instead of WebView2 tab window
notificationUIPopup.webView.CoreWebView2.NewWindowRequested += WebView_NewWindowRequested;
notificationUIPopup.webView.CoreWebView2.NavigateToString(htmlString);
RefreshNotifications();
}
}

Expand All @@ -98,6 +143,7 @@ internal void Dispose()
dynamoView.SizeChanged -= DynamoView_SizeChanged;
dynamoView.LocationChanged -= DynamoView_LocationChanged;
notificationsButton.Click -= NotificationsButton_Click;
notificationUIPopup.webView.NavigationCompleted -= WebView_NavigationCompleted;
}

private void DynamoView_LocationChanged(object sender, EventArgs e)
Expand Down
65 changes: 65 additions & 0 deletions src/Notifications/NotificationsModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Dynamo.Notifications
{
[DataContract]
internal class NotificationsModel
{
[DataMember(Name = "version")]
internal string Version { get; set; }

[DataMember(Name = "last_update_timestamp")]
internal DateTime LastUpdate { get; set; }

[DataMember(Name = "notifications")]
internal List<NotificationItemModel> Notifications { get; set; }
}

[DataContract]
internal class NotificationItemModel
{
[DataMember(Name = "id")]
internal string Id { get; set; }

[DataMember(Name = "title")]
internal string Title { get; set; }

[DataMember(Name = "link")]
internal string Link { get; set; }

[DataMember(Name = "linkTitle")]
internal string LinkTitle { get; set; }

[DataMember(Name = "longDescription")]
internal string LongDescription { get; set; }

[DataMember(Name = "created")]
internal DateTime Created { get; set; }

[DataMember(Name = "thumbnail")]
internal string Thumbnail { get; set; }

[DataMember(Name = "priority")]
internal string Priority { get; set; }

[DataMember(Name = "type")]
internal string Type { get; set; }

[DataMember(Name = "scope")]
internal string Scope { get; set; }

[DataMember(Name = "source")]
internal string Source { get; set; }

[DataMember(Name = "status")]
internal string Status { get; set; }

[DataMember(Name = "isPinned")]
internal bool IsPinned { get; set; }
}
}

0 comments on commit e1e7555

Please sign in to comment.