Tab to view only subscribed posts

This commit is contained in:
Bnyro 2023-06-20 15:26:23 +02:00
parent ce9bf6d3ed
commit 0417cea437
4 changed files with 22 additions and 20 deletions

View File

@ -1,10 +1,11 @@
use lemmy_api_common::{post::{GetPostsResponse, GetPosts}, lemmy_db_views::structs::PostView}; use lemmy_api_common::{post::{GetPostsResponse, GetPosts}, lemmy_db_views::structs::PostView, lemmy_db_schema::ListingType};
use crate::util; use crate::util;
pub fn list_posts(page: i64, community_name: Option<String>) -> std::result::Result<Vec<PostView>, reqwest::Error> { pub fn list_posts(page: i64, community_name: Option<String>, listing_type: Option<ListingType>) -> std::result::Result<Vec<PostView>, reqwest::Error> {
let params = GetPosts { let params = GetPosts {
page: Some(page), page: Some(page),
type_: listing_type,
community_name, community_name,
auth: util::get_auth_token(), auth: util::get_auth_token(),
..Default::default() ..Default::default()

View File

@ -123,7 +123,7 @@ impl SimpleComponent for CommunityPage {
std::thread::spawn(move || { std::thread::spawn(move || {
if community.community_view.counts.posts == 0 { return; } if community.community_view.counts.posts == 0 { return; }
let community_posts = api::posts::list_posts(1, Some(community.community_view.community.name)); let community_posts = api::posts::list_posts(1, Some(community.community_view.community.name), None);
if let Ok(community_posts) = community_posts { if let Ok(community_posts) = community_posts {
sender.input(CommunityInput::DoneFetchPosts(community_posts)); sender.input(CommunityInput::DoneFetchPosts(community_posts));
} }

View File

@ -65,6 +65,7 @@ impl SimpleComponent for PostPage {
set_margin_top: 10, set_margin_top: 10,
set_spacing: 10, set_spacing: 10,
set_vexpand: false, set_vexpand: false,
set_halign: gtk::Align::Center,
gtk::Label { gtk::Label {
set_text: "posted by " set_text: "posted by "
@ -119,16 +120,12 @@ impl SimpleComponent for PostPage {
set_orientation: gtk::Orientation::Horizontal, set_orientation: gtk::Orientation::Horizontal,
set_margin_top: 10, set_margin_top: 10,
set_margin_bottom: 10, set_margin_bottom: 10,
set_halign: gtk::Align::Center,
gtk::Label { gtk::Label {
#[watch] #[watch]
set_text: &format!("{} comments, ", model.info.post_view.counts.comments), set_text: &format!("{} comments, {} score", model.info.post_view.counts.comments, model.info.post_view.counts.score),
}, },
gtk::Label {
#[watch]
set_text: &format!("{} score", model.info.post_view.counts.score),
},
gtk::Button { gtk::Button {
set_label: "Comment", set_label: "Comment",
set_margin_start: 10, set_margin_start: 10,

View File

@ -7,7 +7,7 @@ pub mod dialogs;
use api::{user::default_person, community::default_community, post::default_post}; use api::{user::default_person, community::default_community, post::default_post};
use components::{post_row::PostRow, community_row::CommunityRow, profile_page::{ProfilePage, self}, community_page::{CommunityPage, self}, post_page::{PostPage, self}}; use components::{post_row::PostRow, community_row::CommunityRow, profile_page::{ProfilePage, self}, community_page::{CommunityPage, self}, post_page::{PostPage, self}};
use gtk::prelude::*; use gtk::prelude::*;
use lemmy_api_common::{lemmy_db_views_actor::structs::CommunityView, lemmy_db_views::structs::PostView, person::GetPersonDetailsResponse, lemmy_db_schema::newtypes::PostId, post::GetPostResponse, community::GetCommunityResponse}; use lemmy_api_common::{lemmy_db_views_actor::structs::CommunityView, lemmy_db_views::structs::PostView, person::GetPersonDetailsResponse, lemmy_db_schema::{newtypes::PostId, ListingType}, post::GetPostResponse, community::GetCommunityResponse};
use relm4::{prelude::*, factory::FactoryVecDeque, set_global_css, actions::{RelmAction, RelmActionGroup}}; use relm4::{prelude::*, factory::FactoryVecDeque, set_global_css, actions::{RelmAction, RelmActionGroup}};
static APP_ID: &str = "com.lemmy-gtk.lemoa"; static APP_ID: &str = "com.lemmy-gtk.lemoa";
@ -45,7 +45,7 @@ pub enum AppMsg {
Retry, Retry,
ShowMessage(String), ShowMessage(String),
DoneChoosingInstance(String), DoneChoosingInstance(String),
StartFetchPosts, StartFetchPosts(Option<ListingType>),
DoneFetchPosts(Vec<PostView>), DoneFetchPosts(Vec<PostView>),
DoneFetchCommunities(Vec<CommunityView>), DoneFetchCommunities(Vec<CommunityView>),
ViewCommunities(Option<String>), ViewCommunities(Option<String>),
@ -76,8 +76,12 @@ impl SimpleComponent for App {
set_menu_model: Some(&menu_model), set_menu_model: Some(&menu_model),
}, },
pack_start = &gtk::Button { pack_start = &gtk::Button {
set_label: "Posts", set_label: "Recommended",
connect_clicked => AppMsg::StartFetchPosts, connect_clicked => AppMsg::StartFetchPosts(None),
},
pack_start = &gtk::Button {
set_label: "Subscribed",
connect_clicked => AppMsg::StartFetchPosts(Some(ListingType::Subscribed)),
}, },
pack_start = &gtk::Button { pack_start = &gtk::Button {
set_label: "Communities", set_label: "Communities",
@ -160,7 +164,7 @@ impl SimpleComponent for App {
gtk::Button { gtk::Button {
set_label: "Cancel", set_label: "Cancel",
connect_clicked => AppMsg::StartFetchPosts, connect_clicked => AppMsg::StartFetchPosts(None),
set_margin_end: 10, set_margin_end: 10,
}, },
gtk::Button { gtk::Button {
@ -274,7 +278,7 @@ impl SimpleComponent for App {
let model = App { state, posts, communities, profile_page, community_page, post_page, message: None, latest_action: None }; let model = App { state, posts, communities, profile_page, community_page, post_page, message: None, latest_action: None };
// fetch posts if that's the initial page // fetch posts if that's the initial page
if !instance_url.is_empty() { sender.input(AppMsg::StartFetchPosts) }; if !instance_url.is_empty() { sender.input(AppMsg::StartFetchPosts(None)) };
// setup all widgets and different stack pages // setup all widgets and different stack pages
let posts_box = model.posts.widget(); let posts_box = model.posts.widget();
@ -315,14 +319,14 @@ impl SimpleComponent for App {
preferences.instance_url = instance_url; preferences.instance_url = instance_url;
settings::save_prefs(&preferences); settings::save_prefs(&preferences);
self.state = AppState::Loading; self.state = AppState::Loading;
sender.input(AppMsg::StartFetchPosts); sender.input(AppMsg::StartFetchPosts(None));
} }
AppMsg::ChooseInstance => { AppMsg::ChooseInstance => {
self.state = AppState::ChooseInstance; self.state = AppState::ChooseInstance;
} }
AppMsg::StartFetchPosts => { AppMsg::StartFetchPosts(type_) => {
std::thread::spawn(move || { std::thread::spawn(move || {
let message = match api::posts::list_posts(1, None) { let message = match api::posts::list_posts(1, None, type_) {
Ok(posts) => AppMsg::DoneFetchPosts(posts), Ok(posts) => AppMsg::DoneFetchPosts(posts),
Err(err) => AppMsg::ShowMessage(err.to_string()) Err(err) => AppMsg::ShowMessage(err.to_string())
}; };
@ -406,7 +410,7 @@ impl SimpleComponent for App {
Ok(login) => { Ok(login) => {
if let Some(token) = login.jwt { if let Some(token) = login.jwt {
util::set_auth_token(Some(token)); util::set_auth_token(Some(token));
AppMsg::StartFetchPosts AppMsg::StartFetchPosts(None)
} else { } else {
AppMsg::ShowMessage("Wrong credentials!".to_string()) AppMsg::ShowMessage("Wrong credentials!".to_string())
} }
@ -424,7 +428,7 @@ impl SimpleComponent for App {
self.state = AppState::Message; self.state = AppState::Message;
} }
AppMsg::Retry => { AppMsg::Retry => {
sender.input(self.latest_action.clone().unwrap_or(AppMsg::StartFetchPosts)); sender.input(self.latest_action.clone().unwrap_or(AppMsg::StartFetchPosts(None)));
} }
} }
} }