Load more dialog data from the API

This commit is contained in:
Lonami Exo 2022-10-23 20:25:45 +02:00
parent 51ef704cf7
commit 442b019134
5 changed files with 129 additions and 6 deletions

View File

@ -14,6 +14,11 @@ object Native {
external fun dialogCount(dialogsPtr: Long): Int external fun dialogCount(dialogsPtr: Long): Int
external fun dialogPacked(dialogsPtr: Long, index: Int): String external fun dialogPacked(dialogsPtr: Long, index: Int): String
external fun dialogTitle(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 freeDialogs(dialogsPtr: Long)
external fun sendMessage(packed: String, text: String) external fun sendMessage(packed: String, text: String)
} }

View File

@ -5,6 +5,7 @@ import dev.lonami.talaria.models.Dialog
import dev.lonami.talaria.models.MessageAck import dev.lonami.talaria.models.MessageAck
import dev.lonami.talaria.models.MessagePreview import dev.lonami.talaria.models.MessagePreview
import java.time.LocalDateTime import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
interface DialogRepository { interface DialogRepository {
fun loadDialogs(): List<Dialog>; fun loadDialogs(): List<Dialog>;
@ -19,14 +20,23 @@ class NativeDialogRepository : DialogRepository {
for (i in 0 until dialogCount) { for (i in 0 until dialogCount) {
dialogs.add( dialogs.add(
Dialog( Dialog(
id = Native.dialogPacked(dialogPtr, i),
title = Native.dialogTitle(dialogPtr, i), title = Native.dialogTitle(dialogPtr, i),
lastMessage = MessagePreview( lastMessage = MessagePreview(
sender = "", sender = Native.dialogSender(dialogPtr, i),
text = Native.dialogPacked(dialogPtr, i), text = Native.dialogText(dialogPtr, i),
date = LocalDateTime.now(), date = LocalDateTime.parse(
ack = MessageAck.RECEIVED 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) { for (i in 0 until 10) {
dialogs.add( dialogs.add(
Dialog( Dialog(
id = "$i",
title = "Sample Dialog $i", title = "Sample Dialog $i",
lastMessage = if (i % 4 == 3) { lastMessage = if (i % 4 == 3) {
null null

View File

@ -1,6 +1,7 @@
package dev.lonami.talaria.models package dev.lonami.talaria.models
data class Dialog( data class Dialog(
val id: String,
val title: String, val title: String,
val lastMessage: MessagePreview?, val lastMessage: MessagePreview?,
val pinned: Boolean val pinned: Boolean

View File

@ -106,7 +106,7 @@ fun DialogList(dialogs: List<Dialog>, onDialogSelected: (String) -> Unit) {
LazyColumn { LazyColumn {
items(dialogs.size) { items(dialogs.size) {
DialogCard(dialogs[it], onDialogSelected = { DialogCard(dialogs[it], onDialogSelected = {
onDialogSelected(dialogs[it].lastMessage!!.text) onDialogSelected(dialogs[it].id)
}) })
} }
} }

View File

@ -6,6 +6,7 @@ mod db;
use grammers_client::types::{Dialog, LoginToken}; use grammers_client::types::{Dialog, LoginToken};
use grammers_client::{Client, Config, InitParams}; use grammers_client::{Client, Config, InitParams};
use grammers_session::{PackedChat, Session, UpdateState}; use grammers_session::{PackedChat, Session, UpdateState};
use grammers_tl_types as tl;
use jni::objects::{JObject, JString}; use jni::objects::{JObject, JString};
use jni::sys::{jboolean, jint, jlong, jstring}; use jni::sys::{jboolean, jint, jlong, jstring};
use jni::JNIEnv; use jni::JNIEnv;
@ -335,6 +336,111 @@ pub unsafe extern "C" fn Java_dev_lonami_talaria_bindings_Native_dialogTitle(
output.into_inner() 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<Dialog>);
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<Dialog>);
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<Dialog>);
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<Dialog>);
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<Dialog>);
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] #[no_mangle]
pub unsafe extern "C" fn Java_dev_lonami_talaria_bindings_Native_freeDialogs( pub unsafe extern "C" fn Java_dev_lonami_talaria_bindings_Native_freeDialogs(
_: JNIEnv, _: JNIEnv,