diff options
author | Vika <vika@fireburn.ru> | 2024-08-20 18:31:43 +0300 |
---|---|---|
committer | Vika <vika@fireburn.ru> | 2024-08-20 18:31:43 +0300 |
commit | e6e4de9e15833e4042be5cd71944e9b8929346d4 (patch) | |
tree | 344e43e88c4a14d7fcc384df4a60431acfe03056 /src/lib.rs | |
parent | d0353dbc6ac624f63240ec64b83d238499cb0c7c (diff) | |
download | bowl-e6e4de9e15833e4042be5cd71944e9b8929346d4.tar.zst |
Make the post composer asynchronous
This makes it able to execute unsendable futures, and unlocks ability for us to do asynchronous initialization and updates.
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 31 |
1 files changed, 21 insertions, 10 deletions
diff --git a/src/lib.rs b/src/lib.rs index 718886c..5ee80c2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,11 @@ +use std::sync::Arc; + use adw::prelude::*; use gtk::GridLayoutChild; -use relm4::{gtk, ComponentParts, ComponentSender, RelmWidgetExt, Component}; +use relm4::{gtk, prelude::{AsyncComponent, AsyncComponentParts}, AsyncComponentSender, RelmWidgetExt}; mod widgets; +pub const APPLICATION_ID: &str = "xyz.vikanezrimaya.kittybox.Bowl"; #[tracker::track] #[derive(Debug)] @@ -20,7 +23,7 @@ pub struct PostComposerModel { } impl PostComposerModel { - async fn ai_generate_summary(_content: glib::GString, _sender: ComponentSender<Self>) -> PostComposerCommandOutput { + async fn ai_generate_summary(_content: glib::GString, _sender: AsyncComponentSender<Self>) -> PostComposerCommandOutput { // This is just a UI mock-up. In real-life conditions, this // would send a request to a summarizer API of some sort. // @@ -49,8 +52,8 @@ pub enum PostComposerCommandOutput { AiSummaryDone(Result<(), ()>) } -#[relm4::component(pub)] -impl Component for PostComposerModel { +#[relm4::component(pub async)] +impl AsyncComponent for PostComposerModel { /// The type of the messages that this component can receive. type Input = PostComposerInput; /// The type of the messages that this component can send. @@ -211,11 +214,11 @@ impl Component for PostComposerModel { } /// Initialize the UI and model. - fn init( + async fn init( init: Self::Init, window: Self::Root, - sender: ComponentSender<Self>, - ) -> relm4::ComponentParts<Self> { + sender: AsyncComponentSender<Self>, + ) -> AsyncComponentParts<Self> { let model = PostComposerModel { ai_summary_busy_guard: None, @@ -256,10 +259,16 @@ impl Component for PostComposerModel { widgets.content.set_layout_manager(Some(model.narrow_layout.clone())); - ComponentParts { model, widgets } + AsyncComponentParts { model, widgets } } - fn update(&mut self, message: Self::Input, sender: ComponentSender<Self>, _root: &Self::Root) { + async fn update_with_view( + &mut self, + widgets: &mut Self::Widgets, + message: Self::Input, + sender: AsyncComponentSender<Self>, + _root: &Self::Root + ) { self.reset(); // Reset the tracker match message { @@ -287,9 +296,11 @@ impl Component for PostComposerModel { log::warn!("Submitting posts is not yet implemented."); }, } + + self.update_view(widgets, sender); } - fn update_cmd(&mut self, msg: Self::CommandOutput, _sender: ComponentSender<Self>, _root: &Self::Root) { + async fn update_cmd(&mut self, msg: Self::CommandOutput, _sender: AsyncComponentSender<Self>, _root: &Self::Root) { match msg { Self::CommandOutput::AiSummaryDone(res) => { self.set_ai_summary_busy_guard(None); |