StorageManagementViewController.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2019-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import UIKit
  21. import CocoaLumberjackSwift
  22. class StorageManagementViewController: ThemedTableViewController {
  23. @IBOutlet weak var storageTotal: UILabel!
  24. @IBOutlet weak var storageTotalValue: UILabel!
  25. @IBOutlet weak var storageTotalInUse: UILabel!
  26. @IBOutlet weak var storageTotalInUseValue: UILabel!
  27. @IBOutlet weak var storageTotalFree: UILabel!
  28. @IBOutlet weak var storageTotalFreeValue: UILabel!
  29. @IBOutlet weak var storageThreema: UILabel!
  30. @IBOutlet weak var storageThreemaValue: UILabel!
  31. @IBOutlet weak var storageThreemaActivityIndicator: UIActivityIndicatorView!
  32. @IBOutlet weak var mediaDeleteCell: UITableViewCell!
  33. @IBOutlet weak var mediaDeleteLabel: UILabel!
  34. @IBOutlet weak var mediaDeleteDetailLabel: UILabel!
  35. @IBOutlet weak var mediaDeleteButtonLabel: UILabel!
  36. @IBOutlet weak var mediaDeleteActivityIndicator: UIActivityIndicatorView!
  37. @IBOutlet weak var messageDeleteCell: UITableViewCell!
  38. @IBOutlet weak var messageDeleteLabel: UILabel!
  39. @IBOutlet weak var messageDeleteDetailLabel: UILabel!
  40. @IBOutlet weak var messageDeleteButtonLabel: UILabel!
  41. @IBOutlet weak var messageDeleteActivityIndicator: UIActivityIndicatorView!
  42. private var olderThanCell: String?
  43. private var mediaOlderThanOption = OlderThanOption.oneYear
  44. private var messageOlderThanOption = OlderThanOption.oneYear
  45. enum OlderThanOption: Int, CaseIterable {
  46. case oneYear = 0
  47. case sixMonths
  48. case threeMonths
  49. case oneMonth
  50. case oneWeek
  51. case everything
  52. }
  53. private enum Section: Int, CaseIterable {
  54. case storage = 0
  55. case deletionNote
  56. case deleteMedia
  57. case deleteMessage
  58. }
  59. required init?(coder aDecoder: NSCoder) {
  60. super.init(coder: aDecoder)
  61. }
  62. override func viewDidLoad() {
  63. super.viewDidLoad()
  64. self.title = BundleUtil.localizedString(forKey: "storage_management")
  65. self.storageTotal.text = BundleUtil.localizedString(forKey: "storage_total")
  66. self.storageTotalInUse.text = BundleUtil.localizedString(forKey: "storage_total_in_use")
  67. self.storageTotalFree.text = BundleUtil.localizedString(forKey: "storage_total_free")
  68. self.storageThreema.text = BundleUtil.localizedString(forKey: "storage_threema");
  69. self.storageThreemaActivityIndicator.hidesWhenStopped = true
  70. self.mediaDeleteLabel.text = BundleUtil.localizedString(forKey: "delete_media_older_than")
  71. self.mediaDeleteDetailLabel.text = StorageManagementViewController.titleDescription(for: mediaOlderThanOption)
  72. self.mediaDeleteButtonLabel.text = BundleUtil.localizedString(forKey: "delete_media")
  73. self.mediaDeleteActivityIndicator.hidesWhenStopped = true
  74. self.messageDeleteLabel.text = BundleUtil.localizedString(forKey: "delete_messages_older_than")
  75. self.messageDeleteDetailLabel.text = StorageManagementViewController.titleDescription(for: messageOlderThanOption)
  76. self.messageDeleteButtonLabel.text = BundleUtil.localizedString(forKey: "delete_messages")
  77. self.messageDeleteActivityIndicator.hidesWhenStopped = true
  78. }
  79. override func viewWillAppear(_ animated: Bool) {
  80. super.viewWillAppear(animated)
  81. self.updateColors()
  82. self.updateSizes()
  83. tableView.reloadData()
  84. }
  85. // MARK: - Navigation
  86. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  87. guard let senderCell = sender as? UITableViewCell,
  88. let olderThanViewController = segue.destination as? StorageManagementOlderThanViewController else {
  89. return
  90. }
  91. self.olderThanCell = senderCell.reuseIdentifier
  92. olderThanViewController.selectedIndex = self.olderThanCell == "MediaOlderThanCell" ? mediaOlderThanOption.rawValue : messageOlderThanOption.rawValue
  93. }
  94. @IBAction func refreshOlderThanIndex(_ segue: UIStoryboardSegue) {
  95. if let olderThanViewController = segue.source as? StorageManagementOlderThanViewController {
  96. if self.olderThanCell == "MediaOlderThanCell" {
  97. self.mediaOlderThanOption = OlderThanOption(rawValue: olderThanViewController.selectedIndex) ?? .oneYear
  98. self.mediaDeleteDetailLabel.text = StorageManagementViewController.titleDescription(for: mediaOlderThanOption)
  99. }
  100. else {
  101. self.messageOlderThanOption = OlderThanOption(rawValue: olderThanViewController.selectedIndex) ?? .oneYear
  102. self.messageDeleteDetailLabel.text = StorageManagementViewController.titleDescription(for: messageOlderThanOption)
  103. }
  104. }
  105. }
  106. // MARK: - Table view data source
  107. override func numberOfSections(in tableView: UITableView) -> Int {
  108. return Section.allCases.count
  109. }
  110. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  111. guard let namedSection = Section(rawValue: section) else {
  112. fatalError("Unknown section \(section)")
  113. }
  114. switch namedSection {
  115. case .storage:
  116. return 4
  117. case .deletionNote:
  118. return 0
  119. case .deleteMedia, .deleteMessage:
  120. return 2
  121. }
  122. }
  123. override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  124. if #available(iOS 11.0, *) {
  125. if section == 0 {
  126. return 38.0
  127. }
  128. }
  129. return UITableView.automaticDimension
  130. }
  131. override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
  132. guard let namedSection = Section(rawValue: section) else {
  133. return nil
  134. }
  135. if namedSection == .deleteMedia {
  136. return BundleUtil.localizedString(forKey: "delete_media_explain")
  137. } else if namedSection == .deleteMessage {
  138. return BundleUtil.localizedString(forKey: "delete_messages_explain")
  139. } else if namedSection == .deletionNote {
  140. return BundleUtil.localizedString(forKey: "delete_explain")
  141. }
  142. return nil
  143. }
  144. override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
  145. Colors.update(cell)
  146. self.updateColors()
  147. }
  148. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  149. guard let namedSection = Section(rawValue: indexPath.section) else {
  150. return
  151. }
  152. if namedSection == .deleteMedia && indexPath.row == 1 {
  153. //confirm delete media
  154. UIAlertTemplate.showConfirm(owner: self, popOverSource: self.mediaDeleteButtonLabel!, title: deleteMediaConfirmationSentence(for: mediaOlderThanOption), message: nil, titleOk: BundleUtil.localizedString(forKey: "delete_media"), actionOk: { (action) in
  155. self.startMediaDelete()
  156. }, titleCancel: BundleUtil.localizedString(forKey: "cancel"))
  157. }
  158. else if namedSection == .deleteMessage && indexPath.row == 1 {
  159. //confirm delete messages
  160. UIAlertTemplate.showConfirm(owner: self, popOverSource: self.messageDeleteButtonLabel!, title: deleteMessageConfirmationSentence(for: messageOlderThanOption), message: nil, titleOk: BundleUtil.localizedString(forKey: "delete_messages"), actionOk: { (action) in
  161. self.startMessageDelete()
  162. }, titleCancel: BundleUtil.localizedString(forKey: "cancel"))
  163. }
  164. }
  165. func updateColors() {
  166. self.storageThreemaValue.textColor = Colors.fontLight()
  167. self.mediaDeleteDetailLabel.textColor = Colors.fontLight()
  168. self.messageDeleteDetailLabel.textColor = Colors.fontLight()
  169. self.mediaDeleteButtonLabel.textColor = Colors.red()
  170. self.messageDeleteButtonLabel.textColor = Colors.red()
  171. self.mediaDeleteActivityIndicator.color = Colors.fontLight()
  172. self.storageThreemaActivityIndicator.color = Colors.fontLight()
  173. self.messageDeleteActivityIndicator.color = Colors.fontLight()
  174. }
  175. func updateSizes() {
  176. self.storageThreemaValue.text = ""
  177. self.storageThreemaActivityIndicator.startAnimating()
  178. let deviceStorage = FileUtility.deviceSizeInBytes()
  179. self.storageTotalValue.text = ByteCountFormatter.string(fromByteCount: deviceStorage.totalSize ?? 0, countStyle: ByteCountFormatter.CountStyle.file)
  180. self.storageTotalInUseValue.text = ByteCountFormatter.string(fromByteCount: (deviceStorage.totalSize ?? 0) - (deviceStorage.totalFreeSize ?? 0), countStyle: ByteCountFormatter.CountStyle.file)
  181. self.storageTotalFreeValue.text = ByteCountFormatter.string(fromByteCount: deviceStorage.totalFreeSize ?? 0, countStyle: ByteCountFormatter.CountStyle.file)
  182. DispatchQueue(label: "calcStorageThreema").async {
  183. var dbSize: Int64 = 0
  184. var appSize: Int64 = 0
  185. if let appDataUrl = FileUtility.appDataDirectory {
  186. // Check DatabaseManager.storeSize
  187. let dbUrl = appDataUrl.appendingPathComponent("ThreemaData.sqlite")
  188. dbSize = FileUtility.fileSizeInBytes(fileUrl: dbUrl) ?? 0
  189. DDLogInfo("DB size \(ByteCountFormatter.string(fromByteCount: dbSize, countStyle: ByteCountFormatter.CountStyle.file))")
  190. FileUtility.pathSizeInBytes(pathUrl: appDataUrl, size: &appSize)
  191. DDLogInfo("APP size \(ByteCountFormatter.string(fromByteCount: appSize, countStyle: ByteCountFormatter.CountStyle.file))")
  192. }
  193. DispatchQueue.main.async {
  194. self.storageThreemaValue.text = ByteCountFormatter.string(fromByteCount: appSize, countStyle: ByteCountFormatter.CountStyle.file)
  195. self.storageThreemaActivityIndicator.stopAnimating()
  196. }
  197. }
  198. }
  199. static func titleDescription(for option: OlderThanOption) -> String {
  200. switch option {
  201. case .oneYear:
  202. return BundleUtil.localizedString(forKey: "one_year_title")
  203. case .sixMonths:
  204. return BundleUtil.localizedString(forKey: "six_months_title")
  205. case .threeMonths:
  206. return BundleUtil.localizedString(forKey: "three_months_title")
  207. case .oneMonth:
  208. return BundleUtil.localizedString(forKey: "one_month_title")
  209. case .oneWeek:
  210. return BundleUtil.localizedString(forKey: "one_week_title")
  211. case .everything:
  212. return BundleUtil.localizedString(forKey: "everything")
  213. }
  214. }
  215. private func description(for option: OlderThanOption) -> String {
  216. switch option {
  217. case .oneYear:
  218. return BundleUtil.localizedString(forKey: "one_year")
  219. case .sixMonths:
  220. return BundleUtil.localizedString(forKey: "six_months")
  221. case .threeMonths:
  222. return BundleUtil.localizedString(forKey: "three_months")
  223. case .oneMonth:
  224. return BundleUtil.localizedString(forKey: "one_month")
  225. case .oneWeek:
  226. return BundleUtil.localizedString(forKey: "one_week")
  227. case .everything:
  228. return BundleUtil.localizedString(forKey: "everything")
  229. }
  230. }
  231. private func deleteMediaConfirmationSentence(for option: OlderThanOption) -> String {
  232. switch option {
  233. case .oneYear, .sixMonths, .threeMonths, .oneMonth, .oneWeek:
  234. let defaultString = BundleUtil.localizedString(forKey: "delete_media_confirm") ?? ""
  235. return String(format: defaultString, description(for: option))
  236. case .everything:
  237. return BundleUtil.localizedString(forKey: "delete_media_confirm_all")
  238. }
  239. }
  240. private func deleteMessageConfirmationSentence(for option: OlderThanOption) -> String {
  241. switch option {
  242. case .oneYear, .sixMonths, .threeMonths, .oneMonth, .oneWeek:
  243. let defaultString = BundleUtil.localizedString(forKey: "delete_messages_confirm") ?? ""
  244. return String(format: defaultString, description(for: option))
  245. case .everything:
  246. return BundleUtil.localizedString(forKey: "delete_messages_confirm_all")
  247. }
  248. }
  249. func olderThanDate(_ option: OlderThanOption) -> Date? {
  250. let calendar = Calendar.current
  251. let now = Date()
  252. switch option {
  253. case .oneYear:
  254. return calendar.date(byAdding: .year, value: -1, to: now)
  255. case .sixMonths:
  256. return calendar.date(byAdding: .month, value: -6, to: now)
  257. case .threeMonths:
  258. return calendar.date(byAdding: .month, value: -3, to: now)
  259. case .oneMonth:
  260. return calendar.date(byAdding: .month, value: -1, to: now)
  261. case .oneWeek:
  262. return calendar.date(byAdding: .day, value: -7, to: now)
  263. case .everything:
  264. return nil
  265. }
  266. }
  267. func startMediaDelete() {
  268. self.mediaDeleteActivityIndicator.startAnimating()
  269. self.mediaDeleteButtonLabel.isHidden = true
  270. self.mediaDeleteCell.isUserInteractionEnabled = false
  271. Timer.scheduledTimer(timeInterval: TimeInterval(0.3), target: self, selector: #selector(self.mediaDelete), userInfo: nil, repeats: false)
  272. }
  273. @objc func mediaDelete() {
  274. let dbContext = DatabaseManager.db()!.getDatabaseContext(!Thread.isMainThread)
  275. let destroyer = EntityDestroyer(managedObjectContext: dbContext!.current)
  276. if let count = destroyer.deleteMedias(olderThan: self.olderThanDate(mediaOlderThanOption)) {
  277. DDLogNotice("\(count) media files deleted")
  278. ChatViewControllerCache.clear()
  279. }
  280. DispatchQueue.main.async {
  281. self.mediaDeleteActivityIndicator.stopAnimating()
  282. self.mediaDeleteButtonLabel.isHidden = false
  283. self.mediaDeleteCell.isUserInteractionEnabled = true
  284. self.updateSizes()
  285. }
  286. }
  287. func startMessageDelete() {
  288. self.messageDeleteActivityIndicator.startAnimating()
  289. self.messageDeleteButtonLabel.isHidden = true
  290. self.messageDeleteCell.isUserInteractionEnabled = false
  291. Timer.scheduledTimer(timeInterval: TimeInterval(0.3), target: self, selector: #selector(self.messageDelete), userInfo: nil, repeats: false)
  292. }
  293. @objc func messageDelete() {
  294. let dbContext = DatabaseManager.db()!.getDatabaseContext(!Thread.isMainThread)
  295. let entityManager = EntityManager(databaseContext: dbContext)!
  296. if let count = entityManager.entityDestroyer.deleteMessages(olderThan: self.olderThanDate(messageOlderThanOption)) {
  297. // Delete single conversation or delete last message of group conversation it has no messages
  298. for conversation in entityManager.entityFetcher.allConversations() {
  299. if let conversation = conversation as? Conversation {
  300. let messageFetcher = MessageFetcher(for: conversation, with: entityManager.entityFetcher)
  301. if messageFetcher?.count() == 0 {
  302. if !conversation.isGroup() {
  303. entityManager.performSyncBlockAndSafe({
  304. entityManager.entityDestroyer.deleteObject(object: conversation)
  305. })
  306. }
  307. else {
  308. entityManager.performSyncBlockAndSafe {
  309. conversation.lastMessage = nil
  310. }
  311. }
  312. }
  313. }
  314. }
  315. DDLogNotice("\(count) messages deleted")
  316. ChatViewControllerCache.clear()
  317. }
  318. DispatchQueue.main.async {
  319. self.messageDeleteActivityIndicator.stopAnimating()
  320. self.messageDeleteButtonLabel.isHidden = false
  321. self.messageDeleteCell.isUserInteractionEnabled = true
  322. self.updateSizes()
  323. }
  324. }
  325. }