Tracking Phone Screen States in Real Time
Libraries Used:
- Dagger2
- Recycler View
- RxJava2
- Room
Architectural Choices
- MVVM but I have removed the viewmodel because there was no business logic in the application apart from the polling
- The polling status has been shifted to the EventReceiver
- The EventListener is my contract between the receiver and the list which will dynamically update the view
- Instead of keeping an instance of the list in the fragment. I have created an EVENT model which will update every single event separately. (A major reason behind not using the viewmodel)
- The scoping for the Receiver has been set to @PerFragment just because only a single Activity is present in the entire app. Otherwise it would have been done on an Application Level.
- The dependency of SimpleDateFormat is not done because its instance requires a different parsing logic and producing a single instance throughout the app would defeat its purpose.
- The Service is sustained in Application scope
- The Tracking service runs in a separate process to prevent it from closing down on app exit
- The LiveData handling capabilities of the room database are used to update the list
- The list received in the adapter is reversed to maintain the reverse chronology.
- The listening capabilities have been shifted to the service from the fragment.
- A separate receiver has been added to listen to stop service command from the notification itself.
-
- The database instance is available application wide to allow data transaction integrity
Future Prospects
- Instead of updating the list perhaps a single event can be received from the db
- Separate fragments using Viewpager for all three events.
- Addition of date along with the time stamp.
- Alarm Manager can be used to persist the service in the background on App exit
Working
- The fragment sets the adapter which gets updated on receiving live data from the db
- The service gets started along with the fragment
- The receiver listens for the events and calls the Event Listener which updates the db from the service
- The event listener in turn updates the db and each event gets added as soon as it gets called.
- The adapter and the receiver work simultaneously in cohesion with the db.
- On first run of the Live Data observer the visibility of the list is changed.
- The list is reversed and added to the db (Assumption: The list will remain small and can easily be reversed)