diff --git a/ayudapy.xcworkspace/xcuserdata/roshka.xcuserdatad/UserInterfaceState.xcuserstate b/ayudapy.xcworkspace/xcuserdata/roshka.xcuserdatad/UserInterfaceState.xcuserstate index 46d0901..ccc2aaf 100644 Binary files a/ayudapy.xcworkspace/xcuserdata/roshka.xcuserdatad/UserInterfaceState.xcuserstate and b/ayudapy.xcworkspace/xcuserdata/roshka.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ayudapy/CoreData/CoreDataManager.swift b/ayudapy/CoreData/CoreDataManager.swift index 2bd3e8c..dfc5008 100644 --- a/ayudapy/CoreData/CoreDataManager.swift +++ b/ayudapy/CoreData/CoreDataManager.swift @@ -80,34 +80,54 @@ class CoreDataManager { return bool } - // func saveInCheckedList(id: Int, latitude: Double, longitude: Double, completion: @escaping()-> Void){ - // let context = container.viewContext - // let isCheckedList = IsCheckedList(context: context) - // - // } - // func fetchCheckedList()->[IsCheckedList]{ - // let fetchRequest: NSFetchRequest = IsCheckedList.fetchRequest() - // do { - // let result = try container.viewContext.fetch(fetchRequest) - // return result - // }catch{ - // print("Error al obtener la información de favoritos - \(error)") - // } - // - // return [] - // } -// func deleteCheckObject(id: Int, array: [IsCheckedList]){ -// let context = container.viewContext -// for i in 0.. Void){ + let context = container.viewContext + let isCheckedList = IsCheckedList(context: context) + isCheckedList.id = Int64(id) + isCheckedList.latitude = latitude + isCheckedList.longitude = longitude + do{ + try context.save() + print("\(id) guardado en listos") + completion() + }catch{ + print("error al guardar \(error)") + } + + } + func fetchCheckedList()->[IsCheckedList]{ + let fetchRequest: NSFetchRequest = IsCheckedList.fetchRequest() + do { + let result = try container.viewContext.fetch(fetchRequest) + return result + }catch{ + print("Error al obtener la información de favoritos - \(error)") + } + + return [] + } + func checkReadyList(id: Int, entityList: [IsCheckedList])->Bool{ + let array = entityList + var bool = false + for i in 0.. Posición de Roshka @@ -115,7 +116,10 @@ class MapViewController: UIViewController { let markerView = UIView(frame: CGRect(x: 0, y: 0, width: 30, height: 35)) let markerButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 35)) if self.manager.checkData(id: self.id, entityList: self.favorites){ - markerButton.setImage(UIImage(named: "pinFavGMAPS"), for: .normal) + //markerButton.setImage(UIImage(named: "pinFavGMAPS"), for: .normal) + } + if self.manager.checkReadyList(id: self.id, entityList: self.checkedList){ + // markerButton.setImage(UIImage(named: "pinFavGMAPS"), for: .normal) } else { markerButton.setImage(UIImage(named: "pinGMAPS"), for: .normal) @@ -150,9 +154,24 @@ class MapViewController: UIViewController { //Mostramos en el mapa los marcadores de los pedidos guardados en mis pendientes y/o marcados como listo func showFavoritesOrCheckedIcons(){ - let named = "pinFavGMAPS" + favorites = manager.fetchFavorites() + checkedList = manager.fetchCheckedList() + + for i in 0 ..< checkedList.count{ + let markerView = UIView(frame: CGRect(x: 0, y: 0, width: 30, height: 35)) + let markerButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 35)) + let position = CLLocationCoordinate2D(latitude: checkedList[i].latitude, longitude: checkedList[i].longitude) + markerButton.setImage(UIImage(named: "pinCheckedGMAPS"), for: .normal) + markerView.addSubview(markerButton) + let marker = GMSMarker(position: position) + marker.map = self.mapView + marker.iconView = markerView + marker.title = "\(checkedList[i].id)" + } for i in 0 ..< favorites.count{ + let named = "pinFavGMAPS" + if manager.checkReadyList(id: Int(favorites[i].id), entityList: checkedList) == false { let markerView = UIView(frame: CGRect(x: 0, y: 0, width: 30, height: 35)) let markerButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 35)) let position = CLLocationCoordinate2D(latitude: favorites[i].latitude, longitude: favorites[i].longitude) @@ -162,9 +181,12 @@ class MapViewController: UIViewController { marker.map = self.mapView marker.iconView = markerView marker.title = "\(favorites[i].id)" + } } + } + //Selectors @objc func goToInfoVC(){ let infoVC = self.storyboard?.instantiateViewController(withIdentifier: "infoVC") as! InfoViewController @@ -190,15 +212,21 @@ class MapViewController: UIViewController { requestsVC.isFavorite = true print("El pedido\(id) es favorito") } + if manager.checkReadyList(id: id, entityList: checkedList){ + requestsVC.isChecked = true + print("El pedido\(id) esta marcado como listo") + } + requestsVC.helpRequest() isModalInPresentation = true - present(requestsVC, animated: true, completion: nil) + show(requestsVC, sender: nil) + //present(requestsVC, animated: true, completion: nil) } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(true) favorites = manager.fetchFavorites() - print("Estos son los favoritos \(favorites)") + checkedList = manager.fetchCheckedList() helpRequest() } diff --git a/ayudapy/ViewControllers/RequestViewController.swift b/ayudapy/ViewControllers/RequestViewController.swift index 33728bb..a0fdca3 100644 --- a/ayudapy/ViewControllers/RequestViewController.swift +++ b/ayudapy/ViewControllers/RequestViewController.swift @@ -26,9 +26,10 @@ class RequestViewController: UIViewController { var latitude = Double() var format = Format.init() var isFavorite = Bool() - var checked = Bool() + var isChecked = Bool() private let manager = CoreDataManager() var favorites = [Entity]() + var checkedList = [IsCheckedList]() let isFavoriteButton = UIButton() let isFavoriteLabel = UILabel() @@ -86,7 +87,6 @@ class RequestViewController: UIViewController { messageLabel.font = format.bodyFontStyle messageLabel.numberOfLines = .max messageLabel.lineBreakMode = .byWordWrapping - //print("ha upei \()") //messageLabel.sizeToFit() //Para mostrar la imagen en caso de que el campo no sea nulo @@ -96,6 +96,7 @@ class RequestViewController: UIViewController { imageView.widthAnchor.constraint(equalToConstant: view.frame.width * 0.4).isActive = true imageView.kf.indicatorType = .activity imageView.kf.setImage(with: URL(string: imageUrl!.convertURL())) + imageView.contentMode = .scaleToFill } //Se agrupan las vistas en la cabecera let headerStackView = UIStackView(arrangedSubviews: [dateLabel, titleLbl, messageLabel, imageView]) @@ -119,7 +120,7 @@ class RequestViewController: UIViewController { howToGetButton.setTitle("Como llegar", for: .normal) howToGetButton.addTarget(self, action: #selector(openGoogleMapsLink), for: .touchUpInside) howToGetButton.titleLabel?.font = format.subTitleFontStyle - howToGetButton.heightAnchor.constraint(equalToConstant: 24).isActive = true + howToGetButton.heightAnchor.constraint(equalToConstant: 30).isActive = true let addressStackView = UIStackView(arrangedSubviews: [addressSubTittle, addressLabel, howToGetButton]) addressStackView.axis = .vertical addressStackView.distribution = .equalSpacing @@ -138,17 +139,25 @@ class RequestViewController: UIViewController { contactNumberButton.layer.cornerRadius = 10 contactNumberButton.setTitle("\(phoneNumber)", for: .normal) contactNumberButton.addTarget(self, action: #selector(call), for: .touchUpInside) - contactNumberButton.heightAnchor.constraint(equalToConstant: 24).isActive = true + contactNumberButton.heightAnchor.constraint(equalToConstant: 30).isActive = true let contactInformationStackView = UIStackView(arrangedSubviews: [contactLabel, contactNameLabel, contactNumberButton]) contactInformationStackView.axis = .vertical contactInformationStackView.distribution = .fillEqually contactInformationStackView.spacing = 5 //Se muestra el botón y la etiqueta de marcar como listo - checkLabel.text = "Marcar como listo" + if isChecked{ + checkButton.setImage(UIImage(named: "iconListoChecked"), for: .normal) + checkButton.addTarget(self, action: #selector(addOrRemoveChecked(sender: )), for: .touchUpInside) + checkLabel.text = "Desmarcar como listo" + }else{ + checkButton.setImage(UIImage(named: "iconListoUnchecked"), for: .normal) + checkButton.addTarget(self, action: #selector(addOrRemoveChecked(sender: )), for: .touchUpInside) + checkLabel.text = "Marcar como listo" + } checkLabel.font = format.bodyFontStyle checkLabel.textAlignment = .center - checkButton.setImage(checkImage, for: .normal) + checkLabel.numberOfLines = 2 let checkStackView = UIStackView(arrangedSubviews: [checkButton, checkLabel]) checkStackView.axis = .vertical @@ -188,9 +197,9 @@ class RequestViewController: UIViewController { let scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)) scrollView.addSubview(stackView) + scrollView.isScrollEnabled = true //view.addSubview(stackView) view.addSubview(scrollView) - print(stackView.spacing) } func isReadyCheck()->Bool{ @@ -216,6 +225,26 @@ class RequestViewController: UIViewController { reloadInputViews() } } + + @objc func addOrRemoveChecked(sender: UIButton){ + if isChecked { + manager.deleteCheckObject(id: id, array: checkedList) + self.isChecked = false + sender.setImage(UIImage(named: "iconListoUnchecked"), for: .normal) + self.checkLabel.text = "Marcar como listo" + reloadInputViews() + }else + { + manager.saveInCheckedList(id: id, latitude: latitude, longitude: longitude, completion: { [weak self] in + print("\(self as Any) guardado!") + self?.isChecked = true + }) + self.isChecked = true + sender.setImage(UIImage(named: "iconListoChecked"), for: .normal) + self.checkLabel.text = "Desmarcar como listo" + reloadInputViews() + } + } @objc func openGoogleMapsLink(){ if let url = NSURL(string: "https://www.google.com/maps/dir/\(currentLatitude),\(currentLongitude )/\(latitude),\(longitude)"){ UIApplication.shared.open(url as URL)