Hello everyone. I am working on migrating from Koi...
# koin
a
Hello everyone. I am working on migrating from Koin 2.x to 3.x Currently, on the project, there is a Fragment abstraction that handles view-binding and viewmodels. But with all the 3.x changes, it broke this BaseFragment and I am having a hard time trying to fix it, especially about shared view models across different fragments. I am attaching a snippet showing the current
BaseFragment
implementation. Let me know if you have any ideas on how to work around these issues, I would really appreciate it
Screenshot 2023-05-16 at 15.22.31.png
a
Hello, seems something custom on your side no?
did you check the current Fragment extensions for virwModel?
a
no, nothing custom. In the screenshot, I am showing the old Koin implementation (2.x), where the
getViewModel()
had the option to receive a
class: Class<T>
as a parameter. but in 3.x, those are
reified
functions, they no longer receive a
class: Class<T>
as parameter.
a
yes exact, by viewModel() or activityViewModel() are directly refiefied
a
yes, I noticed. how can I work around that?
maybe I will have to change something on my
BaseFragment
declaration?
a
yep, what do want to do in your basefragment? a generic call to viewModel() functions?
a
yes, create an instance of my
ViewModel: K
a
Ok, I see your need of KClass then 👌
a
perfect, what do I do with it?
should I provide it as a
qualifier
parameter?
a
nope, you need to get access to API that offers KClass usage for Fragment
image.png
viewModelForClass(<KClass>)
a
perrrrrfect, thats exactly what I needed
and what about sharedViewModel? this one is deprecated in favor or
Fragment.getActivityViewModel
can I use the same
viewModelForClass
and just change the
owner
parameter?
a
use the same, but the owner is the parent activity, change
owner
from
{ this }
to
{ requireActivity() }
a
yesss! thank you so so much @arnaud.giuliani! you are the man
just tested and seems like everything is working perfectly!
a
great 👍