From 76efa4e250cdef4e72a078d34bbcaa368b3fd2ed Mon Sep 17 00:00:00 2001 From: ra1028 Date: Thu, 16 May 2024 17:25:50 +0900 Subject: [PATCH] Refactoring --- .../Internal/Utilities/ImageCache.swift | 4 + .../Internal/Utilities/UIColor.swift | 3 +- .../Internal/Views/CatalogSearchPane.swift | 106 ++++++++++-------- .../Internal/Views/CatalogSplit.swift | 15 +-- .../Internal/Views/CatalogTop.swift | 2 +- .../Internal/Views/GalleryDetail.swift | 2 +- .../Internal/Views/GalleryThumbnail.swift | 2 +- Sources/PlaybookUI/PlaybookGallery.swift | 27 ++--- 8 files changed, 85 insertions(+), 76 deletions(-) diff --git a/Sources/PlaybookUI/Internal/Utilities/ImageCache.swift b/Sources/PlaybookUI/Internal/Utilities/ImageCache.swift index f2943af..3cc2b90 100644 --- a/Sources/PlaybookUI/Internal/Utilities/ImageCache.swift +++ b/Sources/PlaybookUI/Internal/Utilities/ImageCache.swift @@ -69,6 +69,10 @@ private extension ImageCache { } func remove(at url: URL) { + guard fileManager.fileExists(atPath: url.path) else { + return + } + do { try fileManager.removeItem(at: url) } diff --git a/Sources/PlaybookUI/Internal/Utilities/UIColor.swift b/Sources/PlaybookUI/Internal/Utilities/UIColor.swift index 3310ad8..3f2fce1 100644 --- a/Sources/PlaybookUI/Internal/Utilities/UIColor.swift +++ b/Sources/PlaybookUI/Internal/Utilities/UIColor.swift @@ -4,10 +4,9 @@ internal extension UIColor { static let primaryBlue = UIColor(hex: 0x048DFF) static let highlight = UIColor.systemYellow static let translucentFill = UIColor.secondarySystemFill - static let primaryBackground = UIColor { traitCollection in + static let background = UIColor { traitCollection in traitCollection.userInterfaceStyle == .light ? .white : .black } - static let secondaryBackground = UIColor.secondarySystemBackground } private extension UIColor { diff --git a/Sources/PlaybookUI/Internal/Views/CatalogSearchPane.swift b/Sources/PlaybookUI/Internal/Views/CatalogSearchPane.swift index 7a1202c..8291b90 100644 --- a/Sources/PlaybookUI/Internal/Views/CatalogSearchPane.swift +++ b/Sources/PlaybookUI/Internal/Views/CatalogSearchPane.swift @@ -10,73 +10,81 @@ internal struct CatalogSearchPane: View { private var isFocused var body: some View { - VStack(spacing: 0) { - Spacer.fixed(length: 16) + HStack(spacing: 0) { + VStack(spacing: 0) { + Spacer.fixed(length: 16) - SearchBar(text: $searchState.query) - .focused($isFocused) - Counter( - count: searchState.result.count, - total: searchState.result.total - ) + SearchBar(text: $searchState.query) + .focused($isFocused) + Counter( + count: searchState.result.count, + total: searchState.result.total + ) - Spacer.fixed(length: 16) + Spacer.fixed(length: 16) - Divider() + Divider() - List { - Group { - if searchState.result.kinds.isEmpty { - UnavailableView( - symbol: .magnifyingglass, - description: "No Result for \"\(searchState.query)\"" - ) - } - else { - ForEach(searchState.result.kinds, id: \.kind) { data in - let isExpanded = catalogState.currentExpandedKinds.contains(data.kind) + List { + Group { + if searchState.result.kinds.isEmpty { + UnavailableView( + symbol: .magnifyingglass, + description: "No Result for \"\(searchState.query)\"" + ) + } + else { + ForEach(searchState.result.kinds, id: \.kind) { data in + let isExpanded = catalogState.currentExpandedKinds.contains(data.kind) - CatalogKindRow(data: data, isExpanded: isExpanded) { - withAnimation(.smooth(duration: 0.1)) { - if isExpanded { - catalogState.currentExpandedKinds.remove(data.kind) - } - else { - catalogState.currentExpandedKinds.insert(data.kind) + CatalogKindRow(data: data, isExpanded: isExpanded) { + withAnimation(.smooth(duration: 0.1)) { + if isExpanded { + catalogState.currentExpandedKinds.remove(data.kind) + } + else { + catalogState.currentExpandedKinds.insert(data.kind) + } } } - } - if isExpanded { - ForEach(data.scenarios, id: \.scenario.name) { data in - let select = SelectData( - kind: data.kind, - scenario: data.scenario - ) + if isExpanded { + ForEach(data.scenarios, id: \.scenario.name) { data in + let select = SelectData( + kind: data.kind, + scenario: data.scenario + ) - CatalogScenarioRow( - data: data, - isSelected: catalogState.selected?.id == select.id - ) { - catalogState.selected = select + CatalogScenarioRow( + data: data, + isSelected: catalogState.selected?.id == select.id + ) { + catalogState.selected = select + } } } } } - } - Spacer.fixed(length: 16) + Spacer.fixed(length: 16) + } + .listRowSpacing(.zero) + .listRowInsets(EdgeInsets()) + .listRowSeparator(.hidden) + .listRowBackground(Color.clear) } - .listRowSpacing(.zero) - .listRowInsets(EdgeInsets()) - .listRowSeparator(.hidden) - .listRowBackground(Color.clear) + .listStyle(.plain) + .environment(\.defaultMinListRowHeight, 0) } - .listStyle(.plain) - .environment(\.defaultMinListRowHeight, 0) + + Rectangle() + .fill(Color(.translucentFill)) + .frame(width: 2) + .fixedSize(horizontal: true, vertical: false) + .ignoresSafeArea() } .background { - Color(.secondaryBackground) + Color(.background) .ignoresSafeArea() } .frame(maxWidth: .infinity, maxHeight: .infinity) diff --git a/Sources/PlaybookUI/Internal/Views/CatalogSplit.swift b/Sources/PlaybookUI/Internal/Views/CatalogSplit.swift index 523ccac..b4d6878 100644 --- a/Sources/PlaybookUI/Internal/Views/CatalogSplit.swift +++ b/Sources/PlaybookUI/Internal/Views/CatalogSplit.swift @@ -11,16 +11,9 @@ internal struct CatalogSplit: View { let sidebarWidth = geometry.size.width * 0.4 ZStack(alignment: .leading) { - HStack(spacing: 0) { - CatalogSearchPane() - Divider() - .ignoresSafeArea() - } - .frame(width: sidebarWidth) - .offset(x: catalogState.isSearchPainCollapsed ? -sidebarWidth / 2 : 0) - HStack(spacing: 0) { Spacer.fixed(length: catalogState.isSearchPainCollapsed ? 0 : sidebarWidth) + CatalogTop() .transformEnvironment(\.horizontalSizeClass) { sizeClass in if !catalogState.isSearchPainCollapsed { @@ -28,8 +21,12 @@ internal struct CatalogSplit: View { } } } + + CatalogSearchPane() + .frame(width: sidebarWidth) + .offset(x: catalogState.isSearchPainCollapsed ? -sidebarWidth : 0) } } - .animation(.smooth(duration: 0.3), value: catalogState.isSearchPainCollapsed) + .animation(.snappy(duration: 0.3), value: catalogState.isSearchPainCollapsed) } } diff --git a/Sources/PlaybookUI/Internal/Views/CatalogTop.swift b/Sources/PlaybookUI/Internal/Views/CatalogTop.swift index d32df1a..539b87e 100644 --- a/Sources/PlaybookUI/Internal/Views/CatalogTop.swift +++ b/Sources/PlaybookUI/Internal/Views/CatalogTop.swift @@ -30,7 +30,7 @@ internal struct CatalogTop: View { } .frame(maxHeight: .infinity) .background { - Color(.primaryBackground) + Color(.background) .ignoresSafeArea() } .ignoresSafeArea() diff --git a/Sources/PlaybookUI/Internal/Views/GalleryDetail.swift b/Sources/PlaybookUI/Internal/Views/GalleryDetail.swift index 7cb149b..7233aa2 100644 --- a/Sources/PlaybookUI/Internal/Views/GalleryDetail.swift +++ b/Sources/PlaybookUI/Internal/Views/GalleryDetail.swift @@ -27,7 +27,7 @@ internal struct GalleryDetail: View { } } .background { - Color(.primaryBackground) + Color(.background) .ignoresSafeArea() } } diff --git a/Sources/PlaybookUI/Internal/Views/GalleryThumbnail.swift b/Sources/PlaybookUI/Internal/Views/GalleryThumbnail.swift index 85f6a90..1b45e23 100644 --- a/Sources/PlaybookUI/Internal/Views/GalleryThumbnail.swift +++ b/Sources/PlaybookUI/Internal/Views/GalleryThumbnail.swift @@ -18,7 +18,7 @@ internal struct GalleryThumbnail: View { var body: some View { ZStack { - Color(.primaryBackground) + Color(.background) if let image { Image(uiImage: image) diff --git a/Sources/PlaybookUI/PlaybookGallery.swift b/Sources/PlaybookUI/PlaybookGallery.swift index c693f9e..898bf07 100644 --- a/Sources/PlaybookUI/PlaybookGallery.swift +++ b/Sources/PlaybookUI/PlaybookGallery.swift @@ -55,17 +55,7 @@ public struct PlaybookGallery: View { } } - Rectangle() - .fill(.clear) - .frame(height: 24) - .frame(maxWidth: .infinity) - .contextMenu { - Button { - galleryState.clearImageCache() - } label: { - Text("Clear image cache") - } - } + Spacer.fixed(length: 24) } .listRowSpacing(.zero) .listRowInsets(EdgeInsets()) @@ -77,7 +67,7 @@ public struct PlaybookGallery: View { .environment(\.defaultMinListRowHeight, 0) .navigationTitleIfPresent(title) .background { - Color(.primaryBackground) + Color(.background) .ignoresSafeArea() } .ignoresSafeArea(.keyboard) @@ -86,7 +76,18 @@ public struct PlaybookGallery: View { } .toolbar { ToolbarItem(placement: .topBarTrailing) { - ColorSchemePicker(colorScheme: $galleryState.colorScheme) + HStack { + Menu { + Button("Clear Thumbnail Cache") { + galleryState.clearImageCache() + } + } label: { + Image(symbol: .ellipsisCircle) + .imageStyle(font: .subheadline) + } + + ColorSchemePicker(colorScheme: $galleryState.colorScheme) + } } } }