Displays checkmark next to current alternate app icon

This commit is contained in:
Riley Testut
2024-02-21 14:06:56 -06:00
committed by Magesh K
parent fd402f924f
commit 44ade05e53

View File

@@ -20,6 +20,8 @@ extension UIApplication
private final class AltIcon: Decodable private final class AltIcon: Decodable
{ {
static let defaultIconName: String = "AppIcon"
var name: String var name: String
var imageName: String var imageName: String
@@ -150,6 +152,15 @@ private extension AltAppIconsViewController
config.imageProperties.cornerRadius = imageWidth / 5.0 // Copied from AppIconImageView config.imageProperties.cornerRadius = imageWidth / 5.0 // Copied from AppIconImageView
cell.contentConfiguration = config cell.contentConfiguration = config
if UIApplication.shared.alternateIconName == icon.imageName || (UIApplication.shared.alternateIconName == nil && icon.imageName == AltIcon.defaultIconName)
{
cell.accessories = [.checkmark(options: .init(tintColor: .white))]
}
else
{
cell.accessories = []
}
var backgroundConfiguration = UIBackgroundConfiguration.listPlainCell() var backgroundConfiguration = UIBackgroundConfiguration.listPlainCell()
backgroundConfiguration.backgroundColorTransformer = UIConfigurationColorTransformer { [weak cell] c in backgroundConfiguration.backgroundColorTransformer = UIConfigurationColorTransformer { [weak cell] c in
@@ -181,11 +192,12 @@ extension AltAppIconsViewController
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{ {
self.collectionView.selectItem(at: indexPath, animated: true, scrollPosition: .centeredVertically)
let icon = self.dataSource.item(at: indexPath) let icon = self.dataSource.item(at: indexPath)
guard UIApplication.shared.alternateIconName != icon.imageName else { return } guard UIApplication.shared.alternateIconName != icon.imageName else { return }
// Deselect previous icon + select new icon
collectionView.reloadData()
// If assigning primary icon, pass "nil" as alternate icon name. // If assigning primary icon, pass "nil" as alternate icon name.
let imageName = (icon.imageName == "AppIcon") ? nil : icon.imageName let imageName = (icon.imageName == "AppIcon") ? nil : icon.imageName
UIApplication.shared.setAlternateIconName(imageName) { error in UIApplication.shared.setAlternateIconName(imageName) { error in
@@ -196,16 +208,13 @@ extension AltAppIconsViewController
preferredStyle: .alert) preferredStyle: .alert)
alertController.addAction(.ok) alertController.addAction(.ok)
self.present(alertController, animated: true) self.present(alertController, animated: true)
collectionView.reloadData()
} }
else else
{ {
NotificationCenter.default.post(name: UIApplication.didChangeAppIconNotification, object: icon) NotificationCenter.default.post(name: UIApplication.didChangeAppIconNotification, object: icon)
} }
if let selectedIndexPath = self.collectionView.indexPathsForSelectedItems?.first
{
self.collectionView.deselectItem(at: selectedIndexPath, animated: true)
}
} }
} }
} }