Introduce ChannelFilter, use it for "All unwatched" virtual channel

This commit is contained in:
Daniel Schulte 2021-07-18 23:57:44 +02:00
parent 0ef7eba6db
commit 02519840a6
3 changed files with 49 additions and 17 deletions

View File

@ -165,7 +165,7 @@ void load_videos_for_channel(const Channel &channel, bool force=false)
std::vector<Video> &channelVideos = videos[channel.id];
if(channel.is_virtual) {
channelVideos = Video::get_all_with_flag_value(channel.virtual_flag, channel.virtual_flag_value);
channelVideos = Video::get_all_with_filter(channel.filter);
} else {
channelVideos = Video::get_all_for_channel(channel.id);
}
@ -182,7 +182,7 @@ int fetch_videos_for_channel(Channel &channel, bool name_in_title=false)
{
if(channel.is_virtual) {
std::vector<Video> &channelVideos = videos[channel.id];
channelVideos = Video::get_all_with_flag_value(channel.virtual_flag, channel.virtual_flag_value);
channelVideos = Video::get_all_with_filter(channel.filter);
for(Video &video: channelVideos) {
video.tui_title_width = string_width(video.title);
}
@ -267,9 +267,11 @@ void add_channel_to_list(Channel &channel)
void make_virtual_unwatched_channel()
{
Channel channel = Channel::add_virtual("All Unwatched", kWatched, false);
ChannelFilter filter;
filter.video_mask = kWatched;
Channel channel = Channel::add_virtual("All Unwatched", filter);
std::vector<Video> &channelVideos = videos[channel.id];
channelVideos = Video::get_all_with_flag_value(channel.virtual_flag, channel.virtual_flag_value);
channelVideos = Video::get_all_with_filter(channel.filter);
for(Video &video: channelVideos) {
video.tui_title_width = string_width(video.title);
}

32
yt.cpp
View File

@ -138,12 +138,12 @@ static json api_request(const std::string &url, const std::map<std::string, std:
}
Channel::Channel(sqlite3_stmt *row): id(get_string(row, 0)), name(get_string(row, 1)), is_virtual(false),
virtual_flag(kNone), virtual_flag_value(false), user_flags(get_int(row, 2)), unwatched(0), tui_name_width(0)
user_flags(get_int(row, 2)), unwatched(0), tui_name_width(0)
{
}
Channel::Channel(const std::string &id, const std::string &name): id(id), name(name), is_virtual(false),
virtual_flag(kNone), virtual_flag_value(false), user_flags(0), unwatched(0), tui_name_width(0)
user_flags(0), unwatched(0), tui_name_width(0)
{
}
@ -182,14 +182,13 @@ Channel Channel::add(sqlite3 *db, const std::string &selector, const std::string
return Channel(channel_id, channel_name);
}
Channel Channel::add_virtual(const std::string &name, const VideoFlag virtual_flag, const bool virtual_flag_value)
Channel Channel::add_virtual(const std::string &name, const ChannelFilter filter)
{
std::string id = name;
std::transform(id.begin(), id.end(), id.begin(), [](char c){ return std::isalnum(c) ? std::tolower(c) : '-'; });
Channel channel("virtual-" + id, name);
channel.is_virtual = true;
channel.virtual_flag = virtual_flag;
channel.virtual_flag_value = virtual_flag_value;
channel.filter = filter;
return channel;
}
@ -396,14 +395,20 @@ std::vector<Video> Video::get_all_for_channel(const std::string &channel_id)
return videos;
}
std::vector<Video> Video::get_all_with_flag_value(const VideoFlag flag, const int value)
std::vector<Video> Video::get_all_with_filter(const ChannelFilter &filter)
{
std::vector<Video> videos;
sqlite3_stmt *query;
SC(sqlite3_prepare_v2(db, "SELECT * FROM videos WHERE flags & ?1 = ?2 ORDER BY published DESC, added_to_playlist DESC;", -1, &query, nullptr));
SC(sqlite3_bind_int(query, 1, flag));
SC(sqlite3_bind_int(query, 2, value));
SC(sqlite3_prepare_v2(db, R"(SELECT videos.*, channels.user_flags
FROM videos JOIN channels ON videos.channelId = channels.channelId
WHERE videos.flags & ?1 = ?2
AND channels.user_flags & ?3 = ?4
ORDER BY published DESC, added_to_playlist DESC;)", -1, &query, nullptr));
SC(sqlite3_bind_int(query, 1, filter.video_mask));
SC(sqlite3_bind_int(query, 2, filter.video_value));
SC(sqlite3_bind_int(query, 3, filter.user_mask));
SC(sqlite3_bind_int(query, 4, filter.user_value));
while(sqlite3_step(query) == SQLITE_ROW) {
videos.emplace_back(query);
@ -413,3 +418,12 @@ std::vector<Video> Video::get_all_with_flag_value(const VideoFlag flag, const in
return videos;
}
ChannelFilter::ChannelFilter(): id(-1), name(std::string()), video_mask(0), video_value(0), user_mask(0), user_value(0)
{
}
ChannelFilter::ChannelFilter(const int id, const std::string &name): id(id), name(name),
video_mask(0), video_value(0), user_mask(0), user_value(0)
{
}

24
yt.h
View File

@ -40,20 +40,36 @@ enum VideoFlag {
kDownloaded = (1<<1),
};
class ChannelFilter {
public:
int id;
std::string name;
uint32_t video_mask;
uint32_t video_value;
uint32_t user_mask;
uint32_t user_value;
ChannelFilter();
private:
ChannelFilter(const int id, const std::string &name);
};
class Channel
{
public:
std::string id;
std::string name;
bool is_virtual;
VideoFlag virtual_flag;
bool virtual_flag_value;
ChannelFilter filter;
int user_flags;
Channel(sqlite3_stmt *row);
static Channel add(sqlite3 *db, const std::string &selector, const std::string &value);
static Channel add_virtual(const std::string &name, const VideoFlag virtual_flag=kNone, const bool virtual_flag_value=true);
static Channel add_virtual(const std::string &name, const ChannelFilter filter);
static std::vector<Channel> get_all(sqlite3 *db);
std::string upload_playlist() const;
@ -82,7 +98,7 @@ struct Video
Video(sqlite3_stmt *row);
void set_flag(sqlite3 *db, VideoFlag flag, bool value=true);
static std::vector<Video> get_all_for_channel(const std::string &channel_id);
static std::vector<Video> get_all_with_flag_value(const VideoFlag flag, const int value);
static std::vector<Video> get_all_with_filter(const ChannelFilter &filter);
size_t tui_title_width;
};