yayan 1 жил өмнө
parent
commit
e1df56f490

+ 12 - 17
app/src/main/java/io/nexilis/alpha/ui/main/Chat.kt

@@ -2,27 +2,21 @@ package io.nexilis.alpha.ui.main
 
 import androidx.compose.foundation.layout.*
 import androidx.compose.foundation.lazy.LazyColumn
+import androidx.compose.foundation.lazy.items
 import androidx.compose.foundation.lazy.rememberLazyListState
-import androidx.compose.foundation.shape.CircleShape
-import androidx.compose.material.icons.Icons
-import androidx.compose.material.icons.filled.KeyboardArrowDown
 import androidx.compose.material3.*
 import androidx.compose.runtime.*
+import androidx.compose.runtime.livedata.observeAsState
 import androidx.compose.ui.Modifier
 import androidx.compose.ui.graphics.Color
-import androidx.compose.ui.unit.dp
-import androidx.constraintlayout.compose.ConstraintLayout
 import androidx.hilt.navigation.compose.hiltViewModel
 import androidx.navigation.NavHostController
-import androidx.paging.compose.collectAsLazyPagingItems
-import androidx.paging.compose.itemKey
 import io.nexilis.alpha.ui.components.ContentChat
 import io.nexilis.alpha.ui.components.InputChat
 import io.nexilis.alpha.ui.components.LeftBubbleChat
 import io.nexilis.alpha.ui.components.RightBubbleChat
 import io.nexilis.service.data.entities.Buddy
 import io.nexilis.service.data.viewmodels.MessageViewModel
-import kotlinx.coroutines.launch
 
 @OptIn(ExperimentalLayoutApi::class)
 @Composable
@@ -32,10 +26,14 @@ fun Chat(
     me: Buddy
 ) {
     val messageModel: MessageViewModel = hiltViewModel()
-    val flow = remember { messageModel.getOpposite(pin) }
-    val lazyPagingItems = flow.collectAsLazyPagingItems()
+    val messages by messageModel.getOpposite(pin).observeAsState()
     val listState = rememberLazyListState()
-    val scope = rememberCoroutineScope()
+    messages?.let {
+        LaunchedEffect(key1 = it.size) {
+            if (it.isNotEmpty())
+                listState.scrollToItem(it.size - 1)
+        }
+    }
     Column(
         modifier = Modifier
             .fillMaxSize()
@@ -46,13 +44,10 @@ fun Chat(
             modifier = Modifier
                 .weight(1f),
             state = listState,
-            reverseLayout = true
+            reverseLayout = false
         ) {
-            items(
-                count = lazyPagingItems.itemCount,
-//                key = lazyPagingItems.itemKey { it.message_id }
-            ) { index ->
-                lazyPagingItems[index]?.let { message ->
+            messages?.let {
+                items(it, key = { m -> m.message_id }) { message ->
                     ListItem(modifier = Modifier.fillMaxWidth(), headlineContent = {
                         Row(
                             modifier = Modifier.fillMaxWidth(),

+ 2 - 2
cpaas-lite/src/main/java/io/nexilis/service/data/daos/MessageDao.kt

@@ -20,8 +20,8 @@ abstract class MessageDao {
     @Query("select * from Message where message_id = :id")
     abstract fun getSync(id: String): Message?
 
-    @Query("select * from Message where opposite_pin = :pin order by server_date desc limit :limit offset :offset")
-    abstract fun getOpposite(pin: String, offset: Int = -1, limit: Int = -1): PagingSource<Int, Message>
+    @Query("select * from (select * from Message where opposite_pin = :pin order by server_date desc limit :limit offset :offset) order by server_date asc")
+    abstract fun getOpposite(pin: String, offset: Int = -1, limit: Int = -1): LiveData<List<Message>>
 
     @Insert(onConflict = OnConflictStrategy.REPLACE)
     abstract suspend fun _insert(entity: Message)

+ 1 - 1
cpaas-lite/src/main/java/io/nexilis/service/data/repositories/MessageRepository.kt

@@ -41,7 +41,7 @@ class MessageRepository @Inject constructor(
     fun get(id: String): LiveData<Message> {
         return dao.get(id)
     }
-    fun getOpposite(pin: String, offset: Int = -1, limit: Int = -1): PagingSource<Int, Message> {
+    fun getOpposite(pin: String, offset: Int = -1, limit: Int = -1): LiveData<List<Message>> {
         return dao.getOpposite(pin, offset, limit)
     }
 

+ 12 - 8
cpaas-lite/src/main/java/io/nexilis/service/data/viewmodels/MessageViewModel.kt

@@ -34,14 +34,18 @@ class MessageViewModel @Inject constructor(
         return repository.get(id)
     }
 
-    fun getOpposite(pin: String, offset: Int = -1, limit: Int = -1): Flow<PagingData<Message>> {
-        val flow = Pager(
-            config = PagingConfig(pageSize = 20),
-            pagingSourceFactory = {
-                repository.getOpposite(pin = pin, offset = offset, limit = limit)
-            }
-        ).flow.cachedIn(viewModelScope)
-        return flow.map { pagingData -> pagingData.map { it } }
+//    fun getOpposite(pin: String, offset: Int = -1, limit: Int = -1): Flow<PagingData<Message>> {
+//        val flow = Pager(
+//            config = PagingConfig(pageSize = 20),
+//            pagingSourceFactory = {
+//                repository.getOpposite(pin = pin, offset = offset, limit = limit)
+//            }
+//        ).flow.cachedIn(viewModelScope)
+//        return flow.map { pagingData -> pagingData.map { it } }
+//    }
+
+    fun getOpposite(pin: String, offset: Int = -1, limit: Int = -1): LiveData<List<Message>> {
+        return repository.getOpposite(pin = pin, offset = offset, limit = limit)
     }
 
     fun insert(entity: Message) = viewModelScope.launch(Dispatchers.IO) {