Add tab to view joined communities
This commit is contained in:
parent
37a9cc244f
commit
598288434f
|
@ -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 crate::settings;
|
||||||
use super::search;
|
use super::search;
|
||||||
|
|
||||||
pub fn fetch_communities(page: i64, query: Option<String>) -> std::result::Result<Vec<CommunityView>, reqwest::Error> {
|
pub fn fetch_communities(page: i64, query: Option<String>,listing_type: Option<ListingType>) -> std::result::Result<Vec<CommunityView>, reqwest::Error> {
|
||||||
if query.is_none() || query.clone().unwrap().trim().is_empty() {
|
if query.is_none() || query.clone().unwrap().trim().is_empty() {
|
||||||
let params = ListCommunities {
|
let params = ListCommunities {
|
||||||
|
type_: listing_type,
|
||||||
sort: Some(SortType::TopMonth),
|
sort: Some(SortType::TopMonth),
|
||||||
page: Some(page),
|
page: Some(page),
|
||||||
auth: settings::get_current_account().jwt,
|
auth: settings::get_current_account().jwt,
|
||||||
|
|
27
src/main.rs
27
src/main.rs
|
@ -38,6 +38,8 @@ struct App {
|
||||||
post_page: Controller<PostPage>,
|
post_page: Controller<PostPage>,
|
||||||
inbox_page: Controller<InboxPage>,
|
inbox_page: Controller<InboxPage>,
|
||||||
logged_in: bool,
|
logged_in: bool,
|
||||||
|
current_communities_type: Option<ListingType>,
|
||||||
|
current_posts_type: Option<ListingType>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
@ -53,7 +55,7 @@ pub enum AppMsg {
|
||||||
StartFetchPosts(Option<ListingType>),
|
StartFetchPosts(Option<ListingType>),
|
||||||
DoneFetchPosts(Vec<PostView>),
|
DoneFetchPosts(Vec<PostView>),
|
||||||
DoneFetchCommunities(Vec<CommunityView>),
|
DoneFetchCommunities(Vec<CommunityView>),
|
||||||
ViewCommunities(Option<String>),
|
ViewCommunities(Option<String>, Option<ListingType>),
|
||||||
OpenCommunity(String),
|
OpenCommunity(String),
|
||||||
DoneFetchCommunity(GetCommunityResponse),
|
DoneFetchCommunity(GetCommunityResponse),
|
||||||
OpenPerson(String),
|
OpenPerson(String),
|
||||||
|
@ -82,19 +84,25 @@ impl SimpleComponent for App {
|
||||||
set_menu_model: Some(&menu_model),
|
set_menu_model: Some(&menu_model),
|
||||||
},
|
},
|
||||||
pack_start = >k::Button {
|
pack_start = >k::Button {
|
||||||
set_label: "Recommended",
|
set_label: "Home",
|
||||||
connect_clicked => AppMsg::StartFetchPosts(None),
|
connect_clicked => AppMsg::StartFetchPosts(None),
|
||||||
},
|
},
|
||||||
pack_start = >k::Button {
|
pack_start = >k::Button {
|
||||||
set_label: "Communities",
|
set_label: "Communities",
|
||||||
connect_clicked => AppMsg::ViewCommunities(None),
|
connect_clicked => AppMsg::ViewCommunities(None, None),
|
||||||
},
|
},
|
||||||
pack_start = >k::Button {
|
pack_start = >k::Button {
|
||||||
set_label: "Subscribed",
|
set_label: "Recommended",
|
||||||
connect_clicked => AppMsg::StartFetchPosts(Some(ListingType::Subscribed)),
|
connect_clicked => AppMsg::StartFetchPosts(Some(ListingType::Subscribed)),
|
||||||
#[watch]
|
#[watch]
|
||||||
set_visible: model.logged_in,
|
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 {
|
pack_start = >k::Button {
|
||||||
set_label: "Inbox",
|
set_label: "Inbox",
|
||||||
connect_clicked => AppMsg::OpenInbox,
|
connect_clicked => AppMsg::OpenInbox,
|
||||||
|
@ -215,7 +223,7 @@ impl SimpleComponent for App {
|
||||||
set_label: "Search",
|
set_label: "Search",
|
||||||
connect_clicked[sender, community_search_query] => move |_| {
|
connect_clicked[sender, community_search_query] => move |_| {
|
||||||
let text = community_search_query.text().as_str().to_string();
|
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 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 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
|
// fetch posts if that's the initial page
|
||||||
if !current_account.instance_url.is_empty() { sender.input(AppMsg::StartFetchPosts(None)) };
|
if !current_account.instance_url.is_empty() { sender.input(AppMsg::StartFetchPosts(None)) };
|
||||||
|
@ -355,6 +363,7 @@ impl SimpleComponent for App {
|
||||||
self.state = AppState::ChooseInstance;
|
self.state = AppState::ChooseInstance;
|
||||||
}
|
}
|
||||||
AppMsg::StartFetchPosts(type_) => {
|
AppMsg::StartFetchPosts(type_) => {
|
||||||
|
self.current_posts_type = type_;
|
||||||
std::thread::spawn(move || {
|
std::thread::spawn(move || {
|
||||||
let message = match api::posts::list_posts(1, None, type_) {
|
let message = match api::posts::list_posts(1, None, type_) {
|
||||||
Ok(posts) => AppMsg::DoneFetchPosts(posts),
|
Ok(posts) => AppMsg::DoneFetchPosts(posts),
|
||||||
|
@ -370,11 +379,11 @@ impl SimpleComponent for App {
|
||||||
self.posts.guard().push_back(post);
|
self.posts.guard().push_back(post);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
AppMsg::ViewCommunities(query) => {
|
AppMsg::ViewCommunities(query, listing_type) => {
|
||||||
self.state = AppState::Communities;
|
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 || {
|
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),
|
Ok(communities) => AppMsg::DoneFetchCommunities(communities),
|
||||||
Err(err) => AppMsg::ShowMessage(err.to_string())
|
Err(err) => AppMsg::ShowMessage(err.to_string())
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue