kkx/kkx/src/main.rs

116 lines
2.5 KiB
Rust

use std::time::Duration;
use kkdisp::{
component::{Plan, Widget},
theme::Color,
token::Token,
view::Event,
Action, PlanLayers, View,
};
use login::LoginPrompt;
use misskey::{
websocket::WebSocketClientBuilder, HttpClient, WebSocketClient,
};
use tokio::sync::mpsc::{self, Receiver, Sender};
mod login;
#[tokio::main]
async fn main() {
kkdisp::run(AppView::default()).await.unwrap();
std::process::exit(0);
}
#[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()?)
}
}
pub enum Page {
Login(LoginPrompt),
}
impl Page {
fn init(&self) -> Result<PlanLayers, anyhow::Error> {
match self {
Page::Login(log) => Ok(vec![log.plan()]),
}
}
}
pub struct AppView {
recv: Receiver<Message>,
auth: Option<AuthDetail>,
page: Page,
}
impl Default for AppView {
fn default() -> Self {
let (snd, recv) = mpsc::channel(256);
tokio::spawn(async move {
// std::thread::sleep(Duration::from_secs(10));
// snd.send(Token::text("this is an event").centered())
// .await
// .unwrap();
});
Self {
recv,
page: Page::Login(LoginPrompt::new()),
auth: None,
}
}
}
pub enum Message {}
#[async_trait::async_trait]
impl View for AppView {
type Message = Message;
async fn init(
&mut self,
) -> std::result::Result<kkdisp::PlanLayers, anyhow::Error> {
self.page.init()
}
async fn update(
&mut self,
event: Event<Self::Message>,
) -> std::result::Result<Action, anyhow::Error> {
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,
},
}
}
}