Skip to content

Instantly share code, notes, and snippets.

View km-segmenter.js
const segmenter = new Intl.Segmenter("km", {granularity: "word"});
const input = "នឹកបងឬនឹកគេ?";
const segments = segmenter.segment(input);
for (const {segment} of segments) {
console.log(segment)
}
// OUTPUT:
// នឹក
@seanghay
seanghay / Tests.kt
Created Dec 9, 2020
Format KH Phone Number
View Tests.kt
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test
import kotlin.jvm.Throws
class PhoneNumberSanitizerTest {
@Test
fun `COUNTRY_CODE should be +855`() {
assertEquals("+855", PhoneNumberSanitizer.COUNTRY_CODE)
@seanghay
seanghay / HomeFragment.kt
Created Oct 25, 2020
ViewBinding inside Fragment
View HomeFragment.kt
class HomeFragment : Fragment() {
private var _binding: FragmentHomeBinding? = null
private val binding: FragmentHomeBinding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
@seanghay
seanghay / MainActivity.kt
Created Oct 25, 2020
The final result of ViewBinding KTX
View MainActivity.kt
class MainActivity : AppCompatActivity() {
private val binding by viewBinding(ActivityMainBinding::inflate)
}
@seanghay
seanghay / view-binding-ktx.kt
Last active Oct 26, 2020
ViewBinding Property Delegate with Android Lifecycle
View view-binding-ktx.kt
import android.os.Looper
import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import androidx.viewbinding.ViewBinding
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
@seanghay
seanghay / MainActivity.kt
Created Oct 25, 2020
Bind ViewBinding to existing view
View MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val root: CoordinatorLayout = findViewById(R.id.coordinator_layout)
binding = ActivityMainBinding.bind(root)
@seanghay
seanghay / MainActivity.kt
Created Oct 25, 2020
Inflate ViewBinding inside Activity
View MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// inflate view binding
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
}
View enable-view-binding.diff
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
defaultConfig {
...
}
@seanghay
seanghay / result-of.kt
Created Aug 27, 2020
ResultOf is a utility result class for handling data and exception.
View result-of.kt
/**
* ResultOf is a utility result class for handling data and exception.
* It's useful for LiveData and fragment/activity communication.
*/
sealed class ResultOf<out T> {
data class Success<out T>(val value: T) : ResultOf<T>()
data class Failure(
val message: String?,
val throwable: Throwable?