Sources/OrgstarMobile/AppIconScreen.swift
50 lines · 1960 bytes
2 symbols in this file
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 /// The icon in use, shared with the Settings row.
9 @Binding var current: String
10 @State private var failure: String?
11
12 var body: some View {
13 ScrollView {
14 LazyVGrid(columns: [GridItem(.adaptive(minimum: 88), spacing: 16)], spacing: 20) {
15 ForEach(AppIcons.ids, id: \.self) { id in
16 Button { choose(id) } label: {
17 VStack(spacing: 6) {
18 Image("IconPreview-\(id)")
19 .resizable()
20 .frame(width: 72, height: 72)
21 .clipShape(RoundedRectangle(cornerRadius: 16, style: .continuous))
22 .overlay(RoundedRectangle(cornerRadius: 16, style: .continuous)
23 .strokeBorder(id == current ? Color.accentColor : .clear, lineWidth: 3).padding(-4))
24 Text(AppIcons.name(id)).font(.caption).foregroundStyle(.primary)
25 }
26 }
27 .buttonStyle(.plain)
28 .accessibilityAddTraits(id == current ? .isSelected : [])
29 }
30 }
31 .padding()
32 if let failure { Text(failure).foregroundStyle(.red).padding(.horizontal) }
33 }
34 .navigationTitle("App Icon")
35 }
36
37 private func choose(_ id: String) {
38 guard id != current, UIApplication.shared.supportsAlternateIcons else { return }
39 Task {
40 do {
41 try await UIApplication.shared.setAlternateIconName(AppIcons.alternateName(id))
42 current = id
43 failure = nil
44 } catch {
45 failure = error.localizedDescription
46 }
47 }
48 }
49}
50#endif