-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChangeAppIconViewModel.swift
81 lines (70 loc) · 2.22 KB
/
ChangeAppIconViewModel.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// SwiftUIView.swift
// Bubbles
//
// Created by Berk Dogan on 28/5/2023.
//
//
// ChangeAppIconViewModel.swift
// Infinity
//
// Created by Berk Dogan on 16/4/2023.
//
// All sorted, thanks to --> https://www.avanderlee.com/swift/alternate-app-icon-configuration-in-xcode/
import Foundation
import SwiftUI
final class ChangeAppIconViewModel: ObservableObject {
enum AppIcon: String, CaseIterable, Identifiable {
case primary = "AppIcon"
case appIcon2 = "AppIcon-2"
var id: String { rawValue }
var iconName: String? {
switch self {
case .primary:
/// `nil` is used to reset the app icon back to its primary icon.
return nil
default:
return rawValue
}
}
// Description of the icons
var description: String {
switch self {
case .primary:
return "Light"
case .appIcon2:
return "Dark"
}
}
var preview: UIImage {
UIImage(named: rawValue) ?? UIImage()
}
}
@Published private(set) var selectedAppIcon: AppIcon
init() {
if let iconName = UIApplication.shared.alternateIconName, let appIcon = AppIcon(rawValue: iconName) {
selectedAppIcon = appIcon
} else {
selectedAppIcon = .primary
}
}
func updateAppIcon(to icon: AppIcon) {
let previousAppIcon = selectedAppIcon
selectedAppIcon = icon
Task { @MainActor in
guard UIApplication.shared.alternateIconName != icon.iconName else {
/// No need to update since we're already using this icon.
return
}
do {
try await UIApplication.shared.setAlternateIconName(icon.iconName)
} catch {
/// We're only logging the error here and not actively handling the app icon failure
/// since it's very unlikely to fail.
print("Updating icon to \(String(describing: icon.iconName)) failed.")
/// Restore previous app icon
selectedAppIcon = previousAppIcon
}
}
}
}