-
Notifications
You must be signed in to change notification settings - Fork 1
/
view.h
223 lines (179 loc) · 5.97 KB
/
view.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#pragma once
#include <gui/gui.h>
#include <gui/modules/submenu.h>
#include <gui/modules/text_input.h>
#include <gui/modules/widget.h>
#include <gui/scene_manager.h>
#include <gui/view_dispatcher.h>
#ifndef MAX_NUMBER_OF_NOTES
#define MAX_NUMBER_OF_NOTES 10
#define MAX_LENGTH_OF_NOTE 30
#endif
struct App {
SceneManager *scene_manager;
ViewDispatcher *view_dispatcher;
Submenu *submenu;
Widget *widget;
TextInput *text_input;
char **notes;
uint8_t number_of_notes;
};
//struct App;
typedef enum {
Notes_Main_Menu,
Notes_TextInput,
Notes_List_of_Notes,
Notes_Scene_Count
} Scenes; // They are the "pages"
typedef enum {
Notes_Submenu_View,
Notes_Widget_View,
Notes_TextInput_View
} Views; // They are the type of pages
typedef enum {
Notes_Main_menu_Add_Item,
Notes_Main_menu_List_Item
} Submenu_Choices;
typedef enum{
Notes_Text_Input_Save_Event
} Notes_Input_Event;
void notes_main_menu_callback(void* context, uint32_t index){
struct App* app = context;
scene_manager_handle_custom_event( app->scene_manager, index );
/*
switch(index) {
case BasicScenesMainMenuSceneLottoNumbers:
scene_manager_handle_custom_event(
app->scene_manager,
BasicScenesMainMenuSceneLottoNumbersEvent);
break;
case BasicScenesMainMenuSceneGreeting:
scene_manager_handle_custom_event(
app->scene_manager,
BasicScenesMainMenuSceneGreetingEvent);
break;
}
*/
// it calls the function below
}
void notes_text_input_callback(void* context){
struct App *app = context;
scene_manager_handle_custom_event(app->scene_manager, Notes_Text_Input_Save_Event);
}
void note_main_menu_on_enter( void * context ){
struct App* app = context;
submenu_reset(app->submenu);
submenu_set_header(app->submenu, "~~~ Notes ~~~");
submenu_add_item(app->submenu, " Add new note ", Notes_Main_menu_Add_Item, notes_main_menu_callback, app);
submenu_add_item(app->submenu, " List your notes ", Notes_Main_menu_List_Item, notes_main_menu_callback, app);
view_dispatcher_switch_to_view(app->view_dispatcher, Notes_Submenu_View);
}
bool note_main_menu_on_event( void * context, SceneManagerEvent event ){
struct App* app = context;
switch( event.type ){
case SceneManagerEventTypeCustom:
switch (event.event){
case Notes_Main_menu_Add_Item:
scene_manager_next_scene( app->scene_manager, Notes_TextInput );
return true;
case Notes_Main_menu_List_Item:
scene_manager_next_scene( app->scene_manager, Notes_List_of_Notes);
return true;
}
break;
default:
return false;
}
return false;
}
void note_main_menu_on_leave( void * context ){
struct App* app = context;
submenu_reset(app->submenu);
}
void note_text_input_on_enter( void * context ){
struct App* app = context;
bool clear_text = true;
text_input_reset(app->text_input);
text_input_set_header_text(app->text_input, "Write down here your note");
text_input_set_result_callback(
app->text_input,
notes_text_input_callback,
app,
app->notes[ app->number_of_notes ],
MAX_LENGTH_OF_NOTE,
clear_text
);
app->number_of_notes++;
view_dispatcher_switch_to_view(app->view_dispatcher, Notes_TextInput_View);
}
bool note_text_input_on_event( void * context, SceneManagerEvent event ){
struct App * app = context;
if(event.type == SceneManagerEventTypeCustom && event.event == Notes_Text_Input_Save_Event){
scene_manager_next_scene(app->scene_manager, Notes_Main_Menu);
return true;
}
return false;
}
void note_text_input_on_leave( void * context ){
UNUSED(context);
}
void note_list_of_notes_on_enter( void * context ){
struct App* app = context;
widget_reset(app->widget);
for (int i = 0; i < MAX_NUMBER_OF_NOTES; i++){
if (app->notes[i] == NULL || app->notes[i][0] == '\0'){
continue;
}
FuriString* message = furi_string_alloc();
furi_string_printf(message, "[ #%d ]%s\n", i,app->notes[i]);
widget_add_string_multiline_element(
app->widget, 5 , 5 + (i * 13), AlignLeft,AlignCenter, FontPrimary, furi_string_get_cstr( message )
);
furi_string_free(message);
}
view_dispatcher_switch_to_view(app->view_dispatcher, Notes_Widget_View);
}
bool note_list_of_notes_on_event( void * context, SceneManagerEvent event ){
UNUSED(context);
UNUSED(event);
return 1;
}
void note_list_of_notes_on_leave( void * context ){
struct App* app = context;
widget_reset(app->widget);
}
void (* const notes_scene_on_enter_handlers[])(void*) = {
note_main_menu_on_enter,
note_text_input_on_enter,
note_list_of_notes_on_enter
};
bool (* const notes_scene_on_event_handlers[])(void*, SceneManagerEvent) = {
note_main_menu_on_event,
note_text_input_on_event,
note_list_of_notes_on_event
};
void (* const notes_scene_on_leave_handlers[])(void*) = {
note_main_menu_on_leave,
note_text_input_on_leave,
note_list_of_notes_on_leave
};
/*
custom event => enum
*/
bool notes_custom_callback( void *context, uint32_t custom_event ){
furi_assert(context);
struct App* app = context;
return scene_manager_handle_custom_event(app->scene_manager, custom_event);
}
// Handles the back button
bool notes_back_button_handler( void* context ){
furi_assert(context);
struct App *app = context;
return scene_manager_handle_back_event(app->scene_manager);
}
static const SceneManagerHandlers notes_scene_manager_handlers = {
.on_enter_handlers = notes_scene_on_enter_handlers,
.on_event_handlers = notes_scene_on_event_handlers,
.on_exit_handlers = notes_scene_on_leave_handlers,
.scene_num = Notes_Scene_Count
};