forked from Lonami/Talaria
Load more dialog data from the API
This commit is contained in:
parent
51ef704cf7
commit
442b019134
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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<Dialog>;
|
||||
|
@ -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
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package dev.lonami.talaria.models
|
||||
|
||||
data class Dialog(
|
||||
val id: String,
|
||||
val title: String,
|
||||
val lastMessage: MessagePreview?,
|
||||
val pinned: Boolean
|
||||
|
|
|
@ -106,7 +106,7 @@ fun DialogList(dialogs: List<Dialog>, onDialogSelected: (String) -> Unit) {
|
|||
LazyColumn {
|
||||
items(dialogs.size) {
|
||||
DialogCard(dialogs[it], onDialogSelected = {
|
||||
onDialogSelected(dialogs[it].lastMessage!!.text)
|
||||
onDialogSelected(dialogs[it].id)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<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]
|
||||
pub unsafe extern "C" fn Java_dev_lonami_talaria_bindings_Native_freeDialogs(
|
||||
_: JNIEnv,
|
||||
|
|
Loading…
Reference in New Issue