1 /**
2  * @file lv_demo_smartwatch_notifications.c
3  * Notifications screen layout & functions. Contains the list of available notifications
4  * as well as each individual notification when opened.
5  */
6 
7 /*********************
8  *      INCLUDES
9  *********************/
10 #include "lv_demo_smartwatch.h"
11 #if LV_USE_DEMO_SMARTWATCH
12 
13 #include "lv_demo_smartwatch_private.h"
14 #include "lv_demo_smartwatch_notifications.h"
15 
16 /*********************
17  *      DEFINES
18  *********************/
19 
20 /**********************
21  *      TYPEDEFS
22  **********************/
23 
24 /**********************
25  *  STATIC PROTOTYPES
26  **********************/
27 static void create_screen_notifications(void);
28 static void notification_screen_events_cb(lv_event_t * e);
29 static void notification_clicked_event_cb(lv_event_t * e);
30 
31 static void set_notification_icon(lv_obj_t * obj, int app_id);
32 static int get_notification_icon_index(int id);
33 
34 static lv_smartwatch_notification_click_cb_t notification_click_cb;
35 
36 /**********************
37  *  STATIC VARIABLES
38  **********************/
39 static lv_obj_t * notification_screen;
40 static lv_obj_t * message_info_panel;
41 static lv_obj_t * message_panel;
42 static lv_obj_t * message_icon;
43 static lv_obj_t * message_time;
44 static lv_obj_t * message_content;
45 static lv_obj_t * message_list_panel;
46 static lv_obj_t * message_list;
47 static lv_obj_t * empty_info;
48 
49 static const lv_image_dsc_t * notification_icons[] = {
50     &img_sms_icon,       /* SMS */
51     &img_mail_icon,      /* Mail */
52     &img_penguin_icon,   /* Penguin (QQ) */
53     &img_skype_icon,     /* Skype */
54     &img_whatsapp_icon,  /* WhatsApp */
55     &img_mail_icon,      /* Mail */
56     &img_line_icon,      /* Line */
57     &img_twitter_x_icon, /* Twitter */
58     &img_facebook_icon,  /* Facebook */
59     &img_messenger_icon, /* Messenger */
60     &img_instagram_icon, /* Instagram */
61     &img_weibo_icon,     /* Weibo */
62     &img_kakao_icon,     /* Kakao */
63     &img_viber_icon,     /* Viber */
64     &img_vkontakte_icon, /* Vkontakte */
65     &img_telegram_icon,  /* Telegram */
66     &img_chrns_icon,     /* Chronos */
67     &img_wechat_icon     /* Wechat */
68 };
69 
70 /**********************
71  *      MACROS
72  **********************/
73 
74 /**********************
75  *   GLOBAL FUNCTIONS
76  **********************/
77 
lv_demo_smartwatch_notifications_create(void)78 void lv_demo_smartwatch_notifications_create(void)
79 {
80     create_screen_notifications();
81 
82 
83     /* add demo notifications */
84     lv_demo_smartwatch_add_notification(0x03, "Sample Message test", 0x03);
85     lv_demo_smartwatch_add_notification(0x0B, "Sample Message test", 0x0B);
86     lv_demo_smartwatch_add_notification(0x08, "Sample Message test", 0x08);
87     lv_demo_smartwatch_add_notification(0x14, "Sample Message test", 0x14);
88     lv_demo_smartwatch_add_notification(0x0A, "Sample Message test", 0x0A);
89     lv_demo_smartwatch_add_notification(0x12, "Sample Message test", 0x12);
90     lv_demo_smartwatch_add_notification(0x16, "Sample Message test", 0x16);
91     lv_demo_smartwatch_add_notification(0x17, "Sample Message test", 0x17);
92 }
93 
lv_demo_smartwatch_notifications_load(lv_screen_load_anim_t anim_type,uint32_t time,uint32_t delay)94 void lv_demo_smartwatch_notifications_load(lv_screen_load_anim_t anim_type, uint32_t time, uint32_t delay)
95 {
96     lv_screen_load_anim(notification_screen, anim_type, time, delay, false);
97 }
98 
lv_demo_smartwatch_clear_notifications(void)99 void lv_demo_smartwatch_clear_notifications(void)
100 {
101     lv_obj_clean(message_list);
102     lv_obj_remove_flag(empty_info, LV_OBJ_FLAG_HIDDEN);
103 }
104 
lv_demo_smartwatch_add_notification(int app_id,const char * message,int index)105 void lv_demo_smartwatch_add_notification(int app_id, const char * message, int index)
106 {
107     lv_obj_t * notification_item = lv_obj_create(message_list);
108     lv_obj_set_width(notification_item, lv_pct(90));
109     lv_obj_set_height(notification_item, LV_SIZE_CONTENT);
110     lv_obj_set_align(notification_item, LV_ALIGN_CENTER);
111     lv_obj_set_flex_flow(notification_item, LV_FLEX_FLOW_ROW);
112     lv_obj_set_flex_align(notification_item, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_START);
113     lv_obj_remove_flag(notification_item, LV_OBJ_FLAG_SCROLLABLE);
114     lv_obj_set_style_radius(notification_item, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
115     lv_obj_set_style_bg_color(notification_item, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT);
116     lv_obj_set_style_bg_opa(notification_item, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
117     lv_obj_set_style_border_color(notification_item, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
118     lv_obj_set_style_border_opa(notification_item, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
119     lv_obj_set_style_border_width(notification_item, 1, LV_PART_MAIN | LV_STATE_DEFAULT);
120     lv_obj_set_style_border_side(notification_item, LV_BORDER_SIDE_BOTTOM, LV_PART_MAIN | LV_STATE_DEFAULT);
121     lv_obj_set_style_pad_left(notification_item, 5, LV_PART_MAIN | LV_STATE_DEFAULT);
122     lv_obj_set_style_pad_right(notification_item, 5, LV_PART_MAIN | LV_STATE_DEFAULT);
123     lv_obj_set_style_pad_top(notification_item, 5, LV_PART_MAIN | LV_STATE_DEFAULT);
124     lv_obj_set_style_pad_bottom(notification_item, 5, LV_PART_MAIN | LV_STATE_DEFAULT);
125 
126     lv_obj_t * notification_icon = lv_image_create(notification_item);
127     lv_image_set_src(notification_icon, &img_chrns_icon);
128     lv_obj_set_width(notification_icon, LV_SIZE_CONTENT);
129     lv_obj_set_height(notification_icon, LV_SIZE_CONTENT);
130     lv_obj_set_align(notification_icon, LV_ALIGN_CENTER);
131     lv_obj_add_flag(notification_icon, LV_OBJ_FLAG_ADV_HITTEST);
132     lv_obj_remove_flag(notification_icon, LV_OBJ_FLAG_SCROLLABLE);
133     set_notification_icon(notification_icon, app_id);
134 
135     lv_obj_t * notification_text = lv_label_create(notification_item);
136     lv_obj_set_width(notification_text, 140);
137     lv_obj_set_height(notification_text, LV_SIZE_CONTENT);
138     lv_obj_set_align(notification_text, LV_ALIGN_CENTER);
139     lv_label_set_long_mode(notification_text, LV_LABEL_LONG_DOT);
140     lv_label_set_text(notification_text, message);
141 
142     lv_obj_add_flag(empty_info, LV_OBJ_FLAG_HIDDEN);
143 
144     lv_obj_add_event_cb(notification_item, notification_clicked_event_cb, LV_EVENT_CLICKED, (void *)(intptr_t)index);
145 }
146 
lv_demo_smartwatch_show_notification(int app_id,const char * message,const char * time)147 void lv_demo_smartwatch_show_notification(int app_id, const char * message, const char * time)
148 {
149     lv_label_set_text(message_time, time);
150     lv_label_set_text(message_content, message);
151     set_notification_icon(message_icon, app_id);
152     lv_obj_scroll_to_y(message_panel, 0, LV_ANIM_ON);
153 
154     lv_tileview_set_tile_by_index(notification_screen, 1, 0, LV_ANIM_ON);
155 }
156 
lv_demo_smartwatch_set_notification_click_cb(lv_smartwatch_notification_click_cb_t cb)157 void lv_demo_smartwatch_set_notification_click_cb(lv_smartwatch_notification_click_cb_t cb)
158 {
159     notification_click_cb = cb;
160 }
161 
162 /**********************
163  *   STATIC FUNCTIONS
164  **********************/
165 
get_notification_icon_index(int id)166 static int get_notification_icon_index(int id)
167 {
168     switch(id) {
169         case 0x03:
170             return 0;
171         case 0x04:
172             return 1;
173         case 0x07:
174             return 2;
175         case 0x08:
176             return 3;
177         case 0x0A:
178             return 4;
179         case 0x0B:
180             return 5;
181         case 0x0E:
182             return 6;
183         case 0x0F:
184             return 7;
185         case 0x10:
186             return 8;
187         case 0x11:
188             return 9;
189         case 0x12:
190             return 10;
191         case 0x13:
192             return 11;
193         case 0x14:
194             return 12;
195         case 0x16:
196             return 13;
197         case 0x17:
198             return 14;
199         case 0x18:
200             return 15;
201         case 0xC0:
202             return 16;
203         case 0x09:
204             return 17;
205         default:
206             return 0;
207     }
208 }
209 
set_notification_icon(lv_obj_t * obj,int app_id)210 static void set_notification_icon(lv_obj_t * obj, int app_id)
211 {
212     lv_image_set_src(obj, notification_icons[get_notification_icon_index(app_id)]);
213 }
214 
notification_clicked_event_cb(lv_event_t * e)215 static void notification_clicked_event_cb(lv_event_t * e)
216 {
217     intptr_t index = (intptr_t)lv_event_get_user_data(e);
218 
219     /* send clicked action to the user callback function if defined, otherwise load a test message */
220     if(notification_click_cb != NULL) {
221         notification_click_cb(index);
222     }
223     else {
224         /* test notification */
225         lv_demo_smartwatch_show_notification(index, "Sample Message test", "12:45");
226     }
227 }
228 
notification_screen_events_cb(lv_event_t * e)229 static void notification_screen_events_cb(lv_event_t * e)
230 {
231     lv_event_code_t event_code = lv_event_get_code(e);
232 
233     if(event_code == LV_EVENT_SCREEN_LOAD_START) {
234         lv_tileview_set_tile_by_index(notification_screen, 0, 0, LV_ANIM_OFF);
235 
236         lv_obj_scroll_by(message_list, 0, 1, LV_ANIM_OFF);
237         lv_obj_scroll_by(message_list, 0, -1, LV_ANIM_OFF);
238 
239         lv_obj_set_scrollbar_mode(notification_screen, lv_demo_smartwatch_get_scrollbar_mode());
240         lv_obj_set_scrollbar_mode(message_panel, lv_demo_smartwatch_get_scrollbar_mode());
241         lv_obj_set_scrollbar_mode(message_list, lv_demo_smartwatch_get_scrollbar_mode());
242 
243         if(lv_demo_smartwatch_get_load_app_list()) {
244             lv_demo_smartwatch_show_scroll_hint(LV_DIR_LEFT);
245         }
246         else {
247             lv_demo_smartwatch_show_scroll_hint(LV_DIR_RIGHT);
248         }
249 
250     }
251 
252     if(event_code == LV_EVENT_VALUE_CHANGED) {
253         if(lv_tileview_get_tile_active(notification_screen) == message_list_panel) {
254 
255             if(lv_demo_smartwatch_get_load_app_list()) {
256                 lv_demo_smartwatch_show_scroll_hint(LV_DIR_LEFT);
257             }
258             else {
259                 lv_demo_smartwatch_show_scroll_hint(LV_DIR_RIGHT);
260             }
261         }
262         else if(lv_tileview_get_tile_active(notification_screen) == message_info_panel) {
263             if(lv_demo_smartwatch_get_load_app_list()) {
264                 lv_demo_smartwatch_show_scroll_hint(LV_DIR_LEFT);
265             }
266             else {
267                 lv_demo_smartwatch_show_scroll_hint(LV_DIR_RIGHT | LV_DIR_LEFT);
268             }
269         }
270     }
271 
272     if(event_code == LV_EVENT_GESTURE && lv_indev_get_gesture_dir(lv_indev_active()) == LV_DIR_LEFT) {
273         if(!lv_demo_smartwatch_get_load_app_list()) {
274             lv_demo_smartwatch_home_load(LV_SCR_LOAD_ANIM_OUT_LEFT, 500, 0);
275         }
276         else {
277             LV_LOG_WARN("Swipe right to exit");
278             lv_demo_smartwatch_show_scroll_hint(LV_DIR_LEFT);
279         }
280     }
281     if(event_code == LV_EVENT_GESTURE && lv_indev_get_gesture_dir(lv_indev_active()) == LV_DIR_RIGHT) {
282         if(lv_demo_smartwatch_get_load_app_list()) {
283             lv_demo_smartwatch_list_load(LV_SCR_LOAD_ANIM_MOVE_RIGHT, 500, 0);
284         }
285         else {
286             LV_LOG_WARN("Swipe left to exit");
287             lv_demo_smartwatch_show_scroll_hint(LV_DIR_RIGHT);
288         }
289     }
290 
291     if(event_code == LV_EVENT_GESTURE && (lv_indev_get_gesture_dir(lv_indev_active()) == LV_DIR_TOP ||
292                                           lv_indev_get_gesture_dir(lv_indev_active()) == LV_DIR_BOTTOM) &&
293        lv_tileview_get_tile_active(notification_screen) == message_list_panel) {
294         if(lv_demo_smartwatch_get_load_app_list()) {
295             LV_LOG_WARN("Swipe right to exit");
296             lv_demo_smartwatch_show_scroll_hint(LV_DIR_LEFT);
297         }
298         else {
299             LV_LOG_WARN("Swipe left to exit");
300             lv_demo_smartwatch_show_scroll_hint(LV_DIR_RIGHT);
301         }
302     }
303 
304 
305 }
306 
create_screen_notifications(void)307 static void create_screen_notifications(void)
308 {
309 
310     notification_screen = lv_tileview_create(NULL);
311     lv_obj_set_style_bg_color(notification_screen, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT);
312     lv_obj_set_style_bg_opa(notification_screen, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
313 
314     message_info_panel = lv_tileview_add_tile(notification_screen, 1, 0, LV_DIR_LEFT);
315     lv_obj_set_width(message_info_panel, lv_pct(100));
316     lv_obj_set_height(message_info_panel, lv_pct(100));
317 
318     message_panel = lv_obj_create(message_info_panel);
319     lv_obj_set_width(message_panel, lv_pct(100));
320     lv_obj_set_height(message_panel, lv_pct(100));
321     lv_obj_set_align(message_panel, LV_ALIGN_TOP_MID);
322     lv_obj_set_flex_flow(message_panel, LV_FLEX_FLOW_COLUMN);
323     lv_obj_set_flex_align(message_panel, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
324     lv_obj_set_scrollbar_mode(message_panel, LV_SCROLLBAR_MODE_OFF);
325     lv_obj_set_scroll_dir(message_panel, LV_DIR_VER);
326     lv_obj_set_style_radius(message_panel, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
327     lv_obj_set_style_bg_color(message_panel, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT);
328     lv_obj_set_style_bg_opa(message_panel, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
329     lv_obj_set_style_border_width(message_panel, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
330     lv_obj_set_style_pad_left(message_panel, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
331     lv_obj_set_style_pad_right(message_panel, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
332     lv_obj_set_style_pad_top(message_panel, 20, LV_PART_MAIN | LV_STATE_DEFAULT);
333     lv_obj_set_style_pad_bottom(message_panel, 70, LV_PART_MAIN | LV_STATE_DEFAULT);
334 
335     message_icon = lv_image_create(message_panel);
336     lv_image_set_src(message_icon, &img_chrns_icon);
337     lv_obj_set_width(message_icon, LV_SIZE_CONTENT);
338     lv_obj_set_height(message_icon, LV_SIZE_CONTENT);
339     lv_obj_set_align(message_icon, LV_ALIGN_CENTER);
340     lv_obj_add_flag(message_icon, LV_OBJ_FLAG_ADV_HITTEST);
341     lv_obj_remove_flag(message_icon, LV_OBJ_FLAG_SCROLLABLE);
342 
343     message_time = lv_label_create(message_panel);
344     lv_obj_set_width(message_time, LV_SIZE_CONTENT);
345     lv_obj_set_height(message_time, LV_SIZE_CONTENT);
346     lv_obj_set_align(message_time, LV_ALIGN_CENTER);
347     lv_label_set_text(message_time, "Chronos");
348 
349     message_content = lv_label_create(message_panel);
350     lv_obj_set_width(message_content, 180);
351     lv_obj_set_height(message_content, LV_SIZE_CONTENT);
352     lv_obj_set_align(message_content, LV_ALIGN_CENTER);
353     lv_label_set_text(message_content, "Download from Google Play to sync time and receive notifications");
354 
355     message_list_panel = lv_tileview_add_tile(notification_screen, 0, 0, LV_DIR_NONE);
356     lv_obj_set_width(message_list_panel, lv_pct(100));
357     lv_obj_set_height(message_list_panel, lv_pct(100));
358 
359     message_list = lv_obj_create(message_list_panel);
360 
361     lv_obj_set_width(message_list, lv_pct(100));
362     lv_obj_set_height(message_list, lv_pct(100));
363     lv_obj_set_align(message_list, LV_ALIGN_TOP_MID);
364     lv_obj_set_flex_flow(message_list, LV_FLEX_FLOW_COLUMN_REVERSE);
365     lv_obj_set_flex_align(message_list, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
366     lv_obj_set_scrollbar_mode(message_list, LV_SCROLLBAR_MODE_OFF);
367     lv_obj_set_scroll_dir(message_list, LV_DIR_VER);
368     lv_obj_set_style_radius(message_list, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
369     lv_obj_set_style_bg_color(message_list, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT);
370     lv_obj_set_style_bg_opa(message_list, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
371     lv_obj_set_style_border_width(message_list, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
372     lv_obj_set_style_pad_left(message_list, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
373     lv_obj_set_style_pad_right(message_list, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
374     lv_obj_set_style_pad_top(message_list, 50, LV_PART_MAIN | LV_STATE_DEFAULT);
375     lv_obj_set_style_pad_bottom(message_list, 70, LV_PART_MAIN | LV_STATE_DEFAULT);
376 
377     empty_info = lv_label_create(message_list_panel);
378     lv_obj_set_width(empty_info, 180);
379     lv_obj_set_height(empty_info, LV_SIZE_CONTENT);
380     lv_obj_set_align(empty_info, LV_ALIGN_CENTER);
381     lv_label_set_text(empty_info, "The are no notifications currently, come back later");
382     lv_obj_add_flag(empty_info, LV_OBJ_FLAG_HIDDEN);
383 
384     lv_obj_add_event_cb(message_list, lv_demo_smartwatch_scroll_event, LV_EVENT_ALL, NULL);
385 
386     lv_obj_add_event_cb(notification_screen, notification_screen_events_cb, LV_EVENT_ALL, NULL);
387 }
388 
389 #endif /*LV_USE_DEMO_SMARTWATCH*/
390