summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2021-12-02 21:45:29 +0100
committerMatthias Beyer <mail@beyermatthias.de>2021-12-08 18:43:34 +0100
commit3b287d4548a9125f3fdcb6e15ea8d85b1167c21a (patch)
treee5c05c527a3dae422ef16be489b3003703bab0e2 /src
parent08c400b793ecfc12b462cac91ee37c780179f5f6 (diff)
Add simple posting GUI implementation
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'src')
-rw-r--r--src/gui/mod.rs140
1 files changed, 128 insertions, 12 deletions
diff --git a/src/gui/mod.rs b/src/gui/mod.rs
index 97751b0..584392e 100644
--- a/src/gui/mod.rs
+++ b/src/gui/mod.rs
@@ -1,5 +1,15 @@
+use std::sync::Arc;
+
use anyhow::Result;
use iced::Application;
+use iced::Column;
+use iced::Container;
+use iced::Element;
+use iced::Length;
+use iced::Scrollable;
+use iced::TextInput;
+use iced::scrollable;
+use iced::text_input;
use ipfs_api_backend_hyper::TryFromUri;
use crate::client::Client;
@@ -7,27 +17,51 @@ use crate::config::Config;
use crate::ipfs_client::IpfsClient;
#[derive(Debug)]
-struct DistroxGui;
+enum Distrox {
+ Loading,
+ Loaded(State),
+ FailedToStart,
+}
#[derive(Debug)]
+struct State {
+ client: Arc<Client>,
+
+ scroll: scrollable::State,
+ input: text_input::State,
+ input_value: String,
+}
+
+#[derive(Debug, Clone)]
enum Message {
- Loaded(Result<Client>),
+ Loaded(Arc<Client>),
+ FailedToLoad,
+
+ InputChanged(String),
+ CreatePost,
+
+ PostCreated(crate::cid::Cid),
+ PostCreationFailed(String),
}
-impl Application for DistroxGui {
+impl Application for Distrox {
type Executor = iced::executor::Default; // tokio
type Message = Message;
type Flags = ();
fn new(_flags: ()) -> (Self, iced::Command<Self::Message>) {
(
- DistroxGui,
+ Distrox::Loading,
iced::Command::perform(async {
- let ipfs = IpfsClient::from_str("http://localhost:5001")?;
- let config = Config::default();
- let client = Client::new(ipfs, config);
- Ok(client)
- }, Message::Loaded)
+ match IpfsClient::from_str("http://localhost:5001") {
+ Err(_) => Message::FailedToLoad,
+ Ok(ipfs) => {
+ let config = Config::default();
+ let client = Client::new(ipfs, config);
+ Message::Loaded(Arc::new(client))
+ }
+ }
+ }, |m: Message| -> Message { m })
)
}
@@ -35,12 +69,94 @@ impl Application for DistroxGui {
String::from("distrox")
}
- fn update(&mut self, _message: Self::Message, _clipboard: &mut iced::Clipboard) -> iced::Command<Self::Message> {
+ fn update(&mut self, message: Self::Message, _clipboard: &mut iced::Clipboard) -> iced::Command<Self::Message> {
+ match self {
+ Distrox::Loading => {
+ match message {
+ Message::Loaded(client) => {
+ let state = State {
+ client: client,
+ scroll: scrollable::State::default(),
+ input: text_input::State::default(),
+ input_value: String::default(),
+ };
+ *self = Distrox::Loaded(state);
+ }
+
+ Message::FailedToLoad => {
+ log::error!("Failed to load");
+ *self = Distrox::FailedToStart;
+ }
+
+ _ => {}
+
+ }
+ }
+
+ Distrox::Loaded(state) => {
+ match message {
+ Message::InputChanged(input) => {
+ state.input_value = input;
+ }
+
+ Message::CreatePost => {
+ if !state.input_value.is_empty() {
+ let client = state.client.clone();
+ iced::Command::perform(async move {
+ client.post_text_blob(state.input_value.clone()).await
+ },
+ |res| match res {
+ Ok(cid) => Message::PostCreated(cid),
+ Err(e) => Message::PostCreationFailed(e.to_string())
+ });
+ }
+ }
+
+ _ => {}
+ }
+ }
+
+ Distrox::FailedToStart => {
+ unimplemented!()
+ }
+ }
iced::Command::none()
}
fn view(&mut self) -> iced::Element<Self::Message> {
- iced::Text::new("Hello, world!").into()
+ match self {
+ Distrox::Loading => {
+ unimplemented!()
+ }
+
+ Distrox::Loaded(state) => {
+ let input = TextInput::new(
+ &mut state.input,
+ "What do you want to tell the world?",
+ &mut state.input_value,
+ Message::InputChanged,
+ )
+ .padding(15)
+ .size(30)
+ .on_submit(Message::CreatePost);
+
+ let content = Column::new()
+ .max_width(800)
+ .spacing(20)
+ .push(input);
+
+ Scrollable::new(&mut state.scroll)
+ .padding(40)
+ .push(
+ Container::new(content).width(Length::Fill).center_x(),
+ )
+ .into()
+ }
+
+ Distrox::FailedToStart => {
+ unimplemented!()
+ }
+ }
}
}
@@ -58,5 +174,5 @@ pub fn run() -> Result<()> {
..iced::Settings::default()
};
- DistroxGui::run(settings).map_err(anyhow::Error::from)
+ Distrox::run(settings).map_err(anyhow::Error::from)
}