150: Learning Kotlin – Returns, Jumps & Labels

Shownotes

Download

Code

data class Customer(val isPlatinum: Boolean)

fun main() {

    val customer = Customer(false)

    println("Number of points customer has: ${calculatePoints(customer)}")

    // Break out of the loop once we're over 25
    for (i in 1..100) {
        if (i > 25) {
            break
        } else {
            println(i)
        }
    }


    // Skip all even numbers
    for (i in 1..100) {
        if (i % 2 == 0) {
            continue
        } else {
            println(i)
        }
    }


    // Break out of the outer loop (which breaks out of the inner too) using a label
    donn@ for (i in 1..100) {
        for (j in 100..200) {
            if (j > 150) break@donn // This will break out of the inner loop and outer loop
            else println("i: $i, j: $j")
        }
    }

    // Continue processing the next outer loop value when a condition is met.
    donn@ for (i in 1..100) {
        for (j in 100..200) {
            if (j > 150) continue@donn // This will break out of the inner loop and outer loop
            else println("i: $i, j: $j")
        }
    }

    // returns with label
    example1()
    example2()

    println("I'm done processing!")
}

fun calculatePoints(customer: Customer): Int {
    if (customer.isPlatinum) {
        return 100000
    } else {
        return 10
    }
}


fun example1() {
    listOf(1, 2, 3, 4, 5, 6, 7, 8, 9).forEach {
        if (it > 7) {
            return
        } else {
            println(it)
        }
    }
    println("This wont print :( because return exited the bar() function")
}

fun example2() {
    listOf(1, 2, 3, 4, 5, 6, 7, 8, 9).forEach bin@ {
        if (it > 7) {
            return@bin
        } else {
            println(it)
        }
    }
    println("This will print! :) return exited the forEach!")
}

Sponsors 🙏

  • Nevercode

    • Nevercode is taking Flutter revolution extremely serious and is prepared to offer kick-ass CI/CD for Flutter projects with codemagic.io. Check it out and get started at https://codemagic.io/
  • Sentry.io

    • Sentry tells you about errors in your code before your customers have a chance to encounter them. Check them out at: https://sentry.io/for/android/

Contact

149: Learning Kotlin: inline classes and Type driven design

In this quick fragment, Kaushik talks about the new Kotlin 1.3 experimental feature “inline classes” and how it helps with Type driven design.

Download

Shownotes

Type driven design resources

Kotlin KEEP

vs typealias

Sponsors 🙏

Contact

148: Evolving Android architectures (Part 1)

In this episode, Donn and Kaushik talk about the state of android architectures and how they’ve stared to evolve. Kaushik recently worked on a project of coming up with an evolved version of an MVVM architecture, very similar to what the Android community now calls MVI.

Special request: if you have follow up questions or clarifications or things you’d like to see specifically with respect to this dicussion, hit us up on the Discord #fragmented-podcast channel with your thoughts and we’ll make sure to address them in future parts!

Download

Shownotes

Salient features of the pattern:

I. Screens are driven by a single ViewModel

II. Screens listen to a single Observable exposed from the ViewModel

III. Events are sent into the VM through a single function processInputs

IV. Break(ing) the Chain VI. Testing strategy

Inspiration

Sponsors 🙏

Contact

147: Disposing RxJava 2 Streams with AutoDispose

In this short fragment episode, Donn explains how you can clean up your RxJava 2 streams and ensure no memory leaks are occurring by using the AutoDispose library from Uber.

Download

Shownotes

Code Samples

Java

myObservable
    .map(...)
    .as(AutoDispose.<SomeType>autoDisposable(AndroidLifecycleScopeProvider.from(this)))
    .subscribe(...)

Kotlin

myObservable
    .map(...)
    .autoDisposable(AndroidLifcycleScopeProvider.from(this))
    .subscribe(...)

With Scope Event Provided

myObservable
    .map(...)
    .autoDisposable(AndroidLifcycleScopeProvider.from(this, Lifecycle.Event.ON_DESTROY))
    .subscribe(...)

Testing

// File: CustomerService.kt
class CustomerService @Inject constructor(...) {
    lateinit var scopeProvider: ScopeProvider
}

// Usage in Fragment/Activity/etc
val service = CustomerService(...).apply {
    scopeProvider = AndroidLifecycleScopeProvider.from(this)
}

// Usage in Test
val service = CustomerService(...).apply {
    scopeProvider = TestScopeProvider.create()
}

Contact

146: 3 Things Every Android Developer Needs to Know

In this episode of Fragmented, Donn digs into three things that every Android developer needs to know.

Download

  • Dependency Injection

    1. Constructor_setter_method injection
    2. Service Locators or other DI frameworks
    3. Common Frameworks
      1. Dagger
      2. Koin
      3. Kodein
      4. ToothPick
  • How to test

    1. Functional / System
    2. Integration
    3. Unit
    4. Tools:
      1. jUnit
      2. Espresso
      3. Spek
  • Keep it simple simple

    1. KISS principle KISS principle – Wikipedia
    2. Examples
      1. Code Duplication
        1. “Extract this into a method”
      2. Lets create a framework for this
      3. Kaushik’s – 3x rule
        • if something is duplicated 3 or more times, think about extracting it
      4. 3/6 Rule – In 6 months, will I be able to understand this in under 3 minutes?

Contact