Commit 89f38952 authored by Filipe de Lima Brito's avatar Filipe de Lima Brito

Implements the file upload using RocketChatClient.

parent 2e5f7c8b
...@@ -15,13 +15,14 @@ import chat.rocket.core.internal.realtime.subscribeRoomMessages ...@@ -15,13 +15,14 @@ import chat.rocket.core.internal.realtime.subscribeRoomMessages
import chat.rocket.core.internal.realtime.unsubscibre import chat.rocket.core.internal.realtime.unsubscibre
import chat.rocket.core.internal.rest.messages import chat.rocket.core.internal.rest.messages
import chat.rocket.core.internal.rest.sendMessage import chat.rocket.core.internal.rest.sendMessage
import chat.rocket.core.internal.rest.uploadFile
import chat.rocket.core.model.Message import chat.rocket.core.model.Message
import chat.rocket.core.model.Value import chat.rocket.core.model.Value
import chat.rocket.core.model.attachment.Attachment
import kotlinx.coroutines.experimental.CommonPool import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.channels.Channel import kotlinx.coroutines.experimental.channels.Channel
import kotlinx.coroutines.experimental.launch import kotlinx.coroutines.experimental.launch
import timber.log.Timber import timber.log.Timber
import java.io.File
import javax.inject.Inject import javax.inject.Inject
class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView, class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
...@@ -90,13 +91,21 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView, ...@@ -90,13 +91,21 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
} }
} }
fun sendFile(chatRoomId: String, fileName: String) { fun uploadFile(roomId: String,
file: File,
mimeType: String,
msg: String,
description: String) {
launchUI(strategy) { launchUI(strategy) {
view.showLoading() view.showLoading()
try { try {
// client.sendMessage(chatRoomId) client.uploadFile(roomId, file, mimeType, msg, description)
} catch (ex: RocketChatException) { } catch (ex: RocketChatException) {
ex.message?.let {
view.showMessage(it)
}.ifNull {
view.showGenericErrorMessage()
}
} finally { } finally {
view.hideLoading() view.hideLoading()
} }
......
...@@ -22,11 +22,11 @@ interface ChatRoomView : LoadingView, MessageView { ...@@ -22,11 +22,11 @@ interface ChatRoomView : LoadingView, MessageView {
fun sendMessage(text: String) fun sendMessage(text: String)
/** /**
* Send a file to a chat room. * Uploads a file to a chat room.
* *
* @param uri The file URI to send. * @param uri The file URI to send.
*/ */
fun sendFile(uri: Uri) fun uploadFile(uri: Uri)
/** /**
* Shows a (recent) message sent to a chat room. * Shows a (recent) message sent to a chat room.
......
...@@ -23,6 +23,7 @@ import dagger.android.support.AndroidSupportInjection ...@@ -23,6 +23,7 @@ import dagger.android.support.AndroidSupportInjection
import kotlinx.android.synthetic.main.fragment_chat_room.* import kotlinx.android.synthetic.main.fragment_chat_room.*
import kotlinx.android.synthetic.main.message_attachment_options.* import kotlinx.android.synthetic.main.message_attachment_options.*
import kotlinx.android.synthetic.main.message_composer.* import kotlinx.android.synthetic.main.message_composer.*
import java.io.File
import javax.inject.Inject import javax.inject.Inject
fun newInstance(chatRoomId: String, chatRoomName: String, chatRoomType: String, isChatRoomReadOnly: Boolean): Fragment { fun newInstance(chatRoomId: String, chatRoomName: String, chatRoomType: String, isChatRoomReadOnly: Boolean): Fragment {
...@@ -88,7 +89,7 @@ class ChatRoomFragment : Fragment(), ChatRoomView { ...@@ -88,7 +89,7 @@ class ChatRoomFragment : Fragment(), ChatRoomView {
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) { override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
if (requestCode == REQUEST_CODE_FOR_PERFORM_SAF && resultCode == Activity.RESULT_OK) { if (requestCode == REQUEST_CODE_FOR_PERFORM_SAF && resultCode == Activity.RESULT_OK) {
if (resultData != null) { if (resultData != null) {
sendFile(resultData.data) uploadFile(resultData.data)
} }
} }
} }
...@@ -120,13 +121,24 @@ class ChatRoomFragment : Fragment(), ChatRoomView { ...@@ -120,13 +121,24 @@ class ChatRoomFragment : Fragment(), ChatRoomView {
} }
} }
override fun sendFile(uri: Uri) { override fun uploadFile(uri: Uri) {
var fileName: String? = null var fileName: String? = null
var mimeType: String? = null
var fileRealPath: String? = null
activity?.apply { activity?.apply {
fileName = uri.getFileName(this) fileName = uri.getFileName(this)
} mimeType = uri.getMimeType(this)
if (fileName != null) { fileRealPath = uri.getRealPathFromURI(this)
presenter.sendFile(chatRoomId, fileName.toString())
if (fileName != null && mimeType != null && fileRealPath != null) {
presenter.uploadFile(
chatRoomId,
File(fileRealPath),
mimeType.toString(),
"", // TODO Just leaving it for now, in the future lets add the possibility to add a message with the file to be uploaded.
fileName.toString()
)
}
} }
} }
......
package chat.rocket.android.util.extensions package chat.rocket.android.util.extensions
import android.app.Activity import android.app.Activity
import android.content.ContentResolver
import android.content.Context
import android.net.Uri import android.net.Uri
import android.widget.TextView import android.widget.TextView
import android.provider.OpenableColumns import android.provider.OpenableColumns
import android.webkit.MimeTypeMap
import android.provider.MediaStore
fun String.ifEmpty(value: String): String { fun String.ifEmpty(value: String): String {
if (isEmpty()) { if (isEmpty()) {
...@@ -51,4 +53,23 @@ fun Uri.getFileSize(activity: Activity): String? { ...@@ -51,4 +53,23 @@ fun Uri.getFileSize(activity: Activity): String? {
} }
} }
return fileSize return fileSize
}
fun Uri.getMimeType(context: Context): String {
return if (scheme == ContentResolver.SCHEME_CONTENT) {
context.contentResolver.getType(this)
} else {
val fileExtension = MimeTypeMap.getFileExtensionFromUrl(toString())
MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension.toLowerCase())
}
}
fun Uri.getRealPathFromURI(context: Context): String? {
val cursor = context.contentResolver.query(this, arrayOf(MediaStore.Images.Media.DATA), null, null, null)
cursor.use { cursor ->
val columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
cursor.moveToFirst()
return cursor.getString(columnIndex)
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment