示例#1
0
        public static async void GetAccountAchievements()
        {
            UiService.OnUiThread(() =>
                                 AccountAchievements.IsAchievementsEnabled = SdkService.MegaSdk.isAchievementsEnabled());

            if (!SdkService.MegaSdk.isAchievementsEnabled())
            {
                return;
            }

            var accountAchievementsRequestListener = new GetAccountAchievementsRequestListenerAsync();
            var accountAchievements = await accountAchievementsRequestListener.ExecuteAsync(() =>
            {
                SdkService.MegaSdk.getAccountAchievements(accountAchievementsRequestListener);
            });

            if (accountAchievements == null)
            {
                return;
            }

            var awards = new List <AwardClassViewModel>
            {
                // Add Base storage & transfer
                new AwardClassViewModel(null, true)
                {
                    StorageReward = accountAchievements.getBaseStorage(),
                }
            };

            var awardedClasses = new List <AwardClassViewModel>
            {
                // Add Base storage & transfer
                new AwardClassViewModel(null, true)
                {
                    StorageReward           = accountAchievements.getBaseStorage(),
                    IsTransferAmountVisible = false,
                }
            };


            // Get all the user awards
            var awardsCount = accountAchievements.getAwardsCount();

            for (uint i = 0; i < awardsCount; i++)
            {
                var awardId    = accountAchievements.getAwardId(i);
                var awardClass = (MAchievementClass)accountAchievements.getAwardClass(i);

                var awardedClass = awardedClasses.FirstOrDefault(a => a.AchievementClass == awardClass);
                if (awardedClass == null)
                {
                    awardedClass = new AwardClassViewModel(awardClass)
                    {
                        IsGranted = true
                    };
                    awardedClasses.Add(awardedClass);
                }

                var storageReward  = accountAchievements.getRewardStorageByAwardId(awardId);
                var transferReward = accountAchievements.getRewardTransferByAwardId(awardId);
                var expireDate     = accountAchievements.getAwardExpirationTs(i).ConvertTimestampToDateTime();
                var achievedOnDate = accountAchievements.getAwardTimestamp(i).ConvertTimestampToDateTime();
                var durationInDays = accountAchievements.getClassExpire((int)awardClass);
                awards.Add(new AwardClassViewModel(awardClass)
                {
                    StorageReward  = storageReward,
                    TransferReward = transferReward,
                    ExpireDate     = expireDate,
                    AchievedOnDate = achievedOnDate,
                    DurationInDays = durationInDays,
                    IsGranted      = true
                });


                if (awardClass == MAchievementClass.MEGA_ACHIEVEMENT_INVITE)
                {
                    var mails    = accountAchievements.getAwardEmails(i);
                    var mailSize = mails.size();
                    for (int m = 0; m < mailSize; m++)
                    {
                        var contact = new ContactViewModel(
                            SdkService.MegaSdk.getContact(mails.get(m)), awardedClass.Contacts)
                        {
                            StorageAmount           = storageReward,
                            TransferAmount          = transferReward,
                            ReferralBonusExpireDate = expireDate,
                        };
                        awardedClass.Contacts.ItemCollection.Items.Add(contact);
                        contact.GetContactFirstname();
                        contact.GetContactLastname();
                        contact.GetContactAvatarColor();
                        contact.GetContactAvatar();
                    }

                    if (expireDate <= DateTime.Now)
                    {
                        continue;
                    }

                    awardedClass.StorageReward  += storageReward;
                    awardedClass.TransferReward += transferReward;
                }
                else
                {
                    awardedClass.ExpireDate     = expireDate;
                    awardedClass.StorageReward  = storageReward;
                    awardedClass.TransferReward = transferReward;
                    awardedClass.AchievedOnDate = achievedOnDate;
                    awardedClass.DurationInDays = durationInDays;
                }
            }

            var awardClasses    = Enum.GetValues(typeof(MAchievementClass)).OfType <MAchievementClass>();
            var availableAwards = new List <AwardClassViewModel>();

            foreach (var awardClass in awardClasses)
            {
                switch (awardClass)
                {
                case MAchievementClass.MEGA_ACHIEVEMENT_WELCOME:
                    continue;

                case MAchievementClass.MEGA_ACHIEVEMENT_INVITE:
                {
                    var inviteClass = new AwardClassViewModel(awardClass)
                    {
                        StorageReward  = accountAchievements.getClassStorage((int)awardClass),
                        TransferReward = accountAchievements.getClassTransfer((int)awardClass),
                        DurationInDays = accountAchievements.getClassExpire((int)awardClass),
                    };

                    inviteClass.Contacts.ItemCollection.Items =
                        awardedClasses.FirstOrDefault(a => a.AchievementClass == awardClass)?.Contacts.ItemCollection.Items;
                    availableAwards.Add(inviteClass);

                    continue;
                };
                }

                var available = awards.FirstOrDefault(a => a.AchievementClass == awardClass);

                if (available != null)
                {
                    continue;
                }

                availableAwards.Add(new AwardClassViewModel(awardClass)
                {
                    StorageReward  = accountAchievements.getClassStorage((int)awardClass),
                    TransferReward = accountAchievements.getClassTransfer((int)awardClass),
                    DurationInDays = accountAchievements.getClassExpire((int)awardClass)
                });
            }

            UiService.OnUiThread(() =>
            {
                if (accountAchievements.currentStorage() != -1)
                {
                    AccountAchievements.CurrentStorageQuota = (ulong)accountAchievements.currentStorage();
                }

                if (accountAchievements.currentTransfer() != -1)
                {
                    AccountAchievements.CurrentTransferQuota = (ulong)accountAchievements.currentTransfer();
                }

                AccountAchievements.Awards          = awards;
                AccountAchievements.AwardedClasses  = awardedClasses;
                AccountAchievements.AvailableAwards = availableAwards;
                AccountAchievements.CompletedAwards = awardedClasses.Where(a => a.IsGranted).ToList();
            });
        }