private async System.Threading.Tasks.Task FinishCreateAccount(string username, string localToken, string token, long accountId, int deviceId, string email)
        {
            if (DefaultAccountToUpgrade != null)
            {
                if (DefaultAccountToUpgrade.IsOnlineAccount)
                {
                    throw new Exception("Should be an offline account. This implies it got created but for some reason stayed on this page.");
                }

                DefaultAccountToUpgrade.Username   = username;
                DefaultAccountToUpgrade.LocalToken = localToken;
                DefaultAccountToUpgrade.Token      = token;
                DefaultAccountToUpgrade.AccountId  = accountId;
                DefaultAccountToUpgrade.DeviceId   = deviceId;

                await AccountsManager.Save(DefaultAccountToUpgrade);

                TelemetryExtension.Current?.TrackEvent("CreatedAccountFromDefault");

                // Transfer the settings
                try
                {
                    await SavedGradeScalesManager.TransferToOnlineAccountAsync(DefaultAccountToUpgrade.LocalAccountId, DefaultAccountToUpgrade.AccountId);
                }

                catch (Exception ex)
                {
                    TelemetryExtension.Current?.TrackException(ex);
                }

                // Make sure to update user info for telemetry
                TelemetryExtension.Current?.UpdateCurrentUser(DefaultAccountToUpgrade);

                // Remove this popup, and show a new one saying success!
                // We have to show first before removing otherwise iOS never shows it
                var parent = Parent;
                parent.ShowPopup(new SuccessfullyCreatedAccountViewModel(parent)
                {
                    Username = username,
                    Email    = email
                });
                base.RemoveViewModel();

                // Trigger a sync (without waiting) so all their content uploads
                Sync.StartSyncAccount(DefaultAccountToUpgrade);
            }
            else
            {
                var account = await AccountsManager.CreateAndInitializeAccountAsync(username, localToken, token, accountId, deviceId);

                if (account != null)
                {
                    // Take us to the account
                    var dontWait = FindAncestor <MainWindowViewModel>().SetCurrentAccount(account);
                }
            }
        }
        private async System.Threading.Tasks.Task finishConvertingToOnline(AccountDataItem account)
        {
            account.AccountId = _response.AccountId;
            account.DeviceId  = _response.DeviceId;
            //await login.Account.ConvertToOnline();

            await AccountsManager.Save(account);

            // Transfer the settings
            try
            {
                await SavedGradeScalesManager.TransferToOnlineAccountAsync(account.LocalAccountId, account.AccountId);
            }

            catch (Exception ex)
            {
                TelemetryExtension.Current?.TrackException(ex);
            }

            //have it sync, without waiting
            Sync.StartSyncAccount(account);
        }
        private async System.Threading.Tasks.Task FinishCreateAccount(string username, string localToken, string token, long accountId, int deviceId, string email)
        {
            if (DefaultAccountToUpgrade != null)
            {
                if (DefaultAccountToUpgrade.IsOnlineAccount)
                {
                    throw new Exception("Should be an offline account. This implies it got created but for some reason stayed on this page.");
                }

                DefaultAccountToUpgrade.Username            = username;
                DefaultAccountToUpgrade.LocalToken          = localToken;
                DefaultAccountToUpgrade.Token               = token;
                DefaultAccountToUpgrade.AccountId           = accountId;
                DefaultAccountToUpgrade.DeviceId            = deviceId;
                DefaultAccountToUpgrade.NeedsToSyncSettings = true; // Make sure we sync settings so things like week info uploads

                await AccountsManager.Save(DefaultAccountToUpgrade);

                TelemetryExtension.Current?.TrackEvent("CreatedAccountFromDefault", new Dictionary <string, string>()
                {
                    { "NewOnlineAccountId", accountId.ToString() }
                });

                // Make sure to update user info for telemetry
                TelemetryExtension.Current?.UpdateCurrentUser(DefaultAccountToUpgrade);

                // Transfer the settings
                try
                {
                    await SavedGradeScalesManager.TransferToOnlineAccountAsync(DefaultAccountToUpgrade.LocalAccountId, DefaultAccountToUpgrade.AccountId);
                }

                catch (Exception ex)
                {
                    TelemetryExtension.Current?.TrackException(ex);
                }

                try
                {
                    // Trigger a sync so all their content uploads
                    DateTime start = DateTime.UtcNow;

                    var syncResult = await Sync.SyncAccountAsync(DefaultAccountToUpgrade);

                    bool retried = false;
                    if (syncResult.Error != null)
                    {
                        // Try one more time
                        retried    = true;
                        syncResult = await Sync.SyncAccountAsync(DefaultAccountToUpgrade);
                    }

                    var duration = DateTime.UtcNow - start;

                    Dictionary <string, string> properties = new Dictionary <string, string>()
                    {
                        { "Duration", ((int)Math.Ceiling(duration.TotalSeconds)).ToString() }
                    };

                    if (retried)
                    {
                        properties["Retried"] = "true";
                    }

                    if (syncResult.Error != null)
                    {
                        properties["Error"] = syncResult.Error;
                    }

                    TelemetryExtension.Current?.TrackEvent("InitialSyncAfterCreatingAccount", properties);
                }
                catch (Exception ex)
                {
                    TelemetryExtension.Current?.TrackException(ex);
                }

                // Remove this popup, and show a new one saying success!
                // We have to show first before removing otherwise iOS never shows it
                var parent = Parent;
                parent.ShowPopup(new SuccessfullyCreatedAccountViewModel(parent)
                {
                    Username = username,
                    Email    = email
                });
                base.RemoveViewModel();
            }
            else
            {
                var account = await AccountsManager.CreateAndInitializeAccountAsync(username, localToken, token, accountId, deviceId);

                if (account != null)
                {
                    // Take us to the account
                    var dontWait = FindAncestor <MainWindowViewModel>().SetCurrentAccount(account);
                }
            }
        }