Skip to content

Instantly share code, notes, and snippets.

View muchbeer's full-sized avatar

George Machibya muchbeer

  • Dar es Salaam
View GitHub Profile
@muchbeer
muchbeer / regex.kt
Created August 12, 2022 15:33
Regex
fun main(args : Array<String>) {
val text = "With multiple \t whitespace"
println(text)
}
fun replaceMultipleWhiteSpace(value : String) : String {
var regex = Regex("\\s+")
return regex.replace(value, " ")
}
@muchbeer
muchbeer / Coil.kt
Created April 7, 2022 07:21
Coil as the best image loader library best for loading images
binding.imageView.load(url) {
placeholder(R.drawable.ic_placeholder)
crossfade(true)
crossfade(500)
transformations(RoundedCornersTransformation(80f))
transformations(CircleCropTransformation())
transformations(GrayscaleTransformation()) //will make image black and white
transformations(BlurTransformation(applicationContext, radius=20f))
//to have multiple transformation see below
@muchbeer
muchbeer / HighOrderFunction.kt
Created February 27, 2022 05:49
Understanding Higher-Order Functions and Lambdas in Kotlin best explained
/*
A higher-order function is a function that takes functions as parameters or returns a function.
It's a function which can take do two things:
Can take functions as parameters
Can return a function
*/
// 1. A function can take functions as parameters.
fun passMeFunction(abc: () -> Unit) {
// I can take function
@muchbeer
muchbeer / BestLazyExplained.kt
Last active February 15, 2022 16:56
Lazy simple explanation pretty awesome : Credit to https://agrawalsuneet.github.io/blogs/lazy-property-in-kotlin/
private const val SALARY_THRESHOLD = 100
private val taxCollector = TaxCollector()
//before using lazy
private val accountant = Accountant()
//after using lazy everything by the keyword is delegate, this lazy is used when we want to utilize account
private val accountant by lazy {
Accountant()
@muchbeer
muchbeer / GraddleModule.kt
Created January 25, 2022 15:00
GraddleNew way of writing dependency version works fine
// kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion"
// architecture components
implementation "androidx.core:core-ktx:$coreVersion"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycleVersion"
@muchbeer
muchbeer / CollectionFunction.kt
Last active August 16, 2022 15:29
Kotlin Collection Functions best use of function simplifying the Array List List good use of List Array and Set Map, use inline and predicate to filter list and the Unzip is soo cool some functions associateBy toSet joinToString reduce partition sorted all groupBy zip filter union single and if you have a very large collection list of data
//*************removing the duplicate strings from an array*******************
// Maintain the original order of items
val devs = arrayOf("Amit", "Ali", "Amit", "Sumit", "Sumit", "Himanshu")
print(devs.distinct()) // [Amit, Ali, Sumit, Himanshu]
// Maintain the original order of items
val devs = arrayOf("Amit", "Ali", "Amit", "Sumit", "Sumit", "Himanshu")
print(devs.toSet()) // [Amit, Ali, Sumit, Himanshu]
// Maintain the original order of items
@muchbeer
muchbeer / KotlinSerializerFounder.kt
Last active February 10, 2022 12:49
Serialization help improve reading the data class model and simplify the json cool stuff More information : https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/basic-serialization.md and from Kotlin documentation: https://blog.jetbrains.com/kotlin/2020/10/kotlinx-serialization-1-0-released/
//*************************When using with Retrofit please use below converter*************************************
implementation("com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:0.8.0")
//A Retrofit 2 Converter.Factory for Kotlin serialization.
val contentType = "application/json".toMediaType()
val retrofit = Retrofit.Builder()
.baseUrl("https://example.com/")
.addConverterFactory(Json.asConverterFactory(contentType))
.build()
@muchbeer
muchbeer / Constant.kt
Last active January 5, 2022 15:45
Utility contained different useful component i.e Constant, declare apiKey and ignore using gitignore
object Constants {
const val UNSPLASH_IMAGE_TABLE = "unsplash_image_table
}
@muchbeer
muchbeer / animation.xml
Last active June 3, 2022 10:47
Animation from one fragment to another using Navigation component Architecture
/*
slide_in_left.xml
*/
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromXDelta="0"
android:toXDelta="-100%p" />
</set>
@muchbeer
muchbeer / LogCatSimplified.kt
Last active December 29, 2021 13:30
The easy way of create Log by using logcat by square and Timber by JakeWhartour way simple and working like a charm
dependencies {
implementation 'com.squareup.logcat:logcat:0.1'
}
*************Install AndroidLogcatLogger in Application.onCreate()******************
import android.app.Application
import logcat.AndroidLogcatLogger
import logcat.LogPriority.VERBOSE