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
{
static let defaultIconName: String = "AppIcon"
var name: String
var imageName: String
@@ -151,6 +153,15 @@ private extension AltAppIconsViewController
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()
backgroundConfiguration.backgroundColorTransformer = UIConfigurationColorTransformer { [weak cell] c in
if let state = cell?.configurationState, state.isHighlighted
@@ -181,11 +192,12 @@ extension AltAppIconsViewController
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
self.collectionView.selectItem(at: indexPath, animated: true, scrollPosition: .centeredVertically)
let icon = self.dataSource.item(at: indexPath)
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.
let imageName = (icon.imageName == "AppIcon") ? nil : icon.imageName
UIApplication.shared.setAlternateIconName(imageName) { error in
@@ -196,16 +208,13 @@ extension AltAppIconsViewController
preferredStyle: .alert)
alertController.addAction(.ok)
self.present(alertController, animated: true)
collectionView.reloadData()
}
else
{
NotificationCenter.default.post(name: UIApplication.didChangeAppIconNotification, object: icon)
}
if let selectedIndexPath = self.collectionView.indexPathsForSelectedItems?.first
{
self.collectionView.deselectItem(at: selectedIndexPath, animated: true)
}
}
}
}