From e6e4de9e15833e4042be5cd71944e9b8929346d4 Mon Sep 17 00:00:00 2001 From: Vika Date: Tue, 20 Aug 2024 18:31:43 +0300 Subject: Make the post composer asynchronous This makes it able to execute unsendable futures, and unlocks ability for us to do asynchronous initialization and updates. --- src/lib.rs | 31 +++++++++++++++++++++---------- src/main.rs | 9 +++++++-- 2 files changed, 28 insertions(+), 12 deletions(-) (limited to 'src') 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) -> PostComposerCommandOutput { + async fn ai_generate_summary(_content: glib::GString, _sender: AsyncComponentSender) -> 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, - ) -> relm4::ComponentParts { + sender: AsyncComponentSender, + ) -> AsyncComponentParts { 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, _root: &Self::Root) { + async fn update_with_view( + &mut self, + widgets: &mut Self::Widgets, + message: Self::Input, + sender: AsyncComponentSender, + _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, _root: &Self::Root) { + async fn update_cmd(&mut self, msg: Self::CommandOutput, _sender: AsyncComponentSender, _root: &Self::Root) { match msg { Self::CommandOutput::AiSummaryDone(res) => { self.set_ai_summary_busy_guard(None); diff --git a/src/main.rs b/src/main.rs index 4f4688e..acb9f66 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ use relm4::{ComponentParts, ComponentSender, RelmApp, Component, ComponentContro use bowl::PostComposerModel; -const APPLICATION_ID: &str = "xyz.vikanezrimaya.kittybox.Bowl"; +use bowl::APPLICATION_ID; static GLIB_LOGGER: glib::GlibLogger = glib::GlibLogger::new( glib::GlibLoggerFormat::Plain, @@ -15,5 +15,10 @@ fn main() { log::set_max_level(log::LevelFilter::Debug); let app = RelmApp::new(APPLICATION_ID); - app.run::( () ); + app.run_async::( + bowl::micropub::Client::new( + glib::Uri::parse(&std::env::var("MICROPUB_URI").unwrap(), glib::UriFlags::NONE).unwrap(), + std::env::var("MICROPUB_TOKEN").unwrap(), + ) + ); } -- cgit 1.4.1