Skip to content

Commit

Permalink
Add Searchbar for snapshots (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
Itaybre authored Jan 29, 2025
1 parent 55198bb commit 694bec0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
14 changes: 14 additions & 0 deletions Sources/PreviewGallery/Array+Filter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Array+Filter.swift
// SnapshotPreviews
//
// Created by Itay Brenner on 23/1/25.
//

extension Array {
func filterWithText(_ text: String, _ nameForElement: @escaping ((Element) -> String)) -> [Element] {
return self.filter { element in
text.isEmpty ? true : nameForElement(element).lowercased().contains(text.lowercased())
}
}
}
7 changes: 5 additions & 2 deletions Sources/PreviewGallery/ModulePreviews.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ import SwiftUI
struct ModulePreviews: View {
let module: String
let data: PreviewData

@State private var searchText = ""

var body: some View {
let allPreviewGroups = data.previews(in: module)
let componentProviders = allPreviewGroups.filter { provider in
provider.previewTypes(requiringFullscreen: false).count > 0
}
}.filterWithText(searchText, { $0.displayName })
let fullScreenCount = allPreviewGroups.flatMap { $0.previews.flatMap { $0.previews(requiringFullscreen: true)} }.count
return NavigationLink(module) {
ScrollView {
LazyVStack(alignment: .leading, spacing: 12) {
if fullScreenCount > 0 {
if fullScreenCount > 0 && (searchText.isEmpty || "screens".contains(searchText.lowercased())) {
NavigationLink(destination: ModuleScreens(module: module, data: data)) {
TitleSubtitleRow(
title: "Screens",
Expand Down Expand Up @@ -50,6 +52,7 @@ struct ModulePreviews: View {
.background(Color(PlatformColor.galleryBackground))
#endif
.navigationTitle(module)
.searchable(text: $searchText)
}
}
}
4 changes: 3 additions & 1 deletion Sources/PreviewGallery/ModuleScreens.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,18 @@ struct ModuleScreens: View {

let module: String
let data: PreviewData
@State private var searchText = ""

var body: some View {
let featureProviders = data.previews(in: module).filter { provider in
!provider.previewTypes(requiringFullscreen: true).isEmpty
}
}.filterWithText(searchText, { $0.displayName })
return List {
ForEach(featureProviders) { provider in
ModuleSelectionView(provider: provider)
}
}.navigationTitle("Screens")
.searchable(text: $searchText)
}

}
5 changes: 4 additions & 1 deletion Sources/PreviewGallery/PreviewGallery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ struct PreviewCellView: View {
public struct PreviewGallery: View {
/// The data source containing preview information.
let data: PreviewData

@State private var searchText = ""

/// Initializes a new `PreviewGallery` with the given preview data.
///
Expand All @@ -65,11 +67,12 @@ public struct PreviewGallery: View {
public var body: some View {
if data.modules.count > 0 {
List {
ForEach(Array(data.modules).sorted(), id: \.self) { module in
ForEach(Array(data.modules).sorted().filterWithText(searchText, { $0 }), id: \.self) { module in
ModulePreviews(module: module, data: data)
}
}
.navigationTitle("Modules")
.searchable(text: $searchText)
} else {
Text("No previews found")
}
Expand Down
4 changes: 3 additions & 1 deletion Sources/PreviewGallery/PreviewsDetail.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import SnapshotPreviewsCore
struct PreviewsDetail: View {

let previewGrouping: PreviewGrouping
@State private var searchText = ""

var previews: [SnapshotPreviewsCore.Preview] {
previewGrouping.previews.flatMap { $0.previews(requiringFullscreen: false) }
previewGrouping.previews.flatMap { $0.previews(requiringFullscreen: false) }.filterWithText(searchText, { $0.displayName ?? "" })
}

var body: some View {
Expand All @@ -41,6 +42,7 @@ struct PreviewsDetail: View {
.background(Color(PlatformColor.galleryBackground))
#endif
.navigationTitle(previewGrouping.displayName)
.searchable(text: $searchText)
}

}

0 comments on commit 694bec0

Please sign in to comment.