import Foundation import SwiftUI import MapKit struct DeviceCard: View { @ObservedObject var mainViewModel: MainViewModel = MainViewModel.shared @State var isExpanded: Bool = false @State var deviceType: String @State var deviceName: String @State var deviceID: String @State var activationDate: Int @State var razaoSocial: String @State var address: String @State var number: String @State var neighbor: String @State var city: String @State var state: String @State var lat: Double @State var lon: Double var body: some View { VStack(spacing: 0) { HStack { Text(self.deviceType) .font(.custom("Nunito-Bold", size: 11)) .textCase(.uppercase) .foregroundColor(Color.accentColor) Spacer() Image(systemName: self.isExpanded ? "chevron.up" : "chevron.down") .resizable() .aspectRatio(contentMode: .fit) .frame(width: 10, height: 6, alignment: .center) .foregroundColor(Color.fontColorSecondary) }.padding(.bottom, 5) HStack { Text(self.deviceName == "" ? "No Device Name" : self.deviceName) .font(.custom("Nunito-Bold", size: 20)) .foregroundColor(Color.fontColor) Spacer() } HStack { VStack { HStack { Text(self.deviceID) .font(.custom("Nunito-Regular", size: 13)) .foregroundColor(Color.fontColorSecondary) Spacer() } .padding(.bottom, 5) HStack { Text("Ativado em \(self.formatDate(activationDate))") .font(.custom("Nunito-Bold", size: 13)) .foregroundColor(Color.fontColorSecondary) Spacer() } } if self.mainViewModel.actualLocation != nil { Button { let directionsURL = "http://maps.apple.com/?saddr=\(self.mainViewModel.actualLocation?.latitude ?? 0),\(self.mainViewModel.actualLocation?.longitude ?? 0)&daddr=\(self.lat),\(self.lon)" guard let url = URL(string: directionsURL) else { return } if #available(iOS 10.0, *) { UIApplication.shared.open(url, options: [:], completionHandler: nil) } else { UIApplication.shared.openURL(url) } } label: { Image(systemName: "location") .resizable() .aspectRatio(contentMode: .fit) .frame(width: 13.5, height: 13.5, alignment: .center) .padding(10) .background( ZStack { Circle() .stroke(Color.borderColor, lineWidth: 1) Circle() .shadow(color: Color.borderColor, radius: 0, x: 0, y: 2) .foregroundColor(Color.cardColor) } ) } .zIndex(3) } } .padding(.bottom, 10) // MARK: - Is Expanded if self.isExpanded { VStack(spacing: 0) { Rectangle() .foregroundColor(Color.borderColor) .frame(height: 1, alignment: .center) HStack { Image(systemName: "mappin.circle") .resizable() .aspectRatio(contentMode: .fit) .frame(width: 10, height: 10, alignment: .center) Text(self.razaoSocial == "" ? "No Business Name" : self.razaoSocial) .font(.custom("Nunito-Regular", size: 13)) .foregroundColor(Color.fontColorSecondary) Spacer() } .padding(.top, 10) .padding(.bottom, 5) HStack { Text("\(self.address), \(self.number) - \(self.neighbor), \(self.city) - \(self.state)") .font(.custom("Nunito-Regular", size: 11)) .foregroundColor(Color.fontColorSecondary) Spacer() } .padding(.bottom, 10) VStack { Map(coordinateRegion: .constant(MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: self.lat, longitude: self.lon), latitudinalMeters: 150, longitudinalMeters: 150)), annotationItems: [IdentifiablePlace(id: UUID(), location: CLLocationCoordinate2D(latitude: self.lat, longitude: self.lon))]) { place in MapAnnotation(coordinate: place.location) { Image("icon-pin") .resizable() .aspectRatio(contentMode: .fit) .frame(width: 30, height: 37, alignment: .center) } } .frame(height: 100) .cornerRadius(5, corners: [.allCorners]) } .onAppear { MKMapView.appearance().pointOfInterestFilter = .excludingAll MKMapView.appearance().isRotateEnabled = false MKMapView.appearance().isZoomEnabled = false MKMapView.appearance().isPitchEnabled = false MKMapView.appearance().isScrollEnabled = false MKMapView.appearance().mapType = .standard } // MapComponent(zoomLevelLat: 150, // zoomLevelLon: 150, // zoom: false, // scroll: false, // regionLat: self.lat, // regionLon: self.lon, // annotations: [AnnotationStruct(lat: self.lat, lon: self.lon)]) // .cornerRadius(5, corners: [.allCorners]) } } } .padding() .background( ZStack { RoundedRectangle(cornerRadius: 10) .stroke(Color.borderColor, lineWidth: 1) RoundedRectangle(cornerRadius: 10) .shadow(color: Color.borderColor, radius: 0, x: 0, y: 2) .foregroundColor(Color.cardColor) } ) .padding(1) .onTapGesture { withAnimation { self.isExpanded.toggle() } } .contextMenu { Button { print("Vitor") } label: { Text("Option 1") } } } private func formatDate(_ timestamp: Int) -> String { let dateFormatter: DateFormatter = DateFormatter() dateFormatter.dateFormat = "dd/MM/yyyy HH:mm" dateFormatter.timeZone = .current return dateFormatter.string(from: Date(timeIntervalSince1970: Double(timestamp / 1000))) } }