Skip to content

Instantly share code, notes, and snippets.

View tpakis's full-sized avatar

Thanos Tsakiridis tpakis

View GitHub Profile
class BindViewDelegate<T>(
private val createView: () -> T,
private val getLifecycle: () -> Lifecycle
) : Lazy<T>, LifecycleObserver {
private var view: T? = null
private val lifecycle: Lifecycle?
get() = try {
getLifecycle()
class Step1Fragment : ABTestFragment() {
private val button: Button by bindView(R.id.button)
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_step1_baseline, container, false)
@tpakis
tpakis / bindview-ext-framgments
Last active January 1, 2021 21:25
Auto clearing view reference for fragments
class BindViewDelegate<T>(
private val createView: () -> T,
private val getLifecycle: () -> Lifecycle
) : Lazy<T>, LifecycleObserver {
private var view: T? = null
private val lifecycle: Lifecycle?
get() = try {
getLifecycle()
@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)
}
@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) }
}
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 / 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)
class App: Application() {
override fun onCreate() {
super.onCreate()
setupChuckerErrors()
}
private fun setupChuckerErrors() {
if (BuildConfig.DEBUG) {
val systemHandler = Thread.getDefaultUncaughtExceptionHandler()
Thread.setDefaultUncaughtExceptionHandler(CustomCrashHandler(systemHandler, this))
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,
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()
}
}
}