//method we will call to get the users data //pass in grid so we can switch visibility when data is ready public async void getSignedInUser(Grid userProfileGrid, ProgressBar progressBar) { //variable to hold response string returnedUserJson = null; //use this bool to check for any errors that occur bool hasError = false; //place web calls in try catch block just incase a network error occurs //try catch will also stop app from crashing if error does occur try { //client.getStringAsync will make the asynchronous call to the desired url //the response (json string) will be stored into the returnedUser string returnedUserJson = await client.GetStringAsync(url); } catch { //set error to true because a web exception occurred hasError = true; } //if has error is false then we can deserialize the string if (!hasError) { //bool to signify error bool jsonError = false; //the object we will deserialize to UserClass.RootObject uc = null; //try catch json incase some unknown error happens like an int being set to null try { //let Json.Net deserialize the json string. passing in the json string we recieved and the typeof uc = (UserClass.RootObject)JsonConvert.DeserializeObject(returnedUserJson, typeof(UserClass.RootObject)); } catch { //if there was an error jsonError = true; } //if there was no error then let us set our variables if (!jsonError) { //we take the variables we need from the json and put it into our userProfile list //UserProfile.Add(new UserProfile() //{ // firstName = uc.response.user.firstName, // lastName = uc.response.user.lastName, // hometown = uc.response.user.homeCity, // tipCount = uc.response.user.tips.count, // badgesCount = uc.response.user.badges.count, // checkInCount = uc.response.user.checkins.count, // friendCount = uc.response.user.friends.count, // email = uc.response.user.contact.email, // image = uc.response.user.photo.prefix + "original" + uc.response.user.photo.suffix, // maxScore = uc.response.user.scores.max, // recentScore = uc.response.user.scores.recent, // mayorshipCount = uc.response.user.mayorships.count, // lastLocation = uc.response.user.firstName + " " +uc.response.user.lastName + " at " + uc.response.user.checkins.items.First().venue.name, // lastLongitudeLocation = uc.response.user.checkins.items.First().venue.location.lng, // lastLatitudeLocation =uc.response.user.checkins.items.First().venue.location.lat //}); //set variables the long way because of null classes that can be sent back if you have an unused account UserItem up = new ClassFolder.UserItem(); up.firstName = uc.response.user.firstName; up.lastName = uc.response.user.lastName; up.hometown = uc.response.user.homeCity; up.tipCount = uc.response.user.tips.count; up.badgesCount = uc.response.user.badges.count; up.checkInCount = uc.response.user.checkins.count; up.friendCount = uc.response.user.friends.count; up.email = uc.response.user.contact.email; up.image = uc.response.user.photo.prefix + "original" + uc.response.user.photo.suffix; //when a user has no recent or max score the server returns null to the class //we need to do this so that the any variables used inside scores class have a default value if (uc.response.user.scores == null) { up.maxScore = 0; up.recentScore = 0; } else { up.maxScore = uc.response.user.scores.max; up.recentScore = uc.response.user.scores.recent; } up.mayorshipCount = uc.response.user.mayorships.count; //have to check count of checkin items. for someone who has never checked in the class will be null //causing the application to crash if (uc.response.user.checkins.items.Count > 0) { up.lastLocation = uc.response.user.firstName + " " + uc.response.user.lastName + " at " + uc.response.user.checkins.items.First().venue.name; up.lastLongitudeLocation = uc.response.user.checkins.items.First().venue.location.lng; up.lastLatitudeLocation = uc.response.user.checkins.items.First().venue.location.lat; //finally add user to list UserProfile.Add(up); up = null; //set map coordinates if there was a last known place setMapToLastVisitedLocation(new GeoCoordinate() { Latitude = UserProfile[0].lastLatitudeLocation, Longitude = userProfile[0].lastLongitudeLocation }); } else { up.lastLocation = uc.response.user.firstName + " " + uc.response.user.lastName + " has no last known location"; //finally add user to list UserProfile.Add(up); up = null; } //change visibility to make profile grid visible //and make visibility of progress bar collapsed Deployment.Current.Dispatcher.BeginInvoke(() => { userProfileGrid.Visibility = Visibility.Visible; progressBar.Visibility = Visibility.Collapsed; progressBar.IsEnabled = false; }); } } //there was an error so just stop the loading and pop up a message box else { } }
public async void loadFriendProfile(string userId, Grid friendProfileGrid, ProgressBar progressBar) { //variable to hold response string returnedUserJson = null; //use this bool to check for any errors that occur bool hasError = false; //url string url = "https://api.foursquare.com/v2/users/" + userId + "?oauth_token=" + App.accessToken + "&v=" + App.date; //place web calls in try catch block just incase a network error occurs //try catch will also stop app from crashing if error does occur try { //client.getStringAsync will make the asynchronous call to the desired url //the response (json string) will be stored into the returnedUser string returnedUserJson = await client.GetStringAsync(url); } catch { //set error to true because a web exception occurred hasError = true; } //if has error is false then we can deserialize the string if (!hasError) { //bool to signify error bool jsonError = false; //the object we will deserialize to UserClass.RootObject uc = null; //try catch json incase some unknown error happens like an int being set to null try { //let Json.Net deserialize the json string. passing in the json string we recieved and the typeof uc = (UserClass.RootObject)JsonConvert.DeserializeObject(returnedUserJson, typeof(UserClass.RootObject)); } catch { //if there was an error jsonError = true; } //if there was no error then let us set our variables if (!jsonError) { //now since we only load one persons profile at a time we will check the count of how mant items are in the lost //if the list has no items then we procede as normal. //but if the list does have an item in it. we do NOT add an item. we just change the values of the current item //this allows up to save memory if (FriendProfileList.Count == 0) { //set variables the long way because of null classes that can be sent back if you have an unused account friendProfileItem up = new friendProfileItem(); up.firstName = uc.response.user.firstName; up.lastName = uc.response.user.lastName; up.hometown = uc.response.user.homeCity; up.tipCount = uc.response.user.tips.count; up.badgesCount = uc.response.user.badges.count; up.checkInCount = uc.response.user.checkins.count; up.friendCount = uc.response.user.friends.count; up.email = uc.response.user.contact.email; up.image = uc.response.user.photo.prefix + "original" + uc.response.user.photo.suffix; up.bio = uc.response.user.bio; //when a user has no recent or max score the server returns null to the class //we need to do this so that the any variables used inside scores class have a default value if (uc.response.user.scores == null) { up.maxScore = 0; up.recentScore = 0; } else { up.maxScore = uc.response.user.scores.max; up.recentScore = uc.response.user.scores.recent; } up.mayorshipCount = uc.response.user.mayorships.count; //have to check count of checkin items. for someone who has never checked in the class will be null //causing the application to crash if (uc.response.user.checkins.items.Count > 0) { up.lastLocation = uc.response.user.checkins.items.First().venue.name; up.categoryImage = uc.response.user.checkins.items.First().venue.categories.First().icon.prefix + "bg_64" + uc.response.user.checkins.items.First().venue.categories.First().icon.suffix; up.lastLongitudeLocation = uc.response.user.checkins.items.First().venue.location.lng; up.lastLatitudeLocation = uc.response.user.checkins.items.First().venue.location.lat; //finally add user to list FriendProfileList.Add(up); up = null; //set map coordinates if there was a last known place setMapToLastVisitedLocation(new GeoCoordinate() { Latitude = UserProfile[0].lastLatitudeLocation, Longitude = userProfile[0].lastLongitudeLocation }); } else { up.lastLocation = uc.response.user.firstName + " " + uc.response.user.lastName + " has no last known location"; //finally add user to list UserProfile.Add(up); up = null; } } else if (FriendProfileList.Count == 1) { //set variables the long way because of null classes that can be sent back if you have an unused account friendProfileItem up = new friendProfileItem(); up.firstName = uc.response.user.firstName; up.lastName = uc.response.user.lastName; up.hometown = uc.response.user.homeCity; up.tipCount = uc.response.user.tips.count; up.badgesCount = uc.response.user.badges.count; up.checkInCount = uc.response.user.checkins.count; up.friendCount = uc.response.user.friends.count; up.email = uc.response.user.contact.email; up.image = uc.response.user.photo.prefix + "original" + uc.response.user.photo.suffix; up.bio = uc.response.user.bio; //when a user has no recent or max score the server returns null to the class //we need to do this so that the any variables used inside scores class have a default value if (uc.response.user.scores == null) { up.maxScore = 0; up.recentScore = 0; } else { up.maxScore = uc.response.user.scores.max; up.recentScore = uc.response.user.scores.recent; } up.mayorshipCount = uc.response.user.mayorships.count; //have to check count of checkin items. for someone who has never checked in the class will be null //causing the application to crash if (uc.response.user.checkins.items.Count > 0) { up.lastLocation = uc.response.user.checkins.items.First().venue.name; up.categoryImage = uc.response.user.checkins.items.First().venue.categories.First().icon.prefix + "bg_64" + uc.response.user.checkins.items.First().venue.categories.First().icon.suffix; up.lastLongitudeLocation = uc.response.user.checkins.items.First().venue.location.lng; up.lastLatitudeLocation = uc.response.user.checkins.items.First().venue.location.lat; //finally add user to list FriendProfileList.Add(up); FriendProfileList.RemoveAt(0); up = null; //set map coordinates if there was a last known place setMapToLastVisitedLocation(new GeoCoordinate() { Latitude = UserProfile[0].lastLatitudeLocation, Longitude = userProfile[0].lastLongitudeLocation }); } else { up.lastLocation = uc.response.user.firstName + " " + uc.response.user.lastName + " has no last known location"; //finally add user to list UserProfile.Add(up); UserProfile.RemoveAt(0); up = null; } } //this delay will allow the ui to load up all information //we delay showing the profile by one second while images load await Task.Delay(1000); //change visibility to make profile grid visible //and make visibility of progress bar collapsed Deployment.Current.Dispatcher.BeginInvoke(() => { friendProfileGrid.Visibility = Visibility.Visible; progressBar.Visibility = Visibility.Collapsed; progressBar.IsEnabled = false; }); } } //there was an error so just stop the loading and pop up a message box else { } }