9

I'm using MVVM clean architecture and need to get current position using fused location provider, so before getting the current position i need to check if user has given permission to access the location and the internet.
I'm retrieving the location from repository by implementing a data source, but i'm not sure where should i ask for permission. In repository when i request the location or in activity, first ask for permission and then request the position?

In first case the flow would be:
1)User clicks the button(view)
2)Viewmodel requests for location (viewmodel->usecase->repository)
3)Repository checks permissions and requests location
4)Observable is returned which emits location(s) or error if no permission were granted (or other error)

In second case:
1)User clicks the button and permission are checked/requested inside activity
2)If permission are granted, viewmodel requests position otherwise view shows an error
3)Repository requests and returns the location

The first method looks more straightforward and logical to me. The presentation layer simply requests the position and then the repository returns the location or error, however maybe it's too much logic inside data layer ?

pdcmb
  • 131
  • 1
  • 5

2 Answers2

4

Everything according user interaction should not be done inside repository.

ViewModel is about handling user actions and putting all together: UI and Data. In other words, repository returns data according some request parameters, ViewModel decides how to react on which action.

Use second case here.

enter image description here

Maksym V.
  • 2,877
  • 17
  • 27
3

As the ViewModel and Repository should not know anything about the Fragment or Activity, I would say the Permission has to be checked in the Activity or Fragment..

Scamparelli
  • 756
  • 1
  • 12
  • 28