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

117 lines
3.1 KiB
Swift
Raw Normal View History

2022-12-29 10:39:34 +01:00
import DesignSystem
2023-01-17 11:36:01 +01:00
import SwiftUI
2022-12-04 09:50:25 +01:00
struct IconSelectorView: View {
2022-12-31 14:01:00 +01:00
enum Icon: Int, CaseIterable, Identifiable {
2022-12-04 09:50:25 +01:00
var id: String {
2022-12-31 14:01:00 +01:00
"\(rawValue)"
2022-12-04 09:50:25 +01:00
}
2023-01-17 11:36:01 +01:00
2022-12-31 14:01:00 +01:00
init(string: String) {
if string == Icon.primary.appIconName {
self = .primary
} else {
self = .init(rawValue: Int(String(string.replacing("AppIconAlternate", with: "")))!)!
2022-12-31 14:01:00 +01:00
}
}
2023-01-17 11:36:01 +01:00
2022-12-31 14:01:00 +01:00
case primary = 0
case alt1, alt2, alt3, alt4, alt5, alt6, alt7, alt8
case alt9, alt10, alt11, alt12, alt13, alt14
2023-01-26 18:27:27 +01:00
case alt15, alt16, alt17, alt18, alt19, alt20, alt21
2023-01-30 19:48:57 +01:00
case alt22, alt23, alt24, alt25
2023-02-01 18:53:25 +01:00
case alt26, alt27, alt28
2023-01-30 07:27:06 +01:00
2023-01-28 08:58:36 +01:00
static var officialIcons: [Icon] {
2023-01-29 16:57:04 +01:00
[.primary, .alt1, .alt2, .alt3, .alt4, .alt5, .alt6, .alt7, .alt8,
2023-01-30 07:27:06 +01:00
.alt9, .alt10, .alt11, .alt12, .alt13, .alt14,
2023-01-30 19:48:57 +01:00
.alt15, .alt16, .alt17, .alt18, .alt19, .alt25]
2023-01-28 08:58:36 +01:00
}
2023-01-30 07:27:06 +01:00
2023-01-28 08:58:36 +01:00
static var albertKinngIcons: [Icon] {
[.alt20, .alt21, .alt22, .alt23, .alt24]
}
2023-02-01 18:53:25 +01:00
static var danIcons: [Icon] {
[.alt26, .alt27, .alt28]
}
2023-01-17 11:36:01 +01:00
2022-12-31 14:01:00 +01:00
var appIconName: String {
2022-12-27 21:35:41 +01:00
switch self {
2022-12-31 14:01:00 +01:00
case .primary:
return "AppIcon"
default:
return "AppIconAlternate\(rawValue)"
2022-12-27 21:35:41 +01:00
}
}
2023-01-17 11:36:01 +01:00
2022-12-31 14:01:00 +01:00
var iconName: String {
"icon\(rawValue)"
}
2022-12-04 09:50:25 +01:00
}
2023-01-17 11:36:01 +01:00
2022-12-29 10:39:34 +01:00
@EnvironmentObject private var theme: Theme
2022-12-31 14:01:00 +01:00
@State private var currentIcon = UIApplication.shared.alternateIconName ?? Icon.primary.appIconName
2023-01-17 11:36:01 +01:00
2022-12-04 09:50:25 +01:00
private let columns = [GridItem(.adaptive(minimum: 125, maximum: 1024))]
2023-01-17 11:36:01 +01:00
2022-12-04 09:50:25 +01:00
var body: some View {
ScrollView {
VStack(alignment: .leading) {
2023-01-28 08:58:36 +01:00
Section {
makeIconGridView(icons: Icon.officialIcons)
} header: {
Text("Official icons")
.font(.scaledHeadline)
}
2023-01-30 07:27:06 +01:00
2023-01-28 08:58:36 +01:00
Section {
makeIconGridView(icons: Icon.albertKinngIcons)
} header: {
2023-01-28 11:00:20 +01:00
Text("Icons by Albert Kinng")
2023-01-28 08:58:36 +01:00
.font(.scaledHeadline)
2022-12-04 09:50:25 +01:00
}
2023-02-01 18:53:25 +01:00
Section {
makeIconGridView(icons: Icon.danIcons)
} header: {
Text("Icons by Dan van Moll")
.font(.scaledHeadline)
}
2022-12-04 09:50:25 +01:00
}
.padding(6)
.navigationTitle("settings.app.icon.navigation-title")
2022-12-04 09:50:25 +01:00
}
2022-12-29 10:39:34 +01:00
.background(theme.primaryBackgroundColor)
2022-12-04 09:50:25 +01:00
}
2023-01-30 07:27:06 +01:00
2023-01-28 08:58:36 +01: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)
}
} label: {
ZStack(alignment: .bottomTrailing) {
Image(uiImage: .init(named: icon.iconName) ?? .init())
.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)
}
}
}
}
}
}
2022-12-04 09:50:25 +01:00
}