diff options
Diffstat (limited to 'gui/src')
-rw-r--r-- | gui/src/app.rs | 57 | ||||
-rw-r--r-- | gui/src/main.rs | 2 | ||||
-rw-r--r-- | gui/src/post.rs | 47 | ||||
-rw-r--r-- | gui/src/timeline.rs | 86 | ||||
-rw-r--r-- | gui/src/timeline_post.rs | 29 |
5 files changed, 170 insertions, 51 deletions
diff --git a/gui/src/app.rs b/gui/src/app.rs index f9dedaa..8f4d579 100644 --- a/gui/src/app.rs +++ b/gui/src/app.rs @@ -11,8 +11,9 @@ use iced::scrollable; use iced::text_input; use distrox_lib::profile::Profile; -use distrox_lib::config::Config; use crate::timeline::Timeline; +use crate::timeline::PostLoadingRecipe; +use crate::post::Post; #[derive(Debug)] enum Distrox { @@ -41,6 +42,11 @@ pub enum Message { PostCreated(cid::Cid), PostCreationFailed(String), + + PostLoaded((distrox_lib::types::Payload, String)), + PostLoadingFailed, + + TimelineScrolled(f32), } impl Application for Distrox { @@ -52,7 +58,7 @@ impl Application for Distrox { ( Distrox::Loading, iced::Command::perform(async move { - match Profile::load(Config::default(), &name).await { + match Profile::load(&name).await { Err(_) => Message::FailedToLoad, Ok(instance) => { Message::Loaded(Arc::new(instance)) @@ -76,7 +82,7 @@ impl Application for Distrox { scroll: scrollable::State::default(), input: text_input::State::default(), input_value: String::default(), - timeline: Timeline::new() + timeline: Timeline::new(), }; *self = Distrox::Loaded(state); } @@ -99,10 +105,10 @@ impl Application for Distrox { Message::CreatePost => { if !state.input_value.is_empty() { - let profile = state.profile.clone(); let input = state.input_value.clone(); + let client = state.profile.client().clone(); iced::Command::perform(async move { - profile.client().post_text_blob(input).await + client.post_text_blob(input).await }, |res| match res { Ok(cid) => Message::PostCreated(cid), @@ -111,6 +117,18 @@ impl Application for Distrox { } } + Message::PostLoaded((payload, content)) => { + state.timeline.push(payload, content); + } + + Message::PostLoadingFailed => { + log::error!("Failed to load some post, TODO: Better error logging"); + } + + Message::TimelineScrolled(f) => { + log::trace!("Timeline scrolled: {}", f); + } + _ => {} } } @@ -149,17 +167,12 @@ impl Application for Distrox { .size(12) .on_submit(Message::CreatePost); - let content = Column::new() - .max_width(800) - .spacing(20) - .push(input) - .push(state.timeline.view()); + let timeline = state.timeline.view(); Scrollable::new(&mut state.scroll) .padding(40) - .push( - Container::new(content).width(Length::Fill).center_x(), - ) + .push(input) + .push(timeline) .into() } @@ -169,6 +182,24 @@ impl Application for Distrox { } } + fn subscription(&self) -> iced::Subscription<Self::Message> { + match self { + Distrox::Loaded(state) => { + let head = state.profile.head(); + + match head { + None => iced::Subscription::none(), + Some(head) => { + iced::Subscription::from_recipe({ + PostLoadingRecipe::new(state.profile.client().clone(), head.clone()) + }) + } + } + } + _ => iced::Subscription::none(), + } + } + } pub fn run(name: String) -> Result<()> { diff --git a/gui/src/main.rs b/gui/src/main.rs index 581877b..6120152 100644 --- a/gui/src/main.rs +++ b/gui/src/main.rs @@ -3,7 +3,7 @@ use anyhow::Result; mod app; mod cli; mod timeline; -mod timeline_post; +mod post; fn main() -> Result<()> { let _ = env_logger::try_init()?; diff --git a/gui/src/post.rs b/gui/src/post.rs new file mode 100644 index 0000000..11c5f6a --- /dev/null +++ b/gui/src/post.rs @@ -0,0 +1,47 @@ +use crate::app::Message; +use distrox_lib::types::Payload; + +#[derive(Clone, Debug)] +pub struct Post { + payload: Payload, + content: String, +} + +impl Post { + pub fn new(payload: Payload, content: String) -> Self { + Self { payload, content } + } + + pub fn view(&self) -> iced::Element<Message> { + iced::Column::new() + .push({ + iced::Row::new() + .height(iced::Length::Shrink) + .width(iced::Length::Fill) + .push({ + iced::Column::new() + .width(iced::Length::Fill) + .align_items(iced::Alignment::Start) + .push({ + iced::Text::new(self.payload.timestamp().inner().to_string()) + .size(10) + }) + }) + .push({ + iced::Column::new() + .width(iced::Length::Fill) + .align_items(iced::Alignment::End) + .push({ + iced::Text::new(self.payload.content().to_string()) + .size(10) + }) + }) + }) + .push(iced::rule::Rule::horizontal(10)) + .push({ + iced::Text::new(self.content.clone()).size(12) + }) + .push(iced::rule::Rule::horizontal(10)) + .into() + } +} diff --git a/gui/src/timeline.rs b/gui/src/timeline.rs index 82c8da1..8a13d5d 100644 --- a/gui/src/timeline.rs +++ b/gui/src/timeline.rs @@ -1,26 +1,96 @@ -use crate::timeline_post::TimelinePost; +use anyhow::Result; +use chrono::DateTime; +use chrono::Utc; +use futures::StreamExt; + +use iced_native::widget::scrollable::State as ScrollableState; + +use crate::app::Message; +use crate::post::Post; +use distrox_lib::client::Client; +use distrox_lib::stream::NodeStreamBuilder; +use distrox_lib::types::Payload; #[derive(Debug)] pub struct Timeline { - posts: Vec<TimelinePost> + posts: Vec<Post>, + scrollable: ScrollableState, } impl Timeline { pub fn new() -> Self { Self { - posts: Vec::with_capacity(100), + posts: Vec::new(), + scrollable: ScrollableState::new(), } } - pub fn update(&mut self) { - self.posts.iter_mut().for_each(|mut post| post.update()); + pub fn push(&mut self, payload: Payload, content: String) { + self.posts.push(Post::new(payload, content)); } - pub fn view(&self) -> iced::Column<crate::app::Message> { + pub fn view(&mut self) -> iced::Element<Message> { + let scrollable = iced::Scrollable::new(&mut self.scrollable) + .padding(10) + .spacing(20) + .width(iced::Length::Fill) + .height(iced::Length::Fill) + .on_scroll(move |offset| { + Message::TimelineScrolled(offset) + }); + self.posts .iter() - .fold(iced::Column::new(), |c, post| { - c.push(post.view()) + .fold(scrollable, |scrollable, post| { + scrollable.push(post.view()) }) + .into() + } +} + +pub struct PostLoadingRecipe { + client: Client, + head: cid::Cid, +} + +impl PostLoadingRecipe { + pub fn new(client: Client, head: cid::Cid) -> Self { + Self { client, head } + } +} + +// Make sure iced can use our download stream +impl<H, I> iced_native::subscription::Recipe<H, I> for PostLoadingRecipe +where + H: std::hash::Hasher, +{ + type Output = Message; + + fn hash(&self, state: &mut H) { + use std::hash::Hash; + self.head.to_bytes().hash(state); + } + + fn stream(self: Box<Self>, _input: futures::stream::BoxStream<'static, I>) -> futures::stream::BoxStream<'static, Self::Output> + { + log::debug!("Streaming posts starting at HEAD = {:?}", self.head); + Box::pin({ + NodeStreamBuilder::starting_from(self.head.clone()) + .into_stream(self.client.clone()) + .then(move |node| { + let client = self.client.clone(); + + async move { + let payload = client.get_payload(node?.payload()).await?; + let content = client.get_content_text(payload.content()).await?; + + Ok((payload, content)) + } + }) + .map(|res: Result<_>| match res { + Err(_) => Message::PostLoadingFailed, + Ok(p) => Message::PostLoaded(p), + }) + }) } } diff --git a/gui/src/timeline_post.rs b/gui/src/timeline_post.rs deleted file mode 100644 index 8b53898..0000000 --- a/gui/src/timeline_post.rs +++ /dev/null @@ -1,29 +0,0 @@ -#[derive(Debug)] -pub struct TimelinePost { - mime: mime::Mime, - content: PostContent, -} - -#[derive(Debug)] -pub enum PostContent { - Text(String) -} - -impl TimelinePost { - pub fn update(&mut self) { - () - } - - pub fn view(&self) -> iced::Row<crate::app::Message> { - iced::Row::new() - .push({ - iced::Text::new(self.mime.as_ref().to_string()) - }) - .push({ - match self.content { - PostContent::Text(ref txt) => iced::Text::new(txt.clone()), - } - }) - .into() - } -} |