Skip to content

Commit

Permalink
Fix ad video cannot be opened
Browse files Browse the repository at this point in the history
  • Loading branch information
xfangfang committed Sep 12, 2024
1 parent 7151d24 commit abd19b8
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 67 deletions.
4 changes: 4 additions & 0 deletions resources/xml/views/video_card.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@
horizontalAlign="center"
textColor="@theme/font/yellow_1"/>
</brls:Box>
<brls:Image
id="video/card/picture/hint"
height="18"
width="18"/>
<SVGImage
id="video/svg/up"
height="14"
Expand Down
51 changes: 41 additions & 10 deletions wiliwili/include/api/bilibili/result/home_result.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,52 @@ inline void from_json(const nlohmann::json& nlohmann_json_j, RecommendReasonResu
}
}

class BusinessVideoResult {
public:
uint64_t aid{};
std::string bvid;
uint64_t cid{};
std::string pic;
std::string title;
int duration{};
int pubdate{};
UserSimpleResult owner;
VideoSimpleStateResult stat;
};
inline void from_json(const nlohmann::json& nlohmann_json_j, BusinessVideoResult& nlohmann_json_t) {
NLOHMANN_JSON_EXPAND(
NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, aid, bvid, cid, pic, title, duration, pubdate, owner, stat));
}

class BusinessMark {
public:
std::string text;
std::string img_url;
int img_height;
int img_width;
};
inline void from_json(const nlohmann::json& nlohmann_json_j, BusinessMark& nlohmann_json_t) {
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, text, img_url, img_height, img_width));
}

class BusinessInfo {
public:
std::string title; // 广告标题
std::string url; // 广告链接
std::string pic; // 广告封面
std::string adver_name; // 广告主
std::string business_mark; // 广告标记,通常来说是 "广告" 二字
std::string title; // 广告标题
std::string url; // 广告链接
std::string pic; // 广告封面
std::string adver_name; // 广告主
BusinessMark business_mark; // 广告标记,可以是 "广告" 二字 或 推广图标
bool is_ad{};
bool is_ad_video{}; // 自定义变量,是否是推广视频
BusinessVideoResult archive;
};
inline void from_json(const nlohmann::json& nlohmann_json_j, BusinessInfo& nlohmann_json_t) {
if (nlohmann_json_j.contains("business_mark") && !nlohmann_json_j.at("business_mark").is_null()) {
nlohmann_json_j.at("business_mark").at("text").get_to(nlohmann_json_t.business_mark);
if (nlohmann_json_j.contains("archive") && !nlohmann_json_j.at("archive").is_null()) {
nlohmann_json_j.at("archive").get_to(nlohmann_json_t.archive);
nlohmann_json_t.is_ad_video = true;
}
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, title, url, pic, adver_name));
NLOHMANN_JSON_EXPAND(
NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, title, url, pic, adver_name, is_ad, business_mark));
}

class RecommendVideoResult {
Expand All @@ -83,15 +116,13 @@ class RecommendVideoResult {
int is_followed{};
RecommendReasonResult rcmd_reason;
BusinessInfo business_info; // 广告
bool isAd{}; // 自定义变量,是否是广告
};
inline void from_json(const nlohmann::json& nlohmann_json_j, RecommendVideoResult& nlohmann_json_t) {
if (nlohmann_json_j.contains("rcmd_reason") && !nlohmann_json_j.at("rcmd_reason").is_null()) {
nlohmann_json_j.at("rcmd_reason").get_to(nlohmann_json_t.rcmd_reason);
}
if (nlohmann_json_j.contains("business_info") && !nlohmann_json_j.at("business_info").is_null()) {
nlohmann_json_j.at("business_info").get_to(nlohmann_json_t.business_info);
nlohmann_json_t.isAd = true;
}
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, id, bvid, cid, pic, title, duration, pubdate, owner,
stat, is_followed));
Expand Down
23 changes: 22 additions & 1 deletion wiliwili/include/view/video_card.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include "view/recycling_grid.hpp"

class NetImage;
class SVGImage;
class TextBox;

Expand Down Expand Up @@ -37,6 +36,25 @@ class RecyclingGridItemVideoCard : public BaseVideoCard {
const std::string& viewCount = "", const std::string& danmakuCount = "",
const std::string& rightBottomBadge = "", const std::string& extra = "");

/**
* 视频卡片基础信息
* @param title 视频标题
* @param pic 视频封面
* @param username 作者名
*/
void setBasicInfo(const std::string& title, const std::string& pic, const std::string& username);

/**
* 视频卡片作者名前的补充信息
* @param extra 可以是:“广告”、“3万点赞”、视频广告图标、创作推广图标等内容,有三类可能的值
* 留空显示 “up” 图标
* 以 “http” 开头则加载网络图片
* 其他文本直接显示
* @param width 网络图片的宽度
* @param height 网络图片的高度
*/
void setExtraInfo(const std::string& extra, float width = 18.0f, float height = 18.0f);

/** 设置视频下方的推荐原因
* 热门 每周必看 视频下方都包含推荐原因
*/
Expand All @@ -47,6 +65,8 @@ class RecyclingGridItemVideoCard : public BaseVideoCard {
*/
void setAchievement(const std::string& explain);

void cacheForReuse() override;

static RecyclingGridItemVideoCard* create();

private:
Expand All @@ -58,6 +78,7 @@ class RecyclingGridItemVideoCard : public BaseVideoCard {
BRLS_BIND(brls::Box, boxPic, "video/card/pic_box");
BRLS_BIND(brls::Box, boxHint, "video/card/hint");
BRLS_BIND(brls::Label, labelHint, "video/card/label/hint");
BRLS_BIND(brls::Image, pictureHint, "video/card/picture/hint");
BRLS_BIND(SVGImage, svgUp, "video/svg/up");
BRLS_BIND(SVGImage, svgView, "video/svg/view");
BRLS_BIND(SVGImage, svgDanmaku, "video/svg/danmaku");
Expand Down
31 changes: 23 additions & 8 deletions wiliwili/source/fragment/home_recommends.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,19 @@ class DataSourceRecommendVideoList : public RecyclingGridDataSource {
RecyclingGridItemVideoCard* item = (RecyclingGridItemVideoCard*)recycler->dequeueReusableCell("Cell");

bilibili::RecommendVideoResult& r = this->recommendList[index];
if (r.isAd) {
item->setCard(r.business_info.pic + ImageHelper::h_ext, r.business_info.title, r.business_info.adver_name,
"", "", "", r.business_info.business_mark);
if (r.business_info.is_ad) {
auto& card = r.business_info.archive;
auto& info = r.business_info;
auto& mark = r.business_info.business_mark;
if (r.business_info.is_ad_video) {
// 推广视频
item->setCard(card.pic + ImageHelper::h_ext, info.title, card.owner.name, card.pubdate, card.stat.view,
card.stat.danmaku, card.duration);
item->setExtraInfo(mark.img_url, (float)mark.img_width * 0.375f, (float)mark.img_height * 0.375f);
} else {
// 广告网页
item->setCard(info.pic + ImageHelper::h_ext, info.title, info.adver_name, "", "", "", mark.text);
}
} else {
item->setCard(r.pic + ImageHelper::h_ext, r.title, r.owner.name, r.pubdate, r.stat.view, r.stat.danmaku,
r.duration, r.rcmd_reason.content);
Expand All @@ -39,8 +49,12 @@ class DataSourceRecommendVideoList : public RecyclingGridDataSource {
size_t getItemCount() override { return recommendList.size(); }

void onItemSelected(RecyclingGrid* recycler, size_t index) override {
if (recommendList[index].isAd) {
brls::Application::getPlatform()->openBrowser(recommendList[index].business_info.url);
if (recommendList[index].business_info.is_ad) {
if (recommendList[index].business_info.is_ad_video) {
Intent::openBV(recommendList[index].business_info.archive.bvid);
} else {
brls::Application::getPlatform()->openBrowser(recommendList[index].business_info.url);
}
} else {
Intent::openBV(recommendList[index].bvid);
}
Expand Down Expand Up @@ -102,9 +116,10 @@ void HomeRecommends::onRecommendVideoList(const bilibili::RecommendVideoListResu
if (ProgramConfig::instance().upFilter.empty()) {
std::copy(originalResult.item.begin(), originalResult.item.end(), result.item.begin());
} else {
auto it = std::copy_if(
originalResult.item.begin(), originalResult.item.end(), result.item.begin(),
[](const bilibili::RecommendVideoResult& r) { return !ProgramConfig::instance().upFilter.count(r.owner.mid); });
auto it = std::copy_if(originalResult.item.begin(), originalResult.item.end(), result.item.begin(),
[](const bilibili::RecommendVideoResult& r) {
return !ProgramConfig::instance().upFilter.count(r.owner.mid);
});
result.item.resize(std::distance(result.item.begin(), it));
}

Expand Down
89 changes: 41 additions & 48 deletions wiliwili/source/view/video_card.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "view/text_box.hpp"
#include "utils/number_helper.hpp"
#include "utils/image_helper.hpp"
#include <pystring.h>

using namespace brls::literals;

Expand Down Expand Up @@ -45,67 +46,68 @@ void RecyclingGridItemVideoCard::setAchievement(const std::string& explain) {

RecyclingGridItemVideoCard* RecyclingGridItemVideoCard::create() { return new RecyclingGridItemVideoCard(); }

void RecyclingGridItemVideoCard::setCard(const std::string& pic, const std::string& title, const std::string& username,
int pubdate, int view_count, int danmaku, int duration,
const std::string& extra) {
if (pubdate)
this->labelUsername->setText(username + "·" + wiliwili::sec2date(pubdate));
else
this->labelUsername->setText(username);

this->labelTitle->setIsWrapping(true);
this->labelTitle->setText(title);
ImageHelper::with(this->picture)->load(pic);
this->labelCount->setText(wiliwili::num2w(view_count));
this->labelDanmaku->setText(wiliwili::num2w(danmaku));

if (duration)
this->labelDuration->setText(wiliwili::sec2Time(duration));
else
this->labelDuration->setText("");
void RecyclingGridItemVideoCard::cacheForReuse() {
//准备回收该项
ImageHelper::clear(this->picture);
ImageHelper::clear(this->pictureHint);
}

void RecyclingGridItemVideoCard::setExtraInfo(const std::string& extra, float width, float height){
if (extra.empty()) {
this->svgUp->setVisibility(brls::Visibility::VISIBLE);
this->boxHint->setVisibility(brls::Visibility::GONE);
this->pictureHint->setVisibility(brls::Visibility::GONE);
} else if (pystring::startswith(extra, "http")) {
this->svgUp->setVisibility(brls::Visibility::GONE);
this->boxHint->setVisibility(brls::Visibility::GONE);
this->pictureHint->setVisibility(brls::Visibility::VISIBLE);
this->pictureHint->setDimensions(width, height);
ImageHelper::with(this->pictureHint)->load(extra);
} else {
this->svgUp->setVisibility(brls::Visibility::GONE);
this->boxHint->setVisibility(brls::Visibility::VISIBLE);
this->labelHint->setText(extra);
this->pictureHint->setVisibility(brls::Visibility::GONE);
}
}

void RecyclingGridItemVideoCard::setBasicInfo(const std::string& title, const std::string& pic, const std::string& username) {
this->labelTitle->setIsWrapping(true);
this->labelTitle->setText(title);

this->labelUsername->setText(username);

ImageHelper::with(this->picture)->load(pic);
}

void RecyclingGridItemVideoCard::setCard(const std::string& pic, const std::string& title, const std::string& username,
int pubdate, int view_count, int danmaku, int duration,
const std::string& extra) {
std::string rightBottomBadge;
if (duration)
rightBottomBadge = wiliwili::sec2Time(duration);

this->setCard(pic, title, username, pubdate, view_count, danmaku, rightBottomBadge, extra);
}

void RecyclingGridItemVideoCard::setCard(const std::string& pic, const std::string& title, const std::string& username,
int pubdate, int view_count, int danmaku, const std::string& rightBottomBadge,
const std::string& extra) {
std::string author;
if (pubdate)
this->labelUsername->setText(username + "·" + wiliwili::sec2date(pubdate));
author = username + "·" + wiliwili::sec2date(pubdate);
else
this->labelUsername->setText(username);
author = username;

this->labelTitle->setIsWrapping(true);
this->labelTitle->setText(title);
ImageHelper::with(this->picture)->load(pic);
this->labelCount->setText(wiliwili::num2w(view_count));
this->labelDanmaku->setText(wiliwili::num2w(danmaku));
this->labelDuration->setText(rightBottomBadge);

if (extra.empty()) {
this->svgUp->setVisibility(brls::Visibility::VISIBLE);
this->boxHint->setVisibility(brls::Visibility::GONE);
} else {
this->svgUp->setVisibility(brls::Visibility::GONE);
this->boxHint->setVisibility(brls::Visibility::VISIBLE);
this->labelHint->setText(extra);
}
this->setCard(pic, title, author, wiliwili::num2w(view_count), wiliwili::num2w(danmaku), rightBottomBadge, extra);
}

void RecyclingGridItemVideoCard::setCard(const std::string& pic, const std::string& title, const std::string& username,
const std::string& viewCount, const std::string& danmakuCount,
const std::string& rightBottomBadge, const std::string& extra) {
this->labelUsername->setText(username);
this->labelTitle->setIsWrapping(true);
this->labelTitle->setText(title);
ImageHelper::with(this->picture)->load(pic);
this->setBasicInfo(title, pic, username);
this->setExtraInfo(extra);

this->labelDuration->setText(rightBottomBadge);

if (viewCount.empty()) {
Expand All @@ -125,15 +127,6 @@ void RecyclingGridItemVideoCard::setCard(const std::string& pic, const std::stri
this->labelDanmaku->setVisibility(brls::Visibility::VISIBLE);
this->labelDanmaku->setText(danmakuCount);
}

if (extra.empty()) {
this->svgUp->setVisibility(brls::Visibility::VISIBLE);
this->boxHint->setVisibility(brls::Visibility::GONE);
} else {
this->svgUp->setVisibility(brls::Visibility::GONE);
this->boxHint->setVisibility(brls::Visibility::VISIBLE);
this->labelHint->setText(extra);
}
}

/// 排行榜视频封面
Expand Down

0 comments on commit abd19b8

Please sign in to comment.