Beispiel #1
0
        public async static Task <List <string> > GetTimesForSchedule(int num, string origin, string dest)
        {
            Tuple <string, string, IEnumerable <string> > routes_and_ids = await MainDataStore.GetCommonRoutesAndStopIds(origin, dest);

            string        originID = routes_and_ids.Item1;
            string        destID   = routes_and_ids.Item2;
            List <string> routes   = routes_and_ids.Item3.ToList <string>();

            // Grab the data store
            string store = await RefreshCache();

            JsonObject obj       = JsonObject.Parse(store);
            JsonArray  routeJson = obj["routes"].GetArray();

            // Grab only the route objects you need
            TimeDictionary timeDict = new TimeDictionary(originID, destID);

            foreach (JsonValue routeVal in routeJson)
            {
                JsonObject routeObj = routeVal.GetObject();
                if (routes.Contains(routeObj["id"].GetString()))
                {
                    timeDict.AddObj(routeObj);
                }
            }

            // Create an ordered list of times
            List <string> times = new List <string>();

            while (times.Count < num)
            {
                string nextTime = timeDict.PopAndUpdate();
                if (nextTime == "")
                {
                    break;                 // this could happen if a user picks a trip that's only on weekends, but it's a weekday
                }
                times.Add(nextTime);
            }

            // cleanup
            routeJson                  = null;
            obj                        = null;
            routes                     = null;
            routes_and_ids             = null;
            timeDict.clear(); timeDict = null;

            return(times);
        }
        private async static Task <Tuple <string, string> > CallService(string origin, string dest, List <string> results)
        {
            string newNumMinutes = "";
            string newUnits      = "";

            List <string> times = await MainDataStore.GetTimesForSchedule(20, origin, dest);

            if (times.Count == 0)
            {
                return(Tuple.Create <string, string>("", ""));
            }

            TileUpdater updater = CreateNewTileUpdater();

            int minuteCountdown  = 0;
            int numNotifications = 0;
            int numListings      = 0;

            foreach (string time in times)
            {
                minuteCountdown = GetNewMinuteCountdown(time);

                // add listings to main UI
                if (results != null)
                {
                    if (numNotifications == 0)
                    {
                        Tuple <string, string> countdownBoxResults = SetCountdownBox(minuteCountdown);
                        newNumMinutes = countdownBoxResults.Item1;
                        newUnits      = countdownBoxResults.Item2;
                    }
                    else
                    {
                        AddListing(minuteCountdown, results);
                    }
                    numListings++;
                }

                // add tile notifications
                if (numNotifications <= maxNotifications)
                {
                    numNotifications = AddTileNotifications(minuteCountdown, numNotifications, origin, dest, updater);
                }

                if (numListings > maxListings)
                {
                    break;
                }
            }

            if (results != null && results.Count == 0)
            {
                results.Add("No further times scheduled.");
            }

            // clean up
            updater = null;
            times.Clear(); times = null;

            return(Tuple.Create <string, string>(newNumMinutes, newUnits));
        }