@@ -26,6 +26,7 @@ namespace ProtonVPN.Streaming
2626{
2727 internal class StreamingServices : IStreamingServices
2828 {
29+ private const string COUNTRY_CODE_ANY = "*" ;
2930 private StreamingServicesResponse _response ;
3031 private readonly IAppSettings _appSettings ;
3132
@@ -37,18 +38,52 @@ public StreamingServices(StreamingServicesUpdater streamingServicesUpdater, IApp
3738
3839 public IReadOnlyList < StreamingService > GetServices ( string countryCode , sbyte tier )
3940 {
40- if ( _response == null ||
41- ! _response . StreamingServices . ContainsKey ( countryCode ) ||
42- ! _response . StreamingServices [ countryCode ] . ContainsKey ( tier ) )
41+ if ( _response == null )
4342 {
4443 return new List < StreamingService > ( ) ;
4544 }
4645
47- IReadOnlyList < StreamingServiceResponse > services = _response . StreamingServices [ countryCode ] [ tier ] ;
46+ Dictionary < string , StreamingService > streamingServicesByName = new ( ) ;
47+ if ( IsStreamingServicesResponseContainingCountryCodeAndTier ( countryCode , tier ) )
48+ {
49+ UpsertStreamingServicesToDictionary ( streamingServicesByName , _response . StreamingServices [ countryCode ] [ tier ] ) ;
50+ }
51+ if ( IsStreamingServicesResponseContainingCountryCodeAndTier ( COUNTRY_CODE_ANY , tier ) )
52+ {
53+ UpsertStreamingServicesToDictionary ( streamingServicesByName , _response . StreamingServices [ COUNTRY_CODE_ANY ] [ tier ] ) ;
54+ }
55+
56+ return streamingServicesByName . Values . OrderBy ( s => s . Name ) . ToList ( ) ;
57+ }
58+
59+ private bool IsStreamingServicesResponseContainingCountryCodeAndTier ( string countryCode , sbyte tier )
60+ {
61+ return countryCode != null &&
62+ _response . StreamingServices . ContainsKey ( countryCode ) &&
63+ _response . StreamingServices [ countryCode ] . ContainsKey ( tier ) ;
64+ }
4865
49- return services . Select ( s => new StreamingService ( s . Name , GetIconUrl ( s . Icon ) ) )
50- . OrderBy ( s => s . Name )
51- . ToList ( ) ;
66+ private void UpsertStreamingServicesToDictionary ( Dictionary < string , StreamingService > streamingServicesByName ,
67+ IList < StreamingServiceResponse > streamingServiceResponses )
68+ {
69+ foreach ( StreamingServiceResponse streamingServiceResponse in streamingServiceResponses )
70+ {
71+ UpsertStreamingServiceToDictionary ( streamingServicesByName , streamingServiceResponse ) ;
72+ }
73+ }
74+
75+ private void UpsertStreamingServiceToDictionary ( Dictionary < string , StreamingService > streamingServicesByName ,
76+ StreamingServiceResponse streamingServiceResponse )
77+ {
78+ StreamingService streamingService = MapStreamingService ( streamingServiceResponse ) ;
79+ streamingServicesByName [ streamingService . Name ] = streamingService ;
80+ }
81+
82+ private StreamingService MapStreamingService ( StreamingServiceResponse streamingServicesResponse )
83+ {
84+ return new StreamingService (
85+ name : streamingServicesResponse . Name ,
86+ iconUrl : GetIconUrl ( streamingServicesResponse . Icon ) ) ;
5287 }
5388
5489 private string GetIconUrl ( string icon )
0 commit comments