summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2024-08-20 18:31:43 +0300
committerVika <vika@fireburn.ru>2024-08-20 18:31:43 +0300
commite6e4de9e15833e4042be5cd71944e9b8929346d4 (patch)
tree344e43e88c4a14d7fcc384df4a60431acfe03056 /src
parentd0353dbc6ac624f63240ec64b83d238499cb0c7c (diff)
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')
-rw-r--r--src/lib.rs31
-rw-r--r--src/main.rs9
2 files changed, 28 insertions, 12 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);
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::<PostComposerModel>( () );
+    app.run_async::<PostComposerModel>(
+        bowl::micropub::Client::new(
+            glib::Uri::parse(&std::env::var("MICROPUB_URI").unwrap(), glib::UriFlags::NONE).unwrap(),
+            std::env::var("MICROPUB_TOKEN").unwrap(),
+        )
+    );
 }