Skip to content

Instantly share code, notes, and snippets.

@drulabs
drulabs / FCM_message_single_device_curl_command.txt
Last active June 14, 2023 15:27
Curl command for sending FCM message
curl -i -H 'Content-type: application/json' -H 'Authorization: key=<your_server_key>' -XPOST https://fcm.googleapis.com/fcm/send -d '{
"registration_ids":["registration_ids", "of the", "target", "devices as array"],
"notification": {
"title":"Title of your notification",
"body":"content of your notification"
},
"data": {
"key1" : "value1",
"key2" : "value2",
"key3" : 23.56565,
// Java POJO
public class Person {
private String name;
private int age;
private char gender;
public Person(String name, int age, char gender) {
this.name = name;
this.age = age;
this.gender = gender;
val blank = " ".isBlank() // true
val first = "Kaushal.Dhruw".substringBefore('.') // "Kaushal"
val last = "Kaushal.Dhruw".substringAfter('.') // "Dhruw"
val addSpaces = "1".padStart(2) // " 1"
val addSpacesEnd = "1".padEnd(3, '0') // "100"
val dropStart = "Dhruw".drop(2) // "ruw"
val dropEnd = "Dhruw".dropLast(2) // "Dhr"
class NEFT: Transaction ... // class NEFT extends Transaction
class IMPS: Transaction ... // class IMPS extends Transaction
class RTGS: Transaction ... // class RTGS extends Transaction
val transaction: Transaction = getCurrentTransaction()
// Now we don't know the type of transaction. It could be either of NEFT, IMPS or RTGS
// So we need to identify the type of trsaction and process it accordingly
when (transaction) {
is NEFT -> transaction.processNEFT()
import android.database.sqlite.SQLiteDatabase
// Add an extension function to perform SQLite operations
// This is a higher order function as it takes a function as parameter (operation)
// @param operation: a function that takes no parameter and returns nothing
fun SQLiteDatabase.performDBTransaction(operation: () -> Unit) {
beginTransaction()
operation() // this is the passed function
endTransaction()
}
// Add a function toINR() to kotlin's Double type
fun Double.toINR() = this * oneDollarInINR
// Yes it is a single line function
val netAmountDeposited = 230.0
println("Net amount deposited(in INR): ${netAmountDeposited.toINR()}")
// Did you notice the string interpolation above
fun main (args: Array<String>) {
val x = arrayListOf("A.B", "B.C", "C.D", "D.E", "E.F")
println(x.map { stringStirrer(it) })
// above line prints [Axx, Bxx, Cxx, Dxx, Exx]
}
fun stringStirrer(param: String): String {
var changes = param.substringBefore(".")
changes += "xx"
return changes
import android.content.Context
import android.content.SharedPreferences
private fun doSomething(context: Context) {
val prefs = context.getSharedPreferences("app_prefs", Context.MODE_PRIVATE).edit()
prefs.persist(key = "my_name", value = "Kaushal Dhruw")
}
fun SharedPreferences.Editor.persist(key: String, value: String) {
putString(key, value)
// THE KOTLIN WAY
// 1. Who did maximum number of transactions
val maxTransactionsBy = customers.maxBy { it.transactions.count() }
// List all transactions irrespective of customers
val transactions = customers.flatMap { it.transactions }
// 2a. Get total amount deposited in the bank (use transactions constant from above)
val totalAmountDeposited = transactions.filter { it.type == "deposit" }.sumByDouble { it.amount }
@drulabs
drulabs / MinBalanceCustomer.java
Last active August 7, 2018 13:03
Find customer with minimum balance
// JAVA WAY - Who has minimum balance
Customer minBalanceCustomer = Collections.min(
customers, (o1, o2) -> (int) (o1.getBalance() - o2.getBalance())
);