feat(calendar) improve MicroPython example
Small improvements:
Remove cast from get_pressed_date
Check return value of get_pressed_date
Call set_today_date on clicked date
Compact highlighted_days
Added a switch to show different header type
@@ -4,8 +4,8 @@ def event_handler(evt):
if code == lv.EVENT.VALUE_CHANGED:
source = evt.get_target()
date = lv.calendar_date_t()
- lv.calendar.get_pressed_date(source,date)
- if date:
+ if source.get_pressed_date(date) == lv.RES.OK:
+ calendar.set_today_date(date.year, date.month, date.day)
print("Clicked date: %02d.%02d.%02d"%(date.day, date.month, date.year))
@@ -18,25 +18,32 @@ calendar.set_today_date(2021, 02, 23)
calendar.set_showed_date(2021, 02)
# Highlight a few days
-highlighted_days=[]
-for i in range(3):
- highlighted_days.append(lv.calendar_date_t())
-
-highlighted_days[0].year=2021
-highlighted_days[0].month=02
-highlighted_days[0].day=6
-
-highlighted_days[1].year=2021
-highlighted_days[1].month=02
-highlighted_days[1].day=11
-
-highlighted_days[2].year=2022
-highlighted_days[2].month=02
-highlighted_days[2].day=22
-
-calendar.set_highlighted_dates(highlighted_days, 3)
-
-header = lv.calendar_header_dropdown(lv.scr_act(),calendar)
-# header = lv.calendar_header_arrow(lv.scr_act(),calendar,25)
-
-
+highlighted_days=[
+ lv.calendar_date_t({'year':2021, 'month':2, 'day':6}),
+ lv.calendar_date_t({'year':2021, 'month':2, 'day':11}),
+ lv.calendar_date_t({'year':2021, 'month':2, 'day':22})
+]
+
+calendar.set_highlighted_dates(highlighted_days, len(highlighted_days))
+
+# 2 options for header
+header1 = lv.calendar_header_dropdown(lv.scr_act(),calendar)
+header2 = lv.calendar_header_arrow(lv.scr_act(),calendar,25)
+
+# Switch to switch headeres
+header2.add_flag(lv.obj.FLAG.HIDDEN)
+header1.clear_flag(lv.obj.FLAG.HIDDEN)
+
+sw = lv.switch(lv.scr_act())
+sw.set_pos(20,20)
+
+def sw_cb(e):
+ obj = e.get_target()
+ if obj.has_state(lv.STATE.CHECKED):
+ header1.add_flag(lv.obj.FLAG.HIDDEN)
+ header2.clear_flag(lv.obj.FLAG.HIDDEN)
+ else:
+ header2.add_flag(lv.obj.FLAG.HIDDEN)
+ header1.clear_flag(lv.obj.FLAG.HIDDEN)
+
+sw.add_event_cb(sw_cb, lv.EVENT.VALUE_CHANGED, None)
