Icons.swift 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import Foundation
  2. import UIKit
  3. enum Icon_: String, CaseIterable, Identifiable {
  4. case primary = "trioBlack"
  5. case trioWhiteShadow
  6. case trioColorBG
  7. case trioWhite
  8. case trio3D
  9. case wilford = "diabeetus"
  10. case catWithPod
  11. case catWithPodWhite = "catWithPodWhiteBG"
  12. var id: String { rawValue }
  13. }
  14. class Icons: ObservableObject, Equatable {
  15. @Published var appIcon: Icon_ = .primary
  16. static func == (lhs: Icons, rhs: Icons) -> Bool {
  17. lhs.appIcon == rhs.appIcon
  18. }
  19. func setAlternateAppIcon(icon: Icon_) {
  20. let iconName: String? = (icon != .primary) ? icon.rawValue : nil
  21. guard UIApplication.shared.alternateIconName != iconName else { return }
  22. UIApplication.shared.setAlternateIconName(iconName) { error in
  23. if let error = error {
  24. print("Failed request to update the app’s icon: \(error)")
  25. }
  26. }
  27. appIcon = icon
  28. }
  29. init() {
  30. let iconName = UIApplication.shared.alternateIconName
  31. if iconName == nil {
  32. appIcon = .primary
  33. } else {
  34. appIcon = Icon_(rawValue: iconName!)!
  35. }
  36. }
  37. }