Skip to content

Commit 91e0335

Browse files
committed
Improve Chromecast application handling and nullability
- Enhanced `DeviceService` to check for existing applications before accessing namespaces, launching a default media receiver if none are found. - Updated `ChromecastStatus` and made properties `Applications`, `Volume`, and `Application` nullable for better error handling.
1 parent dcc13a5 commit 91e0335

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

SharpCaster.Console/Services/DeviceService.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,15 @@ private async Task CheckAndJoinExistingApplicationAsync()
176176

177177
try
178178
{
179-
var hasReceiverNamespace = _state.Client.ChromecastStatus.Application.Namespaces?.Any(ns => ns.Name == "urn:x-cast:com.google.cast.receiver") == true;
179+
var application = _state.Client.ChromecastStatus.Application;
180+
if (application == null)
181+
{
182+
AnsiConsole.MarkupLine("[dim]No existing application found. Launching Default Media Receiver...[/]");
183+
await _state.Client.LaunchApplicationAsync("B3419EF5", false);
184+
AnsiConsole.MarkupLine("[green]✅ Default Media Receiver launched successfully![/]");
185+
return;
186+
}
187+
var hasReceiverNamespace = application?.Namespaces?.Any(ns => ns.Name == "urn:x-cast:com.google.cast.receiver") == true;
180188
// Refresh receiver status to get the most current application information
181189
if (hasReceiverNamespace)
182190
{

Sharpcaster/.editorconfig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,7 @@ csharp_style_prefer_not_pattern = true:suggestion
213213
csharp_style_prefer_extended_property_pattern = true:suggestion
214214
csharp_style_var_when_type_is_apparent = false:silent
215215
csharp_style_var_for_built_in_types = false:silent
216-
csharp_style_var_elsewhere = false:silent
216+
csharp_style_var_elsewhere = false:silent
217+
csharp_prefer_system_threading_lock = true:warning
218+
csharp_style_prefer_implicitly_typed_lambda_expression = true:suggestion
219+
csharp_style_prefer_unbound_generic_type_in_nameof = true:suggestion

Sharpcaster/Models/ChromecastStatus/ChromecastStatus.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Collections.Generic;
2-
using System.Collections.ObjectModel;
1+
using System.Collections.ObjectModel;
32
using System.Linq;
43
using System.Text.Json.Serialization;
54

@@ -12,7 +11,7 @@ namespace Sharpcaster.Models.ChromecastStatus
1211
public class ChromecastStatus
1312
{
1413
[JsonPropertyName("applications")]
15-
public Collection<ChromecastApplication> Applications { get; set; }
14+
public Collection<ChromecastApplication>? Applications { get; set; }
1615

1716
[JsonPropertyName("isActiveInput")]
1817
public bool IsActiveInput { get; set; }
@@ -21,9 +20,9 @@ public class ChromecastStatus
2120
public bool IsStandBy { get; set; }
2221

2322
[JsonPropertyName("volume")]
24-
public Volume Volume { get; set; }
23+
public Volume? Volume { get; set; }
2524

2625
[JsonIgnore]
27-
public ChromecastApplication Application => Applications?.FirstOrDefault();
26+
public ChromecastApplication? Application => Applications?.FirstOrDefault();
2827
}
2928
}

0 commit comments

Comments
 (0)