Skip to content

Commit

Permalink
perf: carousel animation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mairon1206 committed Sep 25, 2024
1 parent 912fdf0 commit 25ba0df
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 42 deletions.
32 changes: 10 additions & 22 deletions src/Controller/View/DiscoveryPage.cc
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#include "Controller/AsyncExecutor.hh"
#include "Controller/Convert.h"
#include "Controller/Core/ViewManager.h"
#include "Controller/UiBridge.h"
#include "Infrastructure/Network/NetworkClient.h"
#include <Controller/AsyncExecutor.hh>
#include <Controller/Convert.h>
#include <Controller/Core/ViewManager.h>
#include <Controller/UiBridge.h>
#include <Controller/View/DiscoveryPage.h>
#include <Infrastructure/Network/NetworkClient.h>
#include <Infrastructure/Network/ResponseStruct.h>
#include <chrono>
#include <spdlog/spdlog.h>

EVENTO_UI_START
Expand All @@ -18,6 +17,7 @@ void DiscoveryPage::onCreate() {
auto& self = *this;
self->on_load_active_events([this] { loadActiveEvents(); });
self->on_load_latest_events([this] { loadLatestEvents(); });
self->on_image_manually_changed([this] { timer.restart(); });
self->on_navigate_to_detail([this](EventStruct eventStruct) {
spdlog::debug("navigate to DetailPage, current event is {}", eventStruct.summary.data());
bridge.getViewManager().navigateTo(ViewName::DetailPage, eventStruct);
Expand Down Expand Up @@ -97,22 +97,10 @@ void DiscoveryPage::loadHomeSlides() {
}

void DiscoveryPage::slidesAutoRotation() {
executor()->asyncExecute([]() -> Task<void> { co_return; },
[&self = *this]() {
int cntIndex = self->get_image_index();
bool manuallyChanged = self->get_image_manually_changed();
if (!manuallyChanged) {
if (cntIndex == self->get_carousel_source()->row_count() - 1) {
self->set_image_index(0);
return;
}
self->set_image_index(cntIndex + 1);
} else {
self->set_image_manually_changed(false);
}
},
std::chrono::milliseconds(5000),
AsyncExecutor::Periodic | AsyncExecutor::Delay);
timer.start(slint::TimerMode::Repeated, 5s, [&self = *this] {
self->set_image_index((self->get_image_index() + 1)
% static_cast<int>(self->get_carousel_source()->row_count()));
});
}

EVENTO_UI_END
1 change: 1 addition & 0 deletions src/Controller/View/DiscoveryPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class DiscoveryPage : public BasicView, private GlobalAgent<DiscoveryPageBridge>
DiscoveryPage(slint::ComponentHandle<UiEntryName> uiEntry, UiBridge& bridge);

private:
slint::Timer timer;
void onCreate() override;
void onShow() override;

Expand Down
26 changes: 6 additions & 20 deletions ui/views/page/discovery.slint
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
import { Page } from "../../components/index.slint";
import { EventCardGroup } from "../../components/event_card.slint";
import { EventStruct } from "../../components/event_struct.slint";
import { SCarousel } from "../../modules/surrealism-ui/index.slint";
import { EventoImageToken } from "../../assets/image/image_token.slint";
import { Token } from "../../global.slint";
import { ScrollView } from "std-widgets.slint";
import { Button, ButtonType, LoadingAnimation, PageState, Empty } from "../../components/index.slint";
import { Button, ButtonType, LoadingAnimation, Page, PageState, Empty, EventStruct, EventCardGroup } from "../../components/index.slint";

export global DiscoveryPageBridge {
in-out property <int> image-index: 0;
in-out property <bool> image-manually-changed: false;
callback image-manually-changed;
in property <PageState> active-events-state: PageState.normal;
in property <PageState> latest-events-state: PageState.normal;
in property <[EventStruct]> active-events: [];
in property <[EventStruct]> latest-events: [];
in property <[image]> carousel-source: [
EventoImageToken.image.display.event.default[0],
EventoImageToken.image.display.event.default[1],
EventoImageToken.image.display.event.default[2],
Token.image.display.event.default[0],
Token.image.display.event.default[1],
Token.image.display.event.default[2],
];
callback load-active-events();
callback load-latest-events();
Expand All @@ -27,7 +22,6 @@ export global DiscoveryPageBridge {
export component ImageCarousel inherits Rectangle {
in property <[image]> sources;
in-out property <int> current-index: 0;
in-out property <bool> image-manually-changed: false;
border-radius: 12px;
clip: true;

Expand Down Expand Up @@ -68,19 +62,12 @@ export component ImageCarousel inherits Rectangle {
height: 100%;
clicked => {
current-index = index;
image-manually-changed = true;
DiscoveryPageBridge.image-manually-changed();
}
}
}
}
}

// ensure image-switching animation works fine
LoadingAnimation {
z: -1;
color: Token.color.on-surface;
height: 40px;
}
}

export component DiscoveryPage inherits Page {
Expand All @@ -93,7 +80,6 @@ export component DiscoveryPage inherits Page {
width: 100%;
height: root.height * 0.5;
current-index <=> DiscoveryPageBridge.image-index;
image-manually-changed <=> DiscoveryPageBridge.image-manually-changed;
sources: DiscoveryPageBridge.carousel-source;
}

Expand Down

0 comments on commit 25ba0df

Please sign in to comment.