-using System.ComponentModel;
+using System;
+using System.ComponentModel;
+using Tizen.Applications.NotificationEventListener;
+using Tizen.NUI.Binding;
+using Notifications.Common;
namespace Notifications.Models
{
class NotificationsModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
- public NotificationsModel(string title, string content)
+
+ private readonly string appId;
+ private readonly int uniqueNumber;
+
+ public NotificationsModel(string appId, int uniqueNumber, string title, string content)
{
+ this.appId = appId;
+ this.uniqueNumber = uniqueNumber;
this.title = title ?? string.Empty;
subTitle = content ?? string.Empty;
+ ClearNotificationCommand = new Command(OnNotificationCleared);
}
private void OnPropertyChanged(string propertyName)
OnPropertyChanged("SubTitle");
}
}
+
+ private Command clearNotificationCommand;
+ public Command ClearNotificationCommand
+ {
+ get => clearNotificationCommand;
+ set
+ {
+ clearNotificationCommand = value;
+ OnPropertyChanged("ClearNotificationCommand");
+ }
+ }
+
+ private void OnNotificationCleared()
+ {
+ try
+ {
+ NotificationListenerManager.Delete(appId, uniqueNumber);
+ }
+ catch (Exception ex)
+ {
+ Tizen.Log.Error(AppConstants.LogTag, "Exception: " + ex.Message);
+ }
+ }
}
}
-namespace Notifications.ViewModels
+using System;
+using System.ComponentModel;
+using Tizen.Applications.NotificationEventListener;
+using Tizen.NUI;
+using Tizen.NUI.Binding;
+
+namespace Notifications.ViewModels
{
- class NotificationsViewModel
+ class NotificationsViewModel : IDisposable, INotifyPropertyChanged
{
+ public event PropertyChangedEventHandler PropertyChanged;
+
public NotificationsViewModel()
{
NotificationsListSource = new NotificationsListViewModel();
+ IsNotificationsPresent = NotificationsListSource.Count > 0;
+
+ BackCommand = new Command(OnBackPressed);
+ ClearAllNotificationsCommand = new Command(OnAllNotificationsCleared);
+
+ NotificationListenerManager.Added += OnNotificationAdded;
+ NotificationListenerManager.Deleted += OnNotificationDeleted;
+ NotificationListenerManager.Updated += OnNotificationUpdated;
}
+
public NotificationsListViewModel NotificationsListSource { get; set; }
+
+ private bool isNotificationsPresent;
+ public bool IsNotificationsPresent
+ {
+ get => isNotificationsPresent;
+ set
+ {
+ isNotificationsPresent = value;
+ OnPropertyChanged("IsNotificationsPresent");
+ }
+ }
+
+ public void Dispose()
+ {
+ NotificationListenerManager.Added -= OnNotificationAdded;
+ NotificationListenerManager.Deleted -= OnNotificationDeleted;
+ NotificationListenerManager.Updated -= OnNotificationUpdated;
+ NotificationsListSource.Clear();
+ NotificationsListSource = null;
+ }
+
+ private void OnPropertyChanged(string propertyName)
+ {
+ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
+ }
+
+ private void OnNotificationUpdated(object sender, NotificationEventArgs e)
+ {
+ NotificationsListSource.CreateData();
+ IsNotificationsPresent = NotificationsListSource.Count > 0;
+ }
+
+ private void OnNotificationDeleted(object sender, NotificationDeleteEventArgs e)
+ {
+ NotificationsListSource.CreateData();
+ IsNotificationsPresent = NotificationsListSource.Count > 0;
+ }
+
+ private void OnNotificationAdded(object sender, NotificationEventArgs e)
+ {
+ NotificationsListSource.CreateData();
+ IsNotificationsPresent = NotificationsListSource.Count > 0;
+ }
+
+ private Command backCommand;
+ public Command BackCommand
+ {
+ get => backCommand;
+ set
+ {
+ backCommand = value;
+ OnPropertyChanged("BackCommand");
+ }
+ }
+
+ private void OnBackPressed()
+ {
+ NUIApplication.Current.Exit();
+ }
+
+ private Command clearAllNotificationsCommand;
+ public Command ClearAllNotificationsCommand
+ {
+ get => clearAllNotificationsCommand;
+ set
+ {
+ clearAllNotificationsCommand = value;
+ OnPropertyChanged("ClearAllNotificationsCommand");
+ }
+ }
+
+ private void OnAllNotificationsCleared()
+ {
+ NotificationListenerManager.DeleteAll();
+ }
}
}
{
class BaseView : View
{
+ public static BindableProperty IsContentAvailableProperty = BindableProperty.Create(nameof(IsContentAvailable), typeof(bool), typeof(BaseView), false, propertyChanged: (bindable, oldValue, newValue) =>
+ {
+ BaseView instance = bindable as BaseView;
+ if (instance == null)
+ {
+ return;
+ }
+ if (newValue != oldValue)
+ {
+ instance.isContentAvailable = (bool)newValue;
+ instance.UpdateContent();
+ }
+ }, defaultValueCreator: (bindable) => (bindable as BaseView).isContentAvailable);
+
private View topView;
private Button backButton;
private TextLabel titleText;
private CollectionView notificationsView;
+ private View bottomView;
+ private Button clearAllButton;
+ private TextLabel noNotificationsText;
+
public BaseView() : base()
{
Size2D = new Size2D(Window.Instance.WindowSize.Width, Window.Instance.WindowSize.Height);
LinearOrientation = LinearLayout.Orientation.Vertical,
HorizontalAlignment = HorizontalAlignment.Begin,
VerticalAlignment = VerticalAlignment.Top,
+ Padding = AppConstants.BaseViewPadding,
};
AddTopView();
- AddNotificationsView();
-
+ UpdateContent();
+ this.SetBinding(IsContentAvailableProperty, "IsNotificationsPresent");
}
private void AddTopView()
backButton.Icon.Size2D = AppConstants.IconSize.SpToPx();
RelativeLayout.SetVerticalAlignment(backButton, RelativeLayout.Alignment.Center);
RelativeLayout.SetHorizontalAlignment(backButton, RelativeLayout.Alignment.Start);
+ backButton.SetBinding(Control.CommandProperty, "BackCommand");
topView.Add(backButton);
titleText = new TextLabel()
topView.Add(titleText);
}
+ private void UpdateContent()
+ {
+ RemoveContent();
+
+ if (IsContentAvailable == true)
+ {
+ AddNotificationsView();
+ AddBottomView();
+ }
+ else
+ {
+ AddNoNotificationsText();
+ }
+ }
+
+ private void RemoveContent()
+ {
+ Remove(noNotificationsText);
+ Remove(notificationsView);
+ Remove(bottomView);
+ }
+
private void AddNotificationsView()
{
- notificationsView = new CollectionView()
+ if (notificationsView == null)
{
- ItemsLayouter = new LinearLayouter(),
- ItemTemplate = new DataTemplate(() =>
+ notificationsView = new CollectionView()
{
- DefaultLinearItem item = new DefaultLinearItem();
- item.WidthSpecification = LayoutParamPolicies.MatchParent;
- item.Label.SetBinding(TextLabel.TextProperty, "Title");
- item.Label.HorizontalAlignment = HorizontalAlignment.Begin;
- item.Label.FontFamily = "BreezeSans";
- item.SubLabel.SetBinding(TextLabel.TextProperty, "SubTitle");
- item.SubLabel.HorizontalAlignment = HorizontalAlignment.Begin;
- item.SubLabel.FontFamily = "BreezeSans";
- return item;
- }),
- ScrollingDirection = ScrollableBase.Direction.Vertical,
- WidthSpecification = LayoutParamPolicies.MatchParent,
- HeightSpecification = LayoutParamPolicies.MatchParent,
- SelectionMode = ItemSelectionMode.Single,
- };
- notificationsView.SetBinding(RecyclerView.ItemsSourceProperty, "NotificationsListSource");
+ ItemsLayouter = new LinearLayouter(),
+ ItemTemplate = new DataTemplate(() =>
+ {
+ DefaultLinearItem item = new DefaultLinearItem();
+ item.WidthSpecification = LayoutParamPolicies.MatchParent;
+ item.Label.SetBinding(TextLabel.TextProperty, "Title");
+ item.Label.HorizontalAlignment = HorizontalAlignment.Begin;
+ item.Label.FontFamily = "BreezeSans";
+ item.SubLabel.SetBinding(TextLabel.TextProperty, "SubTitle");
+ item.SubLabel.HorizontalAlignment = HorizontalAlignment.Begin;
+ item.SubLabel.FontFamily = "BreezeSans";
+ item.Extra = new Button()
+ {
+ Size2D = AppConstants.SmallIconSize.SpToPx().SpToPx(),
+ IconURL = Resources.GetImagePath() + "/light/clear.png",
+ BackgroundColor = Color.Transparent,
+ };
+ (item.Extra as Button).Icon.Size2D = AppConstants.SmallIconSize.SpToPx().SpToPx();
+ item.Extra.SetBinding(Control.CommandProperty, "ClearNotificationCommand");
+ return item;
+ }),
+ ScrollingDirection = ScrollableBase.Direction.Vertical,
+ WidthSpecification = LayoutParamPolicies.MatchParent,
+ HeightSpecification = LayoutParamPolicies.MatchParent,
+ SelectionMode = ItemSelectionMode.Single,
+ };
+ notificationsView.SetBinding(RecyclerView.ItemsSourceProperty, "NotificationsListSource");
+ }
Add(notificationsView);
}
+
+ private void AddBottomView()
+ {
+ if (bottomView == null)
+ {
+ bottomView = new View()
+ {
+ WidthSpecification = LayoutParamPolicies.MatchParent,
+ HeightSpecification = AppConstants.HeaderHeight.SpToPx(),
+ Layout = new RelativeLayout()
+ {
+ Padding = AppConstants.HeaderPadding.SpToPx(),
+ },
+ };
+ }
+ if (clearAllButton == null)
+ {
+ clearAllButton = new Button()
+ {
+ Size2D = AppConstants.TextButtonSize.SpToPx(),
+ Text = "Clear All",
+ TextColor = new Color("#FF6200"),
+ FontFamily = "BreezeSans",
+ BackgroundColor = Color.Transparent,
+ TextAlignment = HorizontalAlignment.Center,
+ };
+ RelativeLayout.SetVerticalAlignment(clearAllButton, RelativeLayout.Alignment.Center);
+ RelativeLayout.SetHorizontalAlignment(clearAllButton, RelativeLayout.Alignment.End);
+ clearAllButton.SetBinding(Control.CommandProperty, "ClearAllNotificationsCommand");
+ bottomView.Add(clearAllButton);
+ }
+ Add(bottomView);
+ }
+
+ private void AddNoNotificationsText()
+ {
+ if (noNotificationsText == null)
+ {
+ noNotificationsText = new TextLabel()
+ {
+ Text = "You don't have notifications",
+ HorizontalAlignment = HorizontalAlignment.Center,
+ VerticalAlignment = VerticalAlignment.Center,
+ FontFamily = "BreezeSans",
+ PixelSize = AppConstants.TitlePixelSize.SpToPx(),
+ TextColor = new Color("#090E21"),
+ WidthSpecification = LayoutParamPolicies.MatchParent,
+ HeightSpecification = LayoutParamPolicies.MatchParent,
+ };
+ }
+ Add(noNotificationsText);
+ }
+
+ private bool isContentAvailable;
+ public bool IsContentAvailable
+ {
+ get => (bool)GetValue(IsContentAvailableProperty);
+ set => SetValue(IsContentAvailableProperty, value);
+ }
}
}