//Class Methods public async void initUserAndConversation(ListView aCurrentUserListView) { //GET request to server to update chatListView with recentely POSTed message using (HttpClient client = new HttpClient()) //using block makes the object disposable (one time use) { using (HttpResponseMessage response = await client.GetAsync("http://159.203.252.197/users/")) { if (App.DEBUG_MODE) { Debug.WriteLine("GET Status Code: " + response.StatusCode); Debug.WriteLine("GET Reason: " + response.ReasonPhrase); } using (HttpContent content = response.Content) { string content_string = await content.ReadAsStringAsync(); System.Net.Http.Headers.HttpContentHeaders content_headers = content.Headers; if (App.DEBUG_MODE) { Debug.WriteLine("GET content: " + content_string); Debug.WriteLine("GET content headers: " + content_headers); } //TEMPORARY METHOD TO POST MESSAGES FROM SERVER TO CHAT VIEW this.userList = JsonConvert.DeserializeObject<List<UserDetails>>(content_string); Debug.WriteLine("CONVERSATION PRINT: "+userList[0].conversations_leading[0]); for (int x = 0; x < userList.Count; x++) { ListViewItem aUser = new ListViewItem(); aUser.Content = userList[x].user_name; //Outputs desired text into the actual chat window aUser.Tag = userList[x]; //References the UserAndConversation object aCurrentUserListView.Items.Add(aUser); } currentUser = userList[0]; } } } }
//Loads the selected conversation from the friends list into chatListView (TASK METHOD INSURES ALL VARIABLES LOAD) public async Task parseSelectedConversation(string urlOfConversation) { using (HttpClient client = new HttpClient()) //using block makes the object disposable (one time use) { using (HttpResponseMessage response = await client.GetAsync(urlOfConversation)) { using (HttpContent content = response.Content) { string content_string = await content.ReadAsStringAsync(); System.Net.Http.Headers.HttpContentHeaders content_headers = content.Headers; currentConversation = JsonConvert.DeserializeObject<ConversationDetails>(content_string); //Load data into the otherUser variable (person you're speaking with) by matching the currentUser's url tag with participants //If they match, set the otherUser variable to the other participant in the conversation if (currentConversation.participant_1 == currentUser.user_url) { HttpClient subClient = new HttpClient(); HttpResponseMessage subResponse = await subClient.GetAsync(this.currentConversation.participant_2); HttpContent subContent = subResponse.Content; string content_string2 = await subContent.ReadAsStringAsync(); //System.Net.Http.Headers.HttpContentHeaders content_headers2 = subContent.Headers; otherUser = JsonConvert.DeserializeObject<UserDetails>(content_string2); } else if (currentConversation.participant_2 == currentUser.user_url) { HttpClient subClient = new HttpClient(); HttpResponseMessage subResponse = await subClient.GetAsync(this.currentConversation.participant_1); HttpContent subContent = subResponse.Content; string content_string2 = await subContent.ReadAsStringAsync(); //System.Net.Http.Headers.HttpContentHeaders content_headers2 = subContent.Headers; otherUser = JsonConvert.DeserializeObject<UserDetails>(content_string2); } //Loads the otherUser's public key into memory { HttpClient subClient = new HttpClient(); HttpResponseMessage subResponse = await subClient.GetAsync(otherUser.public_key); HttpContent subContent = subResponse.Content; string content_string2 = await subContent.ReadAsStringAsync(); dynamic incomingJSON = JsonConvert.DeserializeObject(content_string2); otherUser.public_key_ACTUAL = incomingJSON.key; Debug.WriteLine("PUB KEY ACTUAL: " + otherUser.public_key_ACTUAL); } if (App.DEBUG_MODE == true) { Debug.WriteLine("CONVO URL: "+this.currentConversation.conversation_url); Debug.WriteLine("PART 1: "+this.currentConversation.participant_1); Debug.WriteLine("PART 2: "+this.currentConversation.participant_2); for (int x = 0; x < this.currentConversation.conversation_Messages.Count; x++) { Debug.WriteLine("MESSAGES: "+this.currentConversation.conversation_Messages[x]); } } } } } App.secrets.loadOtherUserPublicKey(otherUser.public_key_ACTUAL); }
//Loads the selected conversation from the friends list into chatListView (TASK METHOD INSURES ALL VARIABLES LOAD) public async Task parseSelectedConversation(string urlOfConversation) { using (HttpClient client = new HttpClient()) //using block makes the object disposable (one time use) { using (HttpResponseMessage response = await client.GetAsync(urlOfConversation)) { using (HttpContent content = response.Content) { string content_string = await content.ReadAsStringAsync(); System.Net.Http.Headers.HttpContentHeaders content_headers = content.Headers; currentConversation = JsonConvert.DeserializeObject <ConversationDetails>(content_string); //Load data into the otherUser variable (person you're speaking with) by matching the currentUser's url tag with participants //If they match, set the otherUser variable to the other participant in the conversation if (currentConversation.participant_1 == currentUser.user_url) { HttpClient subClient = new HttpClient(); HttpResponseMessage subResponse = await subClient.GetAsync(this.currentConversation.participant_2); HttpContent subContent = subResponse.Content; string content_string2 = await subContent.ReadAsStringAsync(); //System.Net.Http.Headers.HttpContentHeaders content_headers2 = subContent.Headers; otherUser = JsonConvert.DeserializeObject <UserDetails>(content_string2); } else if (currentConversation.participant_2 == currentUser.user_url) { HttpClient subClient = new HttpClient(); HttpResponseMessage subResponse = await subClient.GetAsync(this.currentConversation.participant_1); HttpContent subContent = subResponse.Content; string content_string2 = await subContent.ReadAsStringAsync(); //System.Net.Http.Headers.HttpContentHeaders content_headers2 = subContent.Headers; otherUser = JsonConvert.DeserializeObject <UserDetails>(content_string2); } //Loads the otherUser's public key into memory { HttpClient subClient = new HttpClient(); HttpResponseMessage subResponse = await subClient.GetAsync(otherUser.public_key); HttpContent subContent = subResponse.Content; string content_string2 = await subContent.ReadAsStringAsync(); dynamic incomingJSON = JsonConvert.DeserializeObject(content_string2); otherUser.public_key_ACTUAL = incomingJSON.key; Debug.WriteLine("PUB KEY ACTUAL: " + otherUser.public_key_ACTUAL); } if (App.DEBUG_MODE == true) { Debug.WriteLine("CONVO URL: " + this.currentConversation.conversation_url); Debug.WriteLine("PART 1: " + this.currentConversation.participant_1); Debug.WriteLine("PART 2: " + this.currentConversation.participant_2); for (int x = 0; x < this.currentConversation.conversation_Messages.Count; x++) { Debug.WriteLine("MESSAGES: " + this.currentConversation.conversation_Messages[x]); } } } } } App.secrets.loadOtherUserPublicKey(otherUser.public_key_ACTUAL); }
//SET currentUser public void setCurrentUser(UserDetails userObject) { currentUser = userObject; }