CityExploreApp / City Sights App / Views / Home / HomeView.swift
HomeView.swift
Raw
//
//  HomeView.swift
//  City Sights App
//
//  Created by Milos Ilic on 15.1.23..
//

import SwiftUI

struct HomeView: View {
    
    @EnvironmentObject var model: ContentModel
    @State var isMapShowing = false
    @State var selectedBusiness:Business?
    
    var body: some View {
        
        if model.restaurants.count != 0 || model.sights.count != 0 {
            
            NavigationView {
                
                // Determine if we should show list or map
                if !isMapShowing {
                    // Show list
                    
                    VStack (alignment: .leading) {
                        
                        HStack {
                            
                            Image(systemName: "location")
                            
                            Text("San Francisco")
                            
                            Spacer()
                            
                            Button("Map view"){
                                self.isMapShowing = true
                            }
                            
                            
                        }
                        Divider()
                        
                        BusinessList()
                        
                    }
                    .padding([.horizontal, .top])
                    .navigationBarHidden(true)
                    // Add nav bar hidden to child view, not to nav view
                    
                    
                    
                }
                else {
                    
                    ZStack (alignment: .top) {
                        // Show map
                        BusinessMap(selectedBusiness: $selectedBusiness)
                            .ignoresSafeArea()
                            .sheet(item: $selectedBusiness) { business in
                                
                                // Create a business detail view instance
                                // Pass in the selected business
                                BusinessDetail(business: business)
                            }
                        
                        // Rectangle overlay
                        ZStack {
                            
                            Rectangle()
                                .foregroundColor(.white)
                                .cornerRadius(5)
                                .frame(height: 48)
                            
                            HStack {
                                
                                Image(systemName: "location")
                                
                                Text("San Francisco")
                                
                                Spacer()
                                
                                Button("List view"){
                                    self.isMapShowing = false
                                }
                                
                            }
                            .padding()
                            
                        }
                        .padding()
                        
                    }
                   
                }
                
            }

        }
        else {
            
            // Still waiting for data so show spinner
            ProgressView()
            
        }
    }
}

struct HomeView_Previews: PreviewProvider {
    static var previews: some View {
        HomeView()
    }
}