Sunday, 24 April 2022

Android registerForActivityResult


As a very fundamentals that we Android developer has learned, and it shows the way for communicating between two components. The provided startActivityForResult(..), onActivityForResut(..) was simple enough to implement for many years, but now we are hard to accept the change, it would be the evolution of language and its features which really helps to do the code in concise way.  So, as per this we see some pitfalls on the earlier approach like

  • Difficult to search the caller function in our project, 
  • Tedious in getting and handles the results on the fragment, 
  • Chance of missing out the results when the component is recreated, 
  • Conflicts with the same request code, 
  • Handling self-permissions request etc.
Earlier Approach:


Now, with updates to androidx.activity:activity-ktx to 1.2.0. It has deprecated startActivityForResult in favor of registerForActivityResult, notable thing is this implementation avoids and taken care of the above mentioned issues.  Notable thing is here we no need of permission request code, it will be taken care automatically. 

Let’s see the new implementation on implicit and explicit calls with registerForActivityResult, ActivityResultLauncher, ActivityResultContracts, ActivityResultCallback.

The class registerForActivityResult helps to register with ActivityResultContracts (which handles explicit or implicit calls) and other param as the callback action as lambda blocks with (ActivityResultCallback) and returns the launcher (ActivityResultLauncher) object, where we use the launch (...) method with params.

New Approach:

Explicit Calls: We all know calling another activity or component and getting result from there on finish on explicit intent calls.


In the above approach we are creating our explicit launcher which gets data on the resultant callbacks, so this way it would be easy to define our multiple launcher and those are handled independently.  Here the same approach can be used on the Fragment as well.

Implicit Calls: The implicit calls which invoke the system contracts such as take a Picture from camera gallery, accessing contacts, etc.  Please refer the below link for all available ActivityResultContracts


Conclusion:  The approach towards registerForActivityResult is really clean & concise, IMO below are the pros 
  • Improved code readability, here we no need to jump between onActivityResult() & startActivityForResult.
  • ActivityResultLauncher is returned by registerForActivityResult and this used to launch the components, the input parameter is clearly defined to get the desired results is the callback.
  • There is no Boilerplate code for requesting permission from the user.



Saturday, 2 April 2022

Get context in Jetpack Compose

In Android Compose, you can get the context by using the LocalContext, but it should be call'd from the composable function / scope.

val context = LocalContext.current

In the below code snippet we are retrieving the context and show a toast message inside the composable.

@Composable
fun MyToastDisplay(name: String) {
    val ctx = LocalContext.current
    Column(
        Modifier
            .fillMaxHeight()
            .fillMaxWidth(), verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text(text = "Hello $name", color = Color.Red,
            modifier = Modifier
                .background(Color.Green)
                .clickable {
                    Toast
                        .makeText(ctx, "Welcome to the Compose World", Toast.LENGTH_SHORT)
                        .show()
                })
    }
}

If you use the LocalContext.current directly inside the clickable function results in the compilation error “@composable invocations can only happen from the context of an @composable function”.


Since the LocalContext.current is composable, you can’t invoke it within the non-composable function.  i.e. clickable function is not a composable function and so can’t accept other composable functions. 

Alternatively, you can get the context outside the clickable function scope and use, as shown in the above code snippet.


Happy Coding :-)

Sunday, 27 March 2022

Generics – Out, In

Generics feature is template kind of implementation approach, which allow us to define classes, methods and properties which are accessible using different data types and keep a check of the compile-time type safety.


A generic type is a class or function that is parameterized over types. We always use angle brackets (<>) to specify the type parameter in the program.


Pros:

Compile time safety: Generics code is checked at compile time for the parameterized type so that it avoids run time error


Type casting: No need to typecast the object.


Type safety: Generic allows only single type of object at a time.


If you ever defined generic in Kotlin, you’ll notice many a times, it would propose to use the in or out keyword to define the generic. It puzzles me at a start on when which is used, and for what.

Formally, this is a way to define Covariant & Contravariance. Let’s see with the example in simple Word Processing and its implementation on Consumer (User) & Producer (Output Mode).


Out (Covariant):  If your generic class/interface only use the generic type as output of its function/s, it won't allow us to add the setter function with T


interface ProduceOutput<out T> {
    fun getContent(): T
}

This ProduceOutput interface, as it is mainly to produce output of the generic type. 
Side Note: produce = output = out

In (Contravariance): If your generic class/interface only use the generic type as input of its function/s, it won't allow us to getter function with T

interface ConsumeInput<in T> {
    fun consumeContent(data: T)
}

This ConsumeInput interface, as it is mainly to consuming the generic type. 
Side Note: consume = input = in




Before we see how the implementation on Producer and Consumer, lets create a necessary class hierarchy, which is going to handle the publishing industry on their document processing with their consumer base.


open class WordProcessor(val content: String, val isStyle: Boolean = false, val isAnimated: Boolean = false) {
    fun show() {
        println("Type : $content | Style: $isStyle | Animation: $isAnimated")
    }

    fun showConsumer() {
        println("Input : $content | Style: $isStyle | Animation: $isAnimated")
    }
}

open class Notes(input: String) : WordProcessor(input)
class Transcript(input: String) : Notes(input)
class FormattedNote(input: String) : WordProcessor(input, true)
class DigitalContent(input: String) : WordProcessor(input, true, true)

Implementation classes for producing the output via software tools as below

class GeneralProcessing : ProduceOutput<WordProcessor> {
    override fun getContent(): WordProcessor {
        return WordProcessor("Content Processor General")
    }
}

class Notepad : ProduceOutput<Notes> {
    override fun getContent(): Notes {
        return Notes("Notepad Content")
    }
}

class MSWord : ProduceOutput<FormattedNote> {
    override fun getContent(): FormattedNote {
        return FormattedNote("Formatted Content")
    }
}

class Flash : ProduceOutput<DigitalContent> {
    override fun getContent(): DigitalContent {
        return DigitalContent("Graphic Content")
    }
}

class SubEditPlayer : ProduceOutput<Transcript> {
    override fun getContent(): Transcript {
        return Transcript("Subtitle Content")
    }
}

For 'out' generic, we could assign a class of subtype to class of super-type, Please check out the comments in the code.

/*out -- This subtype class will be assigned only to the same type or super type class*/
    
val meetingMinutes: ProduceOutput<WordProcessor> = Notepad()
val wallPoster: ProduceOutput<WordProcessor> = MSWord()
val flashGame: ProduceOutput<WordProcessor> = Flash()
val osDefaultDoc: ProduceOutput<WordProcessor> = GeneralProcessing()
val youtubeSubtitle: ProduceOutput<Transcript> = SubEditPlayer()

// Eg: out compiler error
// val meetingRecording:ProduceOutput<DigitalContent> = GeneralProcessing() // Error
// val poster:ProduceOutput<Notepad> = GeneralProcessing() //Error
val twoDGame: ProduceOutput<DigitalContent> = Flash() // This works - Same Class Type
val subTile: ProduceOutput<WordProcessor> = SubEditPlayer() // This works --  subtype assigned to supertype

meetingMinutes.getContent().show()
wallPoster.getContent().show()
flashGame.getContent().show()
osDefaultDoc.getContent().show()
youtubeSubtitle.getContent().show()
twoDGame.getContent().show()
subTile.getContent().show()

Implementation classes of the users who consumes i.e. prepare or use these tools

class GeneralReviewer : ConsumeInput<WordProcessor> {
    override fun consumeContent(data: WordProcessor) {
        println("GeneralReviewer -- Review the content")
        data.showConsumer()
    }
}

class Reporter : ConsumeInput<Notes> {
    override fun consumeContent(data: Notes) {
        println("Reporter - Taken Rough Notes")
        data.showConsumer()
    }
}

class ContentDesigner : ConsumeInput<FormattedNote> {
    override fun consumeContent(data: FormattedNote) {
        println("Designer - Formatted the content")
        data.showConsumer()
    }
}

class GraphicDesigner : ConsumeInput<DigitalContent> {
    override fun consumeContent(data: DigitalContent) {
        println("Graphic Designer -- Added Animation Effects to the content")
        data.showConsumer()
    }
}

class SubtitleWriter : ConsumeInput<Transcript> {
    override fun consumeContent(data: Transcript) {
        println("Subtitle Writer -- Updated subtitle content")
        data.showConsumer()
    }
}

For ‘in' generic, we could assign a class of super-type to class of subtype, Please check out the comments in the code.

/*in -- This supertype class will be assigned only to the same type or subtype class*/

val takeNotes: ConsumeInput<Notes> = Reporter()
val pageMaker: ConsumeInput<FormattedNote> = ContentDesigner()
val adFilm: ConsumeInput<DigitalContent> = GraphicDesigner()
val generalContent: ConsumeInput<WordProcessor> = GeneralReviewer()
val subTag: ConsumeInput<Transcript> = SubtitleWriter()

// Eg: in compiler error
// val takeNotes2: ConsumeInput<WordProcessor> = Reporter() // Error
// val pageMaker2: ConsumeInput<WordProcessor> = ContentDesigner() // Error
// val adFilm2: ConsumeInput<WordProcessor> = GraphicDesigner() // Error
// val tagline: ConsumeInput<WordProcessor> = SubtitleWriter() // Error
val generalContent2: ConsumeInput<WordProcessor> = GeneralReviewer() // No Error Same Type
val subTag2: ConsumeInput<Transcript> = GeneralReviewer() // No Error -- supertype assigned to subtype

takeNotes.consumeContent(Notes("Hello, Welcome to meeting"))
pageMaker.consumeContent(FormattedNote("Thank you all"))
adFilm.consumeContent(DigitalContent("BGM Rocks"))
generalContent.consumeContent(WordProcessor("Great to review"))
generalContent2.consumeContent(WordProcessor("GeneralContent2: Great to review"))
subTag.consumeContent(Transcript("Display at the bottom"))
subTag2.consumeContent(Transcript("Subtag 2: Display at the bottom"))

Very simple way to remember In and Out to be used on the implementation.  Also, importantly if anything we do mistakes, it will stop us at the compile time itself.  

Use IN => Super Type could be assigned subtype
Use OUT => Sub Type could be assigned to Super Type


Saturday, 5 March 2022

Value Class Kotlin

Value class adds attribute to a value and constraint it’s usage. This class is nothing but a wrapper around a value, but the Kotlin compiler makes sure there is no overhead due to wrapping.



Classes in Kotlin solve two problems:
  1. They convey meaning through their name and make it easier for us to understand what kind of object is passed along.
  2. They enforce type-safety by making sure that an object of class A cannot be passed to a function that expects an object of class B as an input parameter. This prevents serious bugs at compile-time.

Primitive: ❌
Primitive types like Int, Boolean, String or Double also enforce type-safety (you can’t just pass a String where a Boolean is expected), but they don’t really convey a meaning (other than an object being a number of a certain format, for example).

A Double could be pretty much anything: a temperature in degrees Celsius, a weight in kilograms, or your screen’s brightness level in percent. All we know is that we’re dealing with a floating-point number with double precision (64 bits), but it doesn’t tell us what this number represents. For that reason, the semantic type-safety is lost.

Assume below example, there is a function we are about to pass two String param (username and password).

fun getAuthToken(username:String, password:String) {
....
}

see to the above function we are able to pass the param like this and which is wrong doesn't care at the compiler level. (even we have named params both accepts the String), Hence here primitives fails.

val uname = "juno ravi"
val pwd = "P@ssw0rd!123"

val token = getAuthToken(pwd, uname) 

This programming error cannot be detected at compile-time and will most likely lead to unexpected behavior. 💥

There are different approaches to solving the two problems mentioned above. We could just wrap a primitive type in a class, but that comes with a lot of overhead. So let’s see how we can tackle these problems above with

  • data classes, 😈
  • type aliases,  😜
  • and value classes 😎

Data classes: Instantiating data classes is expensive. Primitive values can be written to the stack which is fast and efficient. Instances of data classes are written to the heap, which takes more time and memory.

data class Username(val username: String)
data class Password(val password: String)

fun getAuthToken(username:Username, password:Password) {
....
}

with the above implementation is able to achieve but it is too costly process, comparatively if primitive took 2.x milliseconds means, it will take 8.x milliseconds for the single process.  Hence it is not advisable to do 😈

Type aliases: From the below Example, String & Username and Password are synonyms.
Whenever the compiler sees Username/Password, it basically replaces it with String and moves on.

typealias Username = String
typealias Password = String

Since our new Username/Password type alias is now equal to String, hence it works as same as primitvies which we have seen above, it also receives the same optimization treatment as Primitive but loses the typesafety. 😜

Value classes: It looks pretty similar to data classes. The signature looks exactly the same, except that instead of data class the keyword is value class, also there should be @JvmInline annotation to that class due to the Valhalla JVM release in future (currently JVM supports value class on the built in primitive types).  Important thing is the value class accepts only one constructor param.  

Since this value class acts as a wrapper to that variable, it also receives the same optimization treatment as Primitive which we can see it on the below screenshot on the highlighted section of the Kotlin byte code where as uname is referred it as Username class type, but pwd is referred as String value type, because it is defined with a value class 😎


Side Note: 

Since Kotlin 1.2.xx, we have inline classes, the old name for the value class. Since the class is not actually inlined in comparison to the inline function, it has been renamed to value class and the inline keyword is deprecated from Kotlin 1.5

Happy Coding :-)

Saturday, 26 February 2022

String concatenation using listOfNotNull & joinToString - Kotlin

data class User(val fName: String?, val mName: String?, val lName: String?)
val users = listOf(
        User("Ragavan", null, "V.S."),
        User("Rajeev", "Anbu", "Devan"),
        User("Ravinder", null, null),
        User("", "", ""),
        User(null, null, null)
    )
users.map {
        listOfNotNull(it.fName, it.mName, it.lName)
        .joinToString(" ").trim().takeIf { it.isNotEmpty() } ?: "NA"
}.forEach(::println)

Output:

[Ragavan V.S., Rajeev Anbu Devan, Ravinder, NA, NA]

In the above example here we are using the listOfNotNull method which implicitly handles the null check and ignore the same, then we are easy to concatenate the data using joinToString(...) method. 

Sunday, 16 January 2022

Android FAB Action to appear across the Application

Recently we got a requirement to an existing product and which have some legacy implementation too. Here the requirement to have a FAB (Chat Bot) action to be available on all the application screens (Activity).

Discussion where went like below:

1. Adding FAB to all the activity is a very tedious process, even creating a common layout and add using <include> in all layout also not that simple task. 😟 (Bad Practice too)

2. Will create a simple FAB Action class and planned to initialize it in all the activity class, since it is also a required some more efforts to update the initialization code in all the activity classes. (This implementation will reduces the effort but not fully, It helps to update on our Common BaseActivity and few other independent Activities by doing the initialization)  😌   

3. Finally Achieved!..  Implemented through Application class and ActivityLifecycleCallbacks, which made the implementation easier to work seamlessly across the application. 😎

FabButton

object FabButton {
    fun init(activity: Activity, savedInstanceState: Bundle?=null) {
        val root: ViewGroup = activity.window.decorView as ViewGroup
        val fab = FloatingActionButton(activity).apply {
            id = View.generateViewId()
            setImageResource(android.R.drawable.sym_action_chat)
            elevation = 2f
            imageTintList = ColorStateList.valueOf(Color.WHITE)
            backgroundTintList = ColorStateList.valueOf(Color.RED)
            size = FloatingActionButton.SIZE_NORMAL
            isFocusable = true
            layoutParams = FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT
            ).apply {
                gravity = Gravity.BOTTOM or Gravity.END
                setMargins(24, 24, 24, 24)
            }
        }
        fab.setOnClickListener {
            Toast.makeText(activity, "Yes, May I help you?", Toast.LENGTH_SHORT).show()
        }
        root.addView(fab)
    }
}

Activity

class FirstActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second2)
        FabButton.init(this)  // See here the fab initialization
    }
}

Later as per the #3, we found an other option instead of initializing the FabButton.init(this) to all the activities, implementing the below things resolved the issue without doing a larger code change.  

class TrailsApplication : Application(), Application.ActivityLifecycleCallbacks {
    override fun onCreate() {
        super.onCreate()
        registerActivityLifecycleCallbacks(this)
    }
    override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
        FabButton.init(activity, savedInstanceState)
    }
// Override other class ... 
}

P.S:  The application class has to override all the interface method of the ActivityLifecycleCallbacks, so as per the standard it would be good to handle by creating a separate handler class and register the same on application class.   For the simple and understanding purpose, I have override all the methods of lifecycle callbacks in application class itself. 

Happy Coding :-) 

  

Sunday, 19 December 2021

Passing function as a param to collection filter

It's actually simpler to provide a lambda directly, There's other option that's worth covering, too, which is a function / lambda reference.  If you already had a function that would do the job, you don't need to put it in a lambda, but can refer to it directly, through function or its reference using the :: notation. this will works only if the parameter type(s) are compatible, but it's slightly simpler and can generate slightly more efficient bytecode.


val myValues = listOf(12, 25, 215, 3, 52)
println("All Values: $myValues")
println("Even Filter ${myValues.filter { it % 2 == 0 }} -- Simple implicit lambda")
println("Odd Filter ${myValues.filter { oddFilter(it) }} -- lambda reference as param")
println("odd Filter ${myValues.filter(oddFilter)} -- lambda as param to filter function")
println("Even Filter ${myValues.filter(::evenFilter)} -- function reference as param")

val oddFilter = { a: Int -> a % 2 != 0 }

fun evenFilter(g: Int): Boolean = g % 2 == 0

Output:

All Values: [12, 25, 215, 3, 52]
Even Filter [12, 52] -- Simple implicit lambda
Odd Filter [25, 215, 3] -- lambda reference as param
odd Filter [25, 215, 3] -- lambda as param to filter function
Even Filter [12, 52] -- function reference as param

Happy Coding :-)

Saturday, 13 November 2021

Vetoable - Delegates -- Kotlin

Vetoable, this allows us to modify the values when the argument input by the user(s) fulfills the specified condition, it can be used in place of observable properties if the user wants to intercept assignment. Vetoable is like Observable Properties with additional features to allows to modify and notify the values when the condition is met.

Simple example: 

The string which you never want to be reassigned with empty or to some specific text. 

The number values which is assigned should be match some condition.  

The generic observable kind of where the condition check will happen and then the value will be get assigned to the variable, incase if the condition fails, older value will be retained. 

var acceptOnlyEven: Int by Delegates.vetoable(0) { property, prevStr, nextStr ->
        nextStr.mod(2) == 0
}

var allowNonEmptyText: String by Delegates.vetoable("NA") { property, oldValue, newValue ->
        newValue.isNotEmpty()
}

var acceptStatus: String by Delegates.vetoable(STATUS_START) { property, oldValue, newValue ->
        listOf(STATUS_START, STATUS_INPROGRESS, STATUS_DONE).contains(newValue)
}

println("Str: $acceptStatus")
acceptStatus = "Status"
println("Str: $acceptStatus")
acceptStatus = ""
println("Str: $acceptStatus")
acceptStatus = "DonE"
println("Str: $acceptStatus")
acceptStatus = "Done"
println("Str: $acceptStatus")

println("Value: $acceptOnlyEven")
acceptOnlyEven = 10
println("Value: $acceptOnlyEven")
acceptOnlyEven = 5
println("Value: $acceptOnlyEven")

println("Str: $allowNonEmptyText")
allowNonEmptyText = "Status"
println("Str: $allowNonEmptyText")
allowNonEmptyText = ""
println("Str: $allowNonEmptyText")

Output:

Str: Start
Str: Start
Str: Start
Str: Start
Str: Done
Value: 0
Value: 10
Value: 10
Str: NA
Str: Status
Str: Status

Good Luck, Happy Coding :-)

Sunday, 17 October 2021

Kotlin getOrElse & getOrPut

 getOrElse: 

This provides safe access to elements of a collection. It takes an index and a function that provides the default value in cases when the index is out of bound.

getOrPut: 

This returns the value of the key.  If the key is not found in the map, calls the default value function, puts its result into the map under the given key and returns value of the same.

Code Snippet:

    data class Header(val heading: String, val subHeading: String)    

    val items = listOf("AAA", "BBB", "CCC", "DDD")
    val itemMap = mutableMapOf("x" to "AAA", "y" to "BBB")

    val header = listOf(
        Header("Heading1", "SubHeading1"), Header("Heading2", "SubHeading2")
    )

    println("Item at index 2 ${items.getOrElse(2) { "NA" }}")
    println("Item at index 4 ${items.getOrElse(4) { "NA" }}")
    println("Header at index 1 ${header.getOrElse(1) { "NA" }}")
    println("Header at index 2 ${header.getOrElse(2) { "NA" }}")
    
    println("Map Data: $itemMap")
    println("Map Key x ${itemMap.getOrElse("x") { "NA" }}")
    println("Map Key z ${itemMap.getOrElse("z") { "NA" }}")
    
    println("Map Key x ${itemMap.getOrPut("x") { "XXX" }}")
    println("Map Key z ${itemMap.getOrPut("z") { "ZZZ" }}")

    // Remove Map Key "x"
    itemMap.remove("x")

    println("Refreshed Map Data: $itemMap")
    println("Map Key x ${itemMap.getOrPut("x") { "AAA" }}")
    println("Refreshed Map Data: $itemMap")

Output:

Item at index 2 CCC
Item at index 4 NA
Header at index 1 Header(heading=Heading2, subHeading=SubHeading2)
Header at index 2 NA
Map Data: {x=AAA, y=BBB}
Map Key x AAA
Map Key z NA
Map Key x AAA
Map Key z ZZZ
Refreshed Map Data: {y=BBB, z=ZZZ}
Map Key x AAA
Refreshed Map Data: {y=BBB, z=ZZZ, x=AAA}

Happy Coding :-)

Sunday, 26 September 2021

groupBy vs associateBy in Kotlin

associateBy and groupBy function helps to build the maps from the elements of a collection indexed by the specified key. 

keySelector: The key is defined in the keySelector parameter. 

valueSelector: We can also specify an optional valueSelector to define what will be stored in the value of the map element, in case of not specifying the same it will consider the complete object as a value. 

The difference between associateBy and groupBy is about how they process the objects with the respective key

associateBy - It uses the last suitable element as the result value.

groupBy - it produces the list of all suitable elements and puts it in the result value(s).

P.S. The returned map preserves the entry iteration order of the original collection.

val boardingList = listOf(
        OnBoarding("1", "AAA", "Chennai", "Bang", "IT"),
        OnBoarding("2", "BBB", "Bang", "Hyd", "IT"),
        OnBoarding("3", "CCC", "Bang", "Chennai", "Finance"),
        OnBoarding("4", "DDD", "Hyd", "Pune", "Finance"),
        OnBoarding("5", "DDD", "Chennai", "Bang", "IT")
    )

// Param either be 'it' or field references, both does the same job.
println(boardingList.groupBy({ it.baseLocation }, { it.eName })) 
println(boardingList.groupBy(OnBoarding::baseLocation, OnBoarding::eName))

println(boardingList.associateBy({ it.baseLocation }, { it.eName }))
println(boardingList.associateBy(OnBoarding::baseLocation,OnBoarding::eName))

Output:

{Chennai=[AAA, DDD], Bang=[BBB, CCC], Hyd=[DDD]}
{Chennai=[AAA, DDD], Bang=[BBB, CCC], Hyd=[DDD]}
{Chennai=DDD, Bang=CCC, Hyd=DDD}
{Chennai=DDD, Bang=CCC, Hyd=DDD}

Good Luck, Happy Coding :-)

Sunday, 12 September 2021

DSL Code References

We should use DSL to simplify and improve the complexity of the app and make the code more readable, Before jumping into writing our own DSL we need to aware about the concept of lambda with receiver, invoke and operator.

Assume an example we have a class called Device, Please see below different ways how the object got initialized

class Device() {
    var id: Int = 0
    var name: String = ""
    var make: String = ""

    fun printDeviceInfo() {
        println("ID:  $id | Name: $name | Make: $make")
    }

    operator fun invoke(device: Device.() -> Unit): Device {
        device()
        return this
    }
}

Now, to instantiate the Class we can create an object like below ways,

// Traditional Way
    val env = Device()
    env.id = 102
    env.name = "5.4 GHz Wifi"
    env.make = "Amazon-Cable"
    env.printDeviceInfo()

// Invoke Way
    val en = Device()
    val obj = en {
        id = 102
        name = "5.4 GHz Wifi"
        make = "Amazon-Cable"
    }
    obj.printDeviceInfo()
    en.printDeviceInfo()

There is also other way where we are able to do without using invoke operator function, but here we can't use the traditional way of object creation. Since we pass it as parameter to the class primary constructor and followed by init method should call the same.

class Device(device: Device.() -> Unit) {
    var id: Int = 0
    var name: String = ""
    var make: String = ""

    fun printDeviceInfo() {
        println("ID:  $id | Name: $name | Make: $make")
    }

    init {
        device()
    }
}

val device = Device {
        id = 102
        name = "5.4 GHz Wifi"
        make = "Amazon-Cable"
    }
    device.printDeviceInfo()

Here we can also use it on the Data class also, but we should declare the constructor param as val/var, whereas it is not required on the normal model class like above.

data class User(val obj: User.() -> Unit) {
    var id: String = "NA"
    var age: Long = 0

    init {
        obj()
    }
}

fun main() {
    val userObj = User {}
    printUser(userObj)
    val usersList = listOf(userObj, User {
        id = "ABC567"
        age = 28
    }, User {
        id = "BCG678"
        age = 24
    }, User {
    })

    usersList[0].id = "BGH905"
    usersList.forEach(::printUser)
}

fun printUser(user: User) {
    println("Id: ${user.id}|| Age: ${user.age.takeIf { it > 0 } ?: "NA"}")
}

Good Luck :-) Happy Coding !!!

Sunday, 22 August 2021

Deduplicating Collection Items

Deduplication refers to a method of eliminating a dataset's redundant data, here in Kotlin we normally use the toSet() function to get the unique datasets, now the distinct() and distinctBy{} joins the club. Please refer the below code snippet for more details, the below example uses both primitive typed object and data class object (Be careful on the data order, this will give an impact).

data class Tez(val id: String)

val users = listOf("AAA", "aaa", "BBB", "bbb", "CCC", "ccc", "AAA", "aaa", "BBB", "bbb", "CCC", "ccc")
    val tezUsers = listOf(
        Tez("AAA"),
        Tez("aaa"),
        Tez("BBB"),
        Tez("bbb"),
        Tez("CCC"),
        Tez("ccc"),
        Tez("AAA"),
        Tez("aaa"),
        Tez("BBB"),
        Tez("bbb"),
        Tez("CCC"),
        Tez("ccc")
    )
    println("toSet: ${users.toSet()}")
    println("Data Object toSet: ${tezUsers.toSet()}")
    println("distinct : ${users.distinct()}")
    println("Data object distinct: ${tezUsers.distinct()}")
    println("distinctBy: ${users.distinctBy { it.lowercase() }}")
    println("Data object distinctBy: ${tezUsers.distinctBy { it.id.lowercase() }}")

    val revUsers = users.reversed()
    val revTezUsers = tezUsers.reversed()
    println("\n\nreversed toSet: ${revUsers.toSet()}")
    println("reversed Data object toSet: ${revTezUsers.toSet()}")
    println("reversed Distinct : ${revUsers.distinct()}")
    println("reversed Data object distinct: ${revTezUsers.distinct()}")
    println("reversed object distinctBy: ${revUsers.distinctBy { it.lowercase() }}")
    println("reversed Data object distinctBy: ${revTezUsers.distinctBy { it.id.lowercase() }}")

Output:

toSet: [AAA, aaa, BBB, bbb, CCC, ccc]
Data Object toSet: [Tez(id=AAA), Tez(id=aaa), Tez(id=BBB), Tez(id=bbb), Tez(id=CCC), Tez(id=ccc)]
distinct : [AAA, aaa, BBB, bbb, CCC, ccc]
Data object distinct: [Tez(id=AAA), Tez(id=aaa), Tez(id=BBB), Tez(id=bbb), Tez(id=CCC), Tez(id=ccc)]
distinctBy: [AAA, BBB, CCC]
Data object distinctBy: [Tez(id=AAA), Tez(id=BBB), Tez(id=CCC)]


reversed toSet: [ccc, CCC, bbb, BBB, aaa, AAA]
reversed Data object toSet: [Tez(id=ccc), Tez(id=CCC), Tez(id=bbb), Tez(id=BBB), Tez(id=aaa), Tez(id=AAA)]
reversed Distinct : [ccc, CCC, bbb, BBB, aaa, AAA]
reversed Data object distinct: [Tez(id=ccc), Tez(id=CCC), Tez(id=bbb), Tez(id=BBB), Tez(id=aaa), Tez(id=AAA)]
reversed object distinctBy: [ccc, bbb, aaa]
reversed Data object distinctBy: [Tez(id=ccc), Tez(id=bbb), Tez(id=aaa)]

Please refer the above highlighted output, produces the different output due to dataset order.  Also, in the lambda it.lowercase() that won't change the dataset value, it just use for the comparison purpose only.  

Happy Coding :-)