Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // LoadsDetailsViewController.swift
- // UOnLoad
- //
- // Created by Ivan Lupenkov on 3/25/19.
- // Copyright © 2019 UOnLoad. All rights reserved.
- //
- import UIKit
- import Alamofire
- import SwiftSpinner
- import MapKit
- import CoreLocation
- import Toast_Swift
- import ActionSheetPicker_3_0
- class LoadsDetailsViewController: UIViewController {
- var id: Int!
- var proNumber: String!
- var pickupAddrss: String!
- var deliveryAddress: String!
- var pickupDateTime: String!
- var deliveryDateTime: String!
- var createdAt: String!
- var truckSize: NSArray!
- var pcs: Int!
- var weight: Int!
- var deadHead: Double!
- var distance: Int!
- var mybid: NSObject? = nil
- var length: Int!
- var width: Int!
- var height: Int!
- var toll: Int!
- var from_lat: Double!
- var from_lng: Double!
- var to_lat: Double!
- var to_lng: Double!
- var stackable: Bool!
- var dockHigh: Bool!
- var notes: String? = nil
- @IBOutlet weak var loadIdLabel: UILabel!
- @IBOutlet weak var fromLabel: UILabel!
- @IBOutlet weak var toLabel: UILabel!
- @IBOutlet weak var fromDistanceLabel: UILabel!
- @IBOutlet weak var toDistanceLabel: UILabel!
- @IBOutlet weak var routeView: UIView!
- @IBOutlet weak var deadheadLabel: UILabel!
- @IBOutlet weak var distanceLabel: UILabel!
- @IBOutlet weak var suggestedPriceLabel: UILabel!
- @IBOutlet weak var myBidLabel: UILabel!
- @IBOutlet weak var pcsWeightLabel: UILabel!
- @IBOutlet weak var dimLabel: UILabel!
- @IBOutlet weak var puDateLabel: UILabel!
- @IBOutlet weak var deDateLabel: UILabel!
- @IBOutlet weak var notesLabel: UILabel!
- @IBOutlet weak var truckTypeLabel: UILabel!
- let regionDistance:CLLocationDistance = 1000;
- enum CardState {
- case expanded
- case collapsed
- }
- var newBidCardViewController: NewBidCardViewController!
- var visualEffectView: UIVisualEffectView!
- var cardHeight: CGFloat = 0
- var cardHandleAreaHeight: CGFloat = 80
- var cardVisible = false
- var nextState: CardState {
- return cardVisible ? .collapsed : .expanded
- }
- var runningAnimations = [UIViewPropertyAnimator]()
- var animationProgressWhenInterrupted: CGFloat = 0
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- navigationController?.setNavigationBarHidden(true, animated: animated)
- let navigationTap = UITapGestureRecognizer(target: self, action: #selector(self.navigationService))
- routeView.addGestureRecognizer(navigationTap)
- loadIdLabel.text = String("Load #\(id!)")
- fromLabel.text = pickupAddrss
- toLabel.text = deliveryAddress
- deadheadLabel.text = String("\(deadHead!.rounded()) mil deadhead")
- distanceLabel.text = String("\(distance!) mil")
- myBidLabel.text = "N/A"
- // if mybid != nil {
- // let perMile:Float = Float(mybid) / Float(distance)
- // myBidLabel.text = String("$\(mybid!) / \(String(format:"%.2f", perMile)) mile")
- // myBidLabel.textColor = UIColor(red:0.15, green:0.92, blue:0.18, alpha:1.0)
- // }
- pcsWeightLabel.text = String("\(pcs!) / \(weight!)")
- dimLabel.text = "\(length!)x\(width!)x\(height!) in"
- puDateLabel.text = pickupDateTime.toDate()?.toString(withFormat: "MMM d, h:mm a")
- deDateLabel.text = deliveryDateTime.toDate()?.toString(withFormat: "MMM d, h:mm a")
- let truck_type_id = (truckSize.firstObject as! NSString).intValue
- truckTypeLabel.text = TruckType.getNameById(id: Int(truck_type_id))
- notesLabel.text = notes
- }
- override func viewWillDisappear(_ animated: Bool) {
- navigationController?.setNavigationBarHidden(false, animated: animated)
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- setupCard()
- }
- func setupCard() {
- cardHeight = self.view.bounds.height * 0.7
- if UIDevice.hasTopNotch {
- cardHandleAreaHeight = 70
- }
- visualEffectView = UIVisualEffectView()
- visualEffectView.frame = self.view.frame
- self.view.addSubview(visualEffectView)
- self.visualEffectView.isHidden = true
- newBidCardViewController = NewBidCardViewController(nibName:"NewBidCardView", bundle:nil)
- self.addChild(newBidCardViewController)
- self.view.addSubview(newBidCardViewController.view)
- newBidCardViewController.view.frame = CGRect(x: 0, y: self.view.frame.height - cardHandleAreaHeight, width: self.view.bounds.width, height: cardHeight)
- newBidCardViewController.view.clipsToBounds = true
- if newBidCardViewController.loadId != nil && self.id != nil
- {
- newBidCardViewController.loadId = self.id!
- }
- let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.handleCardTap(recognzier:)))
- let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(self.handleCardPan(recognizer:)))
- newBidCardViewController.handleArea.addGestureRecognizer(tapGestureRecognizer)
- newBidCardViewController.handleArea.addGestureRecognizer(panGestureRecognizer)
- }
- @objc
- func handleCardTap(recognzier:UITapGestureRecognizer) {
- switch recognzier.state {
- case .ended:
- animateTransitionIfNeeded(state: nextState, duration: 0.9)
- default:
- break
- }
- }
- @objc
- func handleCardPan(recognizer: UIPanGestureRecognizer) {
- switch recognizer.state {
- case .began:
- // startTransition
- startInteractiveTransition(state: nextState, duration: 0.9)
- case .changed:
- // updateTransition
- let translation = recognizer.translation(in: self.newBidCardViewController.handleArea)
- var fractionCompleted = translation.y / cardHeight
- fractionCompleted = cardVisible ? fractionCompleted : -fractionCompleted
- updateInteractiveTransition(fractionCompleted: fractionCompleted)
- case .ended:
- // continueTransition
- continueInteractiveTransition()
- default:
- break
- }
- }
- func animateTransitionIfNeeded(state: CardState, duration: TimeInterval) {
- if runningAnimations.isEmpty {
- let frameAnimator = UIViewPropertyAnimator(duration: duration, dampingRatio: 1) {
- switch state {
- case .expanded:
- UIView.animate(withDuration: 2) {
- self.newBidCardViewController.handleArrowImage.transform = CGAffineTransform(rotationAngle: .pi)
- self.newBidCardViewController.handleLabel.text = "Swipe bottom to cancel"
- }
- self.newBidCardViewController.view.frame.origin.y = self.view.frame.height - (self.view.frame.height * 0.7)
- self.visualEffectView.isHidden = false
- case .collapsed:
- UIView.animate(withDuration: 2) {
- self.newBidCardViewController.handleArrowImage.transform = CGAffineTransform.identity
- self.newBidCardViewController.handleLabel.text = "Swipe up to place bid"
- }
- self.newBidCardViewController.view.frame.origin.y = self.view.frame.height - self.view.safeAreaInsets.bottom - self.cardHandleAreaHeight
- }
- }
- frameAnimator.addCompletion { _ in
- self.cardVisible = !self.cardVisible
- self.runningAnimations.removeAll()
- }
- frameAnimator.startAnimation()
- runningAnimations.append(frameAnimator)
- let cornerRadiusAnimator = UIViewPropertyAnimator(duration: duration, curve: .linear) {
- switch state {
- case .expanded:
- self.newBidCardViewController.view.layer.cornerRadius = 14
- case .collapsed:
- self.newBidCardViewController.view.layer.cornerRadius = 14
- }
- }
- cornerRadiusAnimator.startAnimation()
- runningAnimations.append(cornerRadiusAnimator)
- let blurAnimator = UIViewPropertyAnimator.init(duration: duration, dampingRatio: 1) {
- switch state {
- case .expanded:
- self.visualEffectView.effect = UIBlurEffect(style: .dark)
- case .collapsed:
- self.visualEffectView.effect = nil
- }
- }
- blurAnimator.startAnimation()
- runningAnimations.append(blurAnimator)
- blurAnimator.addCompletion{(_) in
- if state == .collapsed{
- self.visualEffectView.isHidden = true
- }
- }
- }
- }
- func startInteractiveTransition(state: CardState, duration: TimeInterval) {
- if runningAnimations.isEmpty {
- animateTransitionIfNeeded(state: state, duration: duration)
- }
- for animator in runningAnimations {
- animator.pauseAnimation()
- animationProgressWhenInterrupted = animator.fractionComplete
- }
- }
- func updateInteractiveTransition(fractionCompleted: CGFloat) {
- for animator in runningAnimations {
- animator.fractionComplete = fractionCompleted + animationProgressWhenInterrupted
- }
- }
- func continueInteractiveTransition() {
- for animator in runningAnimations {
- animator.continueAnimation(withTimingParameters: nil, durationFactor: 0)
- }
- }
- @IBAction func backButton(_ sender: Any) {
- self.navigationController?.popViewController(animated: true)
- }
- func saveBid(bid: Float?, loadId: Int?){
- let token = (defaultValues.string(forKey: "token") ?? "Undefined")
- let headers: HTTPHeaders = [
- "Authorization": "Bearer \(token)",
- ]
- let parameters: Parameters = [
- "amount": bid!,
- "load_id": loadId!
- ]
- requestAPI.make(endpoint: APIEndpoint.bid.rawValue, method: HTTPMethod.post, parameters: parameters, headers: headers, completionHandler: {(data: Any?, success: String) -> Void in
- switch success {
- case "success":
- print("success")
- // if bid != 0 {
- // UIView.animate(withDuration: 1.0) {
- // let perMile:Float = Float(bid!) / Float(self.distance)
- // self.myBidLabel.text = String("$\(bid!) / \(String(format:"%.2f", perMile)) mile")
- // self.myBidLabel.textColor = UIColor(red:0.15, green:0.92, blue:0.18, alpha:1.0)
- // }
- // } else {
- // self.myBidLabel.text = String("N/A")
- // self.myBidLabel.textColor = .gray
- // }
- case "fail":
- let data = data as! NSDictionary
- let error = data.value(forKey: "errors") as! String
- self.view.makeToast("ERROR! \(error)")
- self.view.hideToastActivity()
- case "error":
- print("Error2")
- default:
- print("Error3")
- }
- })
- }
- @objc func navigationService(){
- let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
- let scanAction = UIAlertAction(title: "Apple Maps", style: .default) { (_) in
- self.navigateAppleMaps()
- }
- let selectAction = UIAlertAction(title: "Google Maps", style: .default) { (_) in
- self.navigateGoogleMaps()
- }
- let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .cancel, handler: nil)
- actionSheet.addAction(scanAction)
- actionSheet.addAction(selectAction)
- actionSheet.addAction(cancelAction)
- present(actionSheet, animated: true)
- }
- private func navigateAppleMaps(){
- let url = "http://maps.apple.com/maps?saddr=\(pickupAddrss!)&daddr=\(deliveryAddress!)"
- UIApplication.shared.open(URL(string:url.removingWhitespaces())!)
- }
- private func navigateGoogleMaps(){
- let appDomen: String = "comgooglemaps://"
- let browserDomen: String = "https://www.google.co.in/maps/dir/"
- let directionBody: String = "?saddr=\(pickupAddrss!)&daddr=\(deliveryAddress!)&directionsmode=driving"
- if let appUrl = URL(string: appDomen), UIApplication.shared.canOpenURL(appUrl) {
- guard let appFullPathUrl = URL(string: appDomen + directionBody.removingWhitespaces()) else { return }
- UIApplication.shared.open(appFullPathUrl)
- } else if let browserUrl = URL(string: browserDomen), UIApplication.shared.canOpenURL(browserUrl) {
- guard let browserFullPathUrl = URL(string: browserDomen + directionBody) else { return }
- UIApplication.shared.open(browserFullPathUrl)
- }
- }
- }
- extension String {
- func removingWhitespaces() -> String {
- return components(separatedBy: .whitespaces).joined()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment