use jni::objects::{GlobalRef, JClass, JObject, JString};
use jni::sys::{jint, jlong};
use jni::JNIEnv;
use std::{sync::mpsc, thread, time::Duration};
#[no_mangle]
pub extern "system" fn Java_com_github_lauset_vuetomx_rs_RustJNI_factFn(
mut env: JNIEnv,
_class: JClass,
n: jint,
callback: JObject,
) {
let i = n as i32;
let res: jint = (2..i + 1).product();
env.call_method(callback, "factCallback", "(I)V", &[res.into()])
.unwrap();
}
#[no_mangle]
pub extern "system" fn Java_com_github_lauset_vuetomx_rs_RustJNI_asyncFn(
env: JNIEnv,
_class: JClass,
count: jint,
sleep: jint,
callback: JObject,
) {
let jvm = env.get_java_vm().unwrap();
let callback = env.new_global_ref(callback).unwrap();
let (tx, rx) = mpsc::channel();
let s = sleep as u64;
let _ = thread::spawn(move || {
tx.send(()).unwrap();
let mut env = jvm.attach_current_thread().unwrap();
for i in 0..count + 1 {
let progress = (i * 1) as jint;
env.call_method(&callback, "asyncCallback", "(I)V", &[progress.into()])
.unwrap();
thread::sleep(Duration::from_millis(s));
}
});
rx.recv().unwrap();
}