title: MVVM - Having a hard time understanding how to create the Domain layer in Clean Architecture
body: <p>I'm trying to learn MVVM to make my app's architecture more clean. But I'm having a hard time grasping how to create a &quot;Domain&quot; layer for my app.</p>
  <p>Currently this is how the structure of my project is looking:</p>
  <p>My <code>View</code> is the activity. My <code>ViewModel</code> has a public method that the activity can call. Once the method in the ViewModel is called, it calls a method in my <code>Repository</code> class which performs a network call, which then returns the data back to the ViewModel. I then update the <code>LiveData</code> in the ViewModel so the Activity's UI is updated.</p>
  <p>This is where I'm confused on how to add a <code>Domain</code> layer to the structure. I've read a lot of Stackoverflow answers and blogs about the Domain layer and they mostly all tell you to remove all the business logic from the <code>ViewModel</code> and make a pure Java/Kotlin class.</p>
  <p>So instead of</p>
  <p><code>View --&gt; ViewModel --&gt; Repository</code></p>
  <p>I would be communicating from the <code>ViewModel</code> to the <code>Domain</code> class and the <code>Domain</code> class would communicate with the <code>Repository</code>?</p>
  <p><code>View --&gt; ViewModel --&gt; Domain --&gt; Repository</code></p>
  <p>I'm using <code>RxJava</code> to make the call from my <code>ViewModel</code> to the <code>Repository</code> class.</p>
  <pre><code>@HiltViewModel
  public class PostViewModel extends ViewModel {
  
      private static final String TAG = &quot;PostViewModel&quot;;
  
      private final List&lt;Post&gt;                  listPosts              = new ArrayList&lt;&gt;();
      private final MutableLiveData&lt;List&lt;Post&gt;&gt; getPostsLiveData       = new MutableLiveData&lt;&gt;();
      private final MutableLiveData&lt;Boolean&gt;    centerProgressLiveData = new MutableLiveData&lt;&gt;();
      private final MainRepository              repository;
  
      @Inject
      public PostViewModel(MainRepository repository) {
          this.repository = repository;
          getSubredditPosts();
      }
  
      public void getSubredditPosts() {
          repository.getSubredditPosts()
                  .subscribeOn(Schedulers.io())
                  .observeOn(AndroidSchedulers.mainThread())
                  .subscribe(new Observer&lt;Response&gt;() {
                      @Override
                      public void onSubscribe(@NonNull Disposable d) {
                          centerProgressLiveData.setValue(true);
                      }
  
                      @Override
                      public void onNext(@NonNull Response response) {
                          Log.d(TAG, &quot;onNext: Query called&quot;);
                          centerProgressLiveData.setValue(false);
                          listPosts.clear();
                          listPosts.addAll(response.getData().getChildren());
                          getPostsLiveData.setValue(listPosts);
                      }
  
                      @Override
                      public void onError(@NonNull Throwable e) {
                          Log.e(TAG, &quot;onError: getPosts&quot;, e);
                          centerProgressLiveData.setValue(false);
                      }
  
                      @Override
                      public void onComplete() {
                      }
                  });
      }
  </code></pre>
  <pre><code>public class MainRepository {
  
      private final MainService service;
  
      @Inject
      public MainRepository(MainService service) {
          this.service = service;
      }
  
      public Observable&lt;Response&gt; getSubredditPosts() {
          return service.getSubredditPosts();
      }
  }
  </code></pre>
  <p>Could someone please give me an example of how I could do it? I'm quite lost here</p>