From 442b01913429c1de0ae3c745db366d406ab94d12 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sun, 23 Oct 2022 20:25:45 +0200 Subject: [PATCH] Load more dialog data from the API --- .../dev/lonami/talaria/bindings/Native.kt | 5 + .../lonami/talaria/data/DialogRepository.kt | 21 +++- .../java/dev/lonami/talaria/models/Dialog.kt | 1 + .../lonami/talaria/ui/screens/DialogScreen.kt | 2 +- native/src/lib.rs | 106 ++++++++++++++++++ 5 files changed, 129 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/dev/lonami/talaria/bindings/Native.kt b/app/src/main/java/dev/lonami/talaria/bindings/Native.kt index e666f72..b65436b 100644 --- a/app/src/main/java/dev/lonami/talaria/bindings/Native.kt +++ b/app/src/main/java/dev/lonami/talaria/bindings/Native.kt @@ -14,6 +14,11 @@ object Native { external fun dialogCount(dialogsPtr: Long): Int external fun dialogPacked(dialogsPtr: Long, index: Int): String external fun dialogTitle(dialogsPtr: Long, index: Int): String + external fun dialogSender(dialogsPtr: Long, index: Int): String + external fun dialogText(dialogsPtr: Long, index: Int): String + external fun dialogTime(dialogsPtr: Long, index: Int): String + external fun dialogAck(dialogsPtr: Long, index: Int): Int + external fun dialogPin(dialogsPtr: Long, index: Int): Int external fun freeDialogs(dialogsPtr: Long) external fun sendMessage(packed: String, text: String) } diff --git a/app/src/main/java/dev/lonami/talaria/data/DialogRepository.kt b/app/src/main/java/dev/lonami/talaria/data/DialogRepository.kt index c2d8377..f482244 100644 --- a/app/src/main/java/dev/lonami/talaria/data/DialogRepository.kt +++ b/app/src/main/java/dev/lonami/talaria/data/DialogRepository.kt @@ -5,6 +5,7 @@ import dev.lonami.talaria.models.Dialog import dev.lonami.talaria.models.MessageAck import dev.lonami.talaria.models.MessagePreview import java.time.LocalDateTime +import java.time.format.DateTimeFormatter interface DialogRepository { fun loadDialogs(): List; @@ -19,14 +20,23 @@ class NativeDialogRepository : DialogRepository { for (i in 0 until dialogCount) { dialogs.add( Dialog( + id = Native.dialogPacked(dialogPtr, i), title = Native.dialogTitle(dialogPtr, i), lastMessage = MessagePreview( - sender = "", - text = Native.dialogPacked(dialogPtr, i), - date = LocalDateTime.now(), - ack = MessageAck.RECEIVED + sender = Native.dialogSender(dialogPtr, i), + text = Native.dialogText(dialogPtr, i), + date = LocalDateTime.parse( + Native.dialogTime(dialogPtr, i), + DateTimeFormatter.ISO_OFFSET_DATE_TIME + ), + ack = when (Native.dialogAck(dialogPtr, i)) { + 0 -> MessageAck.RECEIVED + 1 -> MessageAck.SENT + 2 -> MessageAck.SEEN + else -> MessageAck.RECEIVED + } ), - pinned = false + pinned = Native.dialogPin(dialogPtr, i) != 0 ) ) } @@ -42,6 +52,7 @@ class MockDialogRepository : DialogRepository { for (i in 0 until 10) { dialogs.add( Dialog( + id = "$i", title = "Sample Dialog $i", lastMessage = if (i % 4 == 3) { null diff --git a/app/src/main/java/dev/lonami/talaria/models/Dialog.kt b/app/src/main/java/dev/lonami/talaria/models/Dialog.kt index 220b762..37eb7c3 100644 --- a/app/src/main/java/dev/lonami/talaria/models/Dialog.kt +++ b/app/src/main/java/dev/lonami/talaria/models/Dialog.kt @@ -1,6 +1,7 @@ package dev.lonami.talaria.models data class Dialog( + val id: String, val title: String, val lastMessage: MessagePreview?, val pinned: Boolean diff --git a/app/src/main/java/dev/lonami/talaria/ui/screens/DialogScreen.kt b/app/src/main/java/dev/lonami/talaria/ui/screens/DialogScreen.kt index 3a42cd1..790d650 100644 --- a/app/src/main/java/dev/lonami/talaria/ui/screens/DialogScreen.kt +++ b/app/src/main/java/dev/lonami/talaria/ui/screens/DialogScreen.kt @@ -106,7 +106,7 @@ fun DialogList(dialogs: List, onDialogSelected: (String) -> Unit) { LazyColumn { items(dialogs.size) { DialogCard(dialogs[it], onDialogSelected = { - onDialogSelected(dialogs[it].lastMessage!!.text) + onDialogSelected(dialogs[it].id) }) } } diff --git a/native/src/lib.rs b/native/src/lib.rs index b14759a..70be889 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -6,6 +6,7 @@ mod db; use grammers_client::types::{Dialog, LoginToken}; use grammers_client::{Client, Config, InitParams}; use grammers_session::{PackedChat, Session, UpdateState}; +use grammers_tl_types as tl; use jni::objects::{JObject, JString}; use jni::sys::{jboolean, jint, jlong, jstring}; use jni::JNIEnv; @@ -335,6 +336,111 @@ pub unsafe extern "C" fn Java_dev_lonami_talaria_bindings_Native_dialogTitle( output.into_inner() } +#[no_mangle] +pub unsafe extern "C" fn Java_dev_lonami_talaria_bindings_Native_dialogSender( + env: JNIEnv, + _: JObject, + dialogs_ptr: jlong, + index: jint, +) -> jstring { + let dialogs = &mut *(dialogs_ptr as *mut Vec); + + let sender = if let Some(msg) = dialogs[index as usize].last_message.as_ref() { + if let Some(sender) = msg.sender() { + sender.name().to_string() + } else { + "unknown".to_string() + } + } else { + String::new() + }; + let output = env.new_string(sender).unwrap(); + output.into_inner() +} + +#[no_mangle] +pub unsafe extern "C" fn Java_dev_lonami_talaria_bindings_Native_dialogText( + env: JNIEnv, + _: JObject, + dialogs_ptr: jlong, + index: jint, +) -> jstring { + let dialogs = &mut *(dialogs_ptr as *mut Vec); + + let text = if let Some(msg) = dialogs[index as usize].last_message.as_ref() { + msg.text() + } else { + "" + }; + let output = env.new_string(text).unwrap(); + output.into_inner() +} + +#[no_mangle] +pub unsafe extern "C" fn Java_dev_lonami_talaria_bindings_Native_dialogTime( + env: JNIEnv, + _: JObject, + dialogs_ptr: jlong, + index: jint, +) -> jstring { + let dialogs = &mut *(dialogs_ptr as *mut Vec); + + let time = if let Some(msg) = dialogs[index as usize].last_message.as_ref() { + msg.date().to_rfc3339().to_string() + } else { + String::new() + }; + let output = env.new_string(time).unwrap(); + output.into_inner() +} + +#[no_mangle] +pub unsafe extern "C" fn Java_dev_lonami_talaria_bindings_Native_dialogAck( + _: JNIEnv, + _: JObject, + dialogs_ptr: jlong, + index: jint, +) -> jint { + let dialogs = &mut *(dialogs_ptr as *mut Vec); + + let dialog = &dialogs[index as usize]; + let ack = if let Some(msg) = dialog.last_message.as_ref() { + if msg.outgoing() { + match &dialog.dialog { + tl::enums::Dialog::Dialog(d) => { + if msg.id() <= d.read_inbox_max_id { + 2 + } else { + 1 + } + } + tl::enums::Dialog::Folder(_) => 0, + } + } else { + 0 + } + } else { + 0 + }; + ack +} + +#[no_mangle] +pub unsafe extern "C" fn Java_dev_lonami_talaria_bindings_Native_dialogPin( + _: JNIEnv, + _: JObject, + dialogs_ptr: jlong, + index: jint, +) -> jint { + let dialogs = &mut *(dialogs_ptr as *mut Vec); + + let pinned = match &dialogs[index as usize].dialog { + tl::enums::Dialog::Dialog(d) => d.pinned, + tl::enums::Dialog::Folder(f) => f.pinned, + }; + pinned as jint +} + #[no_mangle] pub unsafe extern "C" fn Java_dev_lonami_talaria_bindings_Native_freeDialogs( _: JNIEnv,