Skip to content

Instantly share code, notes, and snippets.

View tpakis's full-sized avatar

Thanos Tsakiridis tpakis

View GitHub Profile
enum class ABTestVariation(val navGraphRes: Int) {
BASELINE(R.navigation.baseline_navigation),
VARIATION1(R.navigation.variant1_navigation),
VARIATION2(R.navigation.variant2_navigation),
}
private val abTestVariation: ABTestVariation by lazy {
intent?.getSerializableExtra(KEY) as? ABTestVariation ?: ABTestVariation.BASELINE
}
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/baseline_navigation"
app:startDestination="@id/step1Fragment">
<fragment
android:id="@+id/step1Fragment"
android:name="com.example.abnavigationexample.ui.abtest.fragments.Step1Fragment"
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/baseline_navigation"
app:startDestination="@id/step1Fragment">
<fragment
android:id="@+id/step1Fragment"
android:name="com.example.abnavigationexample.ui.abtest.fragments.Step1Fragment"
private fun handleNavigationEvent(nextStep: Int?) {
if (nextStep != null) {
try {
navController.navigate(R.id.next, bundleOf(STEP_BUNDLE_KEY to nextStep))
// if there is no next step then finish
} catch (e: IllegalArgumentException) {
finish()
}
}
}
class CustomCrashHandler(
private val defaultHandler: Thread.UncaughtExceptionHandler? = null,
applicationContext: Context
) : Thread.UncaughtExceptionHandler {
private val chuckerCollector: ChuckerCollector by lazy {
ChuckerCollector(
context = applicationContext,
// Toggles visibility of the push notification
showNotification = true,
class App: Application() {
override fun onCreate() {
super.onCreate()
setupChuckerErrors()
}
private fun setupChuckerErrors() {
if (BuildConfig.DEBUG) {
val systemHandler = Thread.getDefaultUncaughtExceptionHandler()
Thread.setDefaultUncaughtExceptionHandler(CustomCrashHandler(systemHandler, this))
@tpakis
tpakis / ClickListener
Last active August 7, 2023 13:18
ClickListener with debounce
class DebouncingOnClickListener(
private val intervalMillis: Long,
private val doClick: ((View) -> Unit)
) : View.OnClickListener {
override fun onClick(v: View) {
if (enabled) {
enabled = false
v.postDelayed(ENABLE_AGAIN, intervalMillis)
doClick(v)
fun View.setOnCLick(intervalMillis: Long = 0, doClick: (View) -> Unit) =
setOnClickListener(DebouncingOnClickListener(intervalMillis = intervalMillis, doClick = doClick))
// use it like this
my_button.setOnCLick(intervalMillis = 500) {
// Do stuff
}
// or this
my_button.setOnCLick {
@tpakis
tpakis / bindview-ext
Created December 16, 2020 12:36
bindView extension methods
fun <T : View> Activity.bindView(@IdRes res: Int): Lazy<T> {
@Suppress("UNCHECKED_CAST")
return lazy(LazyThreadSafetyMode.NONE) { findViewById<T>(res) }
}
fun <T : View> View.bindView(@IdRes res: Int): Lazy<T> {
@Suppress("UNCHECKED_CAST")
return lazy(LazyThreadSafetyMode.NONE) { findViewById<T>(res) }
}
@tpakis
tpakis / bindview-ext-activity
Created December 16, 2020 14:40
Intended use of bindview in activity
class MainActivity : AppCompatActivity() {
private val buttonBaseline: Button by bindView(R.id.button_go_to_baseline)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
buttonBaseline.setOnClickListener {
goToABTest(ABTestVariation.BASELINE)
}