summary refs log tree commit diff
path: root/src/lib.rs
blob: c5705db3de6aba605e968cc6aaa611e69a1092f7 (plain) (blame)
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
use std::sync::Arc;

use adw::prelude::*;
use relm4::{gtk, prelude::{AsyncComponent, AsyncComponentParts, ComponentController, Controller}, AsyncComponentSender, Component, RelmWidgetExt};

pub mod components {
    pub(crate) mod smart_summary;
    pub(crate) use smart_summary::{
        SmartSummaryButton, Output as SmartSummaryOutput, Input as SmartSummaryInput
    };

    pub(crate) mod post_editor;
    pub(crate) use post_editor::{
        PostEditor, Input as PostEditorInput
    };
}

use components::post_editor::{Post, Visibility};

pub mod secrets;
pub mod micropub;
pub mod util;
pub const APPLICATION_ID: &str = "xyz.vikanezrimaya.kittybox.Bowl";

pub const VISIBILITY: [&str; 2] = ["public", "private"];

#[derive(Debug)]
pub struct App {
    micropub: Arc<micropub::Client>,
    submit_busy_guard: Option<gtk::gio::ApplicationBusyGuard>,

    post_editor: Controller<components::PostEditor<micropub::Error>>
}

#[derive(Debug)]
#[doc(hidden)]
pub enum Input {
    SubmitButtonPressed,
    PostEditor(Option<Post>)
}

#[relm4::component(pub async)]
impl AsyncComponent for App {
    /// The type of the messages that this component can receive.
    type Input = Input;
    /// The type of the messages that this component can send.
    type Output = ();
    /// The type of data with which this component will be initialized.
    type Init = micropub::Client;
    /// The type of the command outputs that this component can receive.
    type CommandOutput = ();
 
    view! {
        #[root]
        adw::ApplicationWindow {
            set_title: Some("Create post"),
            set_width_request: 360,
            set_height_request: 294,

            adw::ToolbarView {
                add_top_bar: &{
                    relm4::view! {
                        send_button = gtk::Button {
                            set_icon_name: "document-send-symbolic",
                            set_tooltip: "Send post",

                            connect_clicked => Self::Input::SubmitButtonPressed,
                            #[watch]
                            set_sensitive: model.submit_busy_guard.is_none(),
                        },

                        bar = adw::HeaderBar::new() {
                            pack_end: &send_button,
                        },
                    }

                    bar
                },

                model.post_editor.widget(),
            }
        }
                
    }
    /// Initialize the UI and model.
    async fn init(
        init: Self::Init,
        window: Self::Root,
        sender: AsyncComponentSender<Self>,
    ) -> AsyncComponentParts<Self> {
        let model = App {
            submit_busy_guard: None,
            micropub: Arc::new(init),
            post_editor: components::PostEditor::builder()
                .launch(None)
                .forward(sender.input_sender(), Self::Input::PostEditor),
        };

        let widgets = view_output!();

        #[cfg(debug_assertions)]
        window.add_css_class("devel");

        AsyncComponentParts { model, widgets }
    }

    async fn update(
        &mut self,
        message: Self::Input,
        _sender: AsyncComponentSender<Self>,
        _root: &Self::Root
    ) {
        match message {
            Input::SubmitButtonPressed => {
                self.submit_busy_guard = Some(relm4::main_adw_application().mark_busy());
                self.post_editor.sender().send(components::PostEditorInput::Submit).unwrap();
            },
            Input::PostEditor(None) => {
                self.submit_busy_guard = None;
            }
            Input::PostEditor(Some(post)) => {
                let mf2 = post.into();
                log::debug!("Submitting post: {:#}", serde_json::to_string(&mf2).unwrap());
                match self.micropub.send_post(mf2).await {
                    Ok(location) => {
                        self.post_editor.sender()
                            .send(components::PostEditorInput::SubmitDone(location))
                            .unwrap();
                    },
                    Err(err) => {
                        log::warn!("Error sending post: {}", err);
                        self.post_editor.sender()
                            .send(components::PostEditorInput::SubmitError(err))
                            .unwrap();
                    }
                }
                self.submit_busy_guard = None;
            },
        }
    }
}