MosheBerman/MBCalendarKit

how refresh the data

jonajgs opened this issue · 1 comments

Summary

I get the events for the server, but only show the period in the calendar, but not refreshing the table

this is my code:


import UIKit
import MBCalendarKit

class CalendarUIViewController: CalendarViewController {
    

    var data : [Date:[CalendarEvent]] = [:]
    var refreshControl:UIRefreshControl!
    
    required init?(coder aDecoder: NSCoder) {
        
        self.data = [:]
        
        super.init(coder: aDecoder)
    }
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        
        self.data = [:]
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.delegate = self
        self.dataSource = self
        
        calendarView.leftAnchor.constraint(equalTo: calendarView.leftAnchor).isActive = true
        calendarView.topAnchor.constraint(equalTo: calendarView.topAnchor).isActive = true
        calendarView.rightAnchor.constraint(equalTo: calendarView.rightAnchor).isActive = true
        
        refreshControl = UIRefreshControl()
        refreshControl?.attributedTitle = NSAttributedString(string: "")
        refreshControl?.addTarget(self, action: #selector(refresh), for: UIControlEvents.valueChanged)
        
        tableView.addSubview(refreshControl!)
        
        setupEvents()
    }
    
    @objc func refresh() {
        refreshControl.endRefreshing()
        setupEvents()
    }

    func setupEvents() {
        Service.getInstance().getEvents { (events) in
            self.fillEvents(events)
        }
    }
    
    func fillEvents(_ events: [Event]) {
        var title:String?
        var date:Date?
        var calendarEvent:CalendarEvent?
        
        for event in events {
            title = event.title
            date = getDate(date: event.initial_date)
            
            calendarEvent = CalendarEvent(title: title, andDate: date, andInfo: ["event": event])
            
            if self.data[date!] == nil {
                self.data[date!] = []
            }
            
            self.data[date!]?.append(calendarEvent!)
        }
        
        calendarView.reload(animated: true)
        self.tableView.reloadData()
    }
    
    func getDate(date: String) -> Date {
        let dateSplitted = date.split(separator: "/")
        let date = NSDate(day: UInt(dateSplitted[2])!, month: UInt(dateSplitted[1])!, year: UInt(dateSplitted[0])!)
        
        return date! as Date
    }
    
    override func calendarView(_ calendarView: CalendarView, eventsFor date: Date) -> [CalendarEvent] {
        let eventsForDate = self.data[date] ?? []
        
        return eventsForDate
    }
    
    // MARK: - CKCalendarDelegate
    
    // Called before the selected date changes.
    override func calendarView(_ calendarView: CalendarView, didSelect date: Date) {
        super.calendarView(calendarView, didSelect: date) // Call super to ensure it
    }
    
    // Called after the selected date changes.
    override func calendarView(_ calendarView: CalendarView, willSelect date: Date) {
        
    }
    
    // A row was selected in the events table. (Use this to push a details view or whatever.)
    override func calendarView(_ calendarView: CalendarView, didSelect event: CalendarEvent) {
        performSegue(withIdentifier: "showDetail", sender: event)
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let data: CalendarEvent = sender as? CalendarEvent {
            
            if let destinationVC = segue.destination as? EventViewController {
                destinationVC.event = data.info["event"] as? Event
            }
        }
        
    }

}

closes for inactivity