Adding the Online Presence permission to an EAS application
public AuthScopeFlags ScopeFlags
{
get
{
return AuthScopeFlags.BasicProfile | AuthScopeFlags.Presence;
}
}
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="1">
<Button Width="100" Height="23" Margin="2" Content="Query presence" Command="{Binding PresenceQuery}" />
<Button Width="100" Height="23" Margin="2" Content="Modify presence" Command="{Binding PresenceModifyStatus}" />
</StackPanel>
<StackPanel Grid.Column="0">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Status:" Margin="2" />
<ComboBox Width="150" HorizontalAlignment="Left" SelectedItem="{Binding Status}" ItemsSource="{Binding StatusOptions}" IsEnabled="{Binding ProductIdText, Converter={StaticResource StringToBooleanConverter}}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="ProductId:" Margin="2" />
<TextBlock Text="{Binding ProductIdText}" Margin="2" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="ProductVersion:" Margin="2" />
<TextBlock Text="{Binding ProductVersionText}" Margin="2" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Platform:" Margin="2" />
<TextBlock Text="{Binding PlatformText}" Margin="2" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Rich Text:" Margin="2" />
<TextBlock Text="{Binding RichText}" Margin="2" />
</StackPanel>
</StackPanel>
</Grid>
public partial class PresenceView : UserControl
{
public PresenceViewModel ViewModel { get { return ViewModelLocator.Presence; } }
public PresenceView()
{
InitializeComponent();
DataContext = ViewModel;
}
}
public class PresenceViewModel : BindableBase
{
private Status _status;
public Status Status
{
get { return _status; }
set { SetProperty(ref _status, value); }
}
private string _productIdText;
public string ProductIdText
{
get { return _productIdText; }
set { SetProperty(ref _productIdText, value); }
}
private string _productVersionText;
public string ProductVersionText
{
get { return _productVersionText; }
set { SetProperty(ref _productVersionText, value); }
}
private string _platformText;
public string PlatformText
{
get { return _platformText; }
set { SetProperty(ref _platformText, value); }
}
private string _richText;
public string RichText
{
get { return _richText; }
set { SetProperty(ref _richText, value); }
}
private List<Enum> _statusOptions;
public List<Enum> StatusOptions
{
get { return _statusOptions; }
set { SetProperty(ref _statusOptions, value); }
}
}
private static PresenceViewModel _presence;
public static PresenceViewModel Presence
{
get { return _presence ??= new PresenceViewModel(); }
}
public static void Query(EpicAccountId targetUserId)
{
var queryPresenceOptions = new QueryPresenceOptions()
{
LocalUserId = EpicAccountId.FromString(ViewModelLocator.Main.AccountId),
TargetUserId = targetUserId
};
ViewModelLocator.Main.StatusBarText = "Querying user presence...";
App.Settings.PlatformInterface.GetPresenceInterface()
.QueryPresence(queryPresenceOptions, null, (QueryPresenceCallbackInfo queryPresenceCallbackInfo) =>
{
Debug.WriteLine($"QueryPresence {queryPresenceCallbackInfo.ResultCode}");
if (queryPresenceCallbackInfo.ResultCode == Result.Success)
{
Copy(queryPresenceCallbackInfo.LocalUserId, queryPresenceCallbackInfo.TargetUserId);
}
else if (Common.IsOperationComplete(queryPresenceCallbackInfo.ResultCode))
{
Debug.WriteLine("Query presence failed: " + queryPresenceCallbackInfo.ResultCode);
ViewModelLocator.Main.StatusBarText = string.Empty;
}
});
}
public static void Copy(EpicAccountId localUserId, EpicAccountId targetUserId)
{
var copyPresenceOptions = new CopyPresenceOptions()
{
LocalUserId = localUserId,
TargetUserId = targetUserId
};
var result = App.Settings.PlatformInterface.GetPresenceInterface()
.CopyPresence(copyPresenceOptions, out var info);
if (result == Result.Success)
{
ViewModelLocator.Main.StatusBarText = "User presence retrieved.";
if (localUserId == targetUserId)
{
ViewModelLocator.Presence.Status = info.Status;
ViewModelLocator.Presence.ProductIdText = info.ProductId;
ViewModelLocator.Presence.ProductVersionText = info.ProductVersion;
ViewModelLocator.Presence.PlatformText = info.Platform;
ViewModelLocator.Presence.RichText = info.RichText;
}
ViewModelLocator.Main.StatusBarText = string.Empty;
}
}
public class PresenceQueryCommand : CommandBase
{
public override bool CanExecute(object parameter)
{
return !string.IsNullOrWhiteSpace(ViewModelLocator.Main.AccountId);
}
public override void Execute(object parameter)
{
if (parameter == null)
{
PresenceService.Copy(EpicAccountId
.FromString(ViewModelLocator.Main.AccountId), EpicAccountId.FromString(ViewModelLocator.Main.AccountId));
}
}
}
public PresenceQueryCommand PresenceQuery { get; set; }
public PresenceViewModel()
{
StatusOptions = Enum.GetValues(Status.GetType()).Cast<Enum>().ToList();
PresenceQuery = new PresenceQueryCommand();
}
Presence.PresenceQuery.RaiseCanExecuteChanged();
<TabItem x:Name="Presence" Header="Presence">
<views:PresenceView />
</TabItem>
public static void ModifyStatus()
{
var createPresenceModificationOptions = new CreatePresenceModificationOptions()
{
LocalUserId = EpicAccountId.FromString(ViewModelLocator.Main.AccountId)
};
ViewModelLocator.Main.StatusBarText = "Creating presence modification...";
var result = App.Settings.PlatformInterface.GetPresenceInterface()
.CreatePresenceModification(createPresenceModificationOptions, out var presenceModification);
Debug.WriteLine($"CreatePresenceModification {result}");
if (result == Result.Success)
{
var setStatusOptions = new PresenceModificationSetStatusOptions()
{
Status = ViewModelLocator.Presence.Status
};
result = presenceModification.SetStatus(setStatusOptions);
Debug.WriteLine($"SetStatus {result}");
var setPresenceOptions = new SetPresenceOptions()
{
LocalUserId = EpicAccountId.FromString(ViewModelLocator.Main.AccountId),
PresenceModificationHandle = presenceModification
};
ViewModelLocator.Main.StatusBarText = "Setting presence status...";
App.Settings.PlatformInterface.GetPresenceInterface()
.SetPresence(setPresenceOptions, null, (SetPresenceCallbackInfo setPresenceCallbackInfo) =>
{
Debug.WriteLine($"SetPresence {setPresenceCallbackInfo.ResultCode}");
if (Common.IsOperationComplete(setPresenceCallbackInfo.ResultCode))
{
if (presenceModification != null)
{
presenceModification.Release();
presenceModification = null;
}
ViewModelLocator.Main.StatusBarText = string.Empty;
}
});
}
else if (Common.IsOperationComplete(result))
{
Debug.WriteLine("Create presence modification failed: " + result);
ViewModelLocator.Main.StatusBarText = string.Empty;
}
}
ViewModelLocator.Presence.PresenceModifyStatus.
RaiseCanExecuteChanged();
if (result == Result.Success)
{
ViewModelLocator.Main.StatusBarText = "User presence retrieved.";
ViewModelLocator.Presence.Status = info.Status;
ViewModelLocator.Presence.ProductIdText = info.ProductId;
ViewModelLocator.Presence.ProductVersionText = info.ProductVersion;
ViewModelLocator.Presence.PlatformText = info.Platform;
ViewModelLocator.Presence.RichText = info.RichText;
ViewModelLocator.Main.StatusBarText = string.Empty;
ViewModelLocator.Presence.PresenceModifyStatus.
RaiseCanExecuteChanged();
}
public class PresenceModifyStatusCommand : CommandBase
{
public override bool CanExecute(object parameter)
{
return !string.IsNullOrWhiteSpace(ViewModelLocator.Presence.ProductIdText);
}
public override void Execute(object parameter)
{
PresenceService.ModifyStatus();
}
}
public PresenceModifyStatusCommand PresenceModifyStatus { get; set; }
PresenceModifyStatus = new PresenceModifyStatusCommand();