using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using System.Windows.Input;
using WindowsPhone.Recipes.Push.Server.Services;
using System.Windows;
using WindowsPhone.Recipes.Push.Server.Models;
using WindowsPhone.Recipes.Push.Messasges;
namespace WindowsPhone.Recipes.Push.Server.ViewModels
{
///
/// Represents a base class for all push pattern view models.
///
///
/// A push pattern view model contains the logic and behavior of a server side push notification pattern.
/// For demonstration purposes, and since we are using WPF as the 'face' of this server, each view model
/// exposes properties and commands to be controlled and activated by this server UI.
/// You can find more information about the MVVM pattern in .
///
internal abstract class PushPatternViewModel : ViewModelBase
{
#region Fields
/// Indicates if current pattern is active or not.
private bool _isActive;
/// Collection of tile image relative uri's available in the phone application.
private Uri[] _tileImages;
/// Selected tile background image uri.
private Uri _backgroundImageUri;
/// Tile message count.
private int _count;
/// Tile message title.
private string _title;
/// Toast message title.
private string _toastTitle;
/// Toast message sub title.
private string _toastSubTitle;
/// Raw text message.
private string _rawMessage;
#endregion
#region Properties
[Import]
protected PushService PushService { get; private set; }
[Import]
private IMessageSendResultLogger ResultLogger { get; set; }
///
/// Gets current pattern display name.
///
public string DisplayName { get; protected set; }
///
/// Gets current pattern description text.
///
public string Description { get; protected set; }
///
/// Gets or sets value for indicating whether this pattern is active or not.
///
public bool IsActive
{
get
{
return _isActive;
}
set
{
if (_isActive != value)
{
_isActive = value;
if (_isActive)
{
OnActivated();
}
else
{
OnDeactivated();
}
NotifyPropertyChanged("IsActive");
}
}
}
///
/// Gets a collection of tile image uris available in the phone client application.
///
public Uri[] TileImages
{
get
{
if (_tileImages == null)
{
_tileImages = new Uri[]
{
ToUri("TileBackground1.jpg"),
ToUri("TileBackground2.jpg"),
ToUri("TileBackground3.jpg"),
};
BackgroundImageUri = _tileImages[0];
}
return _tileImages;
}
}
///
/// Gets or sets the tile background uri.
///
public Uri BackgroundImageUri
{
get { return _backgroundImageUri; }
set
{
if (_backgroundImageUri != value)
{
_backgroundImageUri = value;
NotifyPropertyChanged("BackgroundImageUri");
}
}
}
///
/// Gets or sets the tile count.
///
public int Count
{
get { return _count; }
set
{
if (_count != value)
{
_count = value;
NotifyPropertyChanged("Count");
}
}
}
///
/// Gets or sets the tile tiltle.
///
public string Title
{
get { return _title; }
set
{
if (_title != value)
{
_title = value;
NotifyPropertyChanged("Title");
}
}
}
///
/// Gets or sets the toast title.
///
public string ToastTitle
{
get { return _toastTitle; }
set
{
if (_toastTitle != value)
{
_toastTitle = value;
NotifyPropertyChanged("ToastTitle");
}
}
}
///
/// Gets or sets the toast sub-title.
///
public string ToastSubTitle
{
get { return _toastSubTitle; }
set
{
if (_toastSubTitle != value)
{
_toastSubTitle = value;
NotifyPropertyChanged("ToastSubTitle");
}
}
}
///
/// Gets or sets the raw text message.
///
public string RawMessage
{
get { return _rawMessage; }
set
{
if (_rawMessage != value)
{
_rawMessage = value;
NotifyPropertyChanged("RawMessage");
}
}
}
#endregion
#region Commands
///
/// Gets the command which executes the send operation.
///
public ICommand SendCommand
{
get
{
return new RelayCommand(p =>
{
try
{
OnSend();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Send Error");
}
});
}
}
#endregion
#region Protected
///
/// Override this to send push message according to the pattern behavior.
///
protected abstract void OnSend();
///
/// Override this to add additional pattern-activation logic.
///
protected virtual void OnActivated()
{
UpdateClient();
PushService.Subscribed += PushService_Subscribed;
PushService.GetInfo += PushService_GetInfo;
}
///
/// Override this to add additional pattern-deactivation logic.
///
protected virtual void OnDeactivated()
{
PushService.Subscribed -= PushService_Subscribed;
PushService.GetInfo -= PushService_GetInfo;
}
///
/// Override this to add logic when clients login.
///
protected virtual void OnSubscribed(SubscriptionEventArgs args)
{
}
///
/// Override this to add logic when clients request server's info.
///
protected virtual void OnGetInfo(ServerInfoEventArgs args)
{
args.ServerInfo = new ServerInfo
{
PushPattern = DisplayName,
Counter = Count
};
}
///
/// Logs push message result.
///
protected void Log(MessageSendResult result)
{
ResultLogger.Log(DisplayName, result);
}
///
/// Logs push message error.
///
protected void Log(MessageSendException exception)
{
ResultLogger.Log(DisplayName, exception);
}
#endregion
#region Event Handlers
private void UpdateClient()
{
// Notify subscribers to get new info from the server.
foreach (var subscriber in PushService.Subscribers)
{
new RawPushNotificationMessage(MessageSendPriority.High)
{
RawData = Encoding.ASCII.GetBytes("Update Info")
}.SendAsync(subscriber.ChannelUri);
}
}
private void PushService_Subscribed(object sender, SubscriptionEventArgs args)
{
OnSubscribed(args);
}
private void PushService_GetInfo(object sender, ServerInfoEventArgs args)
{
OnGetInfo(args);
}
#endregion
#region Helpers
private static Uri ToUri(string imageName)
{
return new Uri(string.Format("/Resources/TileImages/{0}", imageName), UriKind.Relative);
}
#endregion
}
}