Zacharysp / CalendarHeatmap

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

Date changed but the view not update automatically - using framework in SwiftUI #15

Open AngusYahn opened 3 years ago

AngusYahn commented 3 years ago

Hi, I'm a newbie developer and using SwiftUI for interface development.

So I managed to embed your framework into the SwiftUI and using the custom data function for the heatmap data. WX20210530-205702

Everything works great but when the heatmap data changed, the heatmap view doesn't update automatically - unless I close and reopen the page again. I've searched and tried everything I can, but none of those works. Could you please give me some advice on this, or there is already a solution for this but I'm using it in the wrong way?

Thanks!

Zacharysp commented 3 years ago

Hi, Thank you for using CalendarHeatmap framework. Hope you find it useful.

The thing is currently we don’t support auto refresh functionality. If the data source changed you have to call the reload function to make the UI update, more like the UITableView reload.

Hope this helps.

On Sun, May 30, 2021 at 5:53 AM 杨燚 @.***> wrote:

Hi, I'm a newbie developer and using SwiftUI for interface development.

So I managed to embed your framework into the SwiftUI and using the custom data function for the heatmap data. `import SwiftUI import UIKit import CalendarHeatmap

struct HeatMapView: UIViewControllerRepresentable { var startDate: Date var endDate: Date var heatMapData: [String : Int]?

func makeUIViewController(context: Context) -> UIViewController {

let uiView = ViewController(startDate: startDate, endDate: endDate, heatMapData: heatMapData)

return uiView

}

func updateUIViewController(_ uiViewController: UIViewController, context: Context) {

}

}

struct HeatMapView_Previews: PreviewProvider { static var previews: some View { let url = Bundle.main.url(forResource: "heatmap", withExtension: "plist") HeatMapView(startDate: Date(2019, 1, 20), endDate: Date(2020, 3, 25), heatMapData: NSDictionary(contentsOf: url!) as? [String: Int]) .environment(.colorScheme, .dark) } }

class ViewController: UIViewController {

public var startDate: Date

public var endDate: Date

public var heatMapData: [String : Int]?

public init(startDate: Date, endDate: Date, heatMapData: [String : Int]?) {

self.startDate = startDate

self.endDate = endDate

self.heatMapData = heatMapData

super.init(nibName: nil, bundle: nil)

}

required init?(coder: NSCoder) {

fatalError("init(coder:) has not been implemented")

}

lazy var data: [String: UIColor] = {

guard let data = heatMapData else { return [:] }

return data.mapValues { (colorIndex) -> UIColor in

    switch colorIndex {

    case 0..<500:

        return UIColor(Color.accentColor.opacity(0.2))

    case 500..<2000:

        return UIColor(Color.accentColor.opacity(0.4))

    case 2000..<6000:

        return UIColor(Color.accentColor.opacity(0.6))

    case 6000..<8000:

        return UIColor(Color.accentColor.opacity(0.8))

    case 8000...:

        return UIColor(Color.accentColor)

    default:

        return UIColor(Color(.systemGray5).opacity(0.5))

    }

}

}()

lazy var calendarHeatMap: CalendarHeatmap = {

var config = CalendarHeatmapConfig()

config.backgroundColor = .systemBackground

// config item

config.allowItemSelection = false

config.itemSide = 15

config.contentRightInset = 10

// config month header

config.monthHeight = 30

config.monthStrings = ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"]

config.monthFont = UIFont.systemFont(ofSize: 11)

config.monthColor = UIColor(Color.primary.opacity(0.3))

// config weekday label on left

config.weekDayFont = UIFont.systemFont(ofSize: 10)

config.weekDayStrings = ["日", "一", " ", "三", " ", "五", " "]

config.weekDayStandard = .International

config.weekDayWidth = 20

config.weekDayColor = UIColor(Color.primary.opacity(0.3))

let calendar = CalendarHeatmap(config: config, startDate: startDate, endDate: endDate)

calendar.delegate = self

return calendar

}()

override func viewDidLoad() {

super.viewDidLoad()

view.backgroundColor = .systemBackground

view.addSubview(calendarHeatMap)

calendarHeatMap.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate([

    calendarHeatMap.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 0),

    calendarHeatMap.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 12),

    calendarHeatMap.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 5)

])

}

} ` Everything works great but when the heatmap data changed, the heatmap view doesn't update automatically - unless I close and reopen the page again. I've searched and tried everything I can, but none of those works. Could you please give me some advice on this, or there is already a solution for this but I'm using it in the wrong way?

Thanks!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/Zacharysp/CalendarHeatmap/issues/15, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACUCZYZZCLBSCWSTAXFREBDTQIYNTANCNFSM45ZKQU4Q .

telami commented 1 year ago

@AngusYahn Hi, Can you give me the code in the screenshot, very thankful