Skip to content

Instantly share code, notes, and snippets.

@limkhashing
Created January 11, 2025 18:04
Show Gist options
  • Select an option

  • Save limkhashing/546b8a75e7709f20632cfde9a700ca70 to your computer and use it in GitHub Desktop.

Select an option

Save limkhashing/546b8a75e7709f20632cfde9a700ca70 to your computer and use it in GitHub Desktop.
Mask phone number
fun maskPhoneNumber(input: String): String {
fun removeNonNumericalChar(input: String): String {
val result = StringBuilder()
for (char in input) {
if (char.isDigit()) {
result.append(char)
}
}
return result.toString()
}
val digits = removeNonNumericalChar(input)
val length = digits.length
val maskedDigit = when (length) {
10 -> digits.substring(0, 2) + "****" + digits.substring(6)
11 -> digits.substring(0, 3) + "****" + digits.substring(7)
12 -> digits.substring(0, 4) + "****" + digits.substring(8)
else -> return input
}
return maskedDigit
}
fun main() {
binding.recyclerView.adapter = RecyclerViewAdapter
binding.recyclerView.addOnScrollListener(EndlessRecyclerViewScroll())
}
class RecyclerView: Adapter {
}
class EndlessRecyclerViewScroll: RecyclerView.OnScrollListener() {
val page = 1
override fun onScrollStateChanged(recyclerView: RecyclerView) {
recyclerView.layoutDirection // View.Layout_Direction.RTL
if (!recyclerView.canScrollHorizontal(direction = -1)) { // can scroll left
page ++
onLoadMore(page)
}
if (!recyclerView.canScrollHorizontal(direction = 1)) { // can scroll right
page ++
onLoadMore(page)
}
}
abstract fun onLoadMore(page: Int)
}
@limkhashing

Copy link
Copy Markdown
Author
  1. Write a function to mask the middle 4 digital of the Malaysia phone number, it should support different phone number type ,for example
+60 11-1549 1767 -> 6011****1767
+601115491767 -> 6011****1767
1115491767 -> 11****1767
+60 1115491767 -> 6011****1767

public func maskPhoneNumber(phoneNumber: String) -> String {
    var result = ""
    // add code to implement the mask phone number feature
    return result
}

@limkhashing

Copy link
Copy Markdown
Author
  1. RecyclerView support Right to Left scrolling, and pagination

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment