IceCubesApp/IceCubesApp/App/Tabs/Settings/IconSelectorView.swift

122 lines
4.2 KiB
Swift
Raw Normal View History

2022-12-29 09:39:34 +00:00
import DesignSystem
2023-01-17 10:36:01 +00:00
import SwiftUI
2022-12-04 08:50:25 +00:00
2023-09-18 19:03:52 +00:00
@MainActor
2022-12-04 08:50:25 +00:00
struct IconSelectorView: View {
2022-12-31 13:01:00 +00:00
enum Icon: Int, CaseIterable, Identifiable {
2022-12-04 08:50:25 +00:00
var id: String {
2022-12-31 13:01:00 +00:00
"\(rawValue)"
2022-12-04 08:50:25 +00:00
}
2023-01-17 10:36:01 +00:00
2022-12-31 13:01:00 +00:00
init(string: String) {
if string == "AppIcon" {
2022-12-31 13:01:00 +00:00
self = .primary
} else {
self = .init(rawValue: Int(String(string.replacing("AppIconAlternate", with: "")))!)!
2022-12-31 13:01:00 +00:00
}
}
2023-01-17 10:36:01 +00:00
2022-12-31 13:01:00 +00:00
case primary = 0
2024-01-02 12:29:21 +00:00
case alt1, alt2, alt3, alt4, alt5, alt6, alt7, alt8, alt9, alt10, alt11, alt12, alt13, alt14, alt15
case alt16, alt17, alt18, alt19, alt20, alt21
case alt22, alt23, alt24, alt25, alt26
case alt27, alt28, alt29
case alt30, alt31, alt32, alt33, alt34, alt35, alt36
2023-07-18 07:45:44 +00:00
case alt37
2024-01-02 12:29:21 +00:00
case alt38
case alt39, alt40, alt41, alt42, alt43
case alt44, alt45
2024-01-21 08:31:50 +00:00
case alt46
2023-01-30 06:27:06 +00:00
2022-12-31 13:01:00 +00:00
var appIconName: String {
return "AppIconAlternate\(rawValue)"
2022-12-31 13:01:00 +00:00
}
2022-12-04 08:50:25 +00:00
}
2023-01-17 10:36:01 +00:00
struct IconSelector: Identifiable {
var id = UUID()
let title: String
let icons: [Icon]
static let items = [
2024-01-02 12:29:21 +00:00
IconSelector(title: "settings.app.icon.official".localized, icons: [
2024-01-21 08:31:50 +00:00
.primary, .alt46, .alt1, .alt2, .alt3, .alt4,
2024-01-02 12:29:21 +00:00
.alt5, .alt6, .alt7, .alt8,
.alt9, .alt10, .alt11, .alt12, .alt13, .alt14, .alt15,
.alt16, .alt17, .alt18, .alt19, .alt20, .alt21]),
2024-01-21 08:31:50 +00:00
IconSelector(title: "\("settings.app.icon.designed-by".localized) Louie Mantia, Jr.", icons: []),
2024-01-02 12:29:21 +00:00
IconSelector(title: "\("settings.app.icon.designed-by".localized) Albert Kinng", icons: [.alt22, .alt23, .alt24, .alt25, .alt26]),
IconSelector(title: "\("settings.app.icon.designed-by".localized) Dan van Moll", icons: [.alt27, .alt28, .alt29]),
IconSelector(title: "\("settings.app.icon.designed-by".localized) Chanhwi Joo (GitHub @te6-in)", icons: [.alt30, .alt31, .alt32, .alt33, .alt34, .alt35, .alt36]),
IconSelector(title: "\("settings.app.icon.designed-by".localized) W. Kovács Ágnes (@wildgica)", icons: [.alt37]),
IconSelector(title: "\("settings.app.icon.designed-by".localized) Duncan Horne", icons: [.alt38]),
IconSelector(title: "\("settings.app.icon.designed-by".localized) BeAware@social.beaware.live", icons: [.alt39, .alt40, .alt41, .alt42, .alt43]),
IconSelector(title: "\("settings.app.icon.designed-by".localized) Simone Margio", icons: [.alt44, .alt45]),
]
}
2023-09-18 19:03:52 +00:00
@Environment(Theme.self) private var theme
2022-12-31 13:01:00 +00:00
@State private var currentIcon = UIApplication.shared.alternateIconName ?? Icon.primary.appIconName
2023-01-17 10:36:01 +00:00
2022-12-04 08:50:25 +00:00
private let columns = [GridItem(.adaptive(minimum: 125, maximum: 1024))]
2023-01-17 10:36:01 +00:00
2022-12-04 08:50:25 +00:00
var body: some View {
ScrollView {
VStack(alignment: .leading) {
ForEach(IconSelector.items) { item in
Section {
makeIconGridView(icons: item.icons)
} header: {
Text(item.title)
.font(.scaledHeadline)
}
2023-02-09 11:24:24 +00:00
}
2022-12-04 08:50:25 +00:00
}
.padding(6)
.navigationTitle("settings.app.icon.navigation-title")
2022-12-04 08:50:25 +00:00
}
#if !os(visionOS)
2022-12-29 09:39:34 +00:00
.background(theme.primaryBackgroundColor)
#endif
2022-12-04 08:50:25 +00:00
}
2023-01-30 06:27:06 +00:00
2023-01-28 07:58:36 +00:00
private func makeIconGridView(icons: [Icon]) -> some View {
LazyVGrid(columns: columns, spacing: 6) {
ForEach(icons) { icon in
Button {
currentIcon = icon.appIconName
if icon.rawValue == Icon.primary.rawValue {
UIApplication.shared.setAlternateIconName(nil)
} else {
UIApplication.shared.setAlternateIconName(icon.appIconName) { err in
guard let err else { return }
assertionFailure("\(err.localizedDescription) - Icon name: \(icon.appIconName)")
}
2023-01-28 07:58:36 +00:00
}
} label: {
ZStack(alignment: .bottomTrailing) {
Image(uiImage: .init(named: icon.appIconName) ?? .init())
2023-01-28 07:58:36 +00:00
.resizable()
.aspectRatio(contentMode: .fit)
.frame(minHeight: 125, maxHeight: 1024)
.cornerRadius(6)
.shadow(radius: 3)
if icon.appIconName == currentIcon {
Image(systemName: "checkmark.seal.fill")
.padding(4)
.tint(.green)
}
}
}
.buttonStyle(.plain)
2023-01-28 07:58:36 +00:00
}
}
}
2022-12-04 08:50:25 +00:00
}
extension String {
2023-02-18 06:26:48 +00:00
var localized: String {
2023-09-16 12:15:03 +00:00
NSLocalizedString(self, comment: "")
2023-02-18 06:26:48 +00:00
}
}