Use UDL interface to avoid passing around pointers
This commit is contained in:
parent
912949a079
commit
43421f6e54
|
@ -19,8 +19,7 @@ import androidx.compose.ui.unit.dp
|
|||
import androidx.compose.ui.unit.sp
|
||||
import dev.lonami.talaria.R
|
||||
import dev.lonami.talaria.ui.theme.TalariaTheme
|
||||
import uniffi.talaria.requestLoginCode
|
||||
import uniffi.talaria.signIn
|
||||
import uniffi.talaria.LoginProcedure
|
||||
|
||||
enum class LoginStage {
|
||||
ASK_PHONE,
|
||||
|
@ -106,7 +105,7 @@ fun LoginScreen(onConfirmOtp: () -> Unit, modifier: Modifier = Modifier) {
|
|||
var phone by remember { mutableStateOf("") }
|
||||
var otp by remember { mutableStateOf("") }
|
||||
|
||||
var tokenPtr by remember { mutableStateOf(0UL) }
|
||||
val loginProcedure by remember { mutableStateOf(LoginProcedure()) }
|
||||
|
||||
Column(
|
||||
modifier = modifier
|
||||
|
@ -128,7 +127,7 @@ fun LoginScreen(onConfirmOtp: () -> Unit, modifier: Modifier = Modifier) {
|
|||
phone,
|
||||
onPhoneChanged = { phone = it },
|
||||
onSendCode = {
|
||||
tokenPtr = requestLoginCode(phone)
|
||||
loginProcedure.requestLoginCode(phone)
|
||||
stage = LoginStage.ASK_CODE
|
||||
}
|
||||
)
|
||||
|
@ -136,7 +135,7 @@ fun LoginScreen(onConfirmOtp: () -> Unit, modifier: Modifier = Modifier) {
|
|||
otp,
|
||||
onOtpChanged = { otp = it },
|
||||
onConfirmOtp = {
|
||||
signIn(tokenPtr, otp)
|
||||
loginProcedure.signIn(otp)
|
||||
onConfirmOtp()
|
||||
}
|
||||
)
|
||||
|
|
|
@ -64,6 +64,10 @@ impl fmt::Display for NativeError {
|
|||
|
||||
impl std::error::Error for NativeError {}
|
||||
|
||||
struct LoginProcedure {
|
||||
token: Mutex<Option<LoginToken>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum MessageAck {
|
||||
Received,
|
||||
|
@ -199,15 +203,28 @@ pub fn need_login() -> Result<bool> {
|
|||
block_on(client.is_authorized()).map_err(|_| NativeError::Network)
|
||||
}
|
||||
|
||||
pub fn request_login_code(phone: String) -> Result<u64> {
|
||||
let client = CLIENT.get().ok_or(NativeError::Initialization)?;
|
||||
block_on(client.request_login_code(&phone, API_ID, API_HASH))
|
||||
.map(|token| Box::into_raw(Box::new(token)) as u64)
|
||||
.map_err(|_| NativeError::Network)
|
||||
}
|
||||
impl LoginProcedure {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
token: Mutex::new(None),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sign_in(token_ptr: u64, code: String) -> Result<()> {
|
||||
let token = unsafe { *Box::from_raw(token_ptr as *mut LoginToken) };
|
||||
fn request_login_code(&self, phone: String) -> Result<()> {
|
||||
let client = CLIENT.get().ok_or(NativeError::Initialization)?;
|
||||
let token = block_on(client.request_login_code(&phone, API_ID, API_HASH))
|
||||
.map_err(|_| NativeError::Network)?;
|
||||
*self.token.lock().unwrap() = Some(token);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn sign_in(&self, code: String) -> Result<()> {
|
||||
let token = self
|
||||
.token
|
||||
.lock()
|
||||
.unwrap()
|
||||
.take()
|
||||
.ok_or(NativeError::Initialization)?;
|
||||
let client = CLIENT.get().ok_or(NativeError::Initialization)?;
|
||||
|
||||
block_on(client.sign_in(&token, &code)).map_err(|_| NativeError::Network)?;
|
||||
|
@ -254,6 +271,7 @@ pub fn sign_in(token_ptr: u64, code: String) -> Result<()> {
|
|||
db::update_session(conn, &session).map_err(|_| NativeError::Database)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_dialogs() -> Result<Vec<Dialog>> {
|
||||
|
|
|
@ -25,6 +25,14 @@ dictionary Dialog {
|
|||
boolean pinned;
|
||||
};
|
||||
|
||||
interface LoginProcedure {
|
||||
constructor();
|
||||
[Throws=NativeError]
|
||||
void request_login_code(string phone);
|
||||
[Throws=NativeError]
|
||||
void sign_in(string code);
|
||||
};
|
||||
|
||||
namespace talaria {
|
||||
[Throws=NativeError]
|
||||
void init_database(string path);
|
||||
|
@ -33,10 +41,6 @@ namespace talaria {
|
|||
[Throws=NativeError]
|
||||
boolean need_login();
|
||||
[Throws=NativeError]
|
||||
u64 request_login_code(string phone);
|
||||
[Throws=NativeError]
|
||||
void sign_in(u64 tokenPtr, string code);
|
||||
[Throws=NativeError]
|
||||
sequence<Dialog> get_dialogs();
|
||||
[Throws=NativeError]
|
||||
void send_message(string packed, string text);
|
||||
|
|
Loading…
Reference in New Issue