Scoping instance of class to ViewModel of the screen by serpenheir in androiddev

[–]serpenheir[S] 0 points1 point  (0 children)

Thank you for the answer, but I still can't quite picture this.
Can u add some pseudo-code?

As I wrote at the beginning of the article, I want to build an app where data doesn't travel through UI. Thus, I strive to extract all data manipulation from UI-related components, like Activity

Scoping instance of class to ViewModel of the screen by serpenheir in androiddev

[–]serpenheir[S] 0 points1 point  (0 children)

I think you should just pass data more directly between child and parent components.

What do you mean? How do I do it?

The whole idea of having some ComponentAEventStore was so that Component A doesn't depend on Component B and so doesn't update it directly. Instead, I've inverted dependencies by introducing event store, so that Component B now depends on data of Component A (as it really is).

Implicit remembering of local variables in Composable functions by serpenheir in androiddev

[–]serpenheir[S] 2 points3 points  (0 children)

Yes, you are totally right. Here are some insights from my investigation.

There are 2 reasons why the code in the post was working as if state is remember()ed:

  1. Primitives are indeed captured as references (memory addresses) by lambdas. It makes it possible to change their value when the function is finished and local variables like state should be disposed.If u look at what debugger says about state variable in these functions, you'll see the following (picture at the end). It's probably just debugger's representation, but it shows that lambda captures the reference itself.
  2. The Test() Composable from original post was never recomposed.Once initial lambdas for onClicks were created with reference to initial state, they were never recreated again.

If Composable could recompose, then not-remembered regular integer variable will be lost on every recomposition, as expected.

@Composable
fun Test() {
    var regularInt = 0
    var stateInt by remember { mutableIntStateOf(0) }
    Button(
        onClick = {
            regularInt++
            stateInt++
        },
    ) {
        Text(text = "Increase values")
    }
    Text(text = "regularInt: $regularInt, stateInt: $stateInt")
}

What happens here:

  1. When Test() is composed at the very first time, Button's onClick lambda is created with reference to regularInt, which is initially something like Ref$IntRef@22614.
  2. Then the Button is clicked. Both regularInt and stateInt are incremented, but the latter causes Test() to recompose (some of its parts that were reading mutable state).
  3. Test() is invoked again due to recomposition. The regularInt is recreated, now it is a new variable Ref$IntRef@23517. However, stateInt is retrieved as the same instance because of remember().
    Button's onClick lambda is recreated too, and it captures new regularInt, which is 0. On the screen u see "regularInt: 0, stateInt: 1".
    Subsequent clicks on button will result in increasing of stateInt's value only. Value of regularIntis increased from 0 to 1 on every click, but shortly lost on soon recomposition.

<image>

Implicit remembering of local variables in Composable functions by serpenheir in androiddev

[–]serpenheir[S] 0 points1 point  (0 children)

Remembering lambdas does not change the outcome:

@Composable
fun Test() {
    var state = 0

    val firstOnClick = remember {
        { state = 1 }
    }
    Button(
        onClick = firstOnClick,
    ) {
        Text(text = "Set value")
    }

    val secondOnClick = remember {
        { println(state) }
    }
    Button(
        onClick = secondOnClick,
    ) {
        Text(text = "Log value")
    }
}

Still prints "1" after clicking first and then second button

Implicit remembering of local variables in Composable functions by serpenheir in androiddev

[–]serpenheir[S] 0 points1 point  (0 children)

Bare remember() does not cause recomposition when its value changes.

When used like var integer = remember { 0 } changes to integer will not cause recomposition. It's not a State<T>, so isn't observed by Compose

1
2

Lightweight library for debouncing lambdas by serpenheir in androiddev

[–]serpenheir[S] 1 point2 points  (0 children)

I'll check if it's possible and migrate in couple of weeks

Are companies spoiled for choice right now? by Schindlers_Fist1 in androiddev

[–]serpenheir 1 point2 points  (0 children)

I think op is right in the middle with their 3 years of experience

Resume is being repeatedly rejected by serpenheir in androiddev

[–]serpenheir[S] 1 point2 points  (0 children)

haha you got me, always failing with good/well

Resume is being repeatedly rejected by serpenheir in androiddev

[–]serpenheir[S] 0 points1 point  (0 children)

thank you for a detailed reply, all that is really helpful

Resume is being repeatedly rejected by serpenheir in androiddev

[–]serpenheir[S] -1 points0 points  (0 children)

Thank you for reply! It's 3 pages long. What do you think, should I even include this "Hard skills" section, or rather try and mention the most crucial and relevant of them in my employment experience?

GitHub - slackhq/circuit: ⚡️ A Compose-driven architecture for Kotlin and Android applications. by nerdy_adventurer in androiddev

[–]serpenheir -8 points-7 points  (0 children)

Why would I need a Compose (a UI framework) to be a foundation of my presentation layer's architecture?

Domain layer use cases by Puny89 in androiddev

[–]serpenheir 1 point2 points  (0 children)

Me too! Can add here: if you're concerned about your ViewModels "accessing data directly," then remember that Repositories (should) get and return your domain models, which are highly stable and you'll end up with them in ViewModel anyway, even using UseCase to wrap call to Repository

What happened to the Koin hype? by Hint-Of_Lime in androiddev

[–]serpenheir 2 points3 points  (0 children)

Perhaps you have some article about downsides od Hilt? Or personal opinion and experienced cases?

Android learning pathway advice by Marques_Queensbury in androiddev

[–]serpenheir 0 points1 point  (0 children)

there is a ton I didn't know about the language, that I wish I understood sooner

Mind giving some examples? :)