Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ui): event card #38

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions src/Controller/Convert.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#pragma once

#include <Infrastructure/Network/ResponseStruct.h>
#include <app.h>

Check failure on line 4 in src/Controller/Convert.hh

View workflow job for this annotation

GitHub Actions / review

'app.h' file not found [clang-diagnostic-error]
#include <memory>
#include <spdlog/spdlog.h>
#include <string>
#include <vector>

namespace evento::convert {

namespace details {

slint::SharedString convertTimeRange(const std::string& startTimeStr,
const std::string& endTimeStr) {
std::istringstream ssStart(startTimeStr);
std::istringstream ssEnd(endTimeStr);
std::chrono::sys_seconds startTp, endTp;

ssStart >> std::chrono::parse("%Y-%m-%dT%H:%M:%SZ", startTp);
if (ssStart.fail()) {
spdlog::warn("Failed to parse start-time string: {}", startTimeStr);
return " ";
}
ssEnd >> std::chrono::parse("%Y-%m-%dT%H:%M:%SZ", endTp);
if (ssEnd.fail()) {
spdlog::warn("Failed to parse end-time string: {}", endTimeStr);
return " ";
}

auto startTimer = std::chrono::system_clock::to_time_t(startTp);
auto startDate = *std::gmtime(&startTimer);

auto endTimer = std::chrono::system_clock::to_time_t(endTp);
auto endDate = *std::gmtime(&endTimer);
Comment on lines +31 to +35
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需要考虑时区问题,服务器传过来的是 UTC 时间,需要根据系统时区设置转换为本地时间


std::string startStr = std::format("{:02}.{:02} {:02}:{:02}",
startDate.tm_mon + 1,
startDate.tm_mday,
startDate.tm_hour,
startDate.tm_min);
std::string endStr = std::format("{:02}.{:02} {:02}:{:02}",
endDate.tm_mon + 1,
endDate.tm_mday,
endDate.tm_hour,
endDate.tm_min);

if (startDate.tm_year != endDate.tm_year) {
return slint::SharedString{std::format("{:04} {} - {:04} {}",
startDate.tm_year + 1900,
startStr,
endDate.tm_year + 1900,
endStr)};
}
if (startDate.tm_mon != endDate.tm_mon || startDate.tm_mday != endDate.tm_mday) {
return slint::SharedString{startStr + " - " + endStr};
}
return slint::SharedString{startStr + " - " + endStr.substr(6)};
}

slint::SharedString firstUnicode(const std::string& str) {
if (str.empty()) {
return " ";
}
size_t length = 0;
auto firstByte = static_cast<unsigned char>(str[0]);
if ((firstByte & 0x80) == 0) {
// ASCII character
length = 1;
} else if ((firstByte & 0xE0) == 0xC0) {
// Two-byte character
length = 2;
} else if ((firstByte & 0xF0) == 0xE0) {
// Three-byte character
length = 3;
} else if ((firstByte & 0xF8) == 0xF0) {
// Four-byte character
length = 4;
} else {
// Invalid Unicode
return " ";
}
if (str.size() < length) {
return " ";
}
return slint::SharedString{str.substr(0, length)};
}

} // namespace details

using EventEntityList = std::vector<EventEntity>;
using EventStructModel = std::shared_ptr<slint::VectorModel<EventStruct>>;

auto from(const auto& obj) {
return obj;
}

EventStruct from(const EventEntity& entity) {
return {
.id = entity.id,
.summary = slint::SharedString(entity.summary),
.summary_abbr = details::firstUnicode(entity.summary),
.description = slint::SharedString(entity.description),
.time = details::convertTimeRange(entity.start, entity.end),
.location = slint::SharedString(entity.location),
.tag = slint::SharedString(entity.tag),
.larkMeetingRoomName = slint::SharedString(entity.larkMeetingRoomName),
.larkDepartmentName = slint::SharedString(entity.larkDepartmentName),
.state = static_cast<EventState>(entity.state),
.is_subscribed = entity.isSubscribed,
.is_checkIn = entity.isCheckedIn,
};
}

EventStructModel from(const EventEntityList& list) {
std::vector<EventStruct> model;
model.reserve(list.size());
for (auto& entity : list) {
model.push_back(from(entity));
}
return std::make_shared<slint::VectorModel<EventStruct>>(model);
}

} // namespace evento::convert
1 change: 1 addition & 0 deletions ui/assets/image/icon/pentagon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions ui/assets/image/image_token.slint
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct EventoIconCollection {
topic: image,
tree-list: image,
account-box: image,
pentagon: image,
}

struct EventoDisplayCollection {
Expand Down Expand Up @@ -94,6 +95,7 @@ export global EventoImageToken {
topic: @image-url("./icon/topic.svg"),
tree-list: @image-url("./icon/tree-list.svg"),
account-box: @image-url("./icon/account-box-outline.svg"),
pentagon: @image-url("./icon/pentagon.svg"),
};
// display used as images bigger than icon
// most display image won't change according to darkmode switch
Expand Down
68 changes: 68 additions & 0 deletions ui/components/card.slint
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { Token } from "../global.slint";
import { EventStruct } from "./event_struct.slint";

export component Card inherits Rectangle {
out property <color> surface: Token.color.surface-container-low;
out property <color> on-surface: Token.color.on-surface-variant;
out property <color> inverse-surface: Token.color.inverse-surface;
out property <color> inverse-on-surface: Token.color.inverse-on-surface;
property <length> press-x;
property <length> press-y;
callback clicked;
padding: 14px;
width: 303px;
height: 188px;
clip: true;
background: surface;
border-radius: 12px;

states [
ripple when touch-area.pressed: {
drop-shadow-color: Token.color.surface;
drop-shadow-blur: 0px;
drop-shadow-offset-y: 0px;
animate-circle.radius: root.width * 1.1;
in {
animate drop-shadow-color, drop-shadow-blur, drop-shadow-offset-y, animate-circle.radius { duration: 200ms; }
}
out {
animate drop-shadow-color, drop-shadow-blur, drop-shadow-offset-y, animate-circle.radius { duration: 200ms; }
}
}
origin when !touch-area.pressed: {
drop-shadow-color: touch-area.has-hover ? #070707ce : Token.color.surface;
drop-shadow-blur: touch-area.has-hover ? 4px : 0px;
drop-shadow-offset-y: touch-area.has-hover ? 2px : 0px;
animate-circle.radius: 0px;
}
]

touch-area := TouchArea {
width: 100%;
height: 100%;
clicked => {
root.clicked();
}
}

animate-circle := Rectangle {
in-out property <length> radius;
in-out property <length> center-x;
in-out property <length> center-y;
center-x: touch-area.pressed-x;
center-y: touch-area.pressed-y;
radius: 0px;
x: center-x - radius;
y: center-y - radius;
width: radius * 2;
height: radius * 2;
border-radius: radius;
background: Token.color.surface-variant;
}

content-area := VerticalLayout {
width: root.width - root.padding * 2;
height: root.height - root.padding * 2;
@children
}
}
175 changes: 175 additions & 0 deletions ui/components/event_card.slint
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import { Token } from "../global.slint";
import { EventStruct } from "./event_struct.slint";
import { Card } from "./card.slint";

export component EventCard inherits Card {
in-out property <EventStruct> event: {
summary: "活动标题",
summary-abbr: "活",
time: "活动时间",
location: "活动地点",
description: "活动内容",
id: 0
};
header := HorizontalLayout {
// width: 100%;
height: abbr-circle.height;
alignment: LayoutAlignment.space-between;
spacing: self.width - abbr-circle.width - pentagon.width;
abbr-circle := Rectangle {
width: 28px;
height: self.width;
border-radius: self.width / 2;
background: root.on-surface;
abbr-text := Text {
color: root.inverse-on-surface;
text: event.summary-abbr;
font-size: Token.font.label.small.size;
font-weight: Token.font.label.small.weight;
}
}

VerticalLayout {
width: 10px;
height: abbr-circle.width;
Rectangle {
width: pentagon.width;
height: (abbr-circle.height - pentagon.height) / 2;
}

pentagon := Image {
width: 10px;
height: self.width;
colorize: root.on-surface;
source: Token.image.icon.pentagon;
}

Rectangle {
width: pentagon.width;
height: (abbr-circle.height - pentagon.height) / 2;
}
}
}

title := VerticalLayout {
height: root.height - root.padding * 2 - abbr-circle.height - info.height;
alignment: center;
event-title := Text {
color: root.inverse-surface;
text: event.summary;
wrap: word-wrap;
font-size: Token.font.body.large.size;
font-weight: Token.font.headline.large.weight;
}
}

info := VerticalLayout {
height: time-label.height * 3 + self.spacing * 2;
alignment: center;
spacing: 2px;
time-label := Rectangle {
height: 20px;
HorizontalLayout {
spacing: 6px;
time-icon := Image {
width: 16px;
height: self.width;
source: Token.image.icon.time;
colorize: time-text.color;
}

time-text := Text {
font-size: Token.font.label.small.size;
text: event.time;
width: parent.width - time-icon.width - parent.spacing;
overflow: elide;
}
}
}

location-label := Rectangle {
height: 20px;
HorizontalLayout {
spacing: 6px;
location-icon := Image {
width: 16px;
height: self.width;
source: Token.image.icon.locate;
colorize: location-text.color;
}

location-text := Text {
font-size: Token.font.label.small.size;
text: event.location;
width: parent.width - location-icon.width - parent.spacing;
overflow: elide;
}
}
}

description-label := Rectangle {
height: 20px;
HorizontalLayout {
spacing: 6px;
description-icon := Image {
width: 16px;
height: self.width;
source: Token.image.icon.topic;
colorize: location-text.color;
}

description-text := Text {
font-size: Token.font.label.small.size;
text: event.description;
width: parent.width - description-icon.width - parent.spacing;
overflow: elide;
}
}
}
}
}

export component EventCardGroup inherits Rectangle {
in-out property <[EventStruct]> model;
in-out property <length> horizontal-spacing: 20px;
in-out property <length> vertical-spacing: 20px;
in-out property <length> item-width: 303px;
out property <length> item-height: 188px;
property <int> row-count: floor(model.length / 3) * 3 == model.length ? model.length / 3 : floor(model.length / 3) + 1;
callback item-clicked(EventStruct);
width: item-width * 3 + horizontal-spacing * 4;
height: item-height * 3 + vertical-spacing * (row-count + 1);

function row-indexes(start-idx: int) -> [int] {
if start-idx + 1 < root.model.length {
if start-idx + 2 < root.model.length {
return [start-idx, start-idx + 1, start-idx + 2];
} else {
return [start-idx, start-idx + 1];
}
}
return [start-idx];
}

VerticalLayout {
width: 100%;
height: 100%;
alignment: center;
spacing: root.vertical-spacing;
for row-idx in row-count: HorizontalLayout {
property <int> start-idx: row-idx * 3;
width: 100%;
height: root.item-height;
alignment: start;
padding: (self.width - self.spacing * 2 - root.item-width * 3) / 2;
spacing: root.horizontal-spacing;
for idx in root.row-indexes(start-idx): EventCard {
event: root.model[idx];
width: root.item-width;
clicked => {
root.item-clicked(self.event);
}
}
}
}
}
Loading
Loading