https://kotlinlang.org logo
#android-architecture
Title
# android-architecture
m

Mohammed Akram Hussain

12/11/2023, 6:16 PM
In terms of the architecture what is the best way to maintain global application state without going offline first approach? For example, In the application start up I need to make an API call and the response from that API should be available App wide (Ui and view models)
s

Shahzad Ansari

12/11/2023, 6:53 PM
Just saving it at repository (singleton) level might help you. Or you could have a shared ViewModel but that might be more work.
s

Stylianos Gakis

12/11/2023, 9:31 PM
If you're on a single activity compose-only app, you can save some of that state in something like NiaAppState does here https://github.com/android/nowinandroid/blob/main/app%2Fsrc%2Fmain%2Fkotlin%2Fcom%2Fgoogle%2Fsamples%2Fapps%2Fnowinandroid%2Fui%2FNiaAppState.kt We do that for some global state, and you can initialize it by running some network request. Just gotta define a default before that comes back or if it fails though of course.
m

Mohammed Akram Hussain

12/11/2023, 11:17 PM
We do have single activity compose-only app. The above solution is good if we wanna have state in composables but I want a way where we can just inject the response object in a viewmodel, repository or any class. Something like how we do in DI.
s

Swapnil Musale

12/12/2023, 6:01 AM
In that case go with object (singleton) class which will be available till app lifecycle and you can inject this class anywhere you required.
👍 4
j

Joel Denke

12/17/2023, 12:23 PM
I would most probably using WorkManager if not super critical have to run at certain point. And using a manager / repository cache/store value. Later on, observe changes in ui level like viewmodel get result from storage.
d

dewildte

12/28/2023, 9:04 PM
What's wrong with placing it in the Application class itself?
4 Views