From 598288434f296e83de72db336c3789fb632851ad Mon Sep 17 00:00:00 2001 From: Bnyro Date: Thu, 22 Jun 2023 16:31:08 +0200 Subject: [PATCH] Add tab to view joined communities --- src/api/communities.rs | 5 +++-- src/main.rs | 27 ++++++++++++++++++--------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/api/communities.rs b/src/api/communities.rs index 75b31ee..39ac258 100644 --- a/src/api/communities.rs +++ b/src/api/communities.rs @@ -1,11 +1,12 @@ -use lemmy_api_common::{community::{ListCommunities, ListCommunitiesResponse}, lemmy_db_schema::{SortType, SearchType}, lemmy_db_views_actor::structs::CommunityView}; +use lemmy_api_common::{community::{ListCommunities, ListCommunitiesResponse}, lemmy_db_schema::{SortType, SearchType, ListingType}, lemmy_db_views_actor::structs::CommunityView}; use crate::settings; use super::search; -pub fn fetch_communities(page: i64, query: Option) -> std::result::Result, reqwest::Error> { +pub fn fetch_communities(page: i64, query: Option,listing_type: Option) -> std::result::Result, reqwest::Error> { if query.is_none() || query.clone().unwrap().trim().is_empty() { let params = ListCommunities { + type_: listing_type, sort: Some(SortType::TopMonth), page: Some(page), auth: settings::get_current_account().jwt, diff --git a/src/main.rs b/src/main.rs index 5152058..02aa22b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,6 +38,8 @@ struct App { post_page: Controller, inbox_page: Controller, logged_in: bool, + current_communities_type: Option, + current_posts_type: Option, } #[derive(Debug, Clone)] @@ -53,7 +55,7 @@ pub enum AppMsg { StartFetchPosts(Option), DoneFetchPosts(Vec), DoneFetchCommunities(Vec), - ViewCommunities(Option), + ViewCommunities(Option, Option), OpenCommunity(String), DoneFetchCommunity(GetCommunityResponse), OpenPerson(String), @@ -82,19 +84,25 @@ impl SimpleComponent for App { set_menu_model: Some(&menu_model), }, pack_start = >k::Button { - set_label: "Recommended", + set_label: "Home", connect_clicked => AppMsg::StartFetchPosts(None), }, pack_start = >k::Button { set_label: "Communities", - connect_clicked => AppMsg::ViewCommunities(None), + connect_clicked => AppMsg::ViewCommunities(None, None), }, pack_start = >k::Button { - set_label: "Subscribed", + set_label: "Recommended", connect_clicked => AppMsg::StartFetchPosts(Some(ListingType::Subscribed)), #[watch] set_visible: model.logged_in, }, + pack_start = >k::Button { + set_label: "Joined", + connect_clicked => AppMsg::ViewCommunities(None, Some(ListingType::Subscribed)), + #[watch] + set_visible: model.logged_in, + }, pack_start = >k::Button { set_label: "Inbox", connect_clicked => AppMsg::OpenInbox, @@ -215,7 +223,7 @@ impl SimpleComponent for App { set_label: "Search", connect_clicked[sender, community_search_query] => move |_| { let text = community_search_query.text().as_str().to_string(); - sender.input(AppMsg::ViewCommunities(Some(text))); + sender.input(AppMsg::ViewCommunities(Some(text), model.current_communities_type)); }, } }, @@ -298,7 +306,7 @@ impl SimpleComponent for App { let post_page = PostPage::builder().launch(default_post()).forward(sender.input_sender(), |msg| msg); let inbox_page = InboxPage::builder().launch(()).forward(sender.input_sender(), |msg| msg); - let model = App { state, logged_in, posts, communities, profile_page, community_page, post_page, inbox_page, message: None, latest_action: None }; + let model = App { state, logged_in, posts, communities, profile_page, community_page, post_page, inbox_page, message: None, latest_action: None, current_communities_type: None, current_posts_type: None }; // fetch posts if that's the initial page if !current_account.instance_url.is_empty() { sender.input(AppMsg::StartFetchPosts(None)) }; @@ -355,6 +363,7 @@ impl SimpleComponent for App { self.state = AppState::ChooseInstance; } AppMsg::StartFetchPosts(type_) => { + self.current_posts_type = type_; std::thread::spawn(move || { let message = match api::posts::list_posts(1, None, type_) { Ok(posts) => AppMsg::DoneFetchPosts(posts), @@ -370,11 +379,11 @@ impl SimpleComponent for App { self.posts.guard().push_back(post); } } - AppMsg::ViewCommunities(query) => { + AppMsg::ViewCommunities(query, listing_type) => { self.state = AppState::Communities; - if (query.is_none() || query.clone().unwrap().trim().is_empty()) && !self.communities.is_empty() { return; } + self.current_communities_type = listing_type; std::thread::spawn(move || { - let message = match api::communities::fetch_communities(1, query) { + let message = match api::communities::fetch_communities(1, query, listing_type) { Ok(communities) => AppMsg::DoneFetchCommunities(communities), Err(err) => AppMsg::ShowMessage(err.to_string()) };