Skip to content

Instantly share code, notes, and snippets.

View AndroidPoet's full-sized avatar
🎯
Focusing

Ranbir Singh AndroidPoet

🎯
Focusing
View GitHub Profile
//Add these lines inside your MainActivity.
if (Build.VERSION.SDK_INT >= 30) {
ViewCompat.setOnApplyWindowInsetsListener(binding.root) { view, windowInsets ->
val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
// Apply the insets as a margin to the view. Here the system is setting
// only the bottom, left, and right dimensions, but apply whichever insets are
// appropriate to your layout. You can also update the view padding
// if that's more appropriate.
@AndroidPoet
AndroidPoet / Change author names on all the commits. .txt
Created April 30, 2022 08:58
Change author names on all the commits.
git filter-branch -f --env-filter "
GIT_AUTHOR_NAME='name'
GIT_AUTHOR_EMAIL='exmaple@gmail.com'
GIT_COMMITTER_NAME='name'
GIT_COMMITTER_EMAIL='exmaple@gmail.com'
" HEAD
git push --force --tags origin 'refs/heads/main'
# or
@AndroidPoet
AndroidPoet / eachCount.kt
Last active May 1, 2022 14:35
eachCount
val words = "H,e,l,l,o w,o,r,l,d,H,e,l,l,o w,o,r,l,d".split(',')
val frequenciesofChars = words.groupingBy { it }.eachCount()
println("Counting frequencies letters:")
println(frequenciesofChars)
//Counting frequencies letters::
//{H=2, e=2, l=6, o w=2, o=2, r=2, d=2}
class MutableStack<E>(vararg items: E) {
private var elements = items.toMutableList()
fun push(element: E) = elements.add(element)
fun peek(): E = elements.last()
fun pop(index: Int): E = elements.removeAt(index)
val intList = mutableListOf(1, 2, 3, 4, 5, 6, 7)
val filtered = intList.filter {
it % 2 == 0
}
println(filtered)///[2, 4, 6]
val mapped = intList.map {
it * it
}
@AndroidPoet
AndroidPoet / HigherOrderFunctions.kt
Created May 6, 2022 05:18
Higher-Order Functions
fun main() {
val sumResult = calculate(4, 5, ::sum) //sumResult 9,
val mulResult = calculate(4, 5) { a, b -> a * b } // mulResult 20
println("sumResult $sumResult, mulResult $mulResult")
}
fun calculate(x: Int, y: Int, operation: (Int, Int) -> Int): Int {
return operation(x, y)
fun main() {
printMessage("Hello")
printMessageWithPrefix("Hello", "World")
sum(4, 5)
multiply(4, 5)
}
fun printMessage(message: String) {
println(message)
fun main() {
//var can be reassigned
var varInt: Int = 1
var varString: String = "Hello"
var varDouble: Double = 24.0
var varLong: Long = 30L
//val can't be reassigned
val valInt: Int = 1
//interface
interface CoroutineDispatcherProvider {
val main: CoroutineDispatcher
val io: CoroutineDispatcher
val default: CoroutineDispatcher
val unconfirmed: CoroutineDispatcher
}
//initialization using "by lazy"
open class RealCoroutineDispatcherProvider : CoroutineDispatcherProvider {
import android.util.Log
import com.currency.domain.CurrencyConverter
import io.ktor.client.*
import io.ktor.client.engine.android.*
import io.ktor.client.plugins.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.plugins.logging.*
import io.ktor.client.plugins.observer.*
import io.ktor.serialization.kotlinx.json.*
import kotlinx.serialization.json.Json