https://kotlinlang.org logo
c

Carlton Whitehead

02/11/2020, 3:22 PM
Is there a way, short of creating a new
Scope
, to obtain a new instance of a
Wizard
each time? I'm finding it a bit clunky and error-prone to build out the
Wizard
such that it expects a
Scope
with its dependencies graph prepared. Making the `Wizard`'s
scope
know about all the wizard pages dependencies, those pages dependencies' dependencies, and so on is what makes it feel clunky to me. Is there a way
find()
and
inject()
could act as a factory instead of as a singleton lookup? I feel like that would really simplify this. Is that possible?
The subtext of this is that I want a completely fresh wizard each time. Using
find<MyWizard>()
results in getting the same wizard each time, with old values left-over from prior use.
r

Romanow

02/12/2020, 12:08 AM
without just looking at the code, it seems you wan tthe wizard to behave as a fragment. One thing that is possible is to just remove the reference to the current wizard from the scope, but that may cause a memory leak. Something like FX.getComponents().remove(Wizard::class)
c

Carlton Whitehead

02/12/2020, 12:10 AM
Thats pretty much the issue... Wizard extends View. But it's not just the wizard, it's all the steps and their viewmodels, etc too. Removing the reference to the wizard would still leave all the steps, etc. Still kind of messy
I think a
injectAsFactory()
might be the answer. Inside the wizard could use that to make sure its dependences don't linger in the scope, without having to go to the trouble of setting up or tearing down a new scope.
I wonder if it would make more sense to instantiate it directly, if the point is to make the call site own the instance
I should have known better than to do that. Instantiating views directly misses out on all the extra bits the framework gives you when using its injection mechanics. So maybe a
factory()
method is warranted after all. I'm going to experiment with that offline some and will file a ticket if it seems to pan out.