Zacharysp / CalendarHeatmap

A calendar based heatmap which presenting a time series of data points in colors.
MIT License
194 stars 24 forks source link

The reload() method is not working #12

Closed absenceofnunchi closed 4 years ago

absenceofnunchi commented 4 years ago

The reload() method doesn't retrieve the data from plist. I want the user to input data from one screen and when they visit the screen with the heat map, I want the heat map to reflect the input data. But, the the heat map only reflects the data when the app is loaded for the first time, view.addSubview(calendarHeatMap), but when I use the reload() method any time after, the map doesn't reflect any additional data.

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    calendarHeatMap.reload()
}

lazy var calendarHeatMap: CalendarHeatmap = {
    var config = CalendarHeatmapConfig()
    config.backgroundColor = UIColor(ciColor: .white)
    // config item
    config.selectedItemBorderColor = .white
    config.allowItemSelection = true
    // config month header
    config.monthHeight = 30
    config.monthStrings = DateFormatter().shortMonthSymbols
    config.monthFont = UIFont.systemFont(ofSize: 18)
    config.monthColor = UIColor(ciColor: .black)
    // config weekday label on left
    config.weekDayFont = UIFont.systemFont(ofSize: 12)
    config.weekDayWidth = 30
    config.weekDayColor = UIColor(ciColor: .black)

    var dateComponent = DateComponents()
    let yearsBefore = -1
    dateComponent.year = yearsBefore
    if let startDate = Calendar.current.date(byAdding: dateComponent, to: Date()) {
        let calendar = CalendarHeatmap(config: config, startDate: startDate, endDate: Date())
        calendar.delegate = self
        return calendar
    } else {
        let calendar = CalendarHeatmap(config: config, startDate: Date(2019, 1, 1), endDate: Date())
        calendar.delegate = self
        return calendar
    }
}()

private func readHeatmap() -> [String: Int]? {
    guard let url = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("Heatmap.plist") else { return nil }

    return NSDictionary(contentsOf: url) as? [String: Int]
}

Everything else is pretty much the same as the example in this repo.

When the screen is first loaded, the order of execution is:

  1. lazy var calendarHeatMap: CalendarHeatmap
  2. viewWillAppear() which has the calendarHeapmap.reload() method
  3. func readHeatmap()
  4. lazy var data: [String: UIColor]
  5. func finishLoadCalendar()

or

  1. lazy var calendarHeatMap: CalendarHeatmap
  2. func readHeatmap()
  3. lazy var data: [String: UIColor]
  4. func finishLoadCalendar()
  5. viewDidAppear() which has the calendarHeapmap.reload() method

However, when the screen is revisited, none of these are called, except for the view's life cycle methods.

absenceofnunchi commented 4 years ago

Actually, the issue was the lazy loading of the plist, not anything to do with reloading.