Merge pull request #308 from Dominaezzz/kotlinify-2

Some "Kotlinification"
This commit is contained in:
Benoit Marty 2019-07-05 18:11:18 +02:00 committed by GitHub
commit 3abb7c8de6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 46 additions and 67 deletions

View File

@ -16,7 +16,6 @@
package im.vector.matrix.android.api package im.vector.matrix.android.api
import java.util.regex.Pattern
/** /**
* This class contains pattern to match the different Matrix ids * This class contains pattern to match the different Matrix ids
@ -29,31 +28,31 @@ object MatrixPatterns {
// regex pattern to find matrix user ids in a string. // regex pattern to find matrix user ids in a string.
// See https://matrix.org/speculator/spec/HEAD/appendices.html#historical-user-ids // See https://matrix.org/speculator/spec/HEAD/appendices.html#historical-user-ids
private const val MATRIX_USER_IDENTIFIER_REGEX = "@[A-Z0-9\\x21-\\x39\\x3B-\\x7F]+$DOMAIN_REGEX" private const val MATRIX_USER_IDENTIFIER_REGEX = "@[A-Z0-9\\x21-\\x39\\x3B-\\x7F]+$DOMAIN_REGEX"
private val PATTERN_CONTAIN_MATRIX_USER_IDENTIFIER = Pattern.compile(MATRIX_USER_IDENTIFIER_REGEX, Pattern.CASE_INSENSITIVE) private val PATTERN_CONTAIN_MATRIX_USER_IDENTIFIER = MATRIX_USER_IDENTIFIER_REGEX.toRegex(RegexOption.IGNORE_CASE)
// regex pattern to find room ids in a string. // regex pattern to find room ids in a string.
private const val MATRIX_ROOM_IDENTIFIER_REGEX = "![A-Z0-9]+$DOMAIN_REGEX" private const val MATRIX_ROOM_IDENTIFIER_REGEX = "![A-Z0-9]+$DOMAIN_REGEX"
private val PATTERN_CONTAIN_MATRIX_ROOM_IDENTIFIER = Pattern.compile(MATRIX_ROOM_IDENTIFIER_REGEX, Pattern.CASE_INSENSITIVE) private val PATTERN_CONTAIN_MATRIX_ROOM_IDENTIFIER = MATRIX_ROOM_IDENTIFIER_REGEX.toRegex(RegexOption.IGNORE_CASE)
// regex pattern to find room aliases in a string. // regex pattern to find room aliases in a string.
private const val MATRIX_ROOM_ALIAS_REGEX = "#[A-Z0-9._%#@=+-]+$DOMAIN_REGEX" private const val MATRIX_ROOM_ALIAS_REGEX = "#[A-Z0-9._%#@=+-]+$DOMAIN_REGEX"
private val PATTERN_CONTAIN_MATRIX_ALIAS = Pattern.compile(MATRIX_ROOM_ALIAS_REGEX, Pattern.CASE_INSENSITIVE) private val PATTERN_CONTAIN_MATRIX_ALIAS = MATRIX_ROOM_ALIAS_REGEX.toRegex(RegexOption.IGNORE_CASE)
// regex pattern to find message ids in a string. // regex pattern to find message ids in a string.
private const val MATRIX_EVENT_IDENTIFIER_REGEX = "\\$[A-Z0-9]+$DOMAIN_REGEX" private const val MATRIX_EVENT_IDENTIFIER_REGEX = "\\$[A-Z0-9]+$DOMAIN_REGEX"
private val PATTERN_CONTAIN_MATRIX_EVENT_IDENTIFIER = Pattern.compile(MATRIX_EVENT_IDENTIFIER_REGEX, Pattern.CASE_INSENSITIVE) private val PATTERN_CONTAIN_MATRIX_EVENT_IDENTIFIER = MATRIX_EVENT_IDENTIFIER_REGEX.toRegex(RegexOption.IGNORE_CASE)
// regex pattern to find message ids in a string. // regex pattern to find message ids in a string.
private const val MATRIX_EVENT_IDENTIFIER_V3_REGEX = "\\$[A-Z0-9/+]+" private const val MATRIX_EVENT_IDENTIFIER_V3_REGEX = "\\$[A-Z0-9/+]+"
private val PATTERN_CONTAIN_MATRIX_EVENT_IDENTIFIER_V3 = Pattern.compile(MATRIX_EVENT_IDENTIFIER_V3_REGEX, Pattern.CASE_INSENSITIVE) private val PATTERN_CONTAIN_MATRIX_EVENT_IDENTIFIER_V3 = MATRIX_EVENT_IDENTIFIER_V3_REGEX.toRegex(RegexOption.IGNORE_CASE)
// Ref: https://matrix.org/docs/spec/rooms/v4#event-ids // Ref: https://matrix.org/docs/spec/rooms/v4#event-ids
private const val MATRIX_EVENT_IDENTIFIER_V4_REGEX = "\\$[A-Z0-9\\-_]+" private const val MATRIX_EVENT_IDENTIFIER_V4_REGEX = "\\$[A-Z0-9\\-_]+"
private val PATTERN_CONTAIN_MATRIX_EVENT_IDENTIFIER_V4 = Pattern.compile(MATRIX_EVENT_IDENTIFIER_V4_REGEX, Pattern.CASE_INSENSITIVE) private val PATTERN_CONTAIN_MATRIX_EVENT_IDENTIFIER_V4 = MATRIX_EVENT_IDENTIFIER_V4_REGEX.toRegex(RegexOption.IGNORE_CASE)
// regex pattern to find group ids in a string. // regex pattern to find group ids in a string.
private const val MATRIX_GROUP_IDENTIFIER_REGEX = "\\+[A-Z0-9=_\\-./]+$DOMAIN_REGEX" private const val MATRIX_GROUP_IDENTIFIER_REGEX = "\\+[A-Z0-9=_\\-./]+$DOMAIN_REGEX"
private val PATTERN_CONTAIN_MATRIX_GROUP_IDENTIFIER = Pattern.compile(MATRIX_GROUP_IDENTIFIER_REGEX, Pattern.CASE_INSENSITIVE) private val PATTERN_CONTAIN_MATRIX_GROUP_IDENTIFIER = MATRIX_GROUP_IDENTIFIER_REGEX.toRegex(RegexOption.IGNORE_CASE)
// regex pattern to find permalink with message id. // regex pattern to find permalink with message id.
// Android does not support in URL so extract it. // Android does not support in URL so extract it.
@ -62,16 +61,16 @@ object MatrixPatterns {
const val SEP_REGEX = "/" const val SEP_REGEX = "/"
private const val LINK_TO_ROOM_ID_REGEXP = PERMALINK_BASE_REGEX + MATRIX_ROOM_IDENTIFIER_REGEX + SEP_REGEX + MATRIX_EVENT_IDENTIFIER_REGEX private const val LINK_TO_ROOM_ID_REGEXP = PERMALINK_BASE_REGEX + MATRIX_ROOM_IDENTIFIER_REGEX + SEP_REGEX + MATRIX_EVENT_IDENTIFIER_REGEX
private val PATTERN_CONTAIN_MATRIX_TO_PERMALINK_ROOM_ID = Pattern.compile(LINK_TO_ROOM_ID_REGEXP, Pattern.CASE_INSENSITIVE) private val PATTERN_CONTAIN_MATRIX_TO_PERMALINK_ROOM_ID = LINK_TO_ROOM_ID_REGEXP.toRegex(RegexOption.IGNORE_CASE)
private const val LINK_TO_ROOM_ALIAS_REGEXP = PERMALINK_BASE_REGEX + MATRIX_ROOM_ALIAS_REGEX + SEP_REGEX + MATRIX_EVENT_IDENTIFIER_REGEX private const val LINK_TO_ROOM_ALIAS_REGEXP = PERMALINK_BASE_REGEX + MATRIX_ROOM_ALIAS_REGEX + SEP_REGEX + MATRIX_EVENT_IDENTIFIER_REGEX
private val PATTERN_CONTAIN_MATRIX_TO_PERMALINK_ROOM_ALIAS = Pattern.compile(LINK_TO_ROOM_ALIAS_REGEXP, Pattern.CASE_INSENSITIVE) private val PATTERN_CONTAIN_MATRIX_TO_PERMALINK_ROOM_ALIAS = LINK_TO_ROOM_ALIAS_REGEXP.toRegex(RegexOption.IGNORE_CASE)
private const val LINK_TO_APP_ROOM_ID_REGEXP = APP_BASE_REGEX + MATRIX_ROOM_IDENTIFIER_REGEX + SEP_REGEX + MATRIX_EVENT_IDENTIFIER_REGEX private const val LINK_TO_APP_ROOM_ID_REGEXP = APP_BASE_REGEX + MATRIX_ROOM_IDENTIFIER_REGEX + SEP_REGEX + MATRIX_EVENT_IDENTIFIER_REGEX
private val PATTERN_CONTAIN_APP_LINK_PERMALINK_ROOM_ID = Pattern.compile(LINK_TO_APP_ROOM_ID_REGEXP, Pattern.CASE_INSENSITIVE) private val PATTERN_CONTAIN_APP_LINK_PERMALINK_ROOM_ID = LINK_TO_APP_ROOM_ID_REGEXP.toRegex(RegexOption.IGNORE_CASE)
private const val LINK_TO_APP_ROOM_ALIAS_REGEXP = APP_BASE_REGEX + MATRIX_ROOM_ALIAS_REGEX + SEP_REGEX + MATRIX_EVENT_IDENTIFIER_REGEX private const val LINK_TO_APP_ROOM_ALIAS_REGEXP = APP_BASE_REGEX + MATRIX_ROOM_ALIAS_REGEX + SEP_REGEX + MATRIX_EVENT_IDENTIFIER_REGEX
private val PATTERN_CONTAIN_APP_LINK_PERMALINK_ROOM_ALIAS = Pattern.compile(LINK_TO_APP_ROOM_ALIAS_REGEXP, Pattern.CASE_INSENSITIVE) private val PATTERN_CONTAIN_APP_LINK_PERMALINK_ROOM_ALIAS = LINK_TO_APP_ROOM_ALIAS_REGEXP.toRegex(RegexOption.IGNORE_CASE)
// list of patterns to find some matrix item. // list of patterns to find some matrix item.
val MATRIX_PATTERNS = listOf( val MATRIX_PATTERNS = listOf(
@ -93,7 +92,7 @@ object MatrixPatterns {
* @return true if the string is a valid user id * @return true if the string is a valid user id
*/ */
fun isUserId(str: String?): Boolean { fun isUserId(str: String?): Boolean {
return str != null && PATTERN_CONTAIN_MATRIX_USER_IDENTIFIER.matcher(str).matches() return str != null && str matches PATTERN_CONTAIN_MATRIX_USER_IDENTIFIER
} }
/** /**
@ -103,7 +102,7 @@ object MatrixPatterns {
* @return true if the string is a valid room Id * @return true if the string is a valid room Id
*/ */
fun isRoomId(str: String?): Boolean { fun isRoomId(str: String?): Boolean {
return str != null && PATTERN_CONTAIN_MATRIX_ROOM_IDENTIFIER.matcher(str).matches() return str != null && str matches PATTERN_CONTAIN_MATRIX_ROOM_IDENTIFIER
} }
/** /**
@ -113,7 +112,7 @@ object MatrixPatterns {
* @return true if the string is a valid room alias. * @return true if the string is a valid room alias.
*/ */
fun isRoomAlias(str: String?): Boolean { fun isRoomAlias(str: String?): Boolean {
return str != null && PATTERN_CONTAIN_MATRIX_ALIAS.matcher(str).matches() return str != null && str matches PATTERN_CONTAIN_MATRIX_ALIAS
} }
/** /**
@ -124,9 +123,9 @@ object MatrixPatterns {
*/ */
fun isEventId(str: String?): Boolean { fun isEventId(str: String?): Boolean {
return str != null return str != null
&& (PATTERN_CONTAIN_MATRIX_EVENT_IDENTIFIER.matcher(str).matches() && (str matches PATTERN_CONTAIN_MATRIX_EVENT_IDENTIFIER
|| PATTERN_CONTAIN_MATRIX_EVENT_IDENTIFIER_V3.matcher(str).matches() || str matches PATTERN_CONTAIN_MATRIX_EVENT_IDENTIFIER_V3
|| PATTERN_CONTAIN_MATRIX_EVENT_IDENTIFIER_V4.matcher(str).matches()) || str matches PATTERN_CONTAIN_MATRIX_EVENT_IDENTIFIER_V4)
} }
/** /**
@ -136,6 +135,6 @@ object MatrixPatterns {
* @return true if the string is a valid group id. * @return true if the string is a valid group id.
*/ */
fun isGroupId(str: String?): Boolean { fun isGroupId(str: String?): Boolean {
return str != null && PATTERN_CONTAIN_MATRIX_GROUP_IDENTIFIER.matcher(str).matches() return str != null && str matches PATTERN_CONTAIN_MATRIX_GROUP_IDENTIFIER
} }
} }

View File

@ -37,15 +37,13 @@ object MatrixLinkify {
} }
val text = spannable.toString() val text = spannable.toString()
var hasMatch = false var hasMatch = false
for (index in MatrixPatterns.MATRIX_PATTERNS.indices) { for (pattern in MatrixPatterns.MATRIX_PATTERNS) {
val pattern = MatrixPatterns.MATRIX_PATTERNS[index] for (match in pattern.findAll(spannable)) {
val matcher = pattern.matcher(spannable)
while (matcher.find()) {
hasMatch = true hasMatch = true
val startPos = matcher.start(0) val startPos = match.range.first
if (startPos == 0 || text[startPos - 1] != '/') { if (startPos == 0 || text[startPos - 1] != '/') {
val endPos = matcher.end(0) val endPos = match.range.last
val url = text.substring(matcher.start(0), matcher.end(0)) val url = text.substring(match.range)
val span = MatrixPermalinkSpan(url, callback) val span = MatrixPermalinkSpan(url, callback)
spannable.setSpan(span, startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) spannable.setSpan(span, startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
} }

View File

@ -21,13 +21,4 @@ import im.vector.matrix.android.api.MatrixCallback
/** /**
* Simple MatrixCallback implementation which delegate its calls to another callback * Simple MatrixCallback implementation which delegate its calls to another callback
*/ */
open class MatrixCallbackDelegate<T>(private val callback: MatrixCallback<T>) : MatrixCallback<T> { open class MatrixCallbackDelegate<T>(private val callback: MatrixCallback<T>) : MatrixCallback<T> by callback
override fun onSuccess(data: T) {
callback.onSuccess(data)
}
override fun onFailure(failure: Throwable) {
callback.onFailure(failure)
}
}

View File

@ -25,19 +25,13 @@ import timber.log.Timber
* @return the utf-8 string * @return the utf-8 string
*/ */
fun convertToUTF8(s: String): String? { fun convertToUTF8(s: String): String? {
var out: String? = s return try {
val bytes = s.toByteArray(Charsets.UTF_8)
if (null != out) { String(bytes)
try { } catch (e: Exception) {
val bytes = out.toByteArray(charset("UTF-8")) Timber.e(e, "## convertToUTF8() failed")
out = String(bytes) null
} catch (e: Exception) {
Timber.e(e, "## convertToUTF8() failed")
}
} }
return out
} }
/** /**
@ -47,17 +41,11 @@ fun convertToUTF8(s: String): String? {
* @return the utf-16 string * @return the utf-16 string
*/ */
fun convertFromUTF8(s: String): String? { fun convertFromUTF8(s: String): String? {
var out: String? = s return try {
val bytes = s.toByteArray()
if (null != out) { String(bytes, Charsets.UTF_8)
try { } catch (e: Exception) {
val bytes = out.toByteArray() Timber.e(e, "## convertFromUTF8() failed")
out = String(bytes, charset("UTF-8")) null
} catch (e: Exception) {
Timber.e(e, "## convertFromUTF8() failed")
}
} }
return out
} }

View File

@ -50,7 +50,7 @@ class EmojiCompatFontProvider @Inject constructor(): FontsContractCompat.FontReq
} }
fun addListener(listener: FontProviderListener) { fun addListener(listener: FontProviderListener) {
if (!listeners.contains(listener)) { if (listener !in listeners) {
listeners.add(listener) listeners.add(listener)
} }
} }

View File

@ -26,14 +26,16 @@ import im.vector.riotx.R
internal abstract class DialogAdapter(context: Context) : ArrayAdapter<DialogListItem>(context, R.layout.item_dialog) { internal abstract class DialogAdapter(context: Context) : ArrayAdapter<DialogListItem>(context, R.layout.item_dialog) {
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View { override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
var view = convertView val view: View
if (view == null) { if (convertView == null) {
view = LayoutInflater.from(context).inflate(R.layout.item_dialog, parent, false) view = LayoutInflater.from(context).inflate(R.layout.item_dialog, parent, false)
view.tag = DialogListItemHolder(view) view.tag = DialogListItemHolder(view)
} else {
view = convertView
} }
(view!!.tag as DialogListItemHolder).let { with(view.tag as DialogListItemHolder) {
it.icon.setImageResource(getItem(position).iconRes) icon.setImageResource(getItem(position).iconRes)
it.text.setText(getItem(position).titleRes) text.setText(getItem(position).titleRes)
} }
return view return view
} }

View File

@ -18,6 +18,7 @@ package im.vector.riotx.features.home
import androidx.annotation.ColorRes import androidx.annotation.ColorRes
import im.vector.riotx.R import im.vector.riotx.R
import kotlin.math.abs
@ColorRes @ColorRes
@ -36,7 +37,7 @@ fun getColorFromUserId(userId: String?): Int {
i++ i++
} }
return when (Math.abs(hash) % 8 + 1) { return when (abs(hash) % 8 + 1) {
1 -> R.color.riotx_username_1 1 -> R.color.riotx_username_1
2 -> R.color.riotx_username_2 2 -> R.color.riotx_username_2
3 -> R.color.riotx_username_3 3 -> R.color.riotx_username_3