Skip to content

Commit

Permalink
Update edittext searchOnDelay + show anchorListDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
tuannvm2109 committed May 17, 2021
1 parent 8b0216f commit 4079b54
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@ import androidx.appcompat.app.AppCompatActivity
import com.nvmt.android.mtlibrary.base.iconbadge.IconBadgeView
import com.nvmt.android.mtlibrary.base.ratestar.RateStarView
import com.nvmt.android.mtlibrary.extension.setForceOnlyUpperCaseAlphaBet
import com.nvmt.android.mtlibrary.extension.setOnSearchDelayListener
import com.nvmt.android.mtlibrary.extension.setTextHtml
import com.nvmt.android.mtlibrary.extension.setTextHtmlWithImage

class TestMTActivity : AppCompatActivity() {
val str = "<div style=\"text-align: center; color: #345; padding-top: 10px;\">\r\n<h1 style=\"color: #317399;\">Please Disable Your Ad Blocker 🙏</h1>\r\n<h2><strong>That would help a&nbsp;lot&nbsp;🥺🤑</strong></h2>\r\n<p><a href=\"https://www.youtube.com/watch?v=xB4-XAHiS-U&ab_channel=K%C3%AAnhThi%E1%BA%BFuNhi-BHMEDIA\" target=\"_blank\"><strong>How&nbsp;to disable&nbsp;adBlock?</strong></a> &larr; Ctrl+click</p>\r\n<p><img src=\"/editor/images/ab.png\" alt=\"adBlock icon\" /></p>\r\n</div>"
val str =
"<div style=\"text-align: center; color: #345; padding-top: 10px;\">\r\n<h1 style=\"color: #317399;\">Please Disable Your Ad Blocker 🙏</h1>\r\n<h2><strong>That would help a&nbsp;lot&nbsp;🥺🤑</strong></h2>\r\n<p><a href=\"https://www.youtube.com/watch?v=xB4-XAHiS-U&ab_channel=K%C3%AAnhThi%E1%BA%BFuNhi-BHMEDIA\" target=\"_blank\"><strong>How&nbsp;to disable&nbsp;adBlock?</strong></a> &larr; Ctrl+click</p>\r\n<p><img src=\"/editor/images/ab.png\" alt=\"adBlock icon\" /></p>\r\n</div>"

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// findViewById<TextView>(R.id.tv).setTextHtml(str)
findViewById<EditText>(R.id.edt).setForceOnlyUpperCaseAlphaBet()
findViewById<EditText>(R.id.edt).setOnSearchDelayListener({
Log.d("asdf",it)
}, 1000L)

// findViewById<TextView>(R.id.tv).setOnClickListener {
//// Log.d("asdf", findViewById<EditText>(R.id.img2).text.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,32 @@ object MTConstant {
const val KEY_COLLAPSE = "KEY_COLLAPSE"
const val KEY_NOTIFICATION_ID = "KEY_NOTIFICATION_ID"
const val KEY_SERVICE_ID = "KEY_SERVICE_ID"

val SOURCE_CHARACTERS = charArrayOf(
'À', 'Á', 'Â', 'Ã', 'È', 'É',
'Ê', 'Ì', 'Í', 'Ò', 'Ó', 'Ô', 'Õ', 'Ù', 'Ú', 'Ý', 'à', 'á', 'â',
'ã', 'è', 'é', 'ê', 'ì', 'í', 'ò', 'ó', 'ô', 'õ', 'ù', 'ú', 'ý',
'Ă', 'ă', 'Đ', 'đ', 'Ĩ', 'ĩ', 'Ũ', 'ũ', 'Ơ', 'ơ', 'Ư', 'ư', '',
'', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', 'ế', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '',
)

val DESTINATION_CHARACTERS = charArrayOf(
'A', 'A', 'A', 'A', 'E', 'E',
'E', 'I', 'I', 'O', 'O', 'O', 'O', 'U', 'U', 'Y', 'a', 'a', 'a',
'a', 'e', 'e', 'e', 'i', 'i', 'o', 'o', 'o', 'o', 'u', 'u', 'y',
'A', 'a', 'D', 'd', 'I', 'i', 'U', 'u', 'O', 'o', 'U', 'u', 'A',
'a', 'A', 'a', 'A', 'a', 'A', 'a', 'A', 'a', 'A', 'a', 'A', 'a',
'A', 'a', 'A', 'a', 'A', 'a', 'A', 'a', 'A', 'a', 'E', 'e', 'E',
'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e',
'I', 'i', 'I', 'i', 'O', 'o', 'O', 'o', 'O', 'o', 'O', 'o', 'O',
'o', 'O', 'o', 'O', 'o', 'O', 'o', 'O', 'o', 'O', 'o', 'O', 'o',
'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U',
'u', 'U', 'u', 'Y', 'y', 'Y', 'y', 'Y', 'y', 'Y', 'y',
)
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
package com.nvmt.android.mtlibrary.extension

import android.content.Context
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import androidx.core.widget.addTextChangedListener
import com.nvmt.android.mtlibrary.R
import com.nvmt.android.mtlibrary.base.MTConstant.DESTINATION_CHARACTERS
import com.nvmt.android.mtlibrary.base.MTConstant.SOURCE_CHARACTERS
import kotlinx.coroutines.*
import java.util.*

fun EditText.setOnSearchDelayListener(listener: (String) -> Unit, delay: Long = 1500L) {
val scope = CoroutineScope(context = Dispatchers.Main)
var debounceJob: Job? = null

var lastSearch: String? = null
addTextChangedListener {
if (it.isNullOrBlank() || it.toString() == lastSearch) return@addTextChangedListener
lastSearch = it.toString()
debounceJob?.cancel()
debounceJob = scope.launch {
delay(delay)
listener.invoke(it.toString())
}

}
}

fun EditText.setOnSearchListener(listener: () -> Unit) {
imeOptions = EditorInfo.IME_ACTION_SEARCH
isSingleLine = true
Expand All @@ -30,50 +51,3 @@ fun EditText.setForceOnlyUpperCaseAlphaBet() {
this.setSelection(newStr.length)
}
}

private val SOURCE_CHARACTERS = charArrayOf(
'À', 'Á', 'Â', 'Ã', 'È', 'É',
'Ê', 'Ì', 'Í', 'Ò', 'Ó', 'Ô', 'Õ', 'Ù', 'Ú', 'Ý', 'à', 'á', 'â',
'ã', 'è', 'é', 'ê', 'ì', 'í', 'ò', 'ó', 'ô', 'õ', 'ù', 'ú', 'ý',
'Ă', 'ă', 'Đ', 'đ', 'Ĩ', 'ĩ', 'Ũ', 'ũ', 'Ơ', 'ơ', 'Ư', 'ư', '',
'', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', 'ế', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '',
)

private val DESTINATION_CHARACTERS = charArrayOf(
'A', 'A', 'A', 'A', 'E', 'E',
'E', 'I', 'I', 'O', 'O', 'O', 'O', 'U', 'U', 'Y', 'a', 'a', 'a',
'a', 'e', 'e', 'e', 'i', 'i', 'o', 'o', 'o', 'o', 'u', 'u', 'y',
'A', 'a', 'D', 'd', 'I', 'i', 'U', 'u', 'O', 'o', 'U', 'u', 'A',
'a', 'A', 'a', 'A', 'a', 'A', 'a', 'A', 'a', 'A', 'a', 'A', 'a',
'A', 'a', 'A', 'a', 'A', 'a', 'A', 'a', 'A', 'a', 'E', 'e', 'E',
'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e',
'I', 'i', 'I', 'i', 'O', 'o', 'O', 'o', 'O', 'o', 'O', 'o', 'O',
'o', 'O', 'o', 'O', 'o', 'O', 'o', 'O', 'o', 'O', 'o', 'O', 'o',
'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U',
'u', 'U', 'u', 'Y', 'y', 'Y', 'y', 'Y', 'y', 'Y', 'y',
)

private fun removeAccent(ch: Char): Char {
var ch = ch
val index: Int = Arrays.binarySearch(SOURCE_CHARACTERS, ch)
if (index >= 0) {
ch = DESTINATION_CHARACTERS[index]
}
return ch
}

private fun removeAccent(str: CharSequence): String {
val re = Regex("[^[^\\W_]\\d\\s]")
val newStr = re.replace(str, "").toUpperCase(Locale.getDefault())
val sb = StringBuilder(newStr)
for (i in sb.indices) {
sb.setCharAt(i, removeAccent(sb[i]))
}
return sb.toString()
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ fun Fragment?.showDialogListAnchor(
anchorView: View,
data: List<String?>,
listener: (Int) -> Unit
) {
if (this?.context == null) return
): ListPopupWindow? {
if (this?.context == null) return null

hideKeyboard()
val listPopupWindow =
Expand All @@ -83,4 +83,5 @@ fun Fragment?.showDialogListAnchor(
listPopupWindow.dismiss()
}
listPopupWindow.show()
return listPopupWindow
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.nvmt.android.mtlibrary.extension

import com.nvmt.android.mtlibrary.base.MTConstant.DESTINATION_CHARACTERS
import com.nvmt.android.mtlibrary.base.MTConstant.SOURCE_CHARACTERS
import java.text.NumberFormat
import java.util.*
import kotlin.math.roundToInt

fun removeAccent(ch: Char): Char {
var ch = ch
val index: Int = Arrays.binarySearch(SOURCE_CHARACTERS, ch)
if (index >= 0) {
ch = DESTINATION_CHARACTERS[index]
}
return ch
}

fun removeAccent(str: CharSequence): String {
val re = Regex("[^[^\\W_]\\d\\s]")
val newStr = re.replace(str, "").toUpperCase(Locale.getDefault())
val sb = StringBuilder(newStr)
for (i in sb.indices) {
sb.setCharAt(i, removeAccent(sb[i]))
}
return sb.toString()
}

fun String.equalVietnamese(anotherStr: String?, ignoreCase: Boolean = false): Boolean {
if (anotherStr == null) return false
val newStr1 = removeAccent(this)
val newStr2 = removeAccent(anotherStr)
return newStr1.equals(newStr2, ignoreCase = ignoreCase)
}

0 comments on commit 4079b54

Please sign in to comment.