/// <summary> /// Main Request Thread /// </summary> async void AsyncHttpCall() { //Get the Internet connection profile try { // TODO: Section to detect over the limit/data issues. Create events for roaming or other issues. var _localStorageHelper = new StorageHelper(); var internetConnectionProfile = NetworkInformation.GetInternetConnectionProfile(); if (internetConnectionProfile.GetNetworkConnectivityLevel() < NetworkConnectivityLevel.InternetAccess) { // If the internet is down, check local storage for the last use, if not found, displace a message. await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { NoConnectionText.Visibility = Visibility.Collapsed; MainProgressRing.IsActive = true; MainProgressRing.Visibility = Visibility.Visible; UserListBox.Visibility = Visibility.Collapsed; var messageDialog = new MessageDialog("Sorry, no network detected."); // TODO: Move this out of the async thread. messageDialog.ShowAsync(); }); // TODO: This needs to be moved to the storage helper. var jsonSerializer = new DataContractJsonSerializer(typeof(SlackMembers)); var myStream = await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync(Configurations.LocalStorageFileName); _originalCurrentUsers.AddRangeWithClear(((SlackMembers)jsonSerializer.ReadObject(myStream))); // If there is no internet and the application has no previously saved items, display a small message. if (_originalCurrentUsers.Count == 0) { await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { NoConnectionText.Visibility = Visibility.Visible; }); } else { // If we have no internet, but there is previous data, load that data. _currentUsers = _originalCurrentUsers; await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { // TODO: Have the last update pull from the local store, this will require a new saving method. MainProgressRing.IsActive = false; MainProgressRing.Visibility = Visibility.Collapsed; UserListBox.Visibility = Visibility.Visible; }); } } else { // If we have a network connection, begin to call the web api. await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { MainProgressRing.IsActive = true; MainProgressRing.Visibility = Visibility.Visible; UserListBox.Visibility = Visibility.Collapsed; NoConnectionText.Visibility = Visibility.Collapsed; }); //Create Client var client = new HttpClient(); var uri = new Uri(string.Format(Configurations.UserListUrl, Configurations.ApiKey)); var response = await client.GetAsync(uri); var statusCode = response.StatusCode; switch (statusCode) { // TODO: Error handling for invalid htpp responses. } response.EnsureSuccessStatusCode(); _theResponse = await response.Content.ReadAsStringAsync(); using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(_theResponse))) { var serializer = new DataContractJsonSerializer(typeof(UserRoot)); var theList = serializer.ReadObject(ms) as UserRoot; await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { if (theList != null && theList.ok) { theList.members.LastUpdated = DateTime.Now; // Stamp the last time a user updated the list live. LastUpdatedText.Text = theList.members.LastUpdated.ToString("MMMM, MM dd, yyyy hh: mm:ss"); // TODO: This section needs to be cleaned up. We are wasting space on multiple instances of a similar object. _currentUsers.AddRangeWithClear(theList.members); _originalCurrentUsers.AddRangeWithClear(theList.members); MainProgressRing.IsActive = false; MainProgressRing.Visibility = Visibility.Collapsed; UserListBox.Visibility = Visibility.Visible; // Save to local storage, and ignore the thread. // TODO: This is not recommended. Move this call outside the async method to meet standards. _localStorageHelper.WriteJsonAsync(theList.members); } else { // TODO: Add more code for bad replies or invalid requests. } }); } } } catch (Exception ex) { // TODO: Find a fix for the await in the exception thread. await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { MainProgressRing.IsActive = false; MainProgressRing.Visibility = Visibility.Collapsed; UserListBox.Visibility = Visibility.Collapsed; var messageDialog = new MessageDialog("Sorry, an error occured! " + ex.Message); NoConnectionText.Visibility = Visibility.Visible; messageDialog.ShowAsync(); }); } }
/// <summary> /// Main Request Thread /// </summary> async void AsyncHttpCall() { //Get the Internet connection profile try { // TODO: Section to detect over the limit/data issues. Create events for roaming or other issues. var _localStorageHelper = new StorageHelper(); var internetConnectionProfile = NetworkInformation.GetInternetConnectionProfile(); if (internetConnectionProfile.GetNetworkConnectivityLevel() < NetworkConnectivityLevel.InternetAccess) { // If the internet is down, check local storage for the last use, if not found, displace a message. await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { NoConnectionText.Visibility = Visibility.Collapsed; MainProgressRing.IsActive = true; MainProgressRing.Visibility = Visibility.Visible; UserListBox.Visibility = Visibility.Collapsed; var messageDialog = new MessageDialog("Sorry, no network detected."); // TODO: Move this out of the async thread. messageDialog.ShowAsync(); }); // TODO: This needs to be moved to the storage helper. var jsonSerializer = new DataContractJsonSerializer(typeof(SlackMembers)); var myStream = await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync(Configurations.LocalStorageFileName); _originalCurrentUsers.AddRangeWithClear(((SlackMembers)jsonSerializer.ReadObject(myStream))); // If there is no internet and the application has no previously saved items, display a small message. if (_originalCurrentUsers.Count == 0) { await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { NoConnectionText.Visibility = Visibility.Visible; }); } else { // If we have no internet, but there is previous data, load that data. _currentUsers = _originalCurrentUsers; await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { // TODO: Have the last update pull from the local store, this will require a new saving method. MainProgressRing.IsActive = false; MainProgressRing.Visibility = Visibility.Collapsed; UserListBox.Visibility = Visibility.Visible; }); } } else { // If we have a network connection, begin to call the web api. await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { MainProgressRing.IsActive = true; MainProgressRing.Visibility = Visibility.Visible; UserListBox.Visibility = Visibility.Collapsed; NoConnectionText.Visibility = Visibility.Collapsed; }); //Create Client var client = new HttpClient(); var uri = new Uri(string.Format(Configurations.UserListUrl, Configurations.ApiKey)); var response = await client.GetAsync(uri); var statusCode = response.StatusCode; switch (statusCode) { // TODO: Error handling for invalid htpp responses. } response.EnsureSuccessStatusCode(); _theResponse = await response.Content.ReadAsStringAsync(); using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(_theResponse))) { var serializer = new DataContractJsonSerializer(typeof(UserRoot)); var theList = serializer.ReadObject(ms) as UserRoot; await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { if (theList != null && theList.ok) { theList.members.LastUpdated = DateTime.Now; // Stamp the last time a user updated the list live. LastUpdatedText.Text = theList.members.LastUpdated.ToString("MMMM, MM dd, yyyy hh: mm:ss"); // TODO: This section needs to be cleaned up. We are wasting space on multiple instances of a similar object. _currentUsers.AddRangeWithClear(theList.members); _originalCurrentUsers.AddRangeWithClear(theList.members); MainProgressRing.IsActive = false; MainProgressRing.Visibility = Visibility.Collapsed; UserListBox.Visibility = Visibility.Visible; // Save to local storage, and ignore the thread. // TODO: This is not recommended. Move this call outside the async method to meet standards. _localStorageHelper.WriteJsonAsync(theList.members); } else { // TODO: Add more code for bad replies or invalid requests. } }); } } } catch (Exception ex) { // TODO: Find a fix for the await in the exception thread. await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => { MainProgressRing.IsActive = false; MainProgressRing.Visibility = Visibility.Collapsed; UserListBox.Visibility = Visibility.Collapsed; var messageDialog = new MessageDialog("Sorry, an error occured! " + ex.Message); NoConnectionText.Visibility = Visibility.Visible; messageDialog.ShowAsync(); }); } }