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

78 lines
2.1 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
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 == Icon.primary.appIconName {
self = .primary
} else {
self = .init(rawValue: Int(String(string.last!))!)!
}
}
2023-01-17 10:36:01 +00:00
2022-12-31 13:01:00 +00:00
case primary = 0
case alt1, alt2, alt3, alt4, alt5, alt6, alt7, alt8
case alt9, alt10, alt11, alt12, alt13, alt14
2023-01-07 15:53:34 +00:00
case alt15, alt16, alt17, alt18, alt19
2023-01-17 10:36:01 +00:00
2022-12-31 13:01:00 +00:00
var appIconName: String {
2022-12-27 20:35:41 +00:00
switch self {
2022-12-31 13:01:00 +00:00
case .primary:
return "AppIcon"
default:
return "AppIconAlternate\(rawValue)"
2022-12-27 20:35:41 +00:00
}
}
2023-01-17 10:36:01 +00:00
2022-12-31 13:01:00 +00:00
var iconName: String {
"icon\(rawValue)"
}
2022-12-04 08:50:25 +00:00
}
2023-01-17 10:36:01 +00:00
2022-12-29 09:39:34 +00:00
@EnvironmentObject private var theme: 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) {
LazyVGrid(columns: columns, spacing: 6) {
ForEach(Icon.allCases) { icon in
Button {
2022-12-31 13:01:00 +00:00
currentIcon = icon.appIconName
2022-12-04 08:50:25 +00:00
if icon.rawValue == Icon.primary.rawValue {
UIApplication.shared.setAlternateIconName(nil)
} else {
2022-12-31 13:01:00 +00:00
UIApplication.shared.setAlternateIconName(icon.appIconName)
2022-12-04 08:50:25 +00:00
}
} label: {
ZStack(alignment: .bottomTrailing) {
2022-12-27 20:35:41 +00:00
Image(uiImage: .init(named: icon.iconName) ?? .init())
2022-12-04 08:50:25 +00:00
.resizable()
.aspectRatio(contentMode: .fit)
.frame(minHeight: 125, maxHeight: 1024)
.cornerRadius(6)
.shadow(radius: 3)
2022-12-31 13:01:00 +00:00
if icon.appIconName == currentIcon {
2022-12-04 08:50:25 +00:00
Image(systemName: "checkmark.seal.fill")
.padding(4)
.tint(.green)
}
}
}
}
}
}
.padding(6)
.navigationTitle("Icons")
}
2022-12-29 09:39:34 +00:00
.background(theme.primaryBackgroundColor)
2022-12-04 08:50:25 +00:00
}
}