Hide all actions that require an account while logged out

This commit is contained in:
Bnyro 2023-06-22 16:16:15 +02:00
parent ebd91c10af
commit 37a9cc244f
4 changed files with 44 additions and 20 deletions

View File

@ -4,7 +4,7 @@ use relm4::{prelude::*, factory::FactoryVecDeque, MessageBroker};
use gtk::prelude::*; use gtk::prelude::*;
use relm4_components::web_image::WebImage; use relm4_components::web_image::WebImage;
use crate::{api, util::get_web_image_msg}; use crate::{api, util::get_web_image_msg, settings};
use super::post_row::PostRow; use super::post_row::PostRow;
@ -96,11 +96,15 @@ impl SimpleComponent for CommunityPage {
set_margin_start: 10, set_margin_start: 10,
}, },
if settings::get_current_account().jwt.is_some() {
gtk::Button { gtk::Button {
set_label: "Create post", set_label: "Create post",
set_margin_start: 10, set_margin_start: 10,
connect_clicked => CommunityInput::OpenCreatePostDialog, connect_clicked => CommunityInput::OpenCreatePostDialog,
} }
} else {
gtk::Box {}
}
}, },
gtk::Separator {}, gtk::Separator {},

View File

@ -161,11 +161,15 @@ impl SimpleComponent for PostPage {
#[watch] #[watch]
set_text: &format!("{} comments", model.info.post_view.counts.comments), set_text: &format!("{} comments", model.info.post_view.counts.comments),
}, },
if settings::get_current_account().jwt.is_some() {
gtk::Button { gtk::Button {
set_label: "Comment", set_label: "Comment",
set_margin_start: 10, set_margin_start: 10,
connect_clicked => PostInput::OpenCreateCommentDialog, connect_clicked => PostInput::OpenCreateCommentDialog,
} }
} else {
gtk::Box {}
}
}, },
gtk::Separator {}, gtk::Separator {},

View File

@ -73,6 +73,7 @@ impl SimpleComponent for VotingRowModel {
gtk::ToggleButton { gtk::ToggleButton {
set_icon_name: "go-up", set_icon_name: "go-up",
connect_clicked => VotingRowInput::Vote(1), connect_clicked => VotingRowInput::Vote(1),
#[watch]
set_active: model.stats.own_vote == Some(1), set_active: model.stats.own_vote == Some(1),
}, },
gtk::Label { gtk::Label {
@ -84,6 +85,7 @@ impl SimpleComponent for VotingRowModel {
gtk::ToggleButton { gtk::ToggleButton {
set_icon_name: "go-down", set_icon_name: "go-down",
connect_clicked => VotingRowInput::Vote(-1), connect_clicked => VotingRowInput::Vote(-1),
#[watch]
set_active: model.stats.own_vote == Some(-1), set_active: model.stats.own_vote == Some(-1),
} }
} }

View File

@ -9,6 +9,7 @@ use components::{post_row::PostRow, community_row::CommunityRow, profile_page::{
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, ListingType}, 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}};
use settings::get_current_account;
static APP_ID: &str = "com.lemmy-gtk.lemoa"; static APP_ID: &str = "com.lemmy-gtk.lemoa";
@ -35,7 +36,8 @@ struct App {
profile_page: Controller<ProfilePage>, profile_page: Controller<ProfilePage>,
community_page: Controller<CommunityPage>, community_page: Controller<CommunityPage>,
post_page: Controller<PostPage>, post_page: Controller<PostPage>,
inbox_page: Controller<InboxPage> inbox_page: Controller<InboxPage>,
logged_in: bool,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -43,6 +45,7 @@ pub enum AppMsg {
ChooseInstance, ChooseInstance,
ShowLogin, ShowLogin,
Login(String, String), Login(String, String),
LoggedIn,
Logout, Logout,
Retry, Retry,
ShowMessage(String), ShowMessage(String),
@ -82,17 +85,21 @@ impl SimpleComponent for App {
set_label: "Recommended", set_label: "Recommended",
connect_clicked => AppMsg::StartFetchPosts(None), connect_clicked => AppMsg::StartFetchPosts(None),
}, },
pack_start = &gtk::Button {
set_label: "Communities",
connect_clicked => AppMsg::ViewCommunities(None),
},
pack_start = &gtk::Button { pack_start = &gtk::Button {
set_label: "Subscribed", set_label: "Subscribed",
connect_clicked => AppMsg::StartFetchPosts(Some(ListingType::Subscribed)), connect_clicked => AppMsg::StartFetchPosts(Some(ListingType::Subscribed)),
#[watch]
set_visible: model.logged_in,
}, },
pack_start = &gtk::Button { pack_start = &gtk::Button {
set_label: "Inbox", set_label: "Inbox",
connect_clicked => AppMsg::OpenInbox, connect_clicked => AppMsg::OpenInbox,
}, #[watch]
pack_start = &gtk::Button { set_visible: model.logged_in,
set_label: "Communities",
connect_clicked => AppMsg::ViewCommunities(None),
}, },
}, },
@ -279,8 +286,9 @@ impl SimpleComponent for App {
root: &Self::Root, root: &Self::Root,
sender: ComponentSender<Self>, sender: ComponentSender<Self>,
) -> ComponentParts<Self> { ) -> ComponentParts<Self> {
let instance_url = settings::get_current_account().instance_url; let current_account = settings::get_current_account();
let state = if instance_url.is_empty() { AppState::ChooseInstance } else { AppState::Loading }; let state = if current_account.instance_url.is_empty() { AppState::ChooseInstance } else { AppState::Loading };
let logged_in = current_account.jwt.is_some();
// initialize all controllers and factories // initialize all controllers and factories
let posts = FactoryVecDeque::new(gtk::Box::default(), sender.input_sender()); let posts = FactoryVecDeque::new(gtk::Box::default(), sender.input_sender());
@ -290,10 +298,10 @@ 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, 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 };
// 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(None)) }; if !current_account.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();
@ -320,7 +328,7 @@ impl SimpleComponent for App {
login_sender.input(AppMsg::ShowLogin); login_sender.input(AppMsg::ShowLogin);
}); });
let logout_action: RelmAction<LogoutAction> = RelmAction::new_stateless(move |_| { let logout_action: RelmAction<LogoutAction> = RelmAction::new_stateless(move |_| {
sender.input(AppMsg::ChooseInstance); sender.input(AppMsg::Logout);
}); });
let mut group = RelmActionGroup::<WindowActionGroup>::new(); let mut group = RelmActionGroup::<WindowActionGroup>::new();
@ -426,6 +434,7 @@ impl SimpleComponent for App {
self.state = AppState::Login; self.state = AppState::Login;
} }
AppMsg::Login(username, password) => { AppMsg::Login(username, password) => {
if get_current_account().instance_url.is_empty() { return; }
self.state = AppState::Loading; self.state = AppState::Loading;
std::thread::spawn(move || { std::thread::spawn(move || {
let message = match api::auth::login(username, password) { let message = match api::auth::login(username, password) {
@ -440,7 +449,7 @@ impl SimpleComponent for App {
account.id = user.id.0; account.id = user.id.0;
settings::update_current_account(account); settings::update_current_account(account);
} }
AppMsg::StartFetchPosts(None) AppMsg::LoggedIn
} else { } else {
AppMsg::ShowMessage("Wrong credentials!".to_string()) AppMsg::ShowMessage("Wrong credentials!".to_string())
} }
@ -454,6 +463,7 @@ impl SimpleComponent for App {
let mut account = settings::get_current_account(); let mut account = settings::get_current_account();
account.jwt = None; account.jwt = None;
settings::update_current_account(account); settings::update_current_account(account);
self.logged_in = false;
} }
AppMsg::ShowMessage(message) => { AppMsg::ShowMessage(message) => {
self.message = Some(message); self.message = Some(message);
@ -465,6 +475,10 @@ impl SimpleComponent for App {
AppMsg::OpenInbox => { AppMsg::OpenInbox => {
self.state = AppState::Inbox; self.state = AppState::Inbox;
} }
AppMsg::LoggedIn => {
self.logged_in = true;
sender.input(AppMsg::StartFetchPosts(None));
}
} }
} }
} }