summaryrefslogtreecommitdiffstats
path: root/gui/src/app/mod.rs
blob: 450744ea40ed9c0d86db60db3051a6e679de5d08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use std::sync::Arc;

use anyhow::Result;
use iced::Application;
use iced::Column;
use iced::Container;
use iced::Length;
use iced::Scrollable;
use iced::TextInput;
use iced::scrollable;
use iced::text_input;
use distrox_lib::profile::Profile;

use crate::timeline::Timeline;
use crate::timeline::PostLoadingRecipe;
use crate::post::Post;

mod message;
pub use message::Message;

#[derive(Debug)]
enum Distrox {
    Loading,
    Loaded {
        profile: Arc<Profile>,

        scroll: scrollable::State,
        input: text_input::State,
        input_value: String,
        timeline: Timeline,
    },
    FailedToStart,
}

impl Application for Distrox {
    type Executor = iced::executor::Default; // tokio
    type Message = Message;
    type Flags = String;

    fn new(name: String) -> (Self, iced::Command<Self::Message>) {
        (
            Distrox::Loading,
            iced::Command::perform(async move {
                match Profile::load(&name).await {
                    Err(_) => Message::FailedToLoad,
                    Ok(instance) => {
                        Message::Loaded(Arc::new(instance))
                    }
                }
            }, |m: Message| -> Message { m })
        )
    }

    fn title(&self) -> String {
        String::from("distrox")
    }

    fn update(&mut self, message: Self::Message) -> iced::Command<Self::Message> {
        match self {
            Distrox::Loading => {
                match message {
                    Message::Loaded(profile) => {
                        *self = Distrox::Loaded {
                            profile,
                            scroll: scrollable::State::default(),
                            input: text_input::State::default(),
                            input_value: String::default(),
                            timeline: Timeline::new(),
                        };
                    }

                    Message::FailedToLoad => {
                        log::error!("Failed to load");
                        *self = Distrox::FailedToStart;
                    }

                    _ => {}

                }
            }

            Distrox::Loaded { profile, ref mut input_value, timeline, .. } => {
                match message {
                    Message::InputChanged(input) => {
                        *input_value = input;
                    }

                    Message::CreatePost => {
                        if !input_value.is_empty() {
                            let input = input_value.clone();
                            let client = profile.client().clone();
                            log::trace!("Posting...");
                            iced::Command::perform(async move {
                                log::trace!("Posting: '{}'", input);
                                client.post_text_blob(input).await
                            },
                            |res| match res {
                                Ok(cid) => Message::PostCreated(cid),
                                Err(e) => Message::PostCreationFailed(e.to_string())
                            });
                        }
                    }

                    Message::PostCreated(cid) => {
                        *input_value = String::new();
                        log::info!("Post created: {}", cid);
                    }

                    Message::PostCreationFailed(err) => {
                        log::error!("Post creation failed: {}", err);
                    }

                    Message::PostLoaded((payload, content)) => {
                        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);
                    }

                    _ => {}
                }
            }

            Distrox::FailedToStart => {
                unimplemented!()
            }
        }
        iced::Command::none()
    }

    fn view(&mut self) -> iced::Element<Self::Message> {
        match self {
            Distrox::Loading => {
                let text = iced::Text::new("Loading");

                let content = Column::new()
                    .max_width(800)
                    .spacing(20)
                    .push(text);

                Container::new(content)
                    .width(Length::Fill)
                    .center_x()
                    .into()
            }

            Distrox::Loaded { input, input_value, timeline, scroll, .. } => {
                let input = TextInput::new(
                    input,
                    "What do you want to tell the world?",
                    input_value,
                    Message::InputChanged,
                )
                .padding(15)
                .size(12)
                .on_submit(Message::CreatePost);

                let timeline = timeline.view();

                Scrollable::new(scroll)
                    .padding(40)
                    .push(input)
                    .push(timeline)
                    .into()
            }

            Distrox::FailedToStart => {
                unimplemented!()
            }
        }
    }

    fn subscription(&self) -> iced::Subscription<Self::Message> {
        match self {
            Distrox::Loaded { profile, .. } => {
                let head = profile.head();

                match head {
                    None => iced::Subscription::none(),
                    Some(head) => {
                        iced::Subscription::from_recipe({
                            PostLoadingRecipe::new(profile.client().clone(), head.clone())
                        })
                    }
                }
            }
            _ => iced::Subscription::none(),
        }
    }

}

pub fn run(name: String) -> Result<()> {
    let settings = iced::Settings {
        window: iced::window::Settings {
            resizable: true,
            decorations: true,
            transparent: false,
            always_on_top: false,
            ..iced::window::Settings::default()
        },
        flags: name,
        exit_on_close_request: true,
        ..iced::Settings::default()
    };

    Distrox::run(settings).map_err(anyhow::Error::from)
}