Version one on the App Store
This commit is contained in:
@@ -21,40 +21,44 @@ struct SearchView: View {
|
||||
@State private var searchTask: Task<Void, Never>?
|
||||
|
||||
var body: some View {
|
||||
NavigationStack {
|
||||
Group {
|
||||
if searchResults.isEmpty && !isSearching {
|
||||
if searchText.isEmpty {
|
||||
ContentUnavailableView(
|
||||
"Search Library",
|
||||
systemImage: "magnifyingglass",
|
||||
description: Text("Find artists, albums, tracks, and playlists")
|
||||
)
|
||||
} else {
|
||||
ContentUnavailableView(
|
||||
"No Results",
|
||||
systemImage: "magnifyingglass",
|
||||
description: Text("No results found for '\(searchText)'")
|
||||
)
|
||||
}
|
||||
} else if isSearching {
|
||||
ProgressView()
|
||||
Group {
|
||||
if searchResults.isEmpty && !isSearching {
|
||||
if searchText.isEmpty {
|
||||
ContentUnavailableView(
|
||||
"Search Library",
|
||||
systemImage: "magnifyingglass",
|
||||
description: Text("Find artists, albums, tracks, and playlists")
|
||||
)
|
||||
} else {
|
||||
searchResultsList
|
||||
ContentUnavailableView(
|
||||
"No Results",
|
||||
systemImage: "magnifyingglass",
|
||||
description: Text("No results found for '\(searchText)'")
|
||||
)
|
||||
}
|
||||
} else if isSearching {
|
||||
ProgressView()
|
||||
} else {
|
||||
searchResultsList
|
||||
}
|
||||
.navigationTitle("Search")
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.searchable(text: $searchText, prompt: "Artists, albums, tracks...")
|
||||
.onChange(of: searchText) { _, newValue in
|
||||
performSearch(query: newValue)
|
||||
}
|
||||
.navigationTitle("Search")
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .cancellationAction) {
|
||||
Button("Cancel") { dismiss() }
|
||||
}
|
||||
.alert("Error", isPresented: $showError) {
|
||||
Button("OK", role: .cancel) { }
|
||||
} message: {
|
||||
if let errorMessage {
|
||||
Text(errorMessage)
|
||||
}
|
||||
}
|
||||
.withMANavigation()
|
||||
.searchable(text: $searchText, prompt: "Artists, albums, tracks...")
|
||||
.onChange(of: searchText) { _, newValue in
|
||||
performSearch(query: newValue)
|
||||
}
|
||||
.alert("Error", isPresented: $showError) {
|
||||
Button("OK", role: .cancel) { }
|
||||
} message: {
|
||||
if let errorMessage {
|
||||
Text(errorMessage)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -64,17 +68,146 @@ struct SearchView: View {
|
||||
@ViewBuilder
|
||||
private var searchResultsList: some View {
|
||||
List {
|
||||
ForEach(searchResults) { item in
|
||||
SearchResultRow(item: item)
|
||||
.contentShape(Rectangle())
|
||||
.onTapGesture {
|
||||
// TODO: Navigate to detail view based on media type
|
||||
// Group results by media type
|
||||
let groupedResults = Dictionary(grouping: searchResults) { $0.mediaType ?? .unknown }
|
||||
|
||||
// Artists
|
||||
if let artists = groupedResults[.artist], !artists.isEmpty {
|
||||
Section {
|
||||
ForEach(artists) { item in
|
||||
NavigationLink(value: convertToArtist(item)) {
|
||||
SearchResultRow(item: item)
|
||||
}
|
||||
}
|
||||
} header: {
|
||||
Label("Artists", systemImage: "music.mic")
|
||||
.font(.headline)
|
||||
.foregroundStyle(.primary)
|
||||
}
|
||||
}
|
||||
|
||||
// Albums
|
||||
if let albums = groupedResults[.album], !albums.isEmpty {
|
||||
Section {
|
||||
ForEach(albums) { item in
|
||||
NavigationLink(value: convertToAlbum(item)) {
|
||||
SearchResultRow(item: item)
|
||||
}
|
||||
}
|
||||
} header: {
|
||||
Label("Albums", systemImage: "opticaldisc")
|
||||
.font(.headline)
|
||||
.foregroundStyle(.primary)
|
||||
}
|
||||
}
|
||||
|
||||
// Tracks
|
||||
if let tracks = groupedResults[.track], !tracks.isEmpty {
|
||||
Section {
|
||||
ForEach(tracks) { item in
|
||||
SearchResultRow(item: item)
|
||||
.contentShape(Rectangle())
|
||||
.onTapGesture {
|
||||
print("🎵 Tapped track: \(item.name)")
|
||||
// TODO: Play track
|
||||
}
|
||||
}
|
||||
} header: {
|
||||
Label("Tracks", systemImage: "music.note")
|
||||
.font(.headline)
|
||||
.foregroundStyle(.primary)
|
||||
}
|
||||
}
|
||||
|
||||
// Playlists
|
||||
if let playlists = groupedResults[.playlist], !playlists.isEmpty {
|
||||
Section {
|
||||
ForEach(playlists) { item in
|
||||
NavigationLink(value: convertToPlaylist(item)) {
|
||||
SearchResultRow(item: item)
|
||||
}
|
||||
}
|
||||
} header: {
|
||||
Label("Playlists", systemImage: "music.note.list")
|
||||
.font(.headline)
|
||||
.foregroundStyle(.primary)
|
||||
}
|
||||
}
|
||||
|
||||
// Radios
|
||||
if let radios = groupedResults[.radio], !radios.isEmpty {
|
||||
Section {
|
||||
ForEach(radios) { item in
|
||||
SearchResultRow(item: item)
|
||||
.contentShape(Rectangle())
|
||||
.onTapGesture {
|
||||
print("📻 Tapped radio: \(item.name)")
|
||||
// TODO: Play radio
|
||||
}
|
||||
}
|
||||
} header: {
|
||||
Label("Radio Stations", systemImage: "antenna.radiowaves.left.and.right")
|
||||
.font(.headline)
|
||||
.foregroundStyle(.primary)
|
||||
}
|
||||
}
|
||||
}
|
||||
.listStyle(.plain)
|
||||
}
|
||||
|
||||
// MARK: - Navigation Helper
|
||||
|
||||
private func convertToAlbum(_ item: MAMediaItem) -> MAAlbum {
|
||||
print("🔄 Converting to album: \(item.name) (URI: \(item.uri))")
|
||||
return MAAlbum(
|
||||
uri: item.uri,
|
||||
name: item.name,
|
||||
artists: item.artists,
|
||||
imageUrl: item.imageUrl,
|
||||
imageProvider: item.imageProvider,
|
||||
year: nil
|
||||
)
|
||||
}
|
||||
|
||||
private func convertToPlaylist(_ item: MAMediaItem) -> MAPlaylist {
|
||||
return MAPlaylist(
|
||||
uri: item.uri,
|
||||
name: item.name,
|
||||
imageUrl: item.imageUrl
|
||||
)
|
||||
}
|
||||
|
||||
private func convertToArtist(_ item: MAMediaItem) -> MAArtist {
|
||||
print("🔄 Converting to artist: \(item.name) (URI: \(item.uri))")
|
||||
|
||||
// If the item itself is an artist, use its data
|
||||
if item.mediaType == .artist {
|
||||
return MAArtist(
|
||||
uri: item.uri,
|
||||
name: item.name,
|
||||
imageUrl: item.imageUrl,
|
||||
imageProvider: item.imageProvider,
|
||||
sortName: nil,
|
||||
musicbrainzId: nil
|
||||
)
|
||||
}
|
||||
|
||||
// Otherwise try to use the first artist from the artists array
|
||||
if let firstArtist = item.artists?.first {
|
||||
return firstArtist
|
||||
}
|
||||
|
||||
// Fallback
|
||||
return MAArtist(
|
||||
uri: item.uri,
|
||||
name: item.name,
|
||||
imageUrl: item.imageUrl,
|
||||
imageProvider: item.imageProvider,
|
||||
sortName: nil,
|
||||
musicbrainzId: nil
|
||||
)
|
||||
}
|
||||
|
||||
// MARK: - Search
|
||||
|
||||
private func performSearch(query: String) {
|
||||
@@ -97,21 +230,31 @@ struct SearchView: View {
|
||||
}
|
||||
|
||||
private func executeSearch(query: String) async {
|
||||
print("🔍 Starting search for: '\(query)'")
|
||||
isSearching = true
|
||||
errorMessage = nil
|
||||
|
||||
do {
|
||||
let results = try await service.libraryManager.search(query: query)
|
||||
print("✅ Search returned \(results.count) results")
|
||||
|
||||
// Debug: Print first result
|
||||
if let first = results.first {
|
||||
print("📦 First result: \(first.name) (type: \(first.mediaType?.rawValue ?? "nil"))")
|
||||
}
|
||||
|
||||
await MainActor.run {
|
||||
searchResults = results
|
||||
isSearching = false
|
||||
print("✅ UI updated with \(searchResults.count) results")
|
||||
}
|
||||
} catch {
|
||||
await MainActor.run {
|
||||
errorMessage = error.localizedDescription
|
||||
showError = true
|
||||
searchResults = []
|
||||
isSearching = false
|
||||
print("❌ Search Error: \(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -126,28 +269,20 @@ struct SearchResultRow: View {
|
||||
var body: some View {
|
||||
HStack(spacing: 12) {
|
||||
// Thumbnail
|
||||
if let imageUrl = item.imageUrl {
|
||||
let coverURL = service.imageProxyURL(path: imageUrl, size: 128)
|
||||
|
||||
CachedAsyncImage(url: coverURL) { image in
|
||||
image
|
||||
.resizable()
|
||||
.aspectRatio(contentMode: .fill)
|
||||
} placeholder: {
|
||||
Rectangle()
|
||||
.fill(Color.gray.opacity(0.2))
|
||||
}
|
||||
.frame(width: 60, height: 60)
|
||||
.clipShape(thumbnailShape)
|
||||
} else {
|
||||
CachedAsyncImage(url: service.imageProxyURL(path: item.imageUrl, provider: item.imageProvider, size: 128)) { image in
|
||||
image
|
||||
.resizable()
|
||||
.aspectRatio(contentMode: .fill)
|
||||
} placeholder: {
|
||||
thumbnailShape
|
||||
.fill(Color.gray.opacity(0.2))
|
||||
.frame(width: 60, height: 60)
|
||||
.overlay {
|
||||
Image(systemName: mediaTypeIcon)
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
}
|
||||
.frame(width: 60, height: 60)
|
||||
.clipShape(thumbnailShape)
|
||||
|
||||
// Item Info
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
@@ -167,7 +302,7 @@ struct SearchResultRow: View {
|
||||
.lineLimit(1)
|
||||
}
|
||||
|
||||
Label(item.mediaType.rawValue.capitalized, systemImage: mediaTypeIcon)
|
||||
Label((item.mediaType?.rawValue ?? "unknown").capitalized, systemImage: mediaTypeIcon)
|
||||
.font(.caption2)
|
||||
.foregroundStyle(.tertiary)
|
||||
}
|
||||
@@ -178,12 +313,10 @@ struct SearchResultRow: View {
|
||||
}
|
||||
|
||||
private var thumbnailShape: some Shape {
|
||||
switch item.mediaType {
|
||||
case .artist:
|
||||
if item.mediaType == .artist {
|
||||
return AnyShape(Circle())
|
||||
default:
|
||||
return AnyShape(RoundedRectangle(cornerRadius: 8))
|
||||
}
|
||||
return AnyShape(RoundedRectangle(cornerRadius: 8))
|
||||
}
|
||||
|
||||
private var mediaTypeIcon: String {
|
||||
@@ -193,6 +326,7 @@ struct SearchResultRow: View {
|
||||
case .artist: return "music.mic"
|
||||
case .playlist: return "music.note.list"
|
||||
case .radio: return "antenna.radiowaves.left.and.right"
|
||||
default: return "questionmark"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user