Commit e207fa00 authored by Filipe de Lima Brito's avatar Filipe de Lima Brito

Add UriInteractor.kt and rename relative_layout to root_layout (fragment_chat_room.xml)

parent 3f67f9c2
package chat.rocket.android.chatroom.domain
import android.content.Context
import android.net.Uri
import chat.rocket.android.util.extensions.getFileName
import chat.rocket.android.util.extensions.getMimeType
import chat.rocket.android.util.extensions.getRealPathFromURI
import javax.inject.Inject
class UriInteractor @Inject constructor(private val context: Context) {
/**
* Gets the file name from an [Uri].
*/
fun getFileName(uri: Uri): String? = uri.getFileName(context)
/**
* Gets the MimeType of an [Uri]
*/
fun getMimeType(uri: Uri): String = uri.getMimeType(context)
/**
* Gets the real path of an [Uri]
*/
fun getRealPath(uri: Uri): String? = uri.getRealPathFromURI(context)
}
\ No newline at end of file
package chat.rocket.android.chatroom.presentation
import android.net.Uri
import chat.rocket.android.R
import chat.rocket.android.chatroom.domain.UriInteractor
import chat.rocket.android.chatroom.viewmodel.MessageViewModelMapper
import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.server.domain.*
import chat.rocket.android.server.infraestructure.RocketChatClientFactory
import chat.rocket.android.util.extensions.getFileName
import chat.rocket.android.util.extensions.getMimeType
import chat.rocket.android.util.extensions.getRealPathFromURI
import chat.rocket.android.util.extensions.launchUI
import chat.rocket.common.RocketChatException
import chat.rocket.common.model.RoomType
......@@ -36,6 +41,7 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
getSettingsInteractor: GetSettingsInteractor,
private val serverInteractor: GetCurrentServerInteractor,
private val permissions: GetPermissionsInteractor,
private val uriInteractor: UriInteractor,
private val messagesRepository: MessagesRepository,
factory: RocketChatClientFactory,
private val mapper: MessageViewModelMapper) {
......@@ -65,8 +71,8 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
ex.message?.let {
view.showMessage(it)
}.ifNull {
view.showGenericErrorMessage()
}
view.showGenericErrorMessage()
}
} finally {
view.hideLoading()
}
......@@ -90,30 +96,34 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
ex.message?.let {
view.showMessage(it)
}.ifNull {
view.showGenericErrorMessage()
view.showGenericErrorMessage()
}
view.showGenericErrorMessage()
view.showGenericErrorMessage()
}
view.enableMessageInput()
}
}
}
fun uploadFile(roomId: String,
file: File,
mimeType: String,
msg: String,
description: String) {
fun uploadFile(roomId: String, uri: Uri, msg: String) {
launchUI(strategy) {
view.showLoading()
try {
client.uploadFile(roomId, file, mimeType, msg, description)
val fileName = uriInteractor.getFileName(uri)
val mimeType = uriInteractor.getMimeType(uri)
val fileRealPath = uriInteractor.getRealPath(uri)
if (fileName == null || fileRealPath == null) {
view.showInvalidFileMessage()
} else {
client.uploadFile(roomId, File(fileRealPath), mimeType, msg, fileName)
}
} catch (ex: RocketChatException) {
ex.message?.let {
view.showMessage(it)
}.ifNull {
view.showGenericErrorMessage()
}
view.showGenericErrorMessage()
}
} finally {
view.hideLoading()
}
......@@ -301,7 +311,6 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
if (message.roomId != roomId) {
Timber.d("Ignoring message for room ${message.roomId}, expecting $roomId")
}
updateMessage(message)
}
}
......
......@@ -28,6 +28,11 @@ interface ChatRoomView : LoadingView, MessageView {
*/
fun uploadFile(uri: Uri)
/**
* Shows a invalid file message.
*/
fun showInvalidFileMessage()
/**
* Shows a (recent) message sent to a chat room.
*
......@@ -71,5 +76,6 @@ interface ChatRoomView : LoadingView, MessageView {
fun showEditingAction(roomId: String, messageId: String, text: String)
fun disableMessageInput()
fun enableMessageInput(clear: Boolean = false)
}
\ No newline at end of file
......@@ -28,10 +28,8 @@ import dagger.android.support.AndroidSupportInjection
import kotlinx.android.synthetic.main.fragment_chat_room.*
import kotlinx.android.synthetic.main.message_attachment_options.*
import kotlinx.android.synthetic.main.message_composer.*
import java.io.File
import javax.inject.Inject
fun newInstance(chatRoomId: String, chatRoomName: String, chatRoomType: String, isChatRoomReadOnly: Boolean): Fragment {
return ChatRoomFragment().apply {
arguments = Bundle(1).apply {
......@@ -64,7 +62,7 @@ class ChatRoomFragment : Fragment(), ChatRoomView {
private var editingMessageId: String? = null
// For reveal and unreveal anim.
private val hypotenuse by lazy { Math.hypot(relative_layout.width.toDouble(), relative_layout.height.toDouble()).toFloat() }
private val hypotenuse by lazy { Math.hypot(root_layout.width.toDouble(), root_layout.height.toDouble()).toFloat() }
private val max by lazy { Math.max(layout_message_attachment_options.width.toDouble(), layout_message_attachment_options.height.toDouble()).toFloat() }
private val centerX by lazy { recycler_view.right }
private val centerY by lazy { recycler_view.bottom }
......@@ -98,6 +96,7 @@ class ChatRoomFragment : Fragment(), ChatRoomView {
override fun onDestroyView() {
presenter.unsubscribeMessages()
handler.removeCallbacksAndMessages(null)
super.onDestroyView()
}
......@@ -144,7 +143,6 @@ class ChatRoomFragment : Fragment(), ChatRoomView {
})
}
}
adapter.addDataSet(dataSet)
}
}
......@@ -156,23 +154,12 @@ class ChatRoomFragment : Fragment(), ChatRoomView {
}
override fun uploadFile(uri: Uri) {
activity?.apply {
val fileName = uri.getFileName(this)
val mimeType = uri.getMimeType(this)
val fileRealPath = uri.getRealPathFromURI(this)
if (fileName != null && fileRealPath != null) {
presenter.uploadFile(
chatRoomId,
File(fileRealPath),
mimeType,
"", // 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()
)
}
}
// TODO Just leaving a blank message that comes with the file for now. In the future lets add the possibility to add a message with the file to be uploaded.
presenter.uploadFile(chatRoomId, uri, "")
}
override fun showInvalidFileMessage() = showMessage(getString(R.string.msg_invalid_file))
override fun showNewMessage(message: MessageViewModel) {
text_message.textContent = ""
adapter.addItem(message)
......@@ -220,7 +207,7 @@ class ChatRoomFragment : Fragment(), ChatRoomView {
override fun copyToClipboard(message: String) {
activity?.apply {
val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
clipboard.setPrimaryClip(ClipData.newPlainText("", message))
clipboard.primaryClip = ClipData.newPlainText("", message)
}
}
......
package chat.rocket.android.util.extensions
import android.app.Activity
import android.content.ContentResolver
import android.content.Context
import android.net.Uri
......@@ -46,8 +45,8 @@ var TextView.content: CharSequence
Markwon.scheduleTableRows(this)
}
fun Uri.getFileName(activity: Activity): String? {
val cursor = activity.contentResolver.query(this, null, null, null, null, null)
fun Uri.getFileName(context: Context): String? {
val cursor = context.contentResolver.query(this, null, null, null, null, null)
var fileName: String? = null
cursor.use { cursor ->
......@@ -58,8 +57,8 @@ fun Uri.getFileName(activity: Activity): String? {
return fileName
}
fun Uri.getFileSize(activity: Activity): String? {
val cursor = activity.contentResolver.query(this, null, null, null, null, null)
fun Uri.getFileSize(context: Context): String? {
val cursor = context.contentResolver.query(this, null, null, null, null, null)
var fileSize: String? = null
cursor.use { cursor ->
......
......@@ -2,7 +2,7 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative_layout"
android:id="@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
......
......@@ -20,7 +20,6 @@
<!-- Regular information messages -->
<string name="msg_no_internet_connection">Sem conexão à internet</string>
<string name="msg_invalid_server_url">URL de servidor inválida</string>
<string name="msg_generic_error">Desculpe, ocorreu um erro, tente novamente</string>
<string name="msg_no_data_to_display">Nenhum dado para exibir</string>
<string name="msg_profile_update_successfully">Perfil atualizado com sucesso</string>
......@@ -33,10 +32,12 @@
<string name="msg_new_user">Novo usuário? %1$s</string>
<string name="msg_new_user_agreement">Ao proceder você concorda com nossos %1$s e %2$s</string>
<string name="msg_2fa_code">Código 2FA</string>
<string name="msg_invalid_2fa_code">Código 2FA inválido</string>
<string name="msg_yesterday">ontem</string>
<string name="msg_message">Messagem</string>
<string name="msg_this_room_is_read_only">Este chat é apenas de leitura</string>
<string name="msg_invalid_2fa_code">Código 2FA inválido</string>
<string name="msg_invalid_file">Arquivo inválido</string>
<string name="msg_invalid_server_url">URL de servidor inválida</string>
<string name="msg_content_description_log_in_using_facebook">Fazer login através do Facebook</string>
<string name="msg_content_description_log_in_using_github">Fazer login através do Github</string>
<string name="msg_content_description_log_in_using_google">Fazer login através do Google</string>
......
......@@ -21,7 +21,6 @@
<!-- Regular information messages -->
<string name="msg_no_internet_connection">No internet connection</string>
<string name="msg_invalid_server_url">Invalid server URL</string>
<string name="msg_generic_error">Sorry, an error has occurred, please try again</string>
<string name="msg_no_data_to_display">No data to display</string>
<string name="msg_profile_update_successfully">Profile update successfully</string>
......@@ -34,11 +33,13 @@
<string name="msg_new_user">New user? %1$s</string>
<string name="msg_new_user_agreement">By proceeding you are agreeing to our\n%1$s and %2$s</string>
<string name="msg_2fa_code">2FA Code</string>
<string name="msg_invalid_2fa_code">Invalid 2FA Code</string>
<string name="msg_more_than_ninety_nine_unread_messages" translatable="false">99+</string>
<string name="msg_yesterday">Yesterday</string>
<string name="msg_message">Message</string>
<string name="msg_this_room_is_read_only">This room is read only</string>
<string name="msg_invalid_2fa_code">Invalid 2FA Code</string>
<string name="msg_invalid_file">Invalid file</string>
<string name="msg_invalid_server_url">Invalid server URL</string>
<string name="msg_content_description_log_in_using_facebook">Login using Facebook</string>
<string name="msg_content_description_log_in_using_github">Login using Github</string>
<string name="msg_content_description_log_in_using_google">Login using Google</string>
......
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