From a9f23fc278210626a4f240f50391e281297ebf73 Mon Sep 17 00:00:00 2001 From: Florian Stinglmayr Date: Fri, 19 Jul 2019 21:32:22 +0200 Subject: [PATCH] optimise tree view a tiny bit don't refresh all the time, wait for the data to be ready. waiting on the data is not a good idea either, but it is better than to continously refresh all the data --- libdc/include/dc/session.h | 6 ++++++ libdc/src/session.c | 13 +++++++++++++ ncdc/include/ncdc/treeview.h | 1 + ncdc/src/login.c | 11 ++++++++++- ncdc/src/logout.c | 2 ++ ncdc/src/mainwindow.c | 1 - ncdc/src/treeview.c | 28 +++++++++++++++++++++------- 7 files changed, 53 insertions(+), 9 deletions(-) diff --git a/libdc/include/dc/session.h b/libdc/include/dc/session.h index 943f045..00cba59 100644 --- a/libdc/include/dc/session.h +++ b/libdc/include/dc/session.h @@ -45,6 +45,12 @@ bool dc_session_logout(dc_session_t s); */ bool dc_session_login(dc_session_t s, dc_account_t login); +/** + * Returns true if the session is ready, i.e. a login has been performed + * and the READY event has been parsed from the websocket. + */ +bool dc_session_is_ready(dc_session_t s); + bool dc_session_has_token(dc_session_t s); /** diff --git a/libdc/src/session.c b/libdc/src/session.c index ebd556b..3e507d5 100644 --- a/libdc/src/session.c +++ b/libdc/src/session.c @@ -9,6 +9,7 @@ struct dc_session_ dc_api_t api; dc_account_t login; dc_gateway_t gateway; + bool ready; GHashTable *accounts; GHashTable *channels; @@ -161,6 +162,8 @@ static void dc_session_handle_ready(dc_session_t s, dc_event_t e) dc_session_add_channel(s, chan); } } + + s->ready = true; } static void dc_session_handler(dc_gateway_t gw, dc_event_t e, void *p) @@ -237,6 +240,8 @@ bool dc_session_logout(dc_session_t s) s->gateway = NULL; } + s->ready = false; + return true; } @@ -248,6 +253,8 @@ bool dc_session_login(dc_session_t s, dc_account_t login) dc_session_logout(s); } + s->ready = false; + s->login = dc_ref(login); if (!dc_account_has_token(login)) { if (!dc_api_authenticate(s->api, s->login)) { @@ -276,6 +283,12 @@ bool dc_session_has_token(dc_session_t s) return dc_account_has_token(s->login); } +bool dc_session_is_ready(dc_session_t s) +{ + return_if_true(s == NULL, false); + return s->ready; +} + dc_api_t dc_session_api(dc_session_t s) { return_if_true(s == NULL, NULL); diff --git a/ncdc/include/ncdc/treeview.h b/ncdc/include/ncdc/treeview.h index 116098c..0424f57 100644 --- a/ncdc/include/ncdc/treeview.h +++ b/ncdc/include/ncdc/treeview.h @@ -17,6 +17,7 @@ void ncdc_treeitem_set_label(ncdc_treeitem_t i, wchar_t const *s); void *ncdc_treeitem_tag(ncdc_treeitem_t i); void ncdc_treeitem_set_tag(ncdc_treeitem_t i, void *t); +size_t ncdc_treeitem_size(ncdc_treeitem_t i); void ncdc_treeitem_clear(ncdc_treeitem_t i); void ncdc_treeitem_add(ncdc_treeitem_t i, ncdc_treeitem_t c); void ncdc_treeitem_remove(ncdc_treeitem_t i, ncdc_treeitem_t c); diff --git a/ncdc/src/login.c b/ncdc/src/login.c index 64000bd..e162d70 100644 --- a/ncdc/src/login.c +++ b/ncdc/src/login.c @@ -50,7 +50,16 @@ bool ncdc_cmd_login(ncdc_mainwindow_t n, size_t ac, dc_unref(current_session); current_session = dc_ref(s); - LOG(n, L"login: %ls: authentication successful, waiting for data...", av[1]); + LOG(n, L"login: %ls: authentication successful, waiting for ready from websocket...", + av[1] + ); + + while (!dc_session_is_ready(current_session)) + ; + + LOG(n, L"login: %ls: ready", av[1]); + ncdc_mainwindow_update_guilds(n); + ret = true; cleanup: diff --git a/ncdc/src/logout.c b/ncdc/src/logout.c index ffd831c..c1afc60 100644 --- a/ncdc/src/logout.c +++ b/ncdc/src/logout.c @@ -21,6 +21,8 @@ bool ncdc_cmd_logout(ncdc_mainwindow_t n, size_t ac, dc_unref(current_session); current_session = NULL; + ncdc_mainwindow_update_guilds(n); + LOG(n, L"logout: successfully logged out"); error: diff --git a/ncdc/src/mainwindow.c b/ncdc/src/mainwindow.c index cb27156..6729c3a 100644 --- a/ncdc/src/mainwindow.c +++ b/ncdc/src/mainwindow.c @@ -397,7 +397,6 @@ void ncdc_mainwindow_refresh(ncdc_mainwindow_t n) { ncdc_textview_t v = 0; - ncdc_mainwindow_update_guilds(n); ncdc_treeview_render(n->guildview, n->guilds, n->guilds_h, n->guilds_w); wnoutrefresh(n->guilds); diff --git a/ncdc/src/treeview.c b/ncdc/src/treeview.c index 22002a2..d93312a 100644 --- a/ncdc/src/treeview.c +++ b/ncdc/src/treeview.c @@ -22,6 +22,19 @@ struct ncdc_treeitem_ void *tag; }; +struct ncdc_treeview_ +{ + dc_refable_t ref; + + /* root element + */ + ncdc_treeitem_t root; + + /* currently selected item + */ + ncdc_treeitem_t current; +}; + static void ncdc_treeitem_free(ncdc_treeitem_t t) { return_if_true(t == NULL,); @@ -63,6 +76,12 @@ ncdc_treeitem_t ncdc_treeitem_new_string(wchar_t const *s) return t; } +size_t ncdc_treeitem_size(ncdc_treeitem_t i) +{ + return_if_true(i == NULL || i->children == NULL, 0); + return i->children->len; +} + void ncdc_treeitem_add(ncdc_treeitem_t i, ncdc_treeitem_t c) { return_if_true(i == NULL || c == NULL,); @@ -132,13 +151,6 @@ ncdc_treeitem_render(ncdc_treeitem_t t, WINDOW *win, return off; } -struct ncdc_treeview_ -{ - dc_refable_t ref; - - ncdc_treeitem_t root; -}; - static void ncdc_treeview_free(ncdc_treeview_t t) { return_if_true(t == NULL,); @@ -159,6 +171,8 @@ ncdc_treeview_t ncdc_treeview_new(void) t->root = ncdc_treeitem_new(); goto_if_true(t->root == NULL, error); + t->current = t->root; + return dc_ref(t); error: