rss-tool/src/ui.rs
2024-09-03 22:53:57 -04:00

167 lines
4.7 KiB
Rust

use std::cell::Cell;
use crate::net::download_image;
use super::net;
use iced::{
self,
alignment::Vertical,
theme::Palette,
widget::{
button, column, container,
image::{self, Handle},
row, scrollable, text, Button, Column, Container, Image, Row,
},
Color, Element,
Length::Fill,
};
use rss::{self, Channel, Item};
use super::Message;
use super::Scene;
pub fn channel_view(feeds: &Vec<Channel>) -> Container<Message> {
container(
column(
feeds
.iter()
.map(|c: &Channel| {
let title = c.title();
let index = feeds.iter().position(|i| i.title() == title).unwrap();
channel_preview(c, index)
})
.map(Element::from),
)
.align_x(iced::Alignment::Start)
.spacing(5)
.padding(15),
)
.height(Fill)
.width(Fill)
}
pub fn channel_preview(feed: &rss::Channel, index: usize) -> Button<Message> {
let title = feed.title();
let desc = feed.description();
let fig: Image<Handle> =
iced::widget::Image::new(download_image(feed.image().unwrap().url()).unwrap());
//image needs to be downloaded...
fancy_button(fig, title, desc).on_press(Message::SetChannel(index))
}
pub fn item_list_view(feed: &Channel) -> Container<Message> {
println!("Loading items..\n");
let rw = row![button("Feeds").on_press(Message::SetScene(Scene::Feeds))]
.spacing(10)
.align_y(iced::Alignment::Start);
let item_list = column(
feed.items
.iter()
.map(|i: &Item| {
let title = i.title();
let index = feed.items.iter().position(|n| n.title() == title).unwrap();
item_preview(i, index).width(Fill)
})
.map(Element::from),
)
.width(Fill)
.align_x(iced::Alignment::Start)
.width(Fill)
.spacing(5);
let scroll = scrollable(item_list).width(iced::Length::Fill).height(Fill);
container(column![rw, scroll])
.align_x(iced::alignment::Horizontal::Center)
.align_y(iced::alignment::Vertical::Center)
.width(Fill)
.height(Fill)
.padding(5)
}
pub fn item_preview(item: &rss::Item, index: usize) -> Button<Message> {
let title = match item.title() {
Some(t) => t,
None => "Missing title",
};
let date = match item.pub_date() {
Some(d) => d,
None => "Missing Date",
};
let desc = item.description().unwrap();
let fig = iced::widget::image(match get_item_image(item) {
Some(img) => net::download_image(img).unwrap(),
None => Handle::from("rss.png"),
});
fancy_button(fig, title, desc).on_press(Message::SetItem((index)))
}
pub fn item_view(item: &rss::Item) -> Container<Message> {
let title = item.title().unwrap();
let desc: &str = item.description().unwrap();
let date = match item.pub_date() {
Some(dt) => dt,
None => "",
};
let content = item.content.clone().unwrap();
let desc = item.description().unwrap();
let rw = row![
button("Feeds").on_press(Message::SetScene(Scene::Feeds)),
button("Items").on_press(Message::SetScene(Scene::Items))
]
.spacing(15)
.padding(5)
.align_y(iced::Alignment::Start);
let list = column![
text(title).size(50),
text(date).size(25),
text(desc).size(35),
iced::widget::scrollable(text(super::html::process_html(content)).size(25)),
]
.spacing(10)
.align_x(iced::Alignment::Start);
container(column![rw, list].width(Fill))
.width(Fill)
.height(Fill)
}
pub fn fancy_button<'a>(
icon: iced::widget::Image<Handle>,
title: &'a str,
description: &'a str,
) -> Button<'a, Message> {
let c = container(
row![
icon.width(120),
column![text(title).size(40), text(description).size(25),]
.align_x(iced::Alignment::Start)
.spacing(5)
]
.spacing(5)
.align_y(iced::Alignment::Center),
)
.align_x(iced::alignment::Horizontal::Center);
button(c)
.padding(5)
.width(Fill)
}
fn get_item_image(item: &rss::Item) -> Option<&str> {
// Only bother with itunes:image
print!("{} \n", item.title().unwrap());
match item.itunes_ext() {
Some(e) => match e.image() {
Some(img) => {
println!("Image found: {}", img);
Some(img)
}
None => {
println!("Itunes extension found, but image was not..");
None
}
},
None => {
println!("found no extensions");
None
}
}
}