Example #1
0
        private void ProcessTotal(StorageStatistics value)
        {
            var result = new StorageStatisticsByChat();

            result.ByFileType = new List <StorageStatisticsByFileType>();

            foreach (var chat in value.ByChat)
            {
                result.Count += chat.Count;
                result.Size  += chat.Size;

                foreach (var type in chat.ByFileType)
                {
                    var already = result.ByFileType.FirstOrDefault(x => x.FileType.TypeEquals(type.FileType));
                    if (already == null)
                    {
                        already = new StorageStatisticsByFileType(type.FileType, 0, 0);
                        result.ByFileType.Add(already);
                    }

                    already.Count += type.Count;
                    already.Size  += type.Size;
                }
            }

            TotalStatistics = result;
            IsLoading       = false;
        }
Example #2
0
        public SettingsStorageOptimizationPage(IProtoService protoService, StorageStatisticsByChat statistics)
        {
            InitializeComponent();

            PrimaryButtonText   = Strings.Resources.CacheClear;
            SecondaryButtonText = Strings.Resources.Close;

            var chat = protoService.GetChat(statistics.ChatId);

            Title.Text    = chat == null ? "Other Chats" : protoService.GetTitle(chat);
            Subtitle.Text = FileSizeConverter.Convert(statistics.Size);

            Photo.Source     = chat == null ? null : PlaceholderHelper.GetChat(protoService, chat, (int)Photo.Width);
            Photo.Visibility = chat == null ? Visibility.Collapsed : Visibility.Visible;

            List.ItemsSource = statistics.ByFileType.OrderByDescending(x => x.Size).ToList();

            foreach (var fileType in statistics.ByFileType)
            {
                switch (fileType.FileType)
                {
                case FileTypeAnimation animation:
                case FileTypeAudio audio:
                case FileTypeDocument document:
                case FileTypeNone none:
                case FileTypePhoto photo:
                case FileTypeUnknown unknown:
                case FileTypeVideo video:
                case FileTypeVideoNote videoNote:
                case FileTypeVoiceNote voiceNote:
                    List.SelectedItems.Add(fileType);
                    break;
                }
            }
        }
        private StorageStatistics ProcessTotal(StorageStatistics value)
        {
            var result = new StorageStatisticsByChat();

            result.ByFileType = new List <StorageStatisticsByFileType>();

            for (int i = 0; i < value.ByChat.Count; i++)
            {
                var chat = value.ByChat[i];

                result.Count += chat.Count;
                result.Size  += chat.Size;

                for (int j = 0; j < chat.ByFileType.Count; j++)
                {
                    var type = chat.ByFileType[j];

                    if (type.FileType is FileTypeProfilePhoto || type.FileType is FileTypeWallpaper)
                    {
                        result.Count -= type.Count;
                        result.Size  -= type.Size;

                        chat.Count -= type.Count;
                        chat.Size  -= type.Size;

                        chat.ByFileType.Remove(type);
                        j--;

                        continue;
                    }

                    var already = result.ByFileType.FirstOrDefault(x => x.FileType.TypeEquals(type.FileType));
                    if (already == null)
                    {
                        already = new StorageStatisticsByFileType(type.FileType, 0, 0);
                        result.ByFileType.Add(already);
                    }

                    already.Count += type.Count;
                    already.Size  += type.Size;
                }

                if (chat.ByFileType.IsEmpty())
                {
                    value.ByChat.Remove(chat);
                    i--;
                }
            }

            TotalStatistics = result;
            IsLoading       = false;

            return(value);
        }
Example #4
0
        public async void Clear(StorageStatisticsByChat byChat)
        {
            if (byChat == null)
            {
                return;
            }

            var dialog = new ContentDialogBase();
            var page   = new SettingsStorageOptimizationPage(ProtoService, dialog, byChat);

            dialog.Content = page;

            var confirm = await dialog.ShowAsync();

            if (confirm != ContentDialogBaseResult.OK)
            {
                return;
            }

            var types = page.SelectedItems ?? new FileType[0];

            if (types.IsEmpty())
            {
                return;
            }

            var chatIds         = new long[0];
            var excludedChatIds = new long[0];

            if (byChat.ChatId != 0)
            {
                chatIds = new[] { byChat.ChatId };
            }
            else if (byChat != _totalStatistics)
            {
                excludedChatIds = _statistics.ByChat.Select(x => x.ChatId).Where(x => x != 0).ToArray();
            }

            IsLoading     = true;
            TaskCompleted = false;

            var response = await ProtoService.SendAsync(new OptimizeStorage(long.MaxValue, 0, int.MaxValue, 0, types, chatIds, excludedChatIds, 25));

            if (response is StorageStatistics statistics)
            {
                Statistics = statistics;
            }

            IsLoading     = false;
            TaskCompleted = true;
        }
        public SettingsStorageOptimizationPage(IProtoService protoService, StorageStatisticsByChat statistics)
        {
            InitializeComponent();

            PrimaryButtonText   = Strings.Resources.CacheClear;
            SecondaryButtonText = Strings.Resources.Close;

            var chat = protoService.GetChat(statistics.ChatId);

            TitleChat.Text = chat == null ? Strings.Additional.SettingsStorageOtherChats : protoService.GetTitle(chat);
            Subtitle.Text  = FileSizeConverter.Convert(statistics.Size, true);

            Photo.Source     = chat == null ? null : PlaceholderHelper.GetChat(protoService, chat, (int)Photo.Width);
            Photo.Visibility = chat == null ? Visibility.Collapsed : Visibility.Visible;

            StorageChartItem photo    = null;
            StorageChartItem video    = null;
            StorageChartItem document = null;
            StorageChartItem audio    = null;
            StorageChartItem voice    = null;
            StorageChartItem stickers = null;
            StorageChartItem local    = null;

            foreach (var fileType in statistics.ByFileType)
            {
                switch (fileType.FileType)
                {
                case FileTypePhoto _:
                    photo = new StorageChartItem(fileType);
                    break;

                case FileTypeVideo _:
                case FileTypeAnimation _:
                    video = video?.Add(fileType) ?? new StorageChartItem(fileType);
                    break;

                case FileTypeDocument _:
                    document = new StorageChartItem(fileType);
                    break;

                case FileTypeAudio _:
                    audio = new StorageChartItem(fileType);
                    break;

                case FileTypeVideoNote _:
                case FileTypeVoiceNote _:
                    voice = voice?.Add(fileType) ?? new StorageChartItem(fileType);
                    break;

                case FileTypeSticker _:
                    stickers = new StorageChartItem(fileType);
                    break;

                case FileTypeProfilePhoto _:
                case FileTypeWallpaper _:
                    break;

                default:
                    local = local?.Add(fileType) ?? new StorageChartItem(fileType);
                    break;
                }
            }

            var items = new[] { photo, video, document, audio, voice, stickers, local }.Where(x => x != null).ToList();

            List.ItemsSource = items;
            Chart.Items      = items;

            var size     = Chart.Items.Where(x => x.IsVisible).Sum(x => x.Size);
            var readable = FileSizeConverter.Convert(size, true).Split(' ');

            SizeLabel.Text = readable[0];
            UnitLabel.Text = readable[1];
        }