Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Adiras committed Aug 15, 2023
0 parents commit 4abc9c9
Show file tree
Hide file tree
Showing 18 changed files with 829 additions and 0 deletions.
13 changes: 13 additions & 0 deletions application.fam
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
App(
appid="calendar",
apptype=FlipperAppType.EXTERNAL,
name="Calendar",
entry_point="calendar_app",
stack_size=1 * 1024,
fap_icon="icon.png",
order=1000,
requires=[
"gui",
"dolphin",
]
)
119 changes: 119 additions & 0 deletions calendar.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#include "calendar.h"
#include <furi.h>
#include <core/check.h>
#include <core/record.h>
#include <core/log.h>
#include <core/log.h>
#include <furi_hal_rtc.h>

static bool calendar_app_custom_event_callback(void* context, uint32_t event) {
furi_assert(context);
CalendarApp* app = context;
return scene_manager_handle_custom_event(app->scene_manager, event);
}

static bool calendar_app_back_event_callback(void* context) {
furi_assert(context);
CalendarApp* app = context;
return scene_manager_handle_back_event(app->scene_manager);
}

VariableSharedContext* calendar_app_variable_shared_context_alloc() {
FuriHalRtcDateTime datetime;
furi_hal_rtc_get_datetime(&datetime);
VariableSharedContext* variable_shared_context = malloc(sizeof(VariableSharedContext));
variable_shared_context->year_selected = datetime.year;
variable_shared_context->month_selected = datetime.month;

return variable_shared_context;
}

CalendarApp* calendar_app_alloc() {
CalendarApp* app = malloc(sizeof(CalendarApp));

// Variable shared context
app->variable_shared_context = calendar_app_variable_shared_context_alloc();

// View dispatcher
app->view_dispatcher = view_dispatcher_alloc();

// Scene manager
app->scene_manager = scene_manager_alloc(&calendar_scene_handlers, app);

view_dispatcher_enable_queue(app->view_dispatcher);
view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
view_dispatcher_set_custom_event_callback(
app->view_dispatcher, calendar_app_custom_event_callback);
view_dispatcher_set_navigation_event_callback(
app->view_dispatcher, calendar_app_back_event_callback);

// Open GUI record
app->gui = furi_record_open(RECORD_GUI);
view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);

// Views
app->calendar_year_picker = calendar_year_picker_alloc(
app->variable_shared_context);
view_dispatcher_add_view(
app->view_dispatcher,
CalendarAppViewYearPicker,
calendar_year_picker_get_view(app->calendar_year_picker));

app->calendar_month_picker = calendar_month_picker_alloc(
app->variable_shared_context);
view_dispatcher_add_view(
app->view_dispatcher,
CalendarAppViewMonthPicker,
calendar_month_picker_get_view(app->calendar_month_picker));

app->calendar_month_browser = calendar_month_browser_alloc(
app->variable_shared_context);
view_dispatcher_add_view(
app->view_dispatcher,
CalendarAppViewMonthBrowser,
calendar_month_browser_get_view(app->calendar_month_browser));

scene_manager_next_scene(app->scene_manager, CalendarSceneYearPicker);

return app;
}

void calendar_app_free(CalendarApp* app) {
furi_assert(app);

// Views
view_dispatcher_remove_view(
app->view_dispatcher, CalendarAppViewYearPicker);
calendar_year_picker_free(app->calendar_year_picker);

view_dispatcher_remove_view(
app->view_dispatcher, CalendarAppViewMonthPicker);
calendar_month_picker_free(app->calendar_month_picker);

view_dispatcher_remove_view(
app->view_dispatcher, CalendarAppViewMonthBrowser);
calendar_month_browser_free(app->calendar_month_browser);

// View dispatcher
view_dispatcher_free(app->view_dispatcher);

// Scene manager
scene_manager_free(app->scene_manager);

// GUI
furi_record_close(RECORD_GUI);
app->gui = NULL;

free(app);
}

int32_t calendar_app(void* p) {
UNUSED(p);
CalendarApp* app = calendar_app_alloc();

view_dispatcher_run(app->view_dispatcher);

calendar_app_free(app);

return 0;
}
27 changes: 27 additions & 0 deletions calendar.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <gui/gui.h>
#include <gui/view.h>
#include <gui/view_dispatcher.h>
#include <gui/scene_manager.h>
#include "views/calendar_year_picker.h"
#include "views/calendar_month_picker.h"
#include "views/calendar_month_browser.h"
#include "scenes/calendar_scene.h"
#include "helpers/variable_shared_context.h"

typedef struct {
Gui* gui;
ViewDispatcher* view_dispatcher;
SceneManager* scene_manager;
YearPicker* calendar_year_picker;
MonthPicker* calendar_month_picker;
MonthBrowser* calendar_month_browser;
VariableSharedContext* variable_shared_context;
} CalendarApp;

typedef enum {
CalendarAppViewYearPicker,
CalendarAppViewMonthPicker,
CalendarAppViewMonthBrowser,
} CalendarAppView;
6 changes: 6 additions & 0 deletions helpers/calendar_event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

typedef enum {
CalendarAppCustomEventYearPicked,
CalendarAppCustomEventMontPicked,
} CalendarAppCustomEvent;
8 changes: 8 additions & 0 deletions helpers/variable_shared_context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <furi.h>

typedef struct {
int16_t year_selected;
int8_t month_selected;
} VariableSharedContext;
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions scenes/calendar_scene.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "calendar_scene.h"

// Generate scene on_enter handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_enter,
void (*const calendar_scene_on_enter_handlers[])(void*) = {
#include "calendar_scene_config.h"
};
#undef ADD_SCENE

// Generate scene on_event handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_event,
bool (*const calendar_scene_on_event_handlers[])(void* context, SceneManagerEvent event) = {
#include "calendar_scene_config.h"
};
#undef ADD_SCENE

// Generate scene on_exit handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_exit,
void (*const calendar_scene_on_exit_handlers[])(void* context) = {
#include "calendar_scene_config.h"
};
#undef ADD_SCENE

// Initialize scene handlers configuration structure
const SceneManagerHandlers calendar_scene_handlers = {
.on_enter_handlers = calendar_scene_on_enter_handlers,
.on_event_handlers = calendar_scene_on_event_handlers,
.on_exit_handlers = calendar_scene_on_exit_handlers,
.scene_num = CalendarSceneNum,
};
27 changes: 27 additions & 0 deletions scenes/calendar_scene.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <gui/scene_manager.h>

extern const SceneManagerHandlers calendar_scene_handlers;

// Generate scene id and total number
#define ADD_SCENE(prefix, name, id) CalendarScene##id,
typedef enum {
#include "calendar_scene_config.h"
CalendarSceneNum,
} CalendarScene;
#undef ADD_SCENE

// Generate scene on_enter handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_enter(void*);
#include "calendar_scene_config.h"
#undef ADD_SCENE

// Generate scene on_event handlers declaration
#define ADD_SCENE(prefix, name, id) \
bool prefix##_scene_##name##_on_event(void* context, SceneManagerEvent event);
#include "calendar_scene_config.h"
#undef ADD_SCENE

// Generate scene on_exit handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_exit(void* context);
#include "calendar_scene_config.h"
#undef ADD_SCENE
3 changes: 3 additions & 0 deletions scenes/calendar_scene_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ADD_SCENE(calendar, year_picker, YearPicker)
ADD_SCENE(calendar, month_picker, MonthPicker)
ADD_SCENE(calendar, month_browser, MonthBrowser)
29 changes: 29 additions & 0 deletions scenes/calendar_scene_month_browser.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "../calendar.h"
#include <furi_hal.h>
#include <gui/scene_manager.h>

void calendar_scene_month_browser_callback(CalendarAppCustomEvent event, void* context) {
furi_assert(context);
CalendarApp* app = context;
view_dispatcher_send_custom_event(app->view_dispatcher, event);
}

bool calendar_scene_month_browser_on_event(void* context, SceneManagerEvent event) {
UNUSED(context);
UNUSED(event);

return false;
}

void calendar_scene_month_browser_on_enter(void* context) {
CalendarApp* app = context;

calendar_year_picker_set_callback(
app->calendar_year_picker, calendar_scene_month_browser_callback, app);

view_dispatcher_switch_to_view(app->view_dispatcher, CalendarAppViewMonthBrowser);
}

void calendar_scene_month_browser_on_exit(void* context) {
UNUSED(context);
}
37 changes: 37 additions & 0 deletions scenes/calendar_scene_month_picker.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "../calendar.h"
#include <furi_hal.h>
#include <gui/scene_manager.h>

void calendar_scene_month_picker_callback(CalendarAppCustomEvent event, void* context) {
furi_assert(context);
CalendarApp* app = context;
view_dispatcher_send_custom_event(app->view_dispatcher, event);
}

bool calendar_scene_month_picker_on_event(void* context, SceneManagerEvent event) {
CalendarApp* app = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
switch(event.event) {
case CalendarAppCustomEventMontPicked:
scene_manager_next_scene(app->scene_manager, CalendarSceneMonthBrowser);
consumed = true;
break;
}
}

return consumed;
}

void calendar_scene_month_picker_on_enter(void* context) {
CalendarApp* app = context;

calendar_month_picker_set_callback(
app->calendar_month_picker, calendar_scene_month_picker_callback, app);

view_dispatcher_switch_to_view(app->view_dispatcher, CalendarAppViewMonthPicker);
}

void calendar_scene_month_picker_on_exit(void* context) {
UNUSED(context);
}
37 changes: 37 additions & 0 deletions scenes/calendar_scene_year_picker.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "../calendar.h"
#include <furi_hal.h>
#include <gui/scene_manager.h>

void calendar_scene_year_picker_callback(CalendarAppCustomEvent event, void* context) {
furi_assert(context);
CalendarApp* app = context;
view_dispatcher_send_custom_event(app->view_dispatcher, event);
}

bool calendar_scene_year_picker_on_event(void* context, SceneManagerEvent event) {
CalendarApp* app = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
switch(event.event) {
case CalendarAppCustomEventYearPicked:
scene_manager_next_scene(app->scene_manager, CalendarSceneMonthPicker);
consumed = true;
break;
}
}

return consumed;
}

void calendar_scene_year_picker_on_enter(void* context) {
CalendarApp* app = context;

calendar_year_picker_set_callback(
app->calendar_year_picker, calendar_scene_year_picker_callback, app);

view_dispatcher_switch_to_view(app->view_dispatcher, CalendarAppViewYearPicker);
}

void calendar_scene_year_picker_on_exit(void* context) {
UNUSED(context);
}
Loading

0 comments on commit 4abc9c9

Please sign in to comment.