Sources/OrgstarMobile/AppIconScreen.swift
49 lines · 1972 bytes
1#if os(iOS)
2import OrgPresentation
3import SwiftUI
4import UIKit
5
6/// The app icons, as previews; tapping one makes it the home screen's icon.
7struct AppIconScreen: View {
8 @State private var current = AppIcons.id(alternateName: UIApplication.shared.alternateIconName)
9 @State private var failure: String?
10
11 var body: some View {
12 ScrollView {
13 LazyVGrid(columns: [GridItem(.adaptive(minimum: 88), spacing: 16)], spacing: 20) {
14 ForEach(AppIcons.ids, id: \.self) { id in
15 Button { choose(id) } label: {
16 VStack(spacing: 6) {
17 Image("IconPreview-\(id)")
18 .resizable()
19 .frame(width: 72, height: 72)
20 .clipShape(RoundedRectangle(cornerRadius: 16, style: .continuous))
21 .overlay(RoundedRectangle(cornerRadius: 16, style: .continuous)
22 .strokeBorder(id == current ? Color.accentColor : .clear, lineWidth: 3).padding(-4))
23 Text(AppIcons.name(id)).font(.caption).foregroundStyle(.primary)
24 }
25 }
26 .buttonStyle(.plain)
27 .accessibilityAddTraits(id == current ? .isSelected : [])
28 }
29 }
30 .padding()
31 if let failure { Text(failure).foregroundStyle(.red).padding(.horizontal) }
32 }
33 .navigationTitle("App Icon")
34 }
35
36 private func choose(_ id: String) {
37 guard id != current, UIApplication.shared.supportsAlternateIcons else { return }
38 Task {
39 do {
40 try await UIApplication.shared.setAlternateIconName(AppIcons.alternateName(id))
41 current = id
42 failure = nil
43 } catch {
44 failure = error.localizedDescription
45 }
46 }
47 }
48}
49#endif