From d3f4e63c931c6a2d8f83df48f73ed522c3aa65a3 Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Sun, 19 Jul 2020 19:48:15 +0300 Subject: [PATCH] Use device disk size to pick maximum cache size presets --- .../MaximumCacheSizePickerItem.swift | 29 ++++++++++++++----- .../StorageUsageController.swift | 4 +-- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/submodules/SettingsUI/Sources/Data and Storage/MaximumCacheSizePickerItem.swift b/submodules/SettingsUI/Sources/Data and Storage/MaximumCacheSizePickerItem.swift index 31fe0df115..bb511ddb3a 100644 --- a/submodules/SettingsUI/Sources/Data and Storage/MaximumCacheSizePickerItem.swift +++ b/submodules/SettingsUI/Sources/Data and Storage/MaximumCacheSizePickerItem.swift @@ -11,20 +11,35 @@ import LegacyComponents import ItemListUI import PresentationDataUtils +private func totalDiskSpace() -> Int64 { + do { + let systemAttributes = try FileManager.default.attributesOfFileSystem(forPath: NSHomeDirectory() as String) + return (systemAttributes[FileAttributeKey.systemSize] as? NSNumber)?.int64Value ?? 0 + } catch { + return 0 + } +} + private func stringForCacheSize(strings: PresentationStrings, size: Int32) -> String { - if size > 50 { + if size > 100 { return strings.Cache_NoLimit } else { return dataSizeString(Int64(size) * 1024 * 1024 * 1024) } } -private let maximumCacheSizeValues: [Int32] = [ - 5, - 20, - 50, - Int32.max -] +private let maximumCacheSizeValues: [Int32] = { + let diskSpace = totalDiskSpace() + if diskSpace > 100 * 1024 * 1024 * 1024 { + return [5, 20, 50, Int32.max] + } else if diskSpace > 50 * 1024 * 1024 * 1024 { + return [5, 16, 32, Int32.max] + } else if diskSpace > 24 * 1024 * 1024 * 1024 { + return [2, 8, 16, Int32.max] + } else { + return [1, 4, 8, Int32.max] + } +}() final class MaximumCacheSizePickerItem: ListViewItem, ItemListItem { let theme: PresentationTheme diff --git a/submodules/SettingsUI/Sources/Data and Storage/StorageUsageController.swift b/submodules/SettingsUI/Sources/Data and Storage/StorageUsageController.swift index e76d3bea1b..9b9882a2b2 100644 --- a/submodules/SettingsUI/Sources/Data and Storage/StorageUsageController.swift +++ b/submodules/SettingsUI/Sources/Data and Storage/StorageUsageController.swift @@ -291,9 +291,9 @@ private func storageUsageControllerEntries(presentationData: PresentationData, c entries.append(.keepMedia(presentationData.theme, presentationData.strings, cacheSettings.defaultCacheStorageTimeout)) entries.append(.keepMediaInfo(presentationData.theme, presentationData.strings.Cache_KeepMediaHelp)) - /*entries.append(.maximumSizeHeader(presentationData.theme, presentationData.strings.Cache_MaximumCacheSize.uppercased())) + entries.append(.maximumSizeHeader(presentationData.theme, presentationData.strings.Cache_MaximumCacheSize.uppercased())) entries.append(.maximumSize(presentationData.theme, presentationData.strings, cacheSettings.defaultCacheStorageLimitGigabytes)) - entries.append(.maximumSizeInfo(presentationData.theme, presentationData.strings.Cache_MaximumCacheSizeHelp))*/ + entries.append(.maximumSizeInfo(presentationData.theme, presentationData.strings.Cache_MaximumCacheSizeHelp)) var addedHeader = false