From 3ebcd8f16772bd2e1ca84a5fbe4fd885dd98abb6 Mon Sep 17 00:00:00 2001 From: Skalii Date: Thu, 16 Jan 2025 17:45:11 +0200 Subject: [PATCH] fix downloading cells --- .../OAStatusBackupTableViewController.mm | 59 +----- .../OAManageResourcesViewController.mm | 12 +- .../OAOutdatedResourcesViewController.mm | 5 +- .../OAWeatherForecastViewController.mm | 5 +- .../explore/TravelExploreViewController.swift | 3 +- .../DownloadingCellBaseHelper.swift | 180 ++++++++++++------ .../DownloadingCellCloudHelper.swift | 10 +- ...ownloadingCellMultipleResourceHelper.swift | 4 +- .../DownloadingCellResourceHelper.swift | 47 +++-- .../DownloadingListViewController.swift | 2 +- 10 files changed, 173 insertions(+), 154 deletions(-) diff --git a/Sources/Controllers/Cloud/OAStatusBackupTableViewController.mm b/Sources/Controllers/Cloud/OAStatusBackupTableViewController.mm index 2244be4dd0..6397ccd7a2 100644 --- a/Sources/Controllers/Cloud/OAStatusBackupTableViewController.mm +++ b/Sources/Controllers/Cloud/OAStatusBackupTableViewController.mm @@ -137,10 +137,9 @@ - (void) setupDownloadingCellHelper __weak OAStatusBackupTableViewController *weakSelf = self; _downloadingCellCloudHelper = [[DownloadingCellCloudHelper alloc] init]; [_downloadingCellCloudHelper setHostTableView:weakSelf.tableView]; - _downloadingCellCloudHelper.rightIconStyle = DownloadingCellRightIconTypeShowShevronBeforeDownloading; if (_tableType == EOARecentChangesConflicts) { - _downloadingCellCloudHelper.rightIconStyle = DownloadingCellRightIconTypeShowIconAndShevronAlways; + _downloadingCellCloudHelper.rightIconStyle = DownloadingCellRightIconTypeShowIconAndShevronBeforeDownloading; _downloadingCellCloudHelper.rightIconName = @"ic_custom_alert"; _downloadingCellCloudHelper.rightIconColor = [UIColor colorNamed:ACColorNameIconColorDisruptive]; } @@ -318,15 +317,12 @@ - (void)generateData EOABackupSyncOperationType operation = it.deleted ? EOABackupSyncOperationDelete : _tableType == EOARecentChangesLocal ? EOABackupSyncOperationUpload : EOABackupSyncOperationDownload; OATableRowData *rowData = [self rowFromKey:it.key - mainTint:it.deleted ? [UIColor colorNamed:ACColorNameIconColorActive] : [UIColor colorNamed:ACColorNameIconColorDisabled] - secondaryColorName:it.deleted ? ACColorNameIconColorDisruptive : ACColorNameIconColorActive operation:operation localFile:it.localFile remoteFile:it.remoteFile]; if (rowData) [itemsSection addRow:rowData]; } - } else if (_tableType == EOARecentChangesConflicts) { @@ -403,8 +399,6 @@ - (OATableRowData *)rowFromConflictItems:(NSString *)key remoteFile:(OARemoteFile *)remoteFile { OATableRowData *rowData = [self rowFromKey:key - mainTint:[UIColor colorNamed:ACColorNameIconColorDisabled] - secondaryColorName:ACColorNameIconColorDefault operation:EOABackupSyncOperationNone localFile:localFile remoteFile:remoteFile]; @@ -420,15 +414,11 @@ - (OATableRowData *)rowFromConflictItems:(NSString *)key NSForegroundColorAttributeName : [UIColor colorNamed:ACColorNameTextColorSecondary] } range:[attributedDescr.string rangeOfString:rowData.descr]]; [rowData setObj:attributedDescr forKey:@"descrAttr"]; - [rowData setObj:@"ic_custom_alert" forKey:@"secondaryIconConflict"]; - [rowData setObj:ACColorNameIconColorDisruptive forKey:@"secondaryIconColorName"]; [rowData setIconTintColor:[UIColor colorNamed:ACColorNameIconColorActive]]; return rowData; } - (OATableRowData *)rowFromKey:(NSString *)key - mainTint:(UIColor *)mainTint - secondaryColorName:(NSString *)secondaryColorName operation:(EOABackupSyncOperationType)operation localFile:(OALocalFile *)localFile remoteFile:(OARemoteFile *)remoteFile @@ -505,8 +495,6 @@ - (OATableRowData *)rowFromKey:(NSString *)key kCellTypeKey: [OARightIconTableViewCell getCellIdentifier], kCellTitleKey: name, kCellDescrKey: description, - kCellIconTintColor: mainTint, - @"secondaryIconColorName": secondaryColorName, @"operation": @(operation), @"fileName": fileName, @"settingsItem": settingsItem @@ -649,46 +637,11 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N OASettingsItem *settingsItem = [item objForKey:@"settingsItem"]; NSString *type = [OASettingsItemType typeName:settingsItem.type]; NSString *resourceId = [_downloadingCellCloudHelper getResourceIdWithTypeName:type filename:[item stringForKey:@"fileName"]]; - DownloadingCell *cell = [_downloadingCellCloudHelper getOrCreateCell:resourceId]; - if (cell) - { - BOOL hasConflict = (EOABackupSyncOperationType) [item integerForKey:@"operation"] == EOABackupSyncOperationNone; - cell.separatorInset = UIEdgeInsetsMake(0., [OAUtilities getLeftMargin] + kPaddingToLeftOfContentWithIcon, 0., 0.); - cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; - - NSString *description = item.descr; - NSAttributedString *descriptionAttributed = [item objForKey:@"descrAttr"]; - [cell descriptionVisibility:description != nil || descriptionAttributed != nil]; - if (descriptionAttributed) - { - cell.descriptionLabel.text = nil; - cell.descriptionLabel.attributedText = descriptionAttributed; - } - else - { - cell.descriptionLabel.attributedText = nil; - cell.descriptionLabel.text = description; - } - - cell.titleLabel.text = item.title; - [cell leftIconVisibility:YES]; - cell.leftIconView.image = [[item objForKey:@"icon"] imageFlippedForRightToLeftLayoutDirection]; - cell.leftIconView.tintColor = item.iconTintColor; - - NSString *secondaryIconName = hasConflict ? [item stringForKey:@"secondaryIconConflict"] : item.secondaryIconName; - if (secondaryIconName.length > 0) - { - cell.rightIconView.image = [UIImage templateImageNamed:secondaryIconName]; - cell.rightIconView.tintColor = [UIColor colorNamed:[item stringForKey:@"secondaryIconColorName"]]; - [cell rightIconVisibility:YES]; - } - else - { - cell.rightIconView.image = nil; - [cell rightIconVisibility:NO]; - } - } - return cell; + return [_downloadingCellCloudHelper getOrCreateCell:resourceId + title:item.title + desc:[item objForKey:@"descrAttr"] ?: item.descr + leftIcon:[[item objForKey:@"icon"] imageFlippedForRightToLeftLayoutDirection] + isDownloading:[_downloadingCellCloudHelper isDownloading:resourceId]]; } else if ([item.cellType isEqualToString:[OALargeImageTitleDescrTableViewCell getCellIdentifier]]) { diff --git a/Sources/Controllers/Resources/OAManageResourcesViewController.mm b/Sources/Controllers/Resources/OAManageResourcesViewController.mm index c54c81d5d1..cb7d17df3f 100644 --- a/Sources/Controllers/Resources/OAManageResourcesViewController.mm +++ b/Sources/Controllers/Resources/OAManageResourcesViewController.mm @@ -2670,23 +2670,15 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N if ([item_ isKindOfClass:OAMultipleResourceItem.class] && ([self.region hasGroupItems] || ((OAResourceItem *) item_).resourceType == OsmAndResourceType::SrtmMapRegion)) { OAMultipleResourceItem *item = (OAMultipleResourceItem *) item_; - UIColor *color = [UIColor colorNamed:ACColorNameIconColorDisabled]; - NSArray *items = [self.region hasGroupItems] ? [self.region.groupItem getItems:item.resourceType] : item.items; NSString *resourceId = [item getResourceId]; OAMultipleResourceSwiftItem *mapItem = [[OAMultipleResourceSwiftItem alloc] initWithItem:item]; - DownloadingCell *downloadingCell = [_downloadingCellMultipleResourceHelper getOrCreateCell:resourceId swiftResourceItem:mapItem]; - downloadingCell.titleLabel.text = title; - downloadingCell.descriptionLabel.text = subtitle; - return downloadingCell; + return [_downloadingCellMultipleResourceHelper getOrCreateCell:resourceId swiftResourceItem:mapItem title:title desc:subtitle]; } else if ([item_ isKindOfClass:OAResourceItem.class] || [item_ isKindOfClass:OASearchResult.class]) { OAResourceItem *item = (OAResourceItem *) ([item_ isKindOfClass:OASearchResult.class] ? ((OASearchResult *) item_).relatedObject : item_); OAResourceSwiftItem *mapItem = [[OAResourceSwiftItem alloc] initWithItem:item]; - DownloadingCell *downloadingCell = [_downloadingCellResourceHelper getOrCreateCell:mapItem.resourceId swiftResourceItem:mapItem]; - downloadingCell.titleLabel.text = title; - downloadingCell.descriptionLabel.text = subtitle; - return downloadingCell; + return [_downloadingCellResourceHelper getOrCreateCell:mapItem.resourceId swiftResourceItem:mapItem title:title desc:subtitle]; } else { diff --git a/Sources/Controllers/Resources/OAOutdatedResourcesViewController.mm b/Sources/Controllers/Resources/OAOutdatedResourcesViewController.mm index beabc36537..2c1ab641ef 100644 --- a/Sources/Controllers/Resources/OAOutdatedResourcesViewController.mm +++ b/Sources/Controllers/Resources/OAOutdatedResourcesViewController.mm @@ -496,10 +496,7 @@ - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NS else if (indexPath.section == _availableMapsSection) { OAResourceSwiftItem *mapItem = [[OAResourceSwiftItem alloc] initWithItem:_resourcesItems[indexPath.row]]; - DownloadingCell *cell = [_downloadingCellResourceHelper getOrCreateCell:mapItem.resourceId swiftResourceItem:mapItem]; - cell.titleLabel.text = title; - cell.descriptionLabel.text = description; - return cell; + return [_downloadingCellResourceHelper getOrCreateCell:mapItem.resourceId swiftResourceItem:mapItem title:title desc:description]; } return nil; } diff --git a/Sources/Controllers/Resources/OAWeatherForecastViewController.mm b/Sources/Controllers/Resources/OAWeatherForecastViewController.mm index af977f45f9..e018dfd8c1 100644 --- a/Sources/Controllers/Resources/OAWeatherForecastViewController.mm +++ b/Sources/Controllers/Resources/OAWeatherForecastViewController.mm @@ -928,12 +928,9 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N else if ([item[@"type"] isEqualToString:@"downloading_cell"]) { OAResourceSwiftItem *mapItem = [[OAResourceSwiftItem alloc] initWithItem:item[@"resource"]]; - DownloadingCell *cell = [_downloadingCellResourceHelper getOrCreateCell:item[@"resourceId"] swiftResourceItem:mapItem]; + DownloadingCell *cell = [_downloadingCellResourceHelper getOrCreateCell:item[@"resourceId"] swiftResourceItem:mapItem title:item[@"title"] desc:item[@"description"]]; [cell leftIconVisibility:NO]; [cell rightIconVisibility:NO]; - [cell descriptionVisibility:YES]; - cell.titleLabel.text = item[@"title"]; - cell.descriptionLabel.attributedText = item[@"description"]; return cell; } else if ([item[@"type"] isEqualToString:[OASimpleTableViewCell getCellIdentifier]]) diff --git a/Sources/Controllers/TravelGuides/explore/TravelExploreViewController.swift b/Sources/Controllers/TravelGuides/explore/TravelExploreViewController.swift index 5b026e3384..285d0c8b04 100644 --- a/Sources/Controllers/TravelGuides/explore/TravelExploreViewController.swift +++ b/Sources/Controllers/TravelGuides/explore/TravelExploreViewController.swift @@ -375,9 +375,8 @@ final class TravelExploreViewController: OABaseNavbarViewController, TravelExplo if item.cellType == "kDownloadCellKey" { if let resource = getSwiftResourceByIndex(indexPath: indexPath) { - outCell = downloadingCellResourceHelper.getOrCreateCell(resource.resourceId(), swiftResourceItem: resource) + outCell = downloadingCellResourceHelper.getOrCreateCell(resource.resourceId(), swiftResourceItem: resource, title: nil, desc: "") } - } else if item.cellType == OAFilledButtonCell.getIdentifier() { var cell = tableView.dequeueReusableCell(withIdentifier: OAFilledButtonCell.getIdentifier()) as? OAFilledButtonCell if cell == nil { diff --git a/Sources/Helpers/DownloadingCellHelper/DownloadingCellBaseHelper.swift b/Sources/Helpers/DownloadingCellHelper/DownloadingCellBaseHelper.swift index 486dd6c203..05dc3846eb 100644 --- a/Sources/Helpers/DownloadingCellHelper/DownloadingCellBaseHelper.swift +++ b/Sources/Helpers/DownloadingCellHelper/DownloadingCellBaseHelper.swift @@ -13,7 +13,15 @@ import UIKit } @objc enum DownloadingCellRightIconType: Int { - case hideIconAfterDownloading, showIconAlways, showShevronAlways, showIconAndShevronAlways, showShevronBeforeDownloading, showShevronAfterDownloading, showInfoAndShevronAfterDownloading, showDoneIconAfterDownloading + case hideIconAfterDownloading, + showIconAlways, + showShevronAlways, + showIconAndShevronAlways, + showIconAndShevronBeforeDownloading, + showShevronBeforeDownloading, + showShevronAfterDownloading, + showInfoAndShevronAfterDownloading, + showDoneIconAfterDownloading } @objcMembers @@ -101,52 +109,128 @@ class DownloadingCellBaseHelper: NSObject { // MARK: - Cell setup methods func getOrCreateCell(_ resourceId: String) -> DownloadingCell? { + getOrCreateCell(resourceId, + title: "", + desc: nil, + leftIcon: nil, + isDownloading: false) + } + + func getOrCreateCell(_ resourceId: String, + title: String?, + desc: Any?, + leftIcon: Any?, + isDownloading: Bool) -> DownloadingCell? { if statuses[resourceId] == nil { statuses[resourceId] = .idle } if progresses[resourceId] == nil { progresses[resourceId] = 0 } - var cell = cells[resourceId] - if cell == nil { - cell = setupCell(resourceId) - cells[resourceId] = cell + guard let cell = cells[resourceId] else { + let newCell = setupCell(resourceId, + title: title, + isTitleBold: false, + desc: desc, + leftIcon: leftIcon, + isDownloading: isDownloading) + cells[resourceId] = newCell + return newCell + } + let status = getStatus(resourceId: resourceId) + if status != .idle { + updateDesc(resourceId, cell: cell, desc: desc) + updateLeftIcon(resourceId, cell: cell, leftIcon: leftIcon) + updateRightIcon(resourceId, cell: cell, isDownloading: isDownloading) } return cell } - - // Override in subclass - func setupCell(_ resourceId: String) -> DownloadingCell? { - setupCell(resourceId: resourceId, title: "", isTitleBold: false, desc: nil, leftIconName: nil, rightIconName: nil, isDownloading: false) + + func updateLeftIcon(_ resourceId: String, + cell: DownloadingCell, + leftIcon: Any?) { + if let leftIcon { + cell.leftIconVisibility(true) + if let leftIconName = leftIcon as? String { + cell.leftIconView.image = UIImage.templateImageNamed(leftIconName) + } else if let leftIconImage = leftIcon as? UIImage { + cell.leftIconView.image = leftIconImage + } + updateLeftIconColor(resourceId, cell: cell, color: nil) + } else { + cell.leftIconVisibility(false) + } } - + + func updateLeftIconColor(_ resourceId: String, + cell: DownloadingCell?, + color: UIColor?) { + var downloadingCell = cell + if downloadingCell == nil { + downloadingCell = cells[resourceId] + } + guard let downloadingCell else { return } + if let color { + downloadingCell.leftIconView.tintColor = color + } else if isInstalled(resourceId) && isDownloadedLeftIconRecolored { + downloadingCell.leftIconView.tintColor = leftIconColor != nil ? leftIconColor : .iconColorActive + } else { + downloadingCell.leftIconView.tintColor = .iconColorDefault + } + } + + func updateRightIcon(_ resourceId: String, + cell: DownloadingCell, + isDownloading: Bool) { + if isDownloading { + cell.rightIconVisibility(false) + cells[resourceId] = cell + refreshCellProgress(resourceId) + } else { + setupRightIconForIdleCell(cell: cell, resourceId: resourceId) + } + } + + func updateDesc(_ resourceId: String, + cell: DownloadingCell?, + desc: Any?) { + var downloadingCell = cell + if downloadingCell == nil { + downloadingCell = cells[resourceId] + } + guard let downloadingCell else { return } + if let desc { + downloadingCell.descriptionVisibility(true) + if let descStr = desc as? String, !descStr.isEmpty { + downloadingCell.descriptionLabel.attributedText = nil + downloadingCell.descriptionLabel.text = descStr + downloadingCell.descriptionLabel.font = UIFont.monospacedFont(at: 12, withTextStyle: .body) + downloadingCell.descriptionLabel.textColor = .textColorSecondary + } else if let descAttr = desc as? NSAttributedString, descAttr.length > 0 { + downloadingCell.descriptionLabel.text = nil + downloadingCell.descriptionLabel.attributedText = descAttr + } + } else { + downloadingCell.descriptionVisibility(false) + } + } + // Override in subclass - func setupCell(resourceId: String, title: String?, isTitleBold: Bool, desc: String?, leftIconName: String?, rightIconName: String?, isDownloading: Bool) -> DownloadingCell? { + func setupCell(_ resourceId: String, + title: String?, + isTitleBold: Bool, + desc: Any?, + leftIcon: Any?, + isDownloading: Bool) -> DownloadingCell? { var cell = cells[resourceId] if cell == nil { let nib = Bundle.main.loadNibNamed(DownloadingCell.reuseIdentifier, owner: self, options: nil) cell = nib?.first as? DownloadingCell } guard let cell else { return nil } - + + cell.titleLabel.text = title cell.titleLabel.font = UIFont.preferredFont(forTextStyle: .body) - cell.leftIconView.tintColor = .iconColorDefault - cell.rightIconView.tintColor = getRightIconColor() - cell.rightIconView.image = UIImage.templateImageNamed(getRightIconName(resourceId)) - - if let leftIconName, !leftIconName.isEmpty { - cell.leftIconVisibility(true) - cell.leftIconView.image = UIImage.templateImageNamed(leftIconName) - if isInstalled(resourceId) && isDownloadedLeftIconRecolored { - cell.leftIconView.tintColor = leftIconColor != nil ? leftIconColor : .iconColorActive - } else { - cell.leftIconView.tintColor = .iconColorDefault - } - } else { - cell.leftIconVisibility(false) - } - - cell.titleLabel.text = title != nil ? title : "" if isTitleBold || isBoldTitleStyle { cell.titleLabel.font = UIFont.scaledSystemFont(ofSize: 17, weight: .medium) cell.titleLabel.textColor = .textColorActive @@ -154,28 +238,15 @@ class DownloadingCellBaseHelper: NSObject { cell.titleLabel.font = UIFont.preferredFont(forTextStyle: .body) cell.titleLabel.textColor = .textColorPrimary } - - if let desc, !desc.isEmpty { - cell.descriptionVisibility(true) - cell.descriptionLabel.text = desc - cell.descriptionLabel.font = UIFont.monospacedFont(at: 12, withTextStyle: .body) - cell.descriptionLabel.textColor = .textColorSecondary - } else { - cell.descriptionVisibility(false) - } - - if isDownloading { - cell.rightIconVisibility(false) - cells[resourceId] = cell - refreshCellProgress(resourceId) - } else { - setupRightIconForIdleCell(cell: cell, rightIconName: rightIconName, resourceId: resourceId) - } + + updateDesc(resourceId, cell: cell, desc: desc) + updateLeftIcon(resourceId, cell: cell, leftIcon: leftIcon) + updateRightIcon(resourceId, cell: cell, isDownloading: isDownloading) + return cell } - private func setupRightIconForIdleCell(cell: DownloadingCell, rightIconName: String?, resourceId: String) { - + private func setupRightIconForIdleCell(cell: DownloadingCell, resourceId: String) { var showIcon = false cell.accessoryView = nil cell.accessoryType = .none @@ -190,11 +261,15 @@ class DownloadingCellBaseHelper: NSObject { } else if rightIconStyle == .showIconAndShevronAlways { cell.accessoryType = .disclosureIndicator showIcon = true - } else if rightIconStyle == .showShevronBeforeDownloading { + } else if rightIconStyle == .showIconAndShevronBeforeDownloading { if !isInstalled(resourceId) { cell.accessoryType = .disclosureIndicator showIcon = true } + } else if rightIconStyle == .showShevronBeforeDownloading { + if !isInstalled(resourceId) { + cell.accessoryType = .disclosureIndicator + } } else if rightIconStyle == .showShevronAfterDownloading { if isInstalled(resourceId) { cell.accessoryType = .disclosureIndicator @@ -267,10 +342,7 @@ class DownloadingCellBaseHelper: NSObject { setCellProgress(resourceId: resourceId, progress: progress, status: status) } - func setCellProgress(resourceId: String, progress: Float, status: ItemStatusType) { - - guard helperHasItemFor(resourceId) else { return } - + func setCellProgress(resourceId: String, progress: Float, status: ItemStatusType) { saveStatus(resourceId: resourceId, status: status) saveProgress(resourceId: resourceId, progress: progress) var currentStatus = status @@ -312,7 +384,7 @@ class DownloadingCellBaseHelper: NSObject { // Downloading interupted by user saveStatus(resourceId: resourceId, status: .idle) } - setupRightIconForIdleCell(cell: cell, rightIconName: getRightIconName(resourceId), resourceId: resourceId) + setupRightIconForIdleCell(cell: cell, resourceId: resourceId) } } diff --git a/Sources/Helpers/DownloadingCellHelper/DownloadingCellCloudHelper.swift b/Sources/Helpers/DownloadingCellHelper/DownloadingCellCloudHelper.swift index 9eff8855b9..07b288adf1 100644 --- a/Sources/Helpers/DownloadingCellHelper/DownloadingCellCloudHelper.swift +++ b/Sources/Helpers/DownloadingCellHelper/DownloadingCellCloudHelper.swift @@ -9,7 +9,7 @@ import Foundation @objcMembers -final class DownloadingCellCloudHelper: DownloadingCellBaseHelper { +final class DownloadingCellCloudHelper: DownloadingCellBaseHelper { override init() { super.init() @@ -61,12 +61,8 @@ final class DownloadingCellCloudHelper: DownloadingCellBaseHelper { let name = notification.userInfo?["name"] as? String else { return } let resourceId = getResourceId(typeName: type, filename: name) DispatchQueue.main.async { [weak self] in - guard let self = self else { return } - if self.helperHasItemFor(resourceId) { - self.setCellProgress(resourceId: resourceId, progress: 1, status: .finished) - let cell = self.getOrCreateCell(resourceId) - cell?.leftIconView.tintColor = .iconColorActive - } + self?.setCellProgress(resourceId: resourceId, progress: 1, status: .finished) + self?.updateLeftIconColor(resourceId, cell: nil, color: .iconColorActive) } } } diff --git a/Sources/Helpers/DownloadingCellHelper/DownloadingCellMultipleResourceHelper.swift b/Sources/Helpers/DownloadingCellHelper/DownloadingCellMultipleResourceHelper.swift index 7549070a1d..22abff3650 100644 --- a/Sources/Helpers/DownloadingCellHelper/DownloadingCellMultipleResourceHelper.swift +++ b/Sources/Helpers/DownloadingCellHelper/DownloadingCellMultipleResourceHelper.swift @@ -70,7 +70,7 @@ final class DownloadingCellMultipleResourceHelper: DownloadingCellResourceHelper // MARK: - Cell setup methods - override func getOrCreateCell(_ resourceId: String, swiftResourceItem: OAResourceSwiftItem?) -> DownloadingCell? { + override func getOrCreateCell(_ resourceId: String, swiftResourceItem: OAResourceSwiftItem?, title: String?, desc: Any?) -> DownloadingCell? { guard let multipleItem = swiftResourceItem as? OAMultipleResourceSwiftItem else { return nil } for subitem in multipleItem.items() { subitem.refreshDownloadTask() @@ -85,7 +85,7 @@ final class DownloadingCellMultipleResourceHelper: DownloadingCellResourceHelper saveStatus(resourceId: resourceId, status: .inProgress) } } - return super.getOrCreateCell(resourceId) + return super.getOrCreateCell(resourceId, swiftResourceItem: swiftResourceItem, title: title, desc: desc) } override func onCellClicked(_ resourceId: String) { diff --git a/Sources/Helpers/DownloadingCellHelper/DownloadingCellResourceHelper.swift b/Sources/Helpers/DownloadingCellHelper/DownloadingCellResourceHelper.swift index cc5e86e3de..345055bea0 100644 --- a/Sources/Helpers/DownloadingCellHelper/DownloadingCellResourceHelper.swift +++ b/Sources/Helpers/DownloadingCellHelper/DownloadingCellResourceHelper.swift @@ -26,7 +26,6 @@ class DownloadingCellResourceHelper: DownloadingCellBaseHelper { override init() { super.init() - resourceItems = [String: OAResourceSwiftItem]() downloadTaskProgressObserver = OAAutoObserverProxy(self, withHandler: #selector(onDownloadResourceTaskProgressChanged), andObserve: OsmAndApp.swiftInstance().downloadsManager.progressCompletedObservable) downloadTaskCompletedObserver = OAAutoObserverProxy(self, withHandler: #selector(onDownloadResourceTaskFinished), andObserve: OsmAndApp.swiftInstance().downloadsManager.completedObservable) localResourcesChangedObserver = OAAutoObserverProxy(self, withHandler: #selector(onLocalResourcesChanged), andObserve: OsmAndApp.swiftInstance().localResourcesChangedObservable) @@ -139,7 +138,12 @@ class DownloadingCellResourceHelper: DownloadingCellBaseHelper { // MARK: - Cell setup methods - override func setupCell(_ resourceId: String) -> DownloadingCell? { + override func setupCell(_ resourceId: String, + title: String?, + isTitleBold: Bool, + desc: Any?, + leftIcon: Any?, + isDownloading: Bool) -> DownloadingCell? { if let resourceItem = getResource(resourceId) { resourceItem.refreshDownloadTask() var subtitle = "" @@ -157,10 +161,15 @@ class DownloadingCellResourceHelper: DownloadingCellBaseHelper { withResourceType: true) } let iconName = resourceItem.iconName() - let isDownloading = isDownloading(resourceId) + let isDownloading = self.isDownloading(resourceId) // get cell with default settings - let cell = super.setupCell(resourceId: resourceId, title: title, isTitleBold: false, desc: subtitle, leftIconName: iconName, rightIconName: getRightIconName(resourceId), isDownloading: isDownloading) + let cell = super.setupCell(resourceId, + title: title, + isTitleBold: false, + desc: subtitle, + leftIcon: iconName, + isDownloading: isDownloading) if isDisabled(resourceId) { cell?.titleLabel.textColor = .textColorSecondary @@ -186,6 +195,10 @@ class DownloadingCellResourceHelper: DownloadingCellBaseHelper { } func getOrCreateCell(_ resourceId: String, swiftResourceItem: OAResourceSwiftItem?) -> DownloadingCell? { + getOrCreateCell(resourceId, swiftResourceItem: swiftResourceItem, title: nil, desc: nil) + } + + func getOrCreateCell(_ resourceId: String, swiftResourceItem: OAResourceSwiftItem?, title: String?, desc: Any?) -> DownloadingCell? { if let swiftResourceItem, swiftResourceItem.objcResourceItem != nil { if getResource(resourceId) == nil { saveResource(resource: swiftResourceItem, resourceId: resourceId) @@ -193,7 +206,11 @@ class DownloadingCellResourceHelper: DownloadingCellBaseHelper { if swiftResourceItem.downloadTask() != nil { saveStatus(resourceId: resourceId, status: .inProgress) } - return super.getOrCreateCell(resourceId) + return super.getOrCreateCell(resourceId, + title: title, + desc: desc, + leftIcon: swiftResourceItem.icon(), + isDownloading: isDownloading(resourceId)) } return nil } @@ -228,9 +245,8 @@ class DownloadingCellResourceHelper: DownloadingCellBaseHelper { super.setCellProgress(resourceId: resourceId, progress: progress, status: status) if showDownloadingBytesInDescription { guard let resourceItem = getResource(resourceId) else { return } - guard let cell = getOrCreateCell(resourceId) else { return } guard let subtitle = OAResourcesUISwiftHelper.formatedDownloadingProgressString(resourceItem.sizePkg(), progress: progress) else { return } - cell.descriptionLabel.text = subtitle + updateDesc(resourceId, cell: nil, desc: subtitle) } } @@ -242,17 +258,14 @@ class DownloadingCellResourceHelper: DownloadingCellBaseHelper { } else if let task = key as? OADownloadTask { progress = task.progressCompleted } - - if helperHasItemFor(resourceId) { - DispatchQueue.main.async { [weak self] in - self?.setCellProgress(resourceId: resourceId, progress: progress, status: .finished) - - // Start next downloading if needed - if let tasks = OsmAndApp.swiftInstance().downloadsManager.keysOfDownloadTasks(), !tasks.isEmpty { - if let nextTask = OsmAndApp.swiftInstance().downloadsManager.firstDownloadTasks(withKey: tasks[0] as? String) { - nextTask.resume() - } + DispatchQueue.main.async { [weak self] in + self?.setCellProgress(resourceId: resourceId, progress: progress, status: .finished) + + // Start next downloading if needed + if let tasks = OsmAndApp.swiftInstance().downloadsManager.keysOfDownloadTasks(), !tasks.isEmpty { + if let nextTask = OsmAndApp.swiftInstance().downloadsManager.firstDownloadTasks(withKey: tasks[0] as? String) { + nextTask.resume() } } } diff --git a/Sources/Helpers/DownloadingListHelper/DownloadingListViewController.swift b/Sources/Helpers/DownloadingListHelper/DownloadingListViewController.swift index 08f3a5bcf9..4ab2f7046e 100644 --- a/Sources/Helpers/DownloadingListHelper/DownloadingListViewController.swift +++ b/Sources/Helpers/DownloadingListHelper/DownloadingListViewController.swift @@ -74,7 +74,7 @@ final class DownloadingListViewController: OABaseNavbarViewController, Downloadi if let downloadingCellResourceHelper { let resourceId = item.string(forKey: "resourceId") ?? "" let resource = item.obj(forKey: "item") as? OAResourceSwiftItem - let cell = downloadingCellResourceHelper.getOrCreateCell(resourceId, swiftResourceItem: resource) + let cell = downloadingCellResourceHelper.getOrCreateCell(resourceId, swiftResourceItem: resource, title: item.title, desc: "") if downloadingCellResourceHelper.isFinished(resourceId) { cell?.leftIconView.tintColor = .iconColorActive cell?.rightIconView.image = UIImage.templateImageNamed(downloadingCellResourceHelper.rightIconName)