DownloadsView.swift
1 // 2 // DownloadsView.swift 3 // flo 4 // 5 // Created by rizaldy on 08/06/24. 6 // 7 8 import SwiftUI 9 10 struct DownloadsView: View { 11 @State private var searchAlbum = "" 12 13 @ObservedObject var viewModel: AlbumViewModel 14 15 @EnvironmentObject var playerViewModel: PlayerViewModel 16 17 let columns = [ 18 GridItem(.flexible()), 19 GridItem(.flexible()), 20 ] 21 22 var filteredAlbums: [Album] { 23 if searchAlbum.isEmpty { 24 return viewModel.downloadedAlbums 25 } else { 26 return viewModel.downloadedAlbums.filter { album in 27 album.name.localizedCaseInsensitiveContains(searchAlbum) 28 } 29 } 30 } 31 32 var body: some View { 33 NavigationStack { 34 ScrollView { 35 if viewModel.downloadedAlbums.isEmpty { 36 VStack(alignment: .leading) { 37 Image("Downloads").resizable().aspectRatio(contentMode: .fit).frame(width: 300) 38 .padding() 39 .padding(.bottom, 10) 40 Group { 41 Text("Going off the grid?") 42 .customFont(.title1) 43 .fontWeight(.bold) 44 .multilineTextAlignment(.leading) 45 .padding(.bottom, 10) 46 Text( 47 "Bring your music anywhere, even when you're offline. Your downloaded music will be here." 48 ) 49 .customFont(.subheadline) 50 51 }.padding(.horizontal, 20).foregroundColor(.accent) 52 } 53 } 54 55 LazyVGrid(columns: columns, spacing: 20) { 56 ForEach(filteredAlbums) { album in 57 NavigationLink { 58 AlbumView(viewModel: viewModel, isDownloadScreen: true) 59 .onAppear { 60 viewModel.setActiveAlbum(album: album) 61 } 62 } label: { 63 AlbumsView(viewModel: viewModel, album: album, isDownloadScreen: true) 64 } 65 } 66 }.padding(.top, 10).padding( 67 .bottom, playerViewModel.hasNowPlaying() && !playerViewModel.shouldHidePlayer ? 100 : 0 68 ).navigationTitle("Downloads") 69 .searchable( 70 text: $searchAlbum, 71 placement: .navigationBarDrawer(displayMode: .always), 72 prompt: "Search" 73 ) 74 } 75 } 76 } 77 } 78 79 struct DownloadsView_Previews: PreviewProvider { 80 @StateObject static var viewModel: AlbumViewModel = AlbumViewModel() 81 82 static var previews: some View { 83 DownloadsView(viewModel: viewModel) 84 } 85 }