package es.voghdev.prjdagger2.repository;

import java.util.ArrayList;
import java.util.List;

import es.voghdev.prjdagger2.global.model.User;
import es.voghdev.prjdagger2.usecase.GetUsers;

public class UserRepository implements GetUsers {
    CachePolicy cachePolicy;
    GetUsers apiDataSource;
    GetUsers fileDataSource;
    List<User> users = new ArrayList<>();

    public UserRepository(CachePolicy cachePolicy, GetUsers apiDataSource, GetUsers fileDataSource) {
        this.cachePolicy = cachePolicy;
        this.apiDataSource = apiDataSource;
        this.fileDataSource = fileDataSource;
    }

    public void setCachePolicy(CachePolicy cachePolicy) {
        this.cachePolicy = cachePolicy;
    }

    @Override
    public List<User> get() {
        invalidateCacheIfNecessary(cachePolicy, users);

        if(users != null && users.size() > 0)
            return users;

        List<User> apiUsers = apiDataSource.get();
        cachePolicy = new TimedCachePolicy(TimedCachePolicy.ONE_MINUTE);
        users = apiUsers;
        return users;
    }

    private void invalidateCacheIfNecessary(CachePolicy cachePolicy, List<User> users) {
        if(!cachePolicy.isCacheValid())
            users.clear();
    }

    @Override
    public void getAsync(Listener listener) {

    }
}

@@ -1262,16 +1262,22 @@
 etAsync(
+final 
 Listener
@@ -1289,16 +1289,689 @@
 ener) %7B%0A
+        invalidateCacheIfNecessary(cachePolicy, users);%0A%0A        if(users != null && users.size() %3E 0)%0A            listener.onUsersReceived(users, true);%0A%0A        apiDataSource.getAsync(new Listener() %7B%0A            @Override%0A            public void onUsersReceived(List%3CUser%3E list, boolean isCached) %7B%0A                users = list;%0A                listener.onUsersReceived(list, isCached);%0A            %7D%0A%0A            @Override%0A            public void onError(Exception e) %7B%0A                listener.onError(e);%0A            %7D%0A%0A            @Override%0A            public void onNoInternetAvailable() %7B%0A                listener.onNoInternetAvailable();%0A            %7D%0A        %7D);
 %0A    %7D%0A%7D

Prepare to inject repositories with deps