Debian Codegraph @

podcasts-gtk/src/widgets/content_stack.rs

raw symbols SCIP
1 // content_stack.rs
2 //
3 // Copyright 2017 Jordan Petridis <[email protected]>
4 // Copyright 2024 nee <[email protected]>
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 //
19 // SPDX-License-Identifier: GPL-3.0-or-later
20
21 use adw::prelude::*;
22 use anyhow::Result;
23 use async_channel::Sender;
24 use std::cell::OnceCell;
25 use std::rc::Rc;
26
27 use crate::app::Action;
28 use crate::i18n::i18n;
29 use crate::utils::get_ignored_shows;
30 use crate::widgets::{EmptyView, HomeView, ShowsView};
31 use podcasts_data::EpisodeWidgetModel;
32 use podcasts_data::dbqueries::is_episodes_populated;
33
34 #[derive(Debug, Clone)]
35 pub(crate) struct Content {
36 overlay: gtk::Overlay,
37 sender: Sender<Action>,
38 progress_bar: gtk::ProgressBar,
39 stack: adw::ViewStack,
40 shows_bin: adw::Bin,
41 shows: OnceCell<ShowsView>,
42 // TODO drop the home_bin and just update the model
43 // of HomeView once ported to ListView
44 home_bin: adw::Bin,
45 empty: EmptyView,
46 }
47
48 impl Content {
49 pub(crate) fn new(sender: Sender<Action>) -> Rc<Self> {
50 let stack = adw::ViewStack::new();
51 let shows_bin = adw::Bin::new();
52 let shows = OnceCell::new();
53 let home_bin = adw::Bin::new();
54 let home = HomeView::new(sender.clone());
55 let overlay = gtk::Overlay::new();
56 let empty = EmptyView::default();
57 let progress_bar = gtk::ProgressBar::builder()
58 .valign(gtk::Align::Start)
59 .halign(gtk::Align::Fill)
60 .visible(false)
61 .tooltip_text(i18n("Fetching feeds…"))
62 .build();
63 progress_bar.add_css_class("osd");
64
65 overlay.set_child(Some(&stack));
66 overlay.add_overlay(&progress_bar);
67
68 let home_page = stack.add_titled(&home_bin, Some("home"), &i18n("New"));
69 let shows_page = stack.add_titled(&shows_bin, Some("shows"), &i18n("Shows"));
70 stack.add_named(&empty, Some("empty"));
71
72 home_page.set_icon_name(Some("document-open-recent-symbolic"));
73 shows_page.set_icon_name(Some("audio-input-microphone-symbolic"));
74
75 home_bin.set_child(Some(&home));
76
77 let this = Rc::new(Self {
78 overlay,
79 sender,
80 progress_bar,
81 stack: stack.clone(),
82 shows_bin,
83 shows,
84 home_bin,
85 empty,
86 });
87
88 let weak = Rc::downgrade(&this);
89 stack.connect_visible_child_notify(move |s| {
90 if let Some(name) = s.visible_child_name() {
91 if name == "shows" {
92 if let Some(this) = weak.upgrade() {
93 this.init_shows();
94 }
95 }
96 }
97 });
98
99 if let Err(e) = this.check_empty_state() {
100 error!("Failed to check for empty db state {e}");
101 }
102
103 this
104 }
105
106 pub(crate) fn update(&self) {
107 self.update_home();
108 self.update_shows();
109 if let Err(e) = self.check_empty_state() {
110 error!("Failed to check for empty db state {e}");
111 }
112 }
113
114 pub(crate) fn update_home(&self) {
115 let home = HomeView::new(self.sender.clone());
116 self.home_bin.set_child(Some(&home));
117 }
118
119 pub(crate) fn update_home_episode(&self, ep: &EpisodeWidgetModel) {
120 if let Some(Ok(home)) = self.home_bin.child().map(|w| w.downcast::<HomeView>()) {
121 home.update_episode(ep);
122 }
123 }
124
125 pub(crate) fn update_shows(&self) {
126 if let Some(shows) = self.shows.get() {
127 shows.update_model();
128 }
129 }
130
131 pub(crate) fn progress_bar(&self) -> &gtk::ProgressBar {
132 &self.progress_bar
133 }
134
135 pub(crate) fn stack(&self) -> &adw::ViewStack {
136 &self.stack
137 }
138
139 pub(crate) fn overlay(&self) -> &gtk::Overlay {
140 &self.overlay
141 }
142
143 fn init_shows(&self) {
144 if self.shows.get().is_none() {
145 self.shows
146 .set({
147 info!("Init Shows View");
148 let new_shows = ShowsView::new(self.sender.clone());
149 self.shows_bin.set_child(Some(&new_shows));
150 new_shows
151 })
152 .unwrap();
153 }
154 }
155
156 pub(crate) fn go_to_home(&self) {
157 if !self.is_in_empty_view() {
158 self.stack.set_visible_child_name("home");
159 }
160 }
161
162 pub(crate) fn go_to_shows(&self) {
163 if !self.is_in_empty_view() {
164 self.stack.set_visible_child_name("shows");
165 }
166 }
167
168 pub(crate) fn switch_to_empty_views(&self) {
169 self.stack.set_visible_child(&self.empty);
170 }
171
172 pub(crate) fn switch_to_populated(&self) {
173 self.stack.set_visible_child(&self.home_bin);
174 }
175
176 pub(crate) fn is_in_empty_view(&self) -> bool {
177 self.stack
178 .visible_child_name()
179 .is_some_and(|name| name == "empty")
180 }
181
182 pub fn check_empty_state(&self) -> Result<()> {
183 let ign = get_ignored_shows()?;
184 debug!("IGNORED SHOWS {:?}", ign);
185 if is_episodes_populated(&ign)? {
186 send_blocking!(self.sender, Action::PopulatedState)
187 } else {
188 send_blocking!(self.sender, Action::EmptyState)
189 };
190 Ok(())
191 }
192 }