Skip to content

Instantly share code, notes, and snippets.

View ch8n's full-sized avatar
💻
Always on to writing my next article.

Chetan Gupta ch8n

💻
Always on to writing my next article.
View GitHub Profile
@ch8n
ch8n / hiltInjectionVM.kt
Created December 13, 2020 12:17
View Model Injection Hilt for Medium article
@AndroidEntryPoint
class MovieDetailActivity : AppCompatActivity() {
val viewmodel: MovieViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
...
// viewmodel = Injector.detailViewModel(this) remove this line
...// viewModle is ready to be used...
@ch8n
ch8n / SampleActivityViewBinding.kt
Created December 7, 2020 16:44
sample for activity using base activity for view binding | checkout complete blog from - https://chetangupta.net/viewbinding/
// Source : https://chetangupta.net/viewbinding/
// file name : profile_layout.xml
<FrameLayout ... >
<TextView android:id="@+id/text_name" ... />
<TextView android:id="@+id/text_twitter" ... />
<TextView android:id="@+id/text_blog" ... />
</FrameLayout>
@ch8n
ch8n / ViewBindingFragment.kt
Last active January 15, 2021 17:00
base class fragment using view binding | checkout complete blog @ https://chetangupta.net/viewbinding/
// Source : https://chetangupta.net/viewbinding/
// Author : ChetanGupta.net @Androidbites
abstract class ViewBindingFragment<VB : ViewBinding> : Fragment() {
private var _binding: ViewBinding? = null
abstract val bindingInflater: (LayoutInflater, ViewGroup?, Boolean) -> VB
@Suppress("UNCHECKED_CAST")
protected val binding: VB
@ch8n
ch8n / SampleFragment.kt
Last active December 7, 2020 16:45
sample fragment using base fragment for view binding | checkout complete blog from - https://chetangupta.net/viewbinding/
// Source : https://chetangupta.net/viewbinding/
// file name : profile_layout.xml
<FrameLayout ... >
<TextView android:id="@+id/text_name" ... />
<TextView android:id="@+id/text_twitter" ... />
<TextView android:id="@+id/text_blog" ... />
</FrameLayout>
// controller fragment class
@ch8n
ch8n / ViewBindingActivity.kt
Last active January 15, 2021 16:59
Base Activity for view binding | checkout complete blog from - https://chetangupta.net/viewbinding/
// Author : ChetanGupta.net @Androidbites
abstract class ViewBindingActivity<VB : ViewBinding> : AppCompatActivity() {
private var _binding: ViewBinding? = null
abstract val bindingInflater: (LayoutInflater) -> VB
@Suppress("UNCHECKED_CAST")
protected val binding: VB
get() = _binding as VB
@ch8n
ch8n / map_to_linkedHashmap.kt
Created October 12, 2020 13:40
map to linkedHashmap in kotlin
// Directly use the 4th constructor example
val contact = mapOf("chetan" to "[email protected]")
val linkedMapOfContact = LinkedHashMap(contact)
// Or create and add all entries
val linkedMapOfContact = LinkedHashMap<String,Int>()
linkedMapOfContact.putAll(contact)
@ch8n
ch8n / linkedhashmap_directly.kt
Created October 12, 2020 13:39
linkedhashmap create directly in kotlin
val linkedHashMap = LinkedHashMap<String,Int>()
@ch8n
ch8n / linkedhashMap_defination.kt
Created October 12, 2020 13:37
linkedhashMap definition in kotlin
fun <K, V> linkedMapOf(vararg pairs: Pair<K, V>): LinkedHashMap<K, V>
= pairs.toMap(LinkedHashMap(mapCapacity(pairs.size)))
// this factory function takes pair and uses LinkedHashMap's second constructor which
// start off with the object capacity as the number of pairs passed
@ch8n
ch8n / linkedhashmap_resolve.kt
Created October 12, 2020 13:36
linkedhashmap resolved in kotlin
fun <K, V> linkedMapOf(): LinkedHashMap<K, V> = LinkedHashMap<K, V>()
// linkedMapOf directly create LinkedHashMap object default constructor
// Java LinkedHashMap default constructor create LinkedHashMap with
// * inital capacity 16 and * LoadFactor 0.75
@ch8n
ch8n / linkedhashmap_stdlib_function.kt
Created October 12, 2020 13:35
linkedhashmap stdlib function in kotlin
// Kotlin Standard Library function
fun <K, V> linkedMapOf(): LinkedHashMap<K, V>
fun <K, V> linkedMapOf(vararg pairs: Pair<K, V>): LinkedHashMap<K, V>