private async Task DeleteRemoteActivity() { ShowLoadingOverlay(); ServerResponse <string> resp = await ServerUtils.Delete <string>("/api/learningactivities?id=" + thisActivity.Id); HideLoadingOverlay(); if (resp == null) { var suppress = AppUtils.SignOut(this); return; } if (resp.Success) { DatabaseManager dbManager = await Storage.GetDatabaseManager(false); dbManager.DeleteCachedActivity(thisActivity); Toast.ShowToast("Activity Deleted"); NavigationController.DismissViewController(true, null); } else { Toast.ShowToast("Error connecting to the server"); } }
private async Task GetAndOpenActivity(string code) { LoadingOverlay loadPop = new LoadingOverlay(UIScreen.MainScreen.Bounds); View.Add(loadPop); Common.ServerResponse <LearningActivity> result = await Common.ServerUtils.Get <LearningActivity>("/api/LearningActivities/GetWithCode?code=" + code); loadPop.Hide(); if (result == null) { var suppress = AppUtils.SignOut(this); } if (result.Success) { var suppress = AppUtils.OpenActivity(result.Data, Storyboard, NavigationController); } else { if (result.Message.StartsWith("404")) { AppUtils.ShowSimpleDialog(this, "Not found", "No activity was found with that share code.", "Got it"); } else { AppUtils.ShowSimpleDialog(this, "Connection Error", "Something went wrong! Please try again later.", "Got it"); } } }
private async void RefreshFeed() { ShowLoading(); if (dbManager == null) { dbManager = await Storage.GetDatabaseManager(); } // If we don't have internet, don't bother getting the location or // polling the server if (!AppDelegate.Online) { Console.WriteLine("No Internet access, loading cached feed"); Toast.ShowToast("Couldn't reach the server - please check your connection!"); AppDelegate.WhenOnline = RefreshFeed; // Reload if connection returns HideLoading(); return; } Common.ServerResponse <List <LearningActivity> > results = await Common.ServerUtils.Get <List <LearningActivity> >( "/api/learningactivities/getfromuser/?creatorId=" + dbManager.currentUser.Id); if (results == null) { var suppress = AppUtils.SignOut(this); } List <ActivityFeedSection> feed = new List <ActivityFeedSection>(); string unsubmittedActivitiesJson = dbManager.currentUser.LocalCreatedActivitiesJson; unsubmittedActivities = null; if (!string.IsNullOrWhiteSpace(unsubmittedActivitiesJson)) { unsubmittedActivities = JsonConvert.DeserializeObject <List <LearningActivity> >( unsubmittedActivitiesJson, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects, ReferenceLoopHandling = ReferenceLoopHandling.Serialize, MaxDepth = 10 }); } // Add a section to the feed if the user has activities which they didn't finish creating if (unsubmittedActivities != null && unsubmittedActivities.Count > 0) { feed.Add(new ActivityFeedSection { Title = "In Progress", Description = "Tap to continue making these activities.", Activities = unsubmittedActivities }); } List <LearningActivity> activitiesList = null; if (results.Success && results.Data != null) { activitiesList = results.Data.OrderByDescending((LearningActivity arg) => arg.CreatedAt).ToList(); // Save this in the offline cache dbManager.currentUser.RemoteCreatedActivitiesJson = JsonConvert.SerializeObject(results.Data, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects, ReferenceLoopHandling = ReferenceLoopHandling.Serialize, MaxDepth = 6 }); dbManager.AddUser(dbManager.currentUser); } else { Toast.ShowToast("Couldn't reach the server - please check your connection!"); if (!string.IsNullOrWhiteSpace(dbManager.currentUser.RemoteCreatedActivitiesJson)) { activitiesList = JsonConvert.DeserializeObject <List <LearningActivity> >( dbManager.currentUser.RemoteCreatedActivitiesJson); } } if (activitiesList != null && activitiesList.Count > 0) { feed.Add(new ActivityFeedSection { Title = "Your Uploaded Activities", Description = "These are the activities that you have previously uploaded", Activities = activitiesList }); } hasContent = feed.Count > 0; if (feed.Count == 0) { feed.Add(new ActivityFeedSection { Title = "No Activities Created", Description = "You haven't made any activities yet! Click the '+' button in the top right to get started.", Activities = new List <LearningActivity>() }); } source.Rows = feed; collectionView.ReloadData(); HideLoading(); }
private async Task <bool> UploadResults(int index, Action OnFinish) { ShowLoading(); AppDataUpload upload = viewSource.Rows[index]; DatabaseManager dbManager = await Storage.GetDatabaseManager(); // Upload relevent files bool success = await Storage.UploadFiles( JsonConvert.DeserializeObject <List <FileUpload> >(upload.FilesJson), index, (percentage) => { Console.WriteLine("Upload percentage: " + percentage); loadPop.loadingLabel.Text = string.Format("Uploading: {0}%", percentage); }, (listPos, jsonData) => { viewSource.Rows[listPos].FilesJson = jsonData; dbManager.UpdateUpload(viewSource.Rows[listPos]); viewSource.UpdateData(dbManager.GetUploadQueue().ToList()); upload = viewSource.Rows[index]; }, (upload.UploadType == UploadType.NewActivity || upload.UploadType == UploadType.UpdatedActivity)?Storage.GetCacheFolder() : Storage.GetUploadsFolder() ); if (!success) { HideLoading(); AppUtils.ShowSimpleDialog(this, "Unable to Upload", "Something went wrong, please try again later.", "Got it"); return(false); } ServerResponse <string> resp = new ServerResponse <string>(); if (upload.UploadType == UploadType.NewActivity || upload.UploadType == UploadType.UpdatedActivity) { resp = await ServerUtils.UploadActivity(upload, upload.UploadType == UploadType.UpdatedActivity); } else { // Uploading activity results List <FileUpload> files = JsonConvert.DeserializeObject <List <FileUpload> >(upload.FilesJson); AppTask[] results = JsonConvert.DeserializeObject <AppTask[]>(upload.JsonData) ?? new AppTask[0]; resp = await ServerUtils.UpdateAndPostResults(results, files, upload.UploadRoute); } HideLoading(); if (resp == null) { // do this but for iOS, fool var suppress = AppUtils.SignOut(this); return(false); } if (!resp.Success) { AppUtils.ShowSimpleDialog(this, "Unable to Upload", "Something went wrong, please try again later.", "Got it"); return(false); } dbManager.DeleteUpload(upload); var newList = dbManager.GetUploadQueue().ToList(); viewSource.UpdateData(newList); TableView.ReloadData(); await(ParentViewController as MainTabBarController).UpdateUploadsBadge(newList.Count); ManageNavItems(); OnFinish?.Invoke(); return(true); }
private async Task GetFromServer() { ShowLoading(); double lat = 0; double lon = 0; if (lastLoc != null) { lat = lastLoc.Coordinate.Latitude; lon = lastLoc.Coordinate.Longitude; } Common.ServerResponse <List <ActivityFeedSection> > remoteResults = await Common.ServerUtils.Get <List <ActivityFeedSection> >( string.Format("/api/learningactivities/GetFeed?lat={0}&lon={1}", lat, lon)); HideLoading(); if (remoteResults == null) { var suppress = AppUtils.SignOut(this); } List <ActivityFeedSection> feed = new List <ActivityFeedSection>(); // add already cached section if (recentActivities != null) { feed.Add(recentActivities); } if (remoteResults.Success && remoteResults.Data != null) { lastFromServer = remoteResults.Data; // Save this in the offline cache dbManager.currentUser.CachedActivitiesJson = JsonConvert.SerializeObject(remoteResults.Data, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects, ReferenceLoopHandling = ReferenceLoopHandling.Serialize, MaxDepth = 6 }); dbManager.AddUser(dbManager.currentUser); } else { Toast.ShowToast("Couldn't reach the server - please check your connection!"); lastFromServer = JsonConvert.DeserializeObject <List <ActivityFeedSection> >( dbManager.currentUser.CachedActivitiesJson); } if (lastFromServer != null) { feed.AddRange(lastFromServer); } source.Rows = feed; collectionView.ReloadData(); }