public BadgeNotification(JToken item)
 {
     var badge = item["badge"];
     if (badge != null)
     {
         Badge = Badge.ParseJson(badge);
     }
 }
예제 #2
0
        public static Badge ParseJson(JToken badge)
        {
            Badge b = new Badge();

            b.Id = Json.TryGetJsonProperty(badge, "id");
            b.BadgeId= Json.TryGetJsonProperty(badge, "badgeId");

            b.Name = Json.TryGetJsonProperty(badge, "name");
            b.Hint = Json.TryGetJsonProperty(badge, "hint");
            b.Description = Json.TryGetJsonProperty(badge, "description");

            var image = badge["image"];
            if (image != null)
            {
                try
                {
                    // TODO: Move to using MultiResolutionImage!
                    b.MultiResolutionIcon = MultiResolutionImage.ParseJson(image);

                    string prefix = Json.TryGetJsonProperty(image, "prefix");
                    string imageName = Json.TryGetJsonProperty(image, "name");
                    var sizes = image["sizes"];
                    if (sizes != null)
                    {
                        List<int> szz = sizes.Select(size => int.Parse(size.ToString(), CultureInfo.InvariantCulture)).ToList();
                        b.Sizes = szz;
                        if (szz.Count > 0)
                        {
                            b.IconUri = new Uri(prefix + szz[0] + imageName, UriKind.Absolute);
                            b.IconLargeUri = new Uri(prefix + szz[szz.Count - 1] + imageName, UriKind.Absolute);

                            if (szz.Count > 2)
                            {
                                b.IconMediumUri = new Uri(prefix + szz[1] + imageName, UriKind.Absolute);
                            }
                            else
                            {
                                // No medium-sized icon available.
                                b.IconMediumUri = b.IconUri;
                            }
                        }
                        b.ImagePrefix = prefix;
                        b.ImagePostfix = imageName;
                    }
                }
                catch (Exception)
                {
                }
            }

            var unlocks = badge["unlocks"];
            if (unlocks != null)
            {
                List<Checkin> lc = new List<Checkin>();
                foreach (var unlock in unlocks)
                {
                    try
                    {
                        Checkin c = Checkin.ParseJson(unlock);
                        lc.Add(c);
                    }
                    catch (Exception)
                    {
                        // note: silent watchson?
                    }
                }
                b.Unlocks = lc;
            }

            return b;
        }
예제 #3
0
        public static Badge ParseJson(JToken badge)
        {
            Badge b = new Badge();

            b.Id      = Json.TryGetJsonProperty(badge, "id");
            b.BadgeId = Json.TryGetJsonProperty(badge, "badgeId");

            b.Name        = Json.TryGetJsonProperty(badge, "name");
            b.Hint        = Json.TryGetJsonProperty(badge, "hint");
            b.Description = Json.TryGetJsonProperty(badge, "description");

            var image = badge["image"];

            if (image != null)
            {
                try
                {
                    // TODO: Move to using MultiResolutionImage!
                    b.MultiResolutionIcon = MultiResolutionImage.ParseJson(image);

                    string prefix    = Json.TryGetJsonProperty(image, "prefix");
                    string imageName = Json.TryGetJsonProperty(image, "name");
                    var    sizes     = image["sizes"];
                    if (sizes != null)
                    {
                        List <int> szz = sizes.Select(size => int.Parse(size.ToString(), CultureInfo.InvariantCulture)).ToList();
                        b.Sizes = szz;
                        if (szz.Count > 0)
                        {
                            b.IconUri      = new Uri(prefix + szz[0] + imageName, UriKind.Absolute);
                            b.IconLargeUri = new Uri(prefix + szz[szz.Count - 1] + imageName, UriKind.Absolute);

                            if (szz.Count > 2)
                            {
                                b.IconMediumUri = new Uri(prefix + szz[1] + imageName, UriKind.Absolute);
                            }
                            else
                            {
                                // No medium-sized icon available.
                                b.IconMediumUri = b.IconUri;
                            }
                        }
                        b.ImagePrefix  = prefix;
                        b.ImagePostfix = imageName;
                    }
                }
                catch (Exception)
                {
                }
            }

            var unlocks = badge["unlocks"];

            if (unlocks != null)
            {
                List <Checkin> lc = new List <Checkin>();
                foreach (var unlock in unlocks)
                {
                    try
                    {
                        Checkin c = Checkin.ParseJson(unlock);
                        lc.Add(c);
                    }
                    catch (Exception)
                    {
                        // note: silent watchson?
                    }
                }
                b.Unlocks = lc;
            }

            return(b);
        }
예제 #4
0
            protected override object DeserializeCore(JObject json, Type objectType, LoadContext context)
            {
                try
                {
                    var b = new UserBadges(context);

                    var sets = json["sets"];
                    if (sets != null)
                    {
                        /*
                         * type: "all"
                         * name: "all badges"
                         * image: {
                         * prefix: "http://foursquare.com/img/badge/"
                         * sizes: [
                         * 24
                         * 32
                         * 48
                         * 64
                         * ]
                         * name: "/allbadges.png"
                         * */
                        // TODO: V2: support icons for groups

                        var listOfGroups   = new List <BadgeGroup>();
                        var badgesInGroups = new Dictionary <BadgeGroup, Dictionary <string, bool> >();

                        var groups = sets["groups"];
                        foreach (var group in groups)
                        {
                            BadgeGroup bbgg = new BadgeGroup();
                            bbgg.Name = Json.TryGetJsonProperty(group, "name");
                            bbgg.Type = Json.TryGetJsonProperty(group, "type");
                            // TODO: FUTURE: V2 badge group image support here

                            var items = group["items"];
                            if (items != null)
                            {
                                var contained = new Dictionary <string, bool>();
                                foreach (string s in items)
                                {
                                    contained[s] = true;
                                }
                                badgesInGroups[bbgg] = contained;

                                listOfGroups.Add(bbgg);
                            }
                        }

                        //BadgeGroup all = lbg.Where(z => z.Type == "all").FirstOrDefault();

                        var badges = json["badges"];
                        if (badges != null)
                        {
                            foreach (var bb in badges)
                            {
                                var item = bb.First();

                                string key = Json.TryGetJsonProperty(item, "id");
                                if (!string.IsNullOrEmpty(key))
                                {
                                    var   thisBadget = badges[key];
                                    Badge badge      = Badge.ParseJson(thisBadget);

                                    foreach (var c in badgesInGroups)
                                    {
                                        var badgeGroup = c.Key;
                                        var contained  = c.Value;

                                        if (contained.ContainsKey(key))
                                        {
                                            badgeGroup.Add(badge);
                                        }
                                    }
                                }
                            }
                        }

                        // TODO: P0 SHIP: TESTING NEEDED: If they have zero badges, which group will we have - probably not the right one!
                        // NOTE: V2: For now I only want the first group.
                        var tempList = new List <BadgeGroup>(1);
                        tempList.Add(listOfGroups[0]);
                        b.Groups = tempList; // listOfGroups;
                    }

                    b.IsLoadComplete = true;

                    return(b);
                }
                catch (Exception e)
                {
                    throw new UserIntendedException(
                              "There was a problem trying to read the list of friends.", e);
                }
            }