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 void GetAccountDetails()
        {
            ProgressDialog dialog = new ProgressDialog(this);

            dialog.SetTitle(Resources.GetString(Resource.String.Connecting));
            dialog.SetMessage(Resources.GetString(Resource.String.PleaseWait));
            dialog.Show();

            ApplicationUser tempUser = new ApplicationUser()
            {
                AccessToken      = accessToken,
                AccessExpiresAt  = accessTokenExpiresAt,
                RefreshToken     = refreshToken,
                RefreshExpiresAt = refreshTokenExpiresAt
            };

            (await GetDatabaseManager()).AddUser(tempUser);

            Common.ServerResponse <ApplicationUser> res = await Common.ServerUtils.Get <ApplicationUser>("api/account/");

            dialog.Dismiss();

            if (res == null)
            {
                var suppress = AndroidUtils.ReturnToSignIn(this);
                Toast.MakeText(this, Resource.String.ForceSignOut, ToastLength.Long).Show();
                return;
            }

            if (res.Success)
            {
                // kill the login activity
                Intent message = new Intent("com.park.learn.CALLBACK");
                SendBroadcast(message);

                ApplicationUser updatedUser = res.Data;
                updatedUser.AccessToken      = tempUser.AccessToken;
                updatedUser.AccessExpiresAt  = tempUser.AccessExpiresAt;
                updatedUser.RefreshToken     = tempUser.RefreshToken;
                updatedUser.RefreshExpiresAt = tempUser.RefreshExpiresAt;

                (await GetDatabaseManager()).AddUser(updatedUser);
                Intent intent = new Intent(this, typeof(MainActivity));
                intent.AddFlags(ActivityFlags.ClearTop);
                StartActivity(intent);
                Finish();
            }
            else
            {
                // show error message, return to login screen
                new AlertDialog.Builder(this)
                .SetTitle(Resource.String.ErrorTitle)
                .SetMessage(Resource.String.ErrorLogin)
                .SetCancelable(false)
                .SetOnDismissListener(new OnDismissListener(() =>
                {
                    Intent intent = new Intent(this, typeof(LoginActivity));
                    StartActivity(intent);
                    Finish();
                }))
                .Show();
            }
        }
        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();
        }