Hello guys. We are new to AAC. We are wondering ho...
# android
d
Hello guys. We are new to AAC. We are wondering how we should or where we should keep the state of our Auth. At first glance we thought of having a central AuthViewModel, but we have few activities and the view model couldn't be share as far as I know. Now we are thinking on keeping the state in the AuthRepository. Could anyone share experiences on it? Regards!
a
You can share viewmodels across activities if you provide your own ViewModelStore. Before doing that though, consider the Android source of UI truths below in terms of increasing fragility: Application, Service, Activity, Fragment, Views Therefore at any level, you can provide your own custom
ViewModelStore
, but as to where will it be scoped, it’ll be likely either at Application level (e.g singletons) or Service level if you can’t use activities. Which for simplicity and maintenance sake, you might just as well use a singleton for that.
d
thanks @Amirul Zin, looking into it
i
AuthRepository
is the way to go. Either inject it directly into
ViewMode
(or add another layer and call
UseCase
that access the
Repository
)
d
So keeping the state int the Repository? isn't that opposed to AAC idea of keeping state in VM?
i
General rule I follow is to retrieve state from ViewModel (ideally common-state from LiveData last emitted item) if it exists, otherwise retrieve state from the repository (either network or local cache). Take a look at https://github.com/igorwojda/Android-Showcase
d
@igor.wojda thanks mate, cool project, already got some ideas from it 🙂 . Another question you might know somehow, is it ok to have view models per custom views? so far we wanted but it seems not the kind of things we should do. Kind regards!
i
Thx 🙂 Never had need for view VM, but AFAK they are designed to work only pre Activity or Fragments
👍 1