Skip to content

Commit

Permalink
Migrate Java parts to Kotlin
Browse files Browse the repository at this point in the history
Also fixing some detekt issues.

Signed-off-by: Tim Krüger <[email protected]>
  • Loading branch information
timkrueger authored and AndyScherzinger committed Jul 6, 2022
1 parent 81651a8 commit 31fc5df
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import com.nextcloud.talk.utils.DisplayUtils.getMessageSelector
import com.nextcloud.talk.utils.DisplayUtils.searchAndReplaceWithMentionSpan
import com.nextcloud.talk.utils.TextMatchers
import com.stfalcon.chatkit.messages.MessageHolders.OutcomingTextMessageViewHolder
import java.util.HashMap
import javax.inject.Inject

@AutoInjector(NextcloudTalkApplication::class)
Expand Down Expand Up @@ -98,30 +97,14 @@ class MagicOutcomingTextMessageViewHolder(itemView: View) : OutcomingTextMessage
binding.messageQuote.quotedChatMessageView.visibility = View.GONE
}

val readStatusDrawableInt = when (message.readStatus) {
ReadStatus.READ -> R.drawable.ic_check_all
ReadStatus.SENT -> R.drawable.ic_check
ReadStatus.SENDING -> R.drawable.ic_sending
ReadStatus.FAILED -> R.drawable.ic_warning_white
else -> null
}

val readStatusContentDescriptionString = when (message.readStatus) {
ReadStatus.READ -> context?.resources?.getString(R.string.nc_message_read)
ReadStatus.SENT -> context?.resources?.getString(R.string.nc_message_sent)
ReadStatus.SENDING -> context?.resources?.getString(R.string.nc_message_sending)
ReadStatus.FAILED -> context?.resources?.getString(R.string.nc_message_send_error)
else -> null
}

readStatusDrawableInt?.let { drawableInt ->
readStatusDrawableInt(message)?.let { drawableInt ->
ResourcesCompat.getDrawable(context!!.resources, drawableInt, null)?.let {
it.setColorFilter(ContextCompat.getColor(context!!, R.color.white60), PorterDuff.Mode.SRC_ATOP)
binding.checkMark.setImageDrawable(it)
}
}

binding.checkMark.setContentDescription(readStatusContentDescriptionString)
binding.checkMark.setContentDescription(readStatusContentDescriptionString(message))

itemView.setTag(MessageSwipeCallback.REPLYABLE_VIEW_TAG, message.replyable)

Expand All @@ -135,6 +118,24 @@ class MagicOutcomingTextMessageViewHolder(itemView: View) : OutcomingTextMessage
}
}

private fun readStatusContentDescriptionString(message: ChatMessage) =
when (message.readStatus) {
ReadStatus.READ -> context?.resources?.getString(R.string.nc_message_read)
ReadStatus.SENT -> context?.resources?.getString(R.string.nc_message_sent)
ReadStatus.SENDING -> context?.resources?.getString(R.string.nc_message_sending)
ReadStatus.FAILED -> context?.resources?.getString(R.string.nc_message_send_error)
else -> null
}

private fun readStatusDrawableInt(message: ChatMessage) =
when (message.readStatus) {
ReadStatus.READ -> R.drawable.ic_check_all
ReadStatus.SENT -> R.drawable.ic_check
ReadStatus.SENDING -> R.drawable.ic_sending
ReadStatus.FAILED -> R.drawable.ic_warning_white
else -> null
}

private fun processParentMessage(message: ChatMessage) {
val parentChatMessage = message.parentMessage
parentChatMessage!!.activeUser = message.activeUser
Expand Down
60 changes: 30 additions & 30 deletions app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2023,39 +2023,38 @@ class ChatController(args: Bundle) :

private fun sendMessage(message: CharSequence, replyTo: Int?, sendWithoutNotification: Boolean) {
val messObj = ChatMessage()
messObj.setMessage(message.toString())
messObj.setActiveUser(conversationUser)
messObj.message = message.toString()
messObj.activeUser = conversationUser
val tsLong = System.currentTimeMillis() / 1000
messObj.setTimestamp(tsLong)
messObj.setJsonMessageId(0 - tsLong.toInt())
messObj.timestamp = tsLong
messObj.jsonMessageId = 0 - tsLong.toInt()


messObj.setReadStatus(ReadStatus.SENDING)
messObj.readStatus = ReadStatus.SENDING

if (conversationUser!!.userId != "?") {
// Logged in user
messObj.setActorType("users")
messObj.setActorId(conversationUser.userId)
messObj.setActorDisplayName(conversationUser.username)
messObj.actorType = "users"
messObj.actorId = conversationUser.userId
messObj.actorDisplayName = conversationUser.username
} else if (currentConversation!!.actorType != null) {
// API v3 or later
messObj.setActorType(currentConversation!!.actorType)
messObj.setActorId(currentConversation!!.actorId)
messObj.setActorDisplayName("")
messObj.actorType = currentConversation!!.actorType
messObj.actorId = currentConversation!!.actorId
messObj.actorDisplayName = ""
} else {
// API v1 or v2 as a guest
messObj.setActorType("guests")
messObj.actorType = "guests"

val messageDigest: MessageDigest = MessageDigest.getInstance("SHA-1")
val digest: ByteArray = messageDigest.digest(currentConversation!!.sessionId.toByteArray())
val digest: ByteArray = messageDigest.digest(currentConversation!!.sessionId!!.toByteArray())
val sha1: StringBuilder = StringBuilder()
val i = digest.iterator()
while (i.hasNext()) {
sha1.append(String.format("%02X", i.next()))
}

messObj.setActorId(sha1.toString())
messObj.setActorDisplayName("")
messObj.actorId = sha1.toString()
messObj.actorDisplayName = ""
}

adapter!!.addToStart(
Expand Down Expand Up @@ -2103,7 +2102,7 @@ class ChatController(args: Bundle) :
override fun onError(e: Throwable) {
Log.e(TAG, "An error occured while sending the chat message: " + e.message)

messObj.setReadStatus(ReadStatus.FAILED)
messObj.readStatus = ReadStatus.FAILED
adapter!!.updateAndMoveToStart(messObj)

if (e is HttpException) {
Expand Down Expand Up @@ -2801,11 +2800,12 @@ class ChatController(args: Bundle) :
fun replyPrivately(message: IMessage?) {
val apiVersion =
ApiUtils.getConversationApiVersion(
conversationUser, intArrayOf(
ApiUtils.APIv4,
1
)
)
conversationUser,
intArrayOf(
ApiUtils.APIv4,
1
)
)
val retrofitBucket = ApiUtils.getRetrofitBucketForCreateRoom(
apiVersion,
conversationUser?.baseUrl,
Expand Down Expand Up @@ -2857,20 +2857,20 @@ class ChatController(args: Bundle) :
)
}

override fun onError(e: Throwable) {
Log.e(TAG, e.message, e)
}
override fun onError(e: Throwable) {
Log.e(TAG, e.message, e)
}

override fun onComplete() {// unused atm
override fun onComplete() { // unused atm
}
})
}

override fun onError(e: Throwable) {
Log.e(TAG, e.message, e)
}
override fun onError(e: Throwable) {
Log.e(TAG, e.message, e)
}

override fun onComplete() {
override fun onComplete() {
// unused atm
}
})
Expand Down

0 comments on commit 31fc5df

Please sign in to comment.