Skip to content

Commit

Permalink
Simplify flutter native api
Browse files Browse the repository at this point in the history
  • Loading branch information
Berrysoft committed Sep 23, 2024
1 parent f747aad commit 41dc2f5
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 45 deletions.
61 changes: 30 additions & 31 deletions tunet-flutter/lib/runtime.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,41 +145,41 @@ class ManagedRuntime extends NotifyPropertyChanged {
);

await for (final msg in runtime.start(config: config)) {
await msg.when<Future<void>>(
credential: (username) async {
await queueState();
await queueDetails();
await queueOnlines();
msg.when<void>(
credential: (username) {
queueState();
queueDetails();
queueOnlines();
this.username = username;
},
state: (state) async {
await queueFlux();
state: (state) {
queueFlux();
this.state = state;
},
status: (status) async {
await queueState();
status: (status) {
queueState();
this.status = status;
},
log: (logText) async {
log: (logText) {
this.logText = logText;
},
flux: (netFlux) async {
flux: (netFlux) {
this.netFlux = netFlux;
},
online: (onlines) async {
online: (onlines) {
this.onlines = onlines;
},
details: (details, daily) async {
details: (details, daily) {
detailsData.setData(details);
this.daily = daily;
},
logBusy: (logBusy) async {
logBusy: (logBusy) {
this.logBusy = logBusy;
},
onlineBusy: (onlineBusy) async {
onlineBusy: (onlineBusy) {
this.onlineBusy = onlineBusy;
},
detailBusy: (detailBusy) async {
detailBusy: (detailBusy) {
this.detailBusy = detailBusy;
},
);
Expand Down Expand Up @@ -223,34 +223,33 @@ class ManagedRuntime extends NotifyPropertyChanged {
await storage.write(key: '$u@tunet', value: p);
}

Future<void> queueState({NetState? s}) => runtime.queueState(s: s);
Future<void> queueStatus() async {
await runtime.queueStatus(s: await loadStatus());
void queueState({NetState? s}) => runtime.queueState(s: s);
void queueStatus() async {
runtime.queueStatus(s: await loadStatus());
}

Future<void> queueCredential({required String u, required String p}) async {
void queueCredential({required String u, required String p}) async {
await saveCredential(u, p);
await runtime.queueCredential(u: u, p: p);
runtime.queueCredential(u: u, p: p);
}

Future<void> queueLogin() => runtime.queueLogin();
Future<void> queueLogout() => runtime.queueLogout();
Future<void> queueFlux() => runtime.queueFlux();
void queueLogin() => runtime.queueLogin();
void queueLogout() => runtime.queueLogout();
void queueFlux() => runtime.queueFlux();

Future<void> queueDetails() async {
void queueDetails() {
detailsData.setData(List.empty());
daily = null;
await runtime.queueDetails();
runtime.queueDetails();
}

Future<void> queueOnlines() async {
void queueOnlines() {
onlines = null;
await runtime.queueOnlines();
runtime.queueOnlines();
}

Future<void> queueConnect({required Ipv4AddrWrap ip}) =>
runtime.queueConnect(ip: ip);
Future<void> queueDrop({required List<Ipv4AddrWrap> ips}) =>
void queueConnect({required Ipv4AddrWrap ip}) => runtime.queueConnect(ip: ip);
void queueDrop({required List<Ipv4AddrWrap> ips}) =>
runtime.queueDrop(ips: ips);
}

Expand Down
31 changes: 21 additions & 10 deletions tunet-flutter/native/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::frb_generated::{RustOpaque, StreamSink};
use crate::frb_generated::StreamSink;
use anyhow::Result;
use chrono::Datelike;
use flutter_rust_bridge::{frb, setup_default_user_utils};

pub use netstatus::NetStatus;
use std::net::Ipv6Addr;
pub use std::{net::Ipv4Addr, sync::Mutex};
use std::{net::Ipv6Addr, sync::Arc};
pub use tunet_helper::{
usereg::{NetDateTime, NetDetail},
Balance, Duration as NewDuration, Flux, NaiveDateTime, NaiveDuration as Duration, NetFlux,
Expand Down Expand Up @@ -121,9 +121,9 @@ pub struct RuntimeStartConfig {
}

pub struct Runtime {
pub arx: RustOpaque<Mutex<Option<flume::Receiver<Action>>>>,
pub urx: RustOpaque<Mutex<Option<flume::Receiver<UpdateMsg>>>>,
pub model: RustOpaque<Mutex<Model>>,
arx: Arc<Mutex<Option<flume::Receiver<Action>>>>,
urx: Arc<Mutex<Option<flume::Receiver<UpdateMsg>>>>,
model: Arc<Mutex<Model>>,
}

impl Runtime {
Expand All @@ -141,16 +141,17 @@ impl Runtime {
);
setup_default_user_utils();

let (atx, arx) = flume::bounded(32);
let (utx, urx) = flume::bounded(32);
let (atx, arx) = flume::unbounded();
let (utx, urx) = flume::unbounded();
let model = Model::new(atx, utx)?;
Ok(Self {
arx: RustOpaque::new(Mutex::new(Some(arx))),
urx: RustOpaque::new(Mutex::new(Some(urx))),
model: RustOpaque::new(Mutex::new(model)),
arx: Arc::new(Mutex::new(Some(arx))),
urx: Arc::new(Mutex::new(Some(urx))),
model: Arc::new(Mutex::new(model)),
})
}

#[frb(sync)]
pub fn start(&self, sink: StreamSink<UpdateMsgWrap>, config: RuntimeStartConfig) {
let model = self.model.clone();
let arx = self.arx.lock().unwrap().take().unwrap();
Expand Down Expand Up @@ -246,42 +247,52 @@ impl Runtime {
self.model.lock().unwrap().queue(a);
}

#[frb(sync)]
pub fn queue_credential(&self, u: String, p: String) {
self.queue(Action::Credential(u, p));
}

#[frb(sync)]
pub fn queue_login(&self) {
self.queue(Action::Login);
}

#[frb(sync)]
pub fn queue_logout(&self) {
self.queue(Action::Logout);
}

#[frb(sync)]
pub fn queue_flux(&self) {
self.queue(Action::Flux);
}

#[frb(sync)]
pub fn queue_state(&self, s: Option<NetState>) {
self.queue(Action::State(s));
}

#[frb(sync)]
pub fn queue_status(&self, s: NetStatus) {
self.queue(Action::Status(Some(s)));
}

#[frb(sync)]
pub fn queue_details(&self) {
self.queue(Action::Details);
}

#[frb(sync)]
pub fn queue_onlines(&self) {
self.queue(Action::Online);
}

#[frb(sync)]
pub fn queue_connect(&self, ip: Ipv4AddrWrap) {
self.queue(Action::Connect(Ipv4Addr::from(ip.octets)));
}

#[frb(sync)]
pub fn queue_drop(&self, ips: Vec<Ipv4AddrWrap>) {
for ip in ips {
self.queue(Action::Drop(Ipv4Addr::from(ip.octets)));
Expand Down
16 changes: 12 additions & 4 deletions tunet-model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ impl Model {
action_sender.send_async(Action::Online).await?;
}
Err(e) => {
action_sender.send_async(Action::LogDone(e.to_string())).await?;
action_sender
.send_async(Action::LogDone(e.to_string()))
.await?;
}
}
anyhow::Ok(())
Expand All @@ -210,7 +212,9 @@ impl Model {
action_sender.send_async(Action::Online).await?;
}
Err(e) => {
action_sender.send_async(Action::LogDone(e.to_string())).await?;
action_sender
.send_async(Action::LogDone(e.to_string()))
.await?;
}
}
anyhow::Ok(())
Expand Down Expand Up @@ -368,7 +372,9 @@ impl Model {
.await?;
}
Err(e) => {
action_sender.send_async(Action::LogDone(e.to_string())).await?;
action_sender
.send_async(Action::LogDone(e.to_string()))
.await?;
}
}
anyhow::Ok(())
Expand All @@ -393,7 +399,9 @@ impl Model {
.await?;
}
Err(e) => {
action_sender.send_async(Action::LogDone(e.to_string())).await?;
action_sender
.send_async(Action::LogDone(e.to_string()))
.await?;
}
}
anyhow::Ok(())
Expand Down

0 comments on commit 41dc2f5

Please sign in to comment.