Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 5.27 KB | None | 0 0
  1. import UIKit
  2.  
  3. private let reuseIdentifier = "DateCell"
  4.  
  5. class CalendarViewController: UICollectionViewController {
  6.     let calendar = Calendar.current
  7.     var date: Date = Date().startOfMonth()
  8.    
  9.     var currentMonth: Int {
  10.         get { return calendar.component(.month, from: date) }
  11.     }
  12.    
  13.     var currentMonthSymbol: String {
  14.         get { return calendar.monthSymbols[currentMonth - 1] }
  15.     }
  16.    
  17.     var currentYear: Int {
  18.         get { return calendar.component(.year, from: date) }
  19.     }
  20.    
  21.     var offset: Int {
  22.         get {
  23.             let weekday = calendar.component(.weekday, from: date)
  24.             if weekday == 1 { return 6 }
  25.             return weekday - 2
  26.         }
  27.     }
  28.    
  29.     @objc func didTapPrev(_ sender: Any) {
  30.         date = Calendar.current.date(byAdding: .month, value: -1, to: date)!
  31.         smoothReload()
  32.         setTitle()
  33.     }
  34.    
  35.     @objc func didTapNext(_ sender: Any) {
  36.         date = Calendar.current.date(byAdding: .month, value: 1, to: date)!
  37.         smoothReload()
  38.         setTitle()
  39.     }
  40.    
  41.     @objc func didTapSearch(_ sender: Any) {
  42.         performSegue(withIdentifier: "ShowSearch", sender: sender)
  43.     }
  44.    
  45.     override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  46.         if kind == UICollectionView.elementKindSectionHeader {
  47.             return collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "CalendarHeader", for: indexPath)
  48.         }
  49.         fatalError("Unexpected kind")
  50.     }
  51.    
  52.     override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  53.         return calendar.range(of: .day, in: .month, for: date)!.count + offset
  54.     }
  55.    
  56.     override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  57.         let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! DateCell
  58.         cell.backgroundColor = UIColor(white: 1, alpha: 0)
  59.        
  60.         if indexPath.row < offset {
  61.             cell.dateLabel.text = ""
  62.             cell.taskCountLabel.text = ""
  63.             cell.layer.borderWidth = 0
  64.         } else {
  65.             cell.setDate(calendar.date(byAdding: .day, value: indexPath.row - offset, to: date)!)
  66.             cell.layer.borderWidth = 0.5
  67.             cell.layer.borderColor = UIColor.lightGray.cgColor
  68.            
  69.             if cell.date!.isSameDateAs(Date()) {
  70.                 cell.backgroundColor = UIColor(red: 1, green: 0, blue: 0, alpha: 0.5)
  71.             } else if calendar.isDateInWeekend(cell.date!) {
  72.                 cell.backgroundColor = UIColor(red: 1, green: 0, blue: 0, alpha: 0.2)
  73.             }
  74.         }
  75.        
  76.         return cell
  77.     }
  78.    
  79.     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  80.         if let dateVC = segue.destination as? DateViewController {
  81.             let cell = sender as! DateCell
  82.             dateVC.date = cell.date!
  83.         }
  84.     }
  85.    
  86.     override func viewDidLoad() {
  87.         super.viewDidLoad()
  88.        
  89.         setTitle()
  90.        
  91.         let prevButton = UIBarButtonItem(title: "Prev", style: .plain, target: self, action: #selector(didTapPrev(_:)))
  92.         self.navigationItem.setLeftBarButton(prevButton, animated: true)
  93.        
  94.         let nextButton = UIBarButtonItem(title: "Next", style: .plain, target: self, action: #selector(didTapNext(_:)))
  95.         let searchButton = UIBarButtonItem(barButtonSystemItem: .search, target: self, action: #selector(didTapSearch(_:)))
  96.         self.navigationItem.setRightBarButtonItems([nextButton, searchButton], animated: true)
  97.        
  98.         let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(didTapNext))
  99.         swipeLeft.direction = .left
  100.         self.view.addGestureRecognizer(swipeLeft)
  101.        
  102.         let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(didTapPrev))
  103.         swipeRight.direction = .right
  104.         self.view.addGestureRecognizer(swipeRight)
  105.     }
  106.    
  107.     func setTitle() {
  108.         self.title = "\(currentMonthSymbol) \(currentYear)"
  109.     }
  110. }
  111.  
  112. extension CalendarViewController: UICollectionViewDelegateFlowLayout {
  113.     func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  114.         let width = collectionView.bounds.width / 7.0
  115.         let height = width
  116.        
  117.         return CGSize(width: width, height: height)
  118.     }
  119. }
  120.  
  121. extension Date {
  122.     func startOfMonth() -> Date {
  123.         let components = Calendar.current.dateComponents([.year, .month], from: Calendar.current.startOfDay(for: self))
  124.         return Calendar.current.date(from: components)!
  125.     }
  126.    
  127.     func isSameDateAs(_ other: Date) -> Bool {
  128.         return Calendar.current.startOfDay(for: self) == Calendar.current.startOfDay(for: other)
  129.     }
  130. }
  131.  
  132. extension UICollectionViewController {
  133.     func smoothReload() {
  134.         self.collectionView.performBatchUpdates({
  135.             self.collectionView.reloadSections(NSIndexSet(index: 0) as IndexSet)
  136.         }, completion: { (finished: Bool) -> Void in })
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement