private async Task UpdateAchievements()
        {
            await BackupData.WaitForInitAsync();

            Achievements.Clear();
            if (IsLoggedIn) //current user is logged in to the server
            {
                try
                {
                    var newlyUnlocked = await IoCManager.Resolve <IAchievementFetcher>().UpdateAchievements();

                    AchievementNotification.QueueAchievementNotifications(newlyUnlocked);

                    var achievementCounter = 0;
                    var unlockedCounter    = 0;
                    foreach (var achievement in DbManager.DataAccess.Achievements().GetAchievements())
                    {
                        Achievements.Add(AchievementViewModel.CreateFrom(achievement));

                        if (achievement.IsUnlocked)
                        {
                            unlockedCounter++;
                        }

                        achievementCounter++;
                    }

                    Score            = $"{Strings.AchievementsScreenView_Score} {AppSharedData.CurrentAchievementsScore()}";
                    AchievementCount = $"{unlockedCounter + "/" + achievementCounter}{Strings.AchievementsScreenView_Achievement_Count}";
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e);

                    if ((((WebException)e.InnerException).Response as HttpWebResponse)?.StatusCode == HttpStatusCode.InternalServerError)
                    {
                        await IoCManager.Resolve <INavigationService>()
                        .DisplayAlert(Strings.Alert_Server_Error_Title,
                                      Strings.Alert_Server_Error_Description,
                                      Strings.Alert_Confirm);
                    }
                    else
                    {
                        await IoCManager.Resolve <INavigationService>()
                        .DisplayAlert(Strings.Alert_Network_Error_Title,
                                      Strings.Alert_Network_Error_Description,
                                      Strings.Alert_Confirm);
                    }
                }
            }
        }
Exemple #2
0
        private void StartAutoCheckForAchievements()
        {
            if (Interlocked.Exchange(ref isAutoChecking, 1) == 1)
            {
                return;
            }

            var featureObserver = new CachingObserver <bool>();

            achievementFeatureSubscription = IoCManager.Resolve <IFeatureToggleRouter>()
                                             .IsFeatureEnabled(FeatureId.Achievements)
                                             .Subscribe(featureObserver);

            async Task DequeueAndRepeatAsync()
            {
                try
                {
                    if (featureObserver.Last /* feature is enabled */)
                    {
                        await BackupData.WaitForInitAsync();

                        var pending = await AchievementManager.DequeuePendingAchievementNotifications();

                        AchievementNotification.QueueAchievementNotifications(pending);
                    }
                }
                catch (InvalidTransactionException)
                {
                    // Safe to ignore in this case, just retry later.
                    // This can happen because the auto-refresh can interleave
                    // with other transactions that execute asynchronous code
                    // inside them.
                }

                await Task.Delay(5000);

                if (isAutoChecking == 1)
                {
                    Device.BeginInvokeOnMainThread(async() => await DequeueAndRepeatAsync());
                }
            }

            Device.BeginInvokeOnMainThread(async() => await DequeueAndRepeatAsync());
        }