Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added CastButton, CastDialog and ChromecastService
- Loading branch information
Showing
11 changed files
with
1,412 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using SharpCaster.Controllers; | ||
using SharpCaster.Models; | ||
|
||
namespace SharpCaster | ||
{ | ||
public class ChromecastService | ||
{ | ||
private static ChromecastService _instance; | ||
|
||
public static ChromecastService Current | ||
{ | ||
get | ||
{ | ||
if (_instance == null) | ||
{ | ||
_instance = new ChromecastService(); | ||
} | ||
return _instance; | ||
} | ||
} | ||
|
||
public DeviceLocator DeviceLocator { get; } | ||
public ChromeCastClient ChromeCastClient { get; } | ||
public Chromecast ConnectedChromecast { get; set; } | ||
public CastButton CastButton { get; set; } | ||
private CancellationTokenSource _cancellationTokenSource; | ||
|
||
public ChromecastService() | ||
{ | ||
DeviceLocator = new DeviceLocator(); | ||
DeviceLocator.DeviceFounded += DeviceLocator_DeviceFounded; | ||
ChromeCastClient = new ChromeCastClient(); | ||
ChromeCastClient.Connected += ChromeCastClient_Connected; | ||
|
||
} | ||
|
||
private void DeviceLocator_DeviceFounded(object sender, Chromecast e) | ||
{ | ||
CastButton?.GoToState(CastButtonVisualStates.InteractiveStates.Disconnected); | ||
} | ||
|
||
public void ConnectToChromecast(Chromecast chromecast) | ||
{ | ||
CastButton?.GoToState(CastButtonVisualStates.InteractiveStates.Connecting); | ||
StopLocatingDevices(); | ||
ConnectedChromecast = chromecast; | ||
ChromeCastClient.ConnectChromecast(chromecast.DeviceUri); | ||
} | ||
|
||
private void ChromeCastClient_Connected(object sender, System.EventArgs e) | ||
{ | ||
CastButton?.GoToState(CastButtonVisualStates.InteractiveStates.Connected); | ||
} | ||
|
||
public void StopLocatingDevices() | ||
{ | ||
_cancellationTokenSource.Cancel(); | ||
} | ||
|
||
public async Task StartLocatingDevices() | ||
{ | ||
_cancellationTokenSource = new CancellationTokenSource(); | ||
await DeviceLocator.LocateDevicesAsync(_cancellationTokenSource.Token); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Windows.ApplicationModel.Core; | ||
using Windows.UI.Core; | ||
using Windows.UI.Xaml; | ||
using Windows.UI.Xaml.Controls; | ||
using Windows.UI.Xaml.Controls.Primitives; | ||
using Windows.UI.Xaml.Shapes; | ||
|
||
namespace SharpCaster.Controllers | ||
{ | ||
public class CastButton : Control | ||
{ | ||
public static readonly DependencyProperty ChromecastServiceProperty = DependencyProperty.Register("ChromecastService", typeof(ChromecastService), typeof(CastButton), new PropertyMetadata(null)); | ||
|
||
public ChromecastService ChromecastService | ||
{ | ||
get { return (ChromecastService)GetValue(ChromecastServiceProperty); } | ||
set { SetValue(ChromecastServiceProperty, value); } | ||
} | ||
|
||
public static readonly DependencyProperty CastDialogProperty = DependencyProperty.Register("CastDialog", typeof(CastDialog), typeof(CastButton), new PropertyMetadata(null)); | ||
|
||
public CastDialog CastDialog | ||
{ | ||
get { return (CastDialog) GetValue(CastDialogProperty); } | ||
set { SetValue(CastDialogProperty, value); } | ||
} | ||
|
||
private Popup _popup; | ||
|
||
|
||
public CastButton() | ||
{ | ||
DefaultStyleKey = typeof(CastButton); | ||
} | ||
|
||
protected override void OnApplyTemplate() | ||
{ | ||
base.OnApplyTemplate(); | ||
var castIcon = GetTemplateChild("CastIcon") as Path; | ||
if (ChromecastService != null) ChromecastService.CastButton = this; | ||
if (castIcon != null) Tapped += CastIcon_Tapped; | ||
VisualStateManager.GoToState(this, CastButtonVisualStates.InteractiveStates.Unavailable.ToString(), true); | ||
} | ||
|
||
private void CastIcon_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e) | ||
{ | ||
if (CastDialog == null) CastDialog = new CastDialog {ChromecastService = ChromecastService}; | ||
if (_popup != null) | ||
{ | ||
CastDialog.Visibility = Visibility.Visible; | ||
return; | ||
} | ||
_popup = new Popup | ||
{ | ||
Child = CastDialog, | ||
IsOpen = true, | ||
HorizontalAlignment = HorizontalAlignment.Center | ||
}; | ||
} | ||
|
||
|
||
private static async Task ExecuteOnUiThread(DispatchedHandler yourAction) | ||
{ | ||
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, yourAction); | ||
} | ||
|
||
public async Task GoToState(CastButtonVisualStates.InteractiveStates state) | ||
{ | ||
await ExecuteOnUiThread(() => | ||
{ | ||
VisualStateManager.GoToState(this, state.ToString(), true); | ||
}); | ||
|
||
} | ||
} | ||
public static class CastButtonVisualStates | ||
{ | ||
internal static class GroupNames | ||
{ | ||
internal const string InteractiveStates = "InteractiveStates"; | ||
} | ||
|
||
public enum InteractiveStates | ||
{ | ||
Unavailable, | ||
Disconnected, | ||
Connecting, | ||
Connected | ||
} | ||
} | ||
} |
Oops, something went wrong.