forked from HammerPot/Maple-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch.swift
More file actions
112 lines (103 loc) · 2.82 KB
/
Search.swift
File metadata and controls
112 lines (103 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
// Search.swift
// Maple
//
// Created by Potter on 6/8/25.
//
import SwiftUI
struct Result: Identifiable {
let id = UUID()
var name: String
var type: String
var destination: AnyView
var song: Song? = nil
var album: Album? = nil
var artist: Artist? = nil
}
struct Search: View {
@State private var searchText: String = ""
@State private var songs: [Song] = []
@State private var albums: [Album] = []
@State private var artists: [Artist] = []
@State private var sResults: [Result] = []
@State private var alResults: [Result] = []
@State private var arResults: [Result] = []
@State private var results: [Result] = []
var body: some View {
NavigationStack {
if filteredResults.isEmpty {
if searchText == "" {
Text("Search")
}
else {
Text("No Results Found")
}
} else {
List {
ForEach(filteredResults) { result in
NavigationLink(destination: result.destination) {
HStack {
if let art = result.song?.artwork {
if let uiImage = UIImage(contentsOfFile: FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent(art).path) {
Image(uiImage: uiImage)
.resizable()
.scaledToFit()
.frame(width: 50, height: 50)
}
}
else if let art = result.album?.artwork {
if let uiImage = UIImage(contentsOfFile: FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent(art).path) {
Image(uiImage: uiImage)
.resizable()
.scaledToFit()
.frame(width: 50, height: 50)
}
}
VStack {
Text(result.name)
.font(.headline)
Text(result.type)
.font(.subheadline)
}
}
}
}
}
}
}
.searchable(text: $searchText)
.onAppear {
Task{
sResults = []
arResults = []
alResults = []
results = []
songs = await loadSongsFromJson()
albums = await loadAlbumsFromJson()
artists = await loadArtistsFromJson()
for song in songs {
sResults.append(Result(name: song.title, type: "song", destination: AnyView(AudioPlayerView(song: song, allSongs: [song])), song: song))
}
for album in albums {
alResults.append(Result(name: album.name, type: "album", destination: AnyView(AlbumDetailView(album: album)), album: album))
}
for artist in artists {
arResults.append(Result(name: artist.name, type: "artist", destination: AnyView(ArtistDetailView(artist: artist)), artist: artist))
}
results = sResults + arResults + alResults
}
}
}
var filteredResults: [Result] {
if searchText.isEmpty {
return []
} else {
return results.filter { result in
result.name.lowercased().contains(searchText.lowercased())
}
}
}
}
#Preview {
Search()
}