yayan há 1 ano atrás
pai
commit
a56d32b1c2

+ 9 - 1
app/src/main/java/io/nexilis/alpha/ui/components/ContentChat.kt

@@ -60,6 +60,14 @@ fun ContentChat(modifier: Modifier = Modifier, message: Message) {
                 val exif = ExifInterface(it)
                 width = exif.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, 1)
                 height = exif.getAttributeInt(ExifInterface.TAG_IMAGE_LENGTH, 1)
+                val orientation = exif.getAttributeInt(
+                    ExifInterface.TAG_ORIENTATION,
+                    ExifInterface.ORIENTATION_UNDEFINED
+                )
+                if (orientation == ExifInterface.ORIENTATION_ROTATE_90 || orientation == ExifInterface.ORIENTATION_ROTATE_270) {
+                    height = exif.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, 1)
+                    width = exif.getAttributeInt(ExifInterface.TAG_IMAGE_LENGTH, 1)
+                }
             }
             if (width == 0) {
                 FileInputStream(file).use {
@@ -68,7 +76,7 @@ fun ContentChat(modifier: Modifier = Modifier, message: Message) {
                     if (height == 0) height = bitmap.height
                 }
             }
-            val aspectRatio = width / height.toFloat()
+            val aspectRatio = width.toFloat() / height.toFloat()
             Image(
                 painter = rememberAsyncImagePainter(uri),
                 contentDescription = "",

+ 39 - 33
app/src/main/java/io/nexilis/alpha/ui/components/InputChat.kt

@@ -6,7 +6,6 @@ import androidx.compose.foundation.layout.Row
 import androidx.compose.foundation.layout.fillMaxWidth
 import androidx.compose.foundation.layout.height
 import androidx.compose.foundation.layout.padding
-import androidx.compose.foundation.shape.CircleShape
 import androidx.compose.foundation.shape.RoundedCornerShape
 import androidx.compose.foundation.text.KeyboardOptions
 import androidx.compose.material.icons.Icons
@@ -64,37 +63,6 @@ fun InputChat(
     var openAlertDialog by remember { mutableStateOf(false) }
     val attachmentState = remember { mutableStateListOf<Uri>() }
     attachmentState.addAll(attachments)
-    val leadingIcon: (@Composable () -> Unit)? = if (enableAttachment) {
-        {
-            Attachments(
-                modifier = Modifier
-                    .height(300.dp)
-                    .fillMaxWidth()
-                    .padding(start = 16.dp, end = 16.dp, bottom = 8.dp)
-                    .graphicsLayer {
-                        shape = RoundedCornerShape(16.dp)
-                        clip = true
-                    }
-                    .background(MaterialTheme.colorScheme.surfaceContainerLowest),
-                onAttachment = { list ->
-                    attachmentState.addAll(list)
-                    val items = list.map {
-                        context.toAttachmentItem(it)
-                    }
-                    if (items.size == 1) {
-                        val params = AttachmentItemList(items)
-                        val data = Uri.encode(Json.encodeToString(params))
-                        navController.navigate(Screen.AttachmentCaption.route + "?data=${data}&pin=${pin}") {
-                            launchSingleTop = true
-                            restoreState = true
-                        }
-                    } else if (items.size > 1) {
-                        openAlertDialog = true
-                    }
-                }
-            )
-        }
-    } else null
     TextField(
         modifier = Modifier
             .fillMaxWidth()
@@ -109,7 +77,22 @@ fun InputChat(
         placeholder = {
             Text(text = placeHolderText, color = MaterialTheme.colorScheme.onSurface)
         },
-        leadingIcon = leadingIcon,
+        leadingIcon = addAttachment(enableAttachment = enableAttachment) { list ->
+            attachmentState.addAll(list)
+            val items = list.map {
+                context.toAttachmentItem(it)
+            }
+            if (items.size == 1) {
+                val params = AttachmentItemList(items)
+                val data = Uri.encode(Json.encodeToString(params))
+                navController.navigate(Screen.AttachmentCaption.route + "?data=${data}&pin=${pin}") {
+                    launchSingleTop = true
+                    restoreState = true
+                }
+            } else if (items.size > 1) {
+                openAlertDialog = true
+            }
+        },
         trailingIcon = {
             Row {
                 IconButton(onClick = {
@@ -215,4 +198,27 @@ fun InputChat(
             )
         }
     }
+}
+
+private fun addAttachment(
+    enableAttachment: Boolean = true,
+    onAttachment: (List<Uri>) -> Unit
+): (@Composable () -> Unit)? {
+    val leadingIcon: (@Composable () -> Unit)? = if (enableAttachment) {
+        {
+            Attachments(
+                modifier = Modifier
+                    .height(300.dp)
+                    .fillMaxWidth()
+                    .padding(start = 16.dp, end = 16.dp, bottom = 8.dp)
+                    .graphicsLayer {
+                        shape = RoundedCornerShape(16.dp)
+                        clip = true
+                    }
+                    .background(MaterialTheme.colorScheme.surfaceContainerLowest),
+                onAttachment = onAttachment
+            )
+        }
+    } else null
+    return leadingIcon
 }

+ 4 - 1
cpaas-lite/src/main/java/io/nexilis/service/data/daos/MessageStatusDao.kt

@@ -16,13 +16,16 @@ interface MessageStatusDao {
     @Query("select * from MessageStatus where message_id = :id")
     fun get(id: String): LiveData<List<MessageStatus>>
 
+    @Query("select * from MessageStatus where message_id = :id")
+    fun getAsync(id: String): List<MessageStatus>
+
     @Query("select * from MessageStatus where message_id = :id and f_pin = :pin")
     fun get(id: String, pin: String): LiveData<MessageStatus>
 
     @Insert(onConflict = OnConflictStrategy.REPLACE)
     suspend fun insert(entity: MessageStatus)
 
-    @Query("update MessageStatus set status = :status, last_update = :lastUpdate where message_id = :messageId and f_pin = :pin and status < :status")
+    @Query("update MessageStatus set status = :status, last_update = :lastUpdate where message_id = :messageId and f_pin = :pin")
     suspend fun updateStatus(
         status: Int,
         lastUpdate: Long = System.currentTimeMillis(),

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

@@ -9,6 +9,7 @@ import androidx.work.OutOfQuotaPolicy
 import androidx.work.WorkManager
 import androidx.work.WorkRequest
 import androidx.work.workDataOf
+import io.nexilis.service.apiScope
 import io.nexilis.service.core.Outgoing
 import io.nexilis.service.data.daos.GroupMemberDao
 import io.nexilis.service.data.daos.MessageDao
@@ -16,7 +17,9 @@ import io.nexilis.service.data.daos.MessageStatusDao
 import io.nexilis.service.data.entities.MainEntity
 import io.nexilis.service.data.entities.Message
 import io.nexilis.service.data.entities.MessageStatus
+import io.nexilis.service.tag
 import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.launch
 import kotlinx.coroutines.withContext
 import java.util.UUID
 import javax.inject.Inject
@@ -44,8 +47,7 @@ class MessageRepository @Inject constructor(
 
     suspend fun send(
         context: Context,
-        entity: MainEntity,
-        completion: (String, String, Int) -> Unit
+        entity: MainEntity
     ) {
         val message = entity as Message
         dao.insert(message)
@@ -93,20 +95,27 @@ class MessageRepository @Inject constructor(
                 .build()
         WorkManager.getInstance(context).enqueue(workRequest)
         withContext(Dispatchers.Main) {
-            WorkManager.getInstance(context).getWorkInfoByIdLiveData(id).observe(context as ComponentActivity) { workInfo ->
-                if (workInfo.state.isFinished) {
-                    Log.d(
-                        "SAPI",
-                        "update from work:${workInfo.outputData.getString("message_id")}:${
-                            workInfo.outputData.getString("opposite_pin")
-                        }:${workInfo.outputData.getInt("status", 0)}"
-                    )
-                    completion(workInfo.outputData.getString("message_id") ?: "",
-                        workInfo.outputData.getString("opposite_pin") ?: "",
-                        workInfo.outputData.getInt("status", 0)
+            WorkManager.getInstance(context).getWorkInfoByIdLiveData(id)
+                .observe(context as ComponentActivity) { workInfo ->
+                    if (workInfo.state.isFinished) {
+                        val messageId = workInfo.outputData.getString("message_id") ?: ""
+                        val pin = workInfo.outputData.getString("opposite_pin") ?: ""
+                        val status = workInfo.outputData.getInt("status", 0)
+                        Log.d(
+                            tag,
+                            "update from work:$messageId:$pin:$status"
                         )
+                        apiScope.launch(Dispatchers.IO) {
+                            if (messageId.isNotEmpty()) {
+                                messageStatusDao.updateStatus(
+                                    status,
+                                    messageId = messageId,
+                                    pin = pin
+                                )
+                            }
+                        }
+                    }
                 }
-            }
         }
     }
 }

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

@@ -9,7 +9,6 @@ import dagger.hilt.android.lifecycle.HiltViewModel
 import io.nexilis.service.core.getFileName
 import io.nexilis.service.core.getMimeType
 import io.nexilis.service.core.toFile
-import io.nexilis.service.data.daos.MessageStatusDao
 import io.nexilis.service.data.entities.Message
 import io.nexilis.service.data.repositories.MessageRepository
 import kotlinx.coroutines.Dispatchers
@@ -18,8 +17,7 @@ import javax.inject.Inject
 
 @HiltViewModel
 class MessageViewModel @Inject constructor(
-    private val repository: MessageRepository,
-    private val messageStatusDao: MessageStatusDao
+    private val repository: MessageRepository
 ) :
     ViewModel() {
 
@@ -61,17 +59,7 @@ class MessageViewModel @Inject constructor(
                     entity.message_text = entity.file_id + "|" + entity.message_text
                 }
             }
-            repository.send(context, entity) { id, pin, status ->
-                if (id.isNotEmpty()) {
-                    viewModelScope.launch(Dispatchers.IO) {
-                        messageStatusDao.updateStatus(
-                            status,
-                            messageId = id,
-                            pin = pin
-                        )
-                    }
-                }
-            }
+            repository.send(context, entity)
         }
 
 }