Skip to content

Instantly share code, notes, and snippets.

View trevorhackman's full-sized avatar

Trevor Hackman trevorhackman

View GitHub Profile
@trevorhackman
trevorhackman / ViewBindingFragment.kt
Created May 21, 2022 17:24
Android View Binding Without Boilerplate - Part 2 - 4
abstract class ViewBindingFragment<T: ViewBinding>(
private val inflate: (LayoutInflater, ViewGroup?, Boolean) -> T
): Fragment() {
private var _binding: T? = null
protected val binding get() = _binding!!
final override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
@trevorhackman
trevorhackman / BindingWithViewBindingFragment.kt
Created May 21, 2022 17:06
Android View Binding Without Boilerplate - Part 2 - 3
class ProfileFragment: ViewBindingFragment<ProfileBinding>(ProfileBinding::inflate)
@trevorhackman
trevorhackman / BindingWithNullOnDestroyInFragment.kt
Created May 21, 2022 17:02
Android View Binding Without Boilerplate - Part 2 - 2
private var binding: ProfileBinding by nullOnDestroy()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = ProfileBinding.inflate(inflater, container, false)
return binding.root
}
@trevorhackman
trevorhackman / TypicalBindingInFragment.kt
Created May 21, 2022 16:54
Android View Binding Without Boilerplate - Part 2 - 1
private var _binding: ProfileBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = ProfileBinding.inflate(inflater, container, false)
return binding.root
@trevorhackman
trevorhackman / NullOnDestroy.kt
Created December 11, 2021 23:24
For nulling something on destroy, primary motivation, a better way to do view binding.
/**
* Delegate for use in fragments
*
* When the fragment's view is destroyed the field will be set to null.
* This happens immediately before the fragment's onDestroyView() is called.
*
* Additionally, this delegate gives the same behavior as lateinit.
*
* Property should be a non-nullable var.
*/
@trevorhackman
trevorhackman / TaggerReflection.kt
Created October 20, 2021 21:27
Tagger but with reflection to grab class name.
interface Tag {
val tag: String
val log: Tagger.TaggedLogger
}
open class Tagger() : Tag {
override val tag = javaClass.enclosingClass?.simpleName ?: javaClass.simpleName
@trevorhackman
trevorhackman / Tagger.kt
Created August 29, 2021 21:08
For logging in Android. Use through either companion object inheritance or delegating the implementation of the interface for other cases.
interface Tag {
val tag: String
val log: Tagger.TaggedLogger
}
open class Tagger(override val tag: String) : Tag {
interface TaggedLogger {
fun e(message: String)
@trevorhackman
trevorhackman / KotlinTernaryLazy.kt
Created July 12, 2021 15:35
Kotlin Ternary Operator with higher-order overloads for lazy evaluation of arguments
inline infix fun <T> Boolean.T(trueOutput: T) = TernaryPart(this, trueOutput)
inline infix fun <T> TernaryPart<T>.F(falseOutput: T) = if (condition) trueOutput else falseOutput
class TernaryPart<T>(val condition: Boolean, val trueOutput: T)
// T overload for conditional evaluation of functional arguments
infix fun <T> Boolean.T(trueBranch: () -> T) = TernaryPart(this, trueBranch)
// F overload for conditional evaluation of functional arguments
@trevorhackman
trevorhackman / KotlinTernary.kt
Last active July 9, 2021 07:35
Kotlin Ternary Operator
inline infix fun <T> Boolean.T(trueOutput: T) = TernaryPart(this, trueOutput)
inline infix fun <T> TernaryPart<T>.F(falseOutput: T) = if (condition) trueOutput else falseOutput
class TernaryPart<T>(val condition: Boolean, val trueOutput: T)