Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.88 KB | None | 0 0
  1. //
  2. //  ChartTableViewCell.swift
  3. //  PosgradApp
  4. //
  5. //  Created by Douglas Toneto Pfeifer on 14/02/19.
  6. //  Copyright © 2019 Douglas Tonetto Pfeifer. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import Charts
  11.  
  12. protocol ClassDashboardCellDelegate: class {
  13.     func segueToMissionDetail(mission: Double, points: Double, cellRow: Int, cellTeam: String)
  14.     func changeChartType(cellRow: Int, completion: @escaping (Chart) -> ())
  15. }
  16.  
  17. class DashboardChartTableViewCell: UITableViewCell, ChartViewDelegate {
  18.  
  19.     @IBOutlet weak var configButton: UIButton!
  20.     @IBOutlet weak var chartView: UIView!
  21.     @IBOutlet weak var teamLabel: UILabel!
  22.    
  23.     var chartInView = BarLineChartViewBase()
  24.     var indexPathRow : Int!
  25.     var indexesShown = [Int]()
  26.     let failureLabel = UILabel()
  27.    
  28.     weak var delegate: ClassDashboardCellDelegate?
  29.    
  30.     override func awakeFromNib() {
  31.         super.awakeFromNib()
  32.     }
  33.    
  34.     override func didChangeValue(forKey key: String) {
  35.         indexesShown.removeAll()
  36.     }
  37.    
  38.     @IBAction func configButton(_ sender: Any) {
  39.         delegate!.changeChartType(cellRow: indexPathRow, completion: {
  40.             (newChart) in
  41.             let removableView = self.chartView.viewWithTag(101)
  42.             removableView?.removeFromSuperview()
  43.            
  44.             if newChart.lineChartShouldBeActive {
  45.                 self.configButton.setImage(UIImage(named:"bar_chart_black_48dp")?.withRenderingMode(.alwaysTemplate), for: .normal)
  46.             } else {
  47.                 self.configButton.setImage(UIImage(named:"show_chart_black_48dp")?.withRenderingMode(.alwaysTemplate), for: .normal)
  48.             }
  49.            
  50.             self.chartInView = newChart.activeChart
  51.             self.chartInView.tag = 101
  52.             self.chartView.addSubview(self.chartInView)
  53.             self.chartInView.translatesAutoresizingMaskIntoConstraints = false
  54.             self.chartInView.topAnchor.constraint(equalTo: self.chartView.topAnchor, constant: 0).isActive = true
  55.             self.chartInView.leftAnchor.constraint(equalTo: self.chartView.leftAnchor, constant: 0).isActive = true
  56.             self.chartInView.rightAnchor.constraint(equalTo: self.chartView.rightAnchor, constant: 0).isActive = true
  57.             self.chartInView.bottomAnchor.constraint(equalTo: self.chartView.bottomAnchor, constant: 0).isActive = true
  58.             self.chartInView.delegate = self
  59.             self.chartInView.setNeedsDisplay()
  60.             self.chartInView.animate(yAxisDuration: 1)
  61.         })
  62.     }
  63.    
  64.     func initChart(chart: Chart, indexPathRow: Int) {
  65.         self.indexPathRow = indexPathRow
  66.        
  67.         let removableView = chartView.viewWithTag(101)
  68.         removableView?.removeFromSuperview()
  69.        
  70.         self.chartInView = chart.activeChart
  71.         self.chartInView.tag = 101
  72.         self.chartView.addSubview(self.chartInView)
  73.         self.chartInView.translatesAutoresizingMaskIntoConstraints = false
  74.         self.chartInView.topAnchor.constraint(equalTo: self.chartView.topAnchor, constant: 0).isActive = true
  75.         self.chartInView.leftAnchor.constraint(equalTo: self.chartView.leftAnchor, constant: 0).isActive = true
  76.         self.chartInView.rightAnchor.constraint(equalTo: self.chartView.rightAnchor, constant: 0).isActive = true
  77.         self.chartInView.bottomAnchor.constraint(equalTo: self.chartView.bottomAnchor, constant: 0).isActive = true
  78.         self.chartInView.delegate = self
  79.         self.chartInView.setNeedsDisplay()
  80.        
  81.         if !indexesShown.contains(indexPathRow) {
  82.             indexesShown.append(indexPathRow)
  83.             self.chartInView.animate(yAxisDuration: 1)
  84.         }
  85.     }
  86.    
  87.     func initFailureChart (indexPathRow: Int) {
  88.         self.indexPathRow = indexPathRow
  89.        
  90.         let removableView = chartView.viewWithTag(101)
  91.         removableView?.removeFromSuperview()
  92.        
  93.         self.chartView.addSubview(self.failureLabel)
  94.         self.failureLabel.translatesAutoresizingMaskIntoConstraints = false
  95.         self.failureLabel.centerXAnchor.constraint(equalTo: self.chartView.centerXAnchor).isActive = true
  96.         self.failureLabel.centerYAnchor.constraint(equalTo: self.chartView.centerYAnchor).isActive = true
  97.         self.failureLabel.text = "Não foi possível obter os dados do time, tente novamente mais tarde."
  98.         self.failureLabel.textAlignment = .center
  99.         self.failureLabel.textColor = UIColor.lightGray
  100.         self.failureLabel.numberOfLines = 0
  101.         self.failureLabel.font = UIFont.systemFont(ofSize: 18)
  102.     }
  103.    
  104.     override func setSelected(_ selected: Bool, animated: Bool) {
  105.         super.setSelected(selected, animated: animated)
  106.     }
  107.    
  108.     func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
  109.         delegate?.segueToMissionDetail(mission: entry.x, points: entry.y, cellRow: indexPathRow, cellTeam: teamLabel.text!)
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement