Пример #1
0
        public static async Task <Color> GetStationBackgroundDominantColorAsync(StationItem station)
        {
            if (station == null)
            {
                throw new ArgumentNullException(nameof(station));
            }

            if (station.Background == null)
            {
                return(Colors.Transparent);
            }

            string colorKey = "BgColor|" + station.Name;
            Color  color    = default(Color);

            if (await CookieJar.DeviceCache.ContainsObjectAsync(colorKey))
            {
                var hexCode = await CookieJar.DeviceCache.PeekObjectAsync <string>(colorKey);

                return(ColorUtilities.ParseFromHexString(hexCode));
            }

            IRandomAccessStreamWithContentType stationBgStream = null;

            try
            {
                var streamRef = RandomAccessStreamReference.CreateFromUri(new Uri(station.Background));
                stationBgStream = await streamRef.OpenReadAsync();

                color = await ColorUtilities.GetDominantColorAsync(stationBgStream);
            }
            catch (Exception ex)
            {
                Dictionary <string, string> data = new Dictionary <string, string>();
                data.Add("StationName", station.Name);
                data.Add("StationBackgroundURL", station.Background?.ToString());
                data.Add("StationURL", station.Site);

                Microsoft.HockeyApp.HockeyClient.Current.TrackException(ex, data);
            }
            finally
            {
                stationBgStream?.Dispose();
            }

            await CookieJar.DeviceCache.PushObjectAsync <string>(colorKey, color.ToString());

            return(color);
        }
        private async Task <StationItem> ConvertStationElementToStationAsync(XElement stationElement)
        {
            var streams = stationElement.Element("Streams").Elements("Stream").Select <XElement, StationStream>(x =>
            {
                var stream         = new StationStream(new Uri(x.Value));
                stream.ContentType = x.Attribute("ContentType")?.Value;

                if (x.Attribute("Bitrate") != null)
                {
                    stream.Bitrate = int.Parse(x.Attribute("Bitrate")?.Value);
                }

                if (x.Attribute("RelativePath") != null)
                {
                    stream.RelativePath = x.Attribute("RelativePath")?.Value;
                }

                if (x.Attribute("ServerType") != null)
                {
                    stream.ServerFormat = (StationStreamServerFormat)Enum.Parse(typeof(StationStreamServerFormat), x.Attribute("ServerType").Value);
                }

                if (x.Attribute("RequestMetadata") != null)
                {
                    stream.RequestMetadata = bool.Parse(x.Attribute("RequestMetadata").Value);
                }
                else
                {
                    //defaults to true
                    stream.RequestMetadata = true;
                }

                return(stream);
            }).ToArray();

            var stationLogoData = await NepApp.CacheManager.GetOrCacheUriAsync(NepAppDataCacheManager.CacheType.StationImages, new Uri(stationElement.Element("Logo").Value));

            Uri stationLogoUri = stationLogoData.Item1;

            if (stationLogoData.Item2 != null)
            {
                stationLogoUri = new Uri(stationLogoData.Item2.Path);
            }


            var station = new StationItem(
                name: stationElement.Element("Name").Value,
                description: stationElement.Element("Description").Value,
                stationLogo: stationLogoUri,
                streams: streams);

            station.StationLogoUrlOnline = new Uri(stationElement.Element("Logo").Value);

            if (stationElement.Element("Programs") != null)
            {
                station.Programs = stationElement.Element("Programs").Elements("Program").Select <XElement, StationProgram>(x =>
                {
                    var program     = new StationProgram();
                    program.Name    = x.Attribute("Name")?.Value;
                    program.Station = station.Name;

                    if (x.Attribute("Style") != null)
                    {
                        program.Style = (StationProgramStyle)Enum.Parse(typeof(StationProgramStyle), x.Attribute("Style").Value);
                    }
                    else
                    {
                        program.Style = StationProgramStyle.Hosted;
                    }

                    if (program.Style == StationProgramStyle.Hosted)
                    {
                        //hosted programs rely on the "artist" string (from song metadata) to match in order to activate.

                        program.Host = x.Attribute("Host")?.Value;
                        program.HostRegexExpression = x.Attribute("HostExp")?.Value;
                    }
                    else if (program.Style == StationProgramStyle.Block)
                    {
                        //block programs are activated based on the time and the current station.
                    }

                    if (x.HasElements)
                    {
                        if (x.Elements("Listing") != null)
                        {
                            //if the program has an actual schedule (e.g. always occur on a certain day around a certain time), grab its time listings.

                            var listings = new List <StationProgramTimeListing>();

                            foreach (var listingElement in x.Elements("Listing"))
                            {
                                try
                                {
                                    StationProgramTimeListing listing = new StationProgramTimeListing();
                                    listing.Day  = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), listingElement.Attribute("Day").Value);
                                    listing.Time = DateTime.Parse(listingElement.Attribute("Time").Value);

                                    if (listingElement.Attribute("EndTime") != null)
                                    {
                                        listing.EndTime = DateTime.Parse(listingElement.Attribute("EndTime").Value);
                                    }

                                    listings.Add(listing);
                                }
#if !DEBUG
                                catch (Exception ex)
                                {
                                    Microsoft.HockeyApp.HockeyClient.Current.TrackException(ex);
#else
                                catch (Exception)
                                {
#endif
                                }
                            }

                            program.TimeListings = listings.ToArray();
                        }
                    }

                    return(program);
                }).ToArray();
            }
            else
            {
                station.Programs = null;
            }

            if (stationElement.Element("Background") != null)
            {
                var backgroundElement = stationElement.Element("Background");

                station.Background = backgroundElement.Value;
            }

            station.Site          = stationElement.Element("Site").Value;
            station.Genres        = stationElement.Element("Genres").Value.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            station.PrimaryLocale = stationElement.Element("PrimaryLocale")?.Value;

            if (stationElement.Element("StationMessages") == null)
            {
                station.StationMessages = new string[] { };
            }
            else
            {
                var stationMsgElement = stationElement.Element("StationMessages");
                var messages          = stationMsgElement.Elements("Message").Select(x => x.Value.ToString()).ToArray();
                station.StationMessages = messages;
            }

            station.Group = stationElement.Element("StationGroup")?.Value;

            return(station);
        }