|
| 1 | +// |
| 2 | +// Created by Vyacheslav Karpukhin on 19.02.20. |
| 3 | +// Copyright (c) 2020 Chris Ballinger. All rights reserved. |
| 4 | +// |
| 5 | + |
| 6 | +import Foundation |
| 7 | +import OTRAssets |
| 8 | +import MBProgressHUD |
| 9 | + |
| 10 | +class OTRStorageUsageViewController : XLFormViewController { |
| 11 | + private let ROOT_SECTION_TAG = "rootSection" |
| 12 | + private let DELETE_ALL_TAG = "deleteAll" |
| 13 | + private let NO_MEDIA_FOUND_TAG = "noMediaFound" |
| 14 | + |
| 15 | + private let connections = OTRDatabaseManager.shared.connections |
| 16 | + |
| 17 | + init() { |
| 18 | + super.init(nibName: nil, bundle: nil) |
| 19 | + self.form = self.formDescriptor() |
| 20 | + } |
| 21 | + |
| 22 | + required public init!(coder aDecoder: NSCoder!) { |
| 23 | + fatalError("init(coder:) has not been implemented") |
| 24 | + } |
| 25 | + |
| 26 | + func formDescriptor() -> XLFormDescriptor { |
| 27 | + let form = XLFormDescriptor(title: STORAGE_USAGE_TITLE()) |
| 28 | + |
| 29 | + let firstSection = XLFormSectionDescriptor() |
| 30 | + firstSection.multivaluedTag = ROOT_SECTION_TAG |
| 31 | + form.addFormSection(firstSection) |
| 32 | + |
| 33 | + let deleteAll = XLFormRowDescriptor(tag: DELETE_ALL_TAG, rowType: XLFormRowDescriptorTypeButton, title: STORAGE_USAGE_DELETE_ALL_BUTTON()) |
| 34 | + deleteAll.action.formBlock = { row in |
| 35 | + self.deselectFormRow(row) |
| 36 | + self.deleteMedia() |
| 37 | + } |
| 38 | + firstSection.addFormRow(deleteAll) |
| 39 | + |
| 40 | + let noMediaFound = XLFormRowDescriptor(tag: NO_MEDIA_FOUND_TAG, rowType: XLFormRowDescriptorTypeText) |
| 41 | + noMediaFound.value = STORAGE_USAGE_NO_MEDIA_FOUND() |
| 42 | + noMediaFound.disabled = true |
| 43 | + firstSection.addFormRow(noMediaFound) |
| 44 | + |
| 45 | + return form |
| 46 | + } |
| 47 | + |
| 48 | + override func viewDidLoad() { |
| 49 | + super.viewDidLoad() |
| 50 | + self.tableView.isEditing = false |
| 51 | + DispatchQueue.global().async { |
| 52 | + self.processAllMedia() |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private func processAllMedia() { |
| 57 | + var empty = true |
| 58 | + connections?.read.read { (transaction: YapDatabaseReadTransaction) in |
| 59 | + transaction.enumerateKeysAndObjects(inCollection: OTRMediaItem.collection, using: { (key, object, stop) in |
| 60 | + if let mediaItem = object as? OTRMediaItem { |
| 61 | + let parentMessage = mediaItem.parentMessage(with: transaction) as? OTRBaseMessage |
| 62 | + |
| 63 | + if let threadOwner = parentMessage?.threadOwner(with: transaction), |
| 64 | + let account = threadOwner.account(with: transaction) { |
| 65 | + do { |
| 66 | + let length = try OTRMediaFileManager.shared.dataLength(for: mediaItem, buddyUniqueId: threadOwner.threadIdentifier) |
| 67 | + empty = false |
| 68 | + |
| 69 | + let section = sectionForAccount(account) |
| 70 | + let row = rowForThreadOwner(threadOwner, section) |
| 71 | + let value = row.value as? Int ?? 0 |
| 72 | + row.value = value + length.intValue |
| 73 | + |
| 74 | + DispatchQueue.main.async { |
| 75 | + self.updateFormRow(row) |
| 76 | + } |
| 77 | + } catch { |
| 78 | + return |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + }) |
| 83 | + } |
| 84 | + if let deleteAll = self.form.formRow(withTag: DELETE_ALL_TAG), |
| 85 | + let noMediaFound = self.form.formRow(withTag: NO_MEDIA_FOUND_TAG){ |
| 86 | + DispatchQueue.main.async { |
| 87 | + deleteAll.hidden = empty |
| 88 | + noMediaFound.hidden = !empty |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private func rowForThreadOwner(_ threadOwner: OTRThreadOwner, _ section: XLFormSectionDescriptor) -> XLFormRowDescriptor { |
| 94 | + var row = self.form.formRow(withTag: threadOwner.threadIdentifier) |
| 95 | + if row == nil { |
| 96 | + row = XLFormRowDescriptor(tag: threadOwner.threadIdentifier, rowType: XLFormRowDescriptorTypeInfo, title: threadOwner.threadName) |
| 97 | + row?.valueFormatter = ByteCountFormatter() |
| 98 | + DispatchQueue.main.sync { |
| 99 | + section.addFormRow(row!) |
| 100 | + } |
| 101 | + } |
| 102 | + return row! |
| 103 | + } |
| 104 | + |
| 105 | + private func sectionForAccount(_ account: OTRAccount) -> XLFormSectionDescriptor { |
| 106 | + var section = (self.form.formSections as! [XLFormSectionDescriptor]).first { $0.title == account.displayName } |
| 107 | + if section == nil { |
| 108 | + section = XLFormSectionDescriptor.formSection(withTitle: account.displayName, sectionOptions: .canDelete) |
| 109 | + DispatchQueue.main.sync { |
| 110 | + self.form.addFormSection(section!) |
| 111 | + } |
| 112 | + } |
| 113 | + return section! |
| 114 | + } |
| 115 | + |
| 116 | + override func formRowHasBeenRemoved(_ formRow: XLFormRowDescriptor!, at indexPath: IndexPath!) { |
| 117 | + super.formRowHasBeenRemoved(formRow, at: indexPath) |
| 118 | + deleteMedia(formRow.tag) |
| 119 | + } |
| 120 | + |
| 121 | + private func deleteMedia(_ threadIdentifier: String? = nil) { |
| 122 | + DispatchQueue.main.async { [weak self] in |
| 123 | + guard let strongSelf = self else { return } |
| 124 | + MBProgressHUD.showAdded(to: strongSelf.view, animated: true) |
| 125 | + } |
| 126 | + connections?.write.asyncReadWrite { [weak self] (transaction: YapDatabaseReadWriteTransaction) in |
| 127 | + guard let strongSelf = self else { return } |
| 128 | + let mediaItemsToDelete = strongSelf.findItemsToDelete(transaction, threadIdentifier) |
| 129 | + strongSelf.doDelete(transaction, mediaItemsToDelete) |
| 130 | + DispatchQueue.global().async { |
| 131 | + strongSelf.vacuumAndUpdateUI(deleteAll: threadIdentifier == nil) |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + private func findItemsToDelete(_ transaction: YapDatabaseReadWriteTransaction, _ threadIdentifier: String?) -> [OTRMediaItem] { |
| 137 | + var mediaItemsToDelete: [OTRMediaItem] = [] |
| 138 | + transaction.enumerateKeysAndObjects(inCollection: OTRMediaItem.collection, using: { (key, object, stop) in |
| 139 | + if let mediaItem = object as? OTRMediaItem { |
| 140 | + let parentMessage = mediaItem.parentMessage(with: transaction) as? OTRBaseMessage |
| 141 | + if threadIdentifier != nil, |
| 142 | + let threadOwner = parentMessage?.threadOwner(with: transaction), |
| 143 | + threadOwner.threadIdentifier != threadIdentifier { |
| 144 | + return |
| 145 | + } |
| 146 | + if (parentMessage?.mediaItemUniqueId != nil) { |
| 147 | + mediaItemsToDelete.append(mediaItem) |
| 148 | + } |
| 149 | + } |
| 150 | + }) |
| 151 | + return mediaItemsToDelete |
| 152 | + } |
| 153 | + |
| 154 | + private func doDelete(_ transaction: YapDatabaseReadWriteTransaction, _ mediaItemsToDelete: [OTRMediaItem]) { |
| 155 | + mediaItemsToDelete.forEach { mediaItem in |
| 156 | + if let parentMessage = mediaItem.parentMessage(with: transaction) as? OTRBaseMessage, |
| 157 | + let threadOwner = parentMessage.threadOwner(with: transaction) { |
| 158 | + |
| 159 | + mediaItem.remove(with: transaction) |
| 160 | + |
| 161 | + let media = OTRMediaItem.incomingItem(withFilename: mediaItem.filename, mimeType: nil) |
| 162 | + media.parentObjectKey = parentMessage.uniqueId |
| 163 | + media.parentObjectCollection = parentMessage.messageCollection |
| 164 | + media.save(with: transaction) |
| 165 | + |
| 166 | + parentMessage.mediaItemUniqueId = media.uniqueId |
| 167 | + parentMessage.messageError = FileTransferError.automaticDownloadsDisabled |
| 168 | + parentMessage.save(with: transaction) |
| 169 | + |
| 170 | + OTRMediaFileManager.shared.deleteData(for: mediaItem, |
| 171 | + buddyUniqueId: threadOwner.threadIdentifier, completion: nil, completionQueue: nil) |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + private func vacuumAndUpdateUI(deleteAll: Bool) { |
| 177 | + OTRMediaFileManager.shared.vacuum { [weak self] in |
| 178 | + guard let strongSelf = self else { return } |
| 179 | + if deleteAll { |
| 180 | + strongSelf.form.formSections.forEach { |
| 181 | + let element = $0 as! XLFormSectionDescriptor |
| 182 | + if element.multivaluedTag != strongSelf.ROOT_SECTION_TAG { |
| 183 | + strongSelf.form.removeFormSection(element) |
| 184 | + } |
| 185 | + } |
| 186 | + if let deleteAll = strongSelf.form.formRow(withTag: strongSelf.DELETE_ALL_TAG), |
| 187 | + let noMediaFound = strongSelf.form.formRow(withTag: strongSelf.NO_MEDIA_FOUND_TAG){ |
| 188 | + deleteAll.hidden = true |
| 189 | + noMediaFound.hidden = false |
| 190 | + } |
| 191 | + } |
| 192 | + MBProgressHUD.hide(for: strongSelf.view, animated: true) |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments