Closed nullist0 closed 10 months ago
Thanks for your great proposal.
However, Lich Component is singleton-specific and we have no plans to support component scoping.
This way, it is easy to recognize at a glance that the code context.getComponent(FooComponent)
returns a singleton.
Our goal is not to provide a generic framework for component generation, but to provide a mechanism for handling singletons that are unit testable.
@yamasa Thanks for comments, and kind explanation about the goal of this project. I hope that this feature is checked if there are some plans to make more features!
Hello, I have an idea for lich/component. The following is the proposal for the idea.
Summary
This proposal is to suggest to add some new API for lich component. The new APIs will release the component object when given lifecycle takes
ON_DESTROY
event, and then it will not be used anymore.Proposal
Background
In now, the component created by lich is considered as singleton because
ComponentFactory<T>
is defined byobject
keyword. Also this implementation is guided inREADME.md
. But this will imply some problems.context.getComponent
has some confused definition. This function call looks that the created component will receive the givencontext
instance. But the component will receive theapplicationContext
.Thanksfully, there is a concept of
Lifecycle
andViewModelStore.clear()
in Android framework. I hope that the lifetime of the component is the same as the lifecycle of context.Sketched change area
I don't have the concrete implementation for this proposal. So this section is just a sketch to implement this proposal. After this proposal is approved, the concrete implementation should be followed.
The steps to implement this proposal is followings.
ComponentProvider
DefaultComponentProvider.getComponent
to observe lifecycle.Change of interface
To get the two types of owners, the
ComponentProvider
is needed to be changed.New ViewModel or LifecycleObserver for releasing component
New classes for releasing the component are needed.
LifecycleObserver
for releasing with the lifecycleViewModel
for releasing with the viewmodel lifecycleImplement
ComponentProvider.getComponent
ComponentProvider
should observe the givenlifecycle
using thecontext
.Add new APIs
From the
Components.kt
, the entry point of this new features should be provided.Affected impact
If an application is using Lich-component, there is no things to change immediately. The public APIs are not changed, so it is okay to use lich-component, if there is no need to optimize memory. But:
ViewModelProvider.Factory
to useCreationExtras.getComponent
to handle the lifecycle of components inViewModel
.context.getComponent
calls tocontext.applicationContext.getComponent
manually.Remained problems of above change
The remained problems of the above change are considered as followings.
context
object might be leaked. The developers should useapplicationContext
for multiple lifecycle-aware Android components.If you have an idea, please let me know.