Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Services and ViewModels in Android MVVM - How do they interact?

I've been using ViewModels from Android Architecture for some time now, and abide by never exposing the ViewModel to Context/Views (Android Framework/UI). However, recently I have run into an interesting problem.

When making a timer app, when a timer is started, a Service is run in the background running the timer. This way, when the application is closed, the timer still runs in the foreground in the notification bar until all timers have ceased. However, this means that all of my Timer objects and state are contained in this Service. My UI needs to be updated on each tick, but the Model doesn't necessarily need updated How do ViewModels fit in with this scenario?

Should the Activity receive LocalBroadcasts and notify the ViewModel every time? Should the UI state be read from Service->Activity->VM? It almost seems like the Service is the ViewModel, but this doesn't seem efficient.

like image 774
Josh Ribeiro Avatar asked Nov 01 '18 14:11

Josh Ribeiro


People also ask

Can we use ViewModel in service?

It is not recommended using a ViewModel in a service. You could call your repository from your service itself. The ViewModel should be used closely with an Activity or a Fragment, so it's destined to live in the UI layer of your application. Therefore, I don't recommend using the ViewModel in a Service.

How do you communicate between view and ViewModel?

What is a proper way to communicate between the ViewModel and the View , Google architecture components give use LiveData in which the view subscribes to the changes and update itself accordingly, but this communication not suitable for single events, for example show message, show progress, hide progress etc.

How does MVVM works in android?

In Android, MVC refers to the default pattern where an Activity acts as a controller and XML files are views. MVVM treats both Activity classes and XML files as views, and ViewModel classes are where you write your business logic. It completely separates an app's UI from its logic.

How does a ViewModel work internally?

ViewModel class is designed to store and manage UI-related data in a lifecycle conscious way.It is the main component in the MVVM architecture. ViewModel can be created with activity context or fragment context. When a ViewModel object is created, it is stored inside Activity OR FragmentManager.


Video Answer


1 Answers

After some toying with different structures, the Service has found it's place in MVVM. What was throwing me off in this situation was thinking a Service shouldn't be started from a ViewModel and the fact that two repositories were necessary: Room Database to store Timers and a Service to represent the state of ongoing timers (onTick, play/pause status, etc). A ViewModel should not have any reference to views, but application context is OK. So starting the Service from a ViewModel is doable by extending the AndroidViewModel class. Here is the final structure:

Model Layer

  • Service - Maintains a list of active timers, emits onTick() EventBus events, maintains active timer play/pause status. Ends itself once there are no active timers.
  • Room Database - Stores timers for future use (name, total time, etc.)

ViewModel

  • ViewModel - Listens for UI events, performs business logic, and emits EventBus posts. Any change in Model is communicated through the ViewModel

UI

  • Activity - performs application flow tasks. Listens for relevant ViewModel communications to swap fragments/start new activities, etc.
  • Fragment - handles animations and UI. Also notifies ViewModel of user interaction
like image 69
Josh Ribeiro Avatar answered Sep 28 '22 05:09

Josh Ribeiro