Benjamin K
Benjamin K

Reputation: 316

Kotlin MVVM / clean architecture some questions

I'm trying to use clean architecture but even if I search a lot, it remain a lot of basics question. I understand it's probably a "it depends of your project / needs" but if there is some

  1. AndroidViewModel or hilt @ApplicationContext injection ?
  2. I use Interface/Implementation for repositories. Should i use it for viewmodels, datasources and usecases or is it overkill ? I'm used to do interface for repositories in my domain layer and implementation is the data layer, without interface for datasource (data layer), usecase (domain layer) and viewmodel (presentation layer). Am i right ?
  3. If I have a local source and a remote source (2 datasources), is it the repository or the usecase who have to handle the second source if the first one is down ?
  4. If I want to display the battery level in real time, who have the responsability of polling ? The repository (probably bad for battery if app goes in background) ? Usecases ? ViewModels ?
  5. Again, for battery level, show I use flow (cold, one by collectors) or stateflow (hot, common) ?
  6. In my case I have to display a lot of system information. I will use mainly the native's android package. Should I proxy them in a datasource ? In a repository implementation ? Or should i use it directly in my usecase, considering it's "basics" ?
  7. If I want to display the list of installed applications (in real time aswell). Is it a cold flow in the repository who emit package one by one ? A cold flow who emit a List<> ? An hot flow in repository ? Or my repository is just a method who give me the list of app, and the "polling" is triggered by a broadcast receiver in my usecase/viewmodel ?
  8. Imagine now I want to call a repository's method who take few seconds. What should I return in my repository's method (classic method, flow, stateflow) ? How handle the 3 commons states I want to display in my UI : Loading, Error, Success (I use sealed class). Should I transform flow in flow<State/Result> or T in State/Result.

I know it's a lot of questions and it's kind of generic / abstract, but I don't think each of them deserve a dedicated topic.

Thanks for your help !

Upvotes: 0

Views: 421

Answers (1)

Nazarii Moshenskyi
Nazarii Moshenskyi

Reputation: 2108

Lots of questions from different domains. I will answer everything from Clean Architecture.

I use Interface/Implementation for repositories. Should i use it for viewmodels, datasources and usecases or is it overkill ? I'm used to do interface for repositories in my domain layer and implementation is the data layer, without interface for datasource (data layer), usecase (domain layer) and viewmodel (presentation layer). Am i right ?

The reason why you use repository interfaces in the domain layer and implementation in the data layer is dependency inversion. You don't want the domain layer to depend on the concrete implementation of the repository in another layer, so you make a UseCase depend on the interface in the domain layer and the concrete repository depends on this interface. That's how you inverse.

Boundaries in Clean Architecture are those interfaces you are using. You need them to safely cross boundaries between "layers" (circles).

If you ask if you need to add additional interfaces inside one layer, my answer is - it depends. Depends on the purpose. You don't want to add an interface just to have a meaningless interface. Interfaces are used to abstract something, add common use for several classes, etc. If you see no need for it - you don't need it. Remember that overengineering is the most common mistake in Clean Architecture.

If I have a local source and a remote source (2 datasources), is it the repository or the usecase who have to handle the second source if the first one is down?

According to the official Android doc, the repository handles multiple data sources, and UseCase knows nothing about them, hence shouldn't handle data-layer-specific problems. https://developer.android.com/topic/architecture#data-layer

If I want to display the battery level in real time, who have the responsability of polling ? The repository (probably bad for battery if app goes in background) ? Usecases ? ViewModels ?

It seems like a good candidate to infrastructure or framework layer. You can find how to implement framework layer in this article: https://antonioleiva.com/clean-architecture-android/

It uses location polling as an example. It will answer lots of your questions.

Upvotes: 1

Related Questions