From 75ba95121ddc9fcc544bb2a34e6ad174dcdb1c0a Mon Sep 17 00:00:00 2001 From: water2015sit Date: Sat, 20 Apr 2019 09:59:00 -0400 Subject: [PATCH 1/2] update regular expression --- .../chatdetails/ui/ChatDetailsFragment.kt | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/chat/rocket/android/chatdetails/ui/ChatDetailsFragment.kt b/app/src/main/java/chat/rocket/android/chatdetails/ui/ChatDetailsFragment.kt index b913770180..a21f2f3b0c 100644 --- a/app/src/main/java/chat/rocket/android/chatdetails/ui/ChatDetailsFragment.kt +++ b/app/src/main/java/chat/rocket/android/chatdetails/ui/ChatDetailsFragment.kt @@ -1,7 +1,13 @@ package chat.rocket.android.chatdetails.ui import DrawableHelper +import android.content.Intent +import android.net.Uri import android.os.Bundle +import android.text.Spannable +import android.text.SpannableStringBuilder +import android.text.style.ClickableSpan +import android.util.Patterns import android.view.LayoutInflater import android.view.Menu import android.view.MenuItem @@ -132,7 +138,8 @@ class ChatDetailsFragment : Fragment(), ChatDetailsView { content_announcement.text = if (room.announcement.isNullOrEmpty()) getString(R.string.msg_no_announcement) else room.announcement content_description.text = - if (room.description.isNullOrEmpty()) getString(R.string.msg_no_description) else room.description + if (room.description.isNullOrEmpty()) getString(R.string.msg_no_description) + else getClickableSpan(room.description) } } @@ -242,4 +249,31 @@ class ChatDetailsFragment : Fragment(), ChatDetailsView { setupToolbarTitle(getString(R.string.title_channel_details)) } } + + private fun getClickableSpan(description: String): CharSequence { + val spannableContent = SpannableStringBuilder(description) + val clickablePartList = getUrlList(description) + if (clickablePartList != null) { + for (clickablePart in clickablePartList) { + val clickableSpan = object : ClickableSpan() { + override fun onClick(widget: View) { + val intent = Intent(Intent.ACTION_VIEW, Uri.parse(clickablePart)) + } + } + val clickablePartStart = description.indexOf(clickablePart) + spannableContent.setSpan( + clickableSpan, + clickablePartStart, + clickablePartStart + clickablePart.length, + Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) + } + } + return spannableContent + } + + private fun getUrlList(description: String): List? { + // Extract all urls from the room description and put them in a list. + val urlRegex = Patterns.WEB_URL.toRegex() + return urlRegex.findAll(description).map { it.value }.toList() + } } \ No newline at end of file From de1d0799addbde2db61abb9984f5f892b9ecb9a6 Mon Sep 17 00:00:00 2001 From: Filipe de Lima Brito Date: Fri, 7 Jun 2019 09:31:30 -0300 Subject: [PATCH 2/2] Improve code --- .../chatdetails/ui/ChatDetailsFragment.kt | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/chat/rocket/android/chatdetails/ui/ChatDetailsFragment.kt b/app/src/main/java/chat/rocket/android/chatdetails/ui/ChatDetailsFragment.kt index a8f5696d98..f075821f8e 100644 --- a/app/src/main/java/chat/rocket/android/chatdetails/ui/ChatDetailsFragment.kt +++ b/app/src/main/java/chat/rocket/android/chatdetails/ui/ChatDetailsFragment.kt @@ -129,12 +129,23 @@ class ChatDetailsFragment : Fragment(), ChatDetailsView { name.text = text bindImage(chatRoomType) content_topic.text = - if (room.topic.isNullOrEmpty()) getString(R.string.msg_no_topic) else room.topic + if (room.topic.isNullOrEmpty()) { + getString(R.string.msg_no_topic) + } else { + room.topic + } content_announcement.text = - if (room.announcement.isNullOrEmpty()) getString(R.string.msg_no_announcement) else room.announcement + if (room.announcement.isNullOrEmpty()) { + getString(R.string.msg_no_announcement) + } else { + room.announcement + } content_description.text = - if (room.description.isNullOrEmpty()) getString(R.string.msg_no_description) - else getClickableSpan(room.description) + if (room.description.isNullOrEmpty()) { + getString(R.string.msg_no_description) + } else { + getClickableSpan(room.description) + } } } @@ -247,19 +258,19 @@ class ChatDetailsFragment : Fragment(), ChatDetailsView { private fun getClickableSpan(description: String): CharSequence { val spannableContent = SpannableStringBuilder(description) - val clickablePartList = getUrlList(description) - if (clickablePartList != null) { - for (clickablePart in clickablePartList) { + val clickableUrlList = getUrlList(description) + if (clickableUrlList != null) { + for (clickableUrl in clickableUrlList) { val clickableSpan = object : ClickableSpan() { override fun onClick(widget: View) { - val intent = Intent(Intent.ACTION_VIEW, Uri.parse(clickablePart)) + Intent(Intent.ACTION_VIEW, Uri.parse(clickableUrl)) } } - val clickablePartStart = description.indexOf(clickablePart) + val clickableUrlStart = description.indexOf(clickableUrl) spannableContent.setSpan( clickableSpan, - clickablePartStart, - clickablePartStart + clickablePart.length, + clickableUrlStart, + clickableUrlStart + clickableUrl.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) } } @@ -267,7 +278,7 @@ class ChatDetailsFragment : Fragment(), ChatDetailsView { } private fun getUrlList(description: String): List? { - // Extract all urls from the room description and put them in a list. + // Extract all urls from the room description and return a list with them. val urlRegex = Patterns.WEB_URL.toRegex() return urlRegex.findAll(description).map { it.value }.toList() }