public async Task <ApplicationUser> GetCurrentUser() { DatabaseManager manager = await AndroidUtils.GetDbManager().ConfigureAwait(false); if (manager.CurrentUser == null) { // Something bad has happened, log out var suppress = AndroidUtils.ReturnToSignIn(this); } return(manager.CurrentUser); }
public override async void OnResume() { base.OnResume(); if (viewLoaded && !refreshingData && ForceRefresh) { LoadRemoteData(); } bool hasUploads = (await AndroidUtils.GetDbManager().ConfigureAwait(false)).GetUploadQueue().Any(); Activity.RunOnUiThread(() => uploadsHint.Visibility = hasUploads ? ViewStates.Visible : ViewStates.Gone); }
public override async void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Load from cached data from the database if available, // just in case we can't contact the server List <FeedSection> cached = await((MainActivity)Activity).GetCachedContent(false); var metrics = Resources.DisplayMetrics; var widthInDp = AndroidUtils.ConvertPixelsToDp(metrics.WidthPixels, Activity); int cols = Math.Max(widthInDp / 300, 1); adapter = new FeedItemsAdapter(cached, await AndroidUtils.GetDbManager().ConfigureAwait(false)); adapter.ItemClick += OnItemClick; layoutManager = new GridLayoutManager(Activity, cols); layoutManager.SetSpanSizeLookup(new GridSpanner(adapter, cols)); }
public override async void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Load from cached data from the database if available, // just in case we can't contact the server List <FeedSection> cached = await((MainActivity)Activity).GetCachedContent(false); // Check for recently opened activities FeedSection recents = await LoadRecent(); if (recents != null) { cached.Insert(0, recents); } var metrics = Resources.DisplayMetrics; var widthInDp = AndroidUtils.ConvertPixelsToDp(metrics.WidthPixels, Activity); int cols = Math.Max(widthInDp / 300, 1); adapter = new FeedItemsAdapter(cached, await AndroidUtils.GetDbManager().ConfigureAwait(false)); adapter.ItemClick += OnItemClick; if (savedInstanceState != null) { adapter.Data = JsonConvert.DeserializeObject <List <FeedSection> >(savedInstanceState.GetString("MAIN_ADAPTER_DATA")); adapter.NotifyDataSetChanged(); } layoutManager = new GridLayoutManager(Activity, cols); layoutManager.SetSpanSizeLookup(new GridSpanner(adapter, cols)); if (!AndroidUtils.IsGooglePlayServicesInstalled(Activity) || googleApiClient != null) { return; } googleApiClient = new GoogleApiClient.Builder(Activity) .AddConnectionCallbacks(this) .AddOnConnectionFailedListener(this) .AddApi(LocationServices.API) .Build(); googleApiClient?.Connect(); }
// Update the TaskTypes available in the background public async Task UpdateTaskTypes() { DatabaseManager db = await AndroidUtils.GetDbManager().ConfigureAwait(false); List <TaskType> taskTypes = await ServerUtils.RefreshTaskTypes(db).ConfigureAwait(false); if (taskTypes == null) { var suppress = AndroidUtils.ReturnToSignIn(this); RunOnUiThread(() => Toast.MakeText(this, Resource.String.ForceSignOut, ToastLength.Long).Show()); return; } if (!taskTypes.Any()) { return; } db.AddTaskTypes(taskTypes); }
public async void SaveProgress() { newActivity.LearningTasks = adapter.Data; // Hide the prompt if the user has added a task fabPrompt.Visibility = (newActivity.LearningTasks != null && newActivity.LearningTasks.Any()) ? ViewStates.Gone : ViewStates.Visible; // Don't save changes to uploaded activities until we're ready to submit if (editingSubmitted) { return; } DatabaseManager db = await AndroidUtils.GetDbManager().ConfigureAwait(false); // Add/update this new activity in the user's inprogress cache string cacheJson = db.CurrentUser.LocalCreatedActivitiesJson; List <LearningActivity> inProgress = (string.IsNullOrWhiteSpace(cacheJson)) ? new List <LearningActivity>() : JsonConvert.DeserializeObject <List <LearningActivity> >(cacheJson); int existingInd = inProgress.FindIndex((la) => la.Id == newActivity.Id); if (existingInd == -1) { inProgress.Insert(0, newActivity); } else { inProgress.RemoveAt(existingInd); inProgress.Insert(0, newActivity); } db.CurrentUser.LocalCreatedActivitiesJson = JsonConvert.SerializeObject(inProgress); db.AddUser(db.CurrentUser); MainMyCreationsFragment.ForceRefresh = true; }
public async Task <List <FeedSection> > GetCachedContent(bool ownedOnly) { ApplicationUser currentUser = await GetCurrentUser().ConfigureAwait(false); string contentJsonCache = (ownedOnly) ? currentUser?.RemoteCreatedContentJson : currentUser?.CachedContentJson; try { if (string.IsNullOrWhiteSpace(contentJsonCache)) { return(new List <FeedSection>()); } return(JsonConvert.DeserializeObject <List <FeedSection> >(contentJsonCache, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects, ReferenceLoopHandling = ReferenceLoopHandling.Serialize, MaxDepth = 12 })); } catch (Exception e) { Console.WriteLine(e.Message); RunOnUiThread(() => Toast.MakeText(this, Resource.String.errorCache, ToastLength.Long).Show()); if (currentUser != null) { currentUser.CachedContentJson = null; (await AndroidUtils.GetDbManager().ConfigureAwait(false)).AddUser(currentUser); } return(new List <FeedSection>()); } }
public override bool OnOptionsItemSelected(IMenuItem item) { if (item.ItemId == global::Android.Resource.Id.Home) { OnBackPressed(); return(true); } if (item.ItemId != Resource.Id.menudelete) { return(base.OnOptionsItemSelected(item)); } using (var builder = new global::Android.Support.V7.App.AlertDialog.Builder(this)) { builder.SetTitle(Resource.String.deleteTitle) .SetMessage(Resource.String.deleteMessage) .SetNegativeButton(Resource.String.dialog_cancel, (a, e) => { }) .SetPositiveButton(Resource.String.DeleteBtn, async(a, e) => { if (editingSubmitted) { using (ProgressDialog prog = new ProgressDialog(this)) { prog.SetMessage(Resources.GetString(Resource.String.PleaseWait)); prog.Show(); ServerResponse <string> resp = await ServerUtils.Delete <string>("/api/learningactivities?id=" + newActivity.Id).ConfigureAwait(false); RunOnUiThread(() => prog.Dismiss()); if (resp == null) { var suppress = AndroidUtils.ReturnToSignIn(this); RunOnUiThread(() => Toast.MakeText(this, Resource.String.ForceSignOut, ToastLength.Long).Show()); } else if (resp.Success) { RunOnUiThread(() => Toast.MakeText(this, Resource.String.uploadsUploadSuccessTitle, ToastLength.Long).Show()); MainMyCreationsFragment.ForceRefresh = true; Finish(); } else { RunOnUiThread(() => Toast.MakeText(this, Resource.String.ConnectionError, ToastLength.Long).Show()); } } } else { DatabaseManager db = await AndroidUtils.GetDbManager().ConfigureAwait(false); var localActivities = JsonConvert.DeserializeObject <List <LearningActivity> >(db.CurrentUser.LocalCreatedActivitiesJson); localActivities.Remove(localActivities.FirstOrDefault(act => act.Id == newActivity.Id)); db.CurrentUser.LocalCreatedActivitiesJson = JsonConvert.SerializeObject(localActivities); db.AddUser(db.CurrentUser); MainMyCreationsFragment.ForceRefresh = true; Finish(); } }) .Show(); } return(true); }