kkx/kkx/src/main.rs

116 lines
2.5 KiB
Rust
Raw Normal View History

use std::time::Duration;
2023-01-23 00:18:55 +00:00
use kkdisp::{
component::{Plan, Widget},
theme::Color,
token::Token,
view::Event,
2023-01-26 14:27:25 +00:00
Action, PlanLayers, View,
2023-01-23 00:18:55 +00:00
};
2023-01-26 14:27:25 +00:00
use login::LoginPrompt;
use misskey::{
websocket::WebSocketClientBuilder, HttpClient, WebSocketClient,
};
2023-01-26 14:27:25 +00:00
use tokio::sync::mpsc::{self, Receiver, Sender};
mod login;
2023-01-19 23:15:50 +00:00
2023-01-23 00:18:55 +00:00
#[tokio::main]
async fn main() {
2023-01-26 14:27:25 +00:00
kkdisp::run(AppView::default()).await.unwrap();
std::process::exit(0);
2023-01-23 00:18:55 +00:00
}
#[derive(Clone, Debug)]
pub struct AuthDetail {
pub hostname: String,
pub token: String,
}
impl AuthDetail {
pub async fn streaming(
&self,
) -> Result<WebSocketClient, anyhow::Error> {
Ok(WebSocketClientBuilder::with_host(&self.hostname)
.token(&self.token)
.connect()
.await?)
}
pub async fn normal(&self) -> Result<HttpClient, anyhow::Error> {
Ok(HttpClient::builder(
format!("https://{}", &self.hostname).as_str(),
)
.token(&self.token)
.build()?)
}
}
2023-01-26 14:27:25 +00:00
pub enum Page {
Login(LoginPrompt),
2023-01-23 00:18:55 +00:00
}
2023-01-26 14:27:25 +00:00
impl Page {
fn init(&self) -> Result<PlanLayers, anyhow::Error> {
match self {
2023-01-27 12:35:24 +00:00
Page::Login(log) => Ok(vec![log.plan()]),
2023-01-26 14:27:25 +00:00
}
}
}
pub struct AppView {
recv: Receiver<Message>,
auth: Option<AuthDetail>,
2023-01-26 14:27:25 +00:00
page: Page,
}
impl Default for AppView {
2023-01-23 00:18:55 +00:00
fn default() -> Self {
let (snd, recv) = mpsc::channel(256);
tokio::spawn(async move {
2023-01-26 14:27:25 +00:00
// std::thread::sleep(Duration::from_secs(10));
// snd.send(Token::text("this is an event").centered())
// .await
// .unwrap();
});
2023-01-23 00:18:55 +00:00
Self {
recv,
2023-01-26 14:27:25 +00:00
page: Page::Login(LoginPrompt::new()),
auth: None,
2023-01-23 00:18:55 +00:00
}
}
}
2023-01-26 14:27:25 +00:00
pub enum Message {}
#[async_trait::async_trait]
2023-01-26 14:27:25 +00:00
impl View for AppView {
type Message = Message;
2023-01-24 12:54:39 +00:00
async fn init(
2023-01-23 00:18:55 +00:00
&mut self,
) -> std::result::Result<kkdisp::PlanLayers, anyhow::Error> {
2023-01-26 14:27:25 +00:00
self.page.init()
2023-01-23 00:18:55 +00:00
}
async fn update(
2023-01-23 00:18:55 +00:00
&mut self,
event: Event<Self::Message>,
) -> std::result::Result<Action, anyhow::Error> {
2023-01-26 14:27:25 +00:00
let page = &mut self.page;
match page {
Page::Login(login) => login.update(event).await,
}
}
fn query(&mut self) -> Option<Self::Message> {
match &mut self.page {
Page::Login(log) => match log.query() {
Some(auth) => {
self.auth = Some(auth);
None
}
None => None,
},
2023-01-23 00:18:55 +00:00
}
}
2023-01-17 02:06:16 +00:00
}