Ahmed3Elshaer
4/6/2018 - 8:40 PM

Repository Pattern

public class NetworkRequest implements NetworkRequestSource{
  
  //TODO("not implemented") this class should be Singleton
  
  @override
  public void getRemoteUser(OnRemoteUserCallback onRemoteUserCallback){
    //code that makes WebService Request to get the user from the server.
    //if we got the user object then we send it through the Callback
    onRemoteUserCallback.onUserExists(//user that we got from the servcer 
                                      user);
    //if we got error from the server 
     onRemoteUserCallback.onUserError(errorMessage)
  }
}
interface NetworkRequestSource{
  public void getRemoteUser(OnRemoteUserCallback onRemoteUserCallback);
  
  interface OnRemoteUserCallback{
    
    public void onUserExists(User user);
    public void onUserError(String msg);
  }
}

//"Source" in object means it's an Interface object from any class that implments this Interface

public class DataRepository implements DataSource {
  
  //this is the networking Interfcae object that is resposible for handling all the WebService APIs
  NetworkRequestSource networkRequest;
  
  //this is the local Class object that is resposible for handling all the local database(SQL, etc..) 
  LocalRequestSource localRequest
  
  @override
   public void getCurrentUser(OnUserCallback userCallback){
    User user= localRequest.getUser();
    if (user==null){
      networkRequest.getRemoteUser(new OnRemoteUserCallback(){
        @override
        public void onUserExists(User user){
          userCallback.onUserExists(user)
          localRequest.cacheUser(user)
          
    
           }
        @override
        public void onUserError (String msg){
           userCallback.onUserError(msg)
    
            }
      });
    }else{
      userCallback.onUserExists(user)
    }
     
     
   }
   
   
   private DataRepository(NetworkRequestSource networkRequest,LocalRequestSource localRequest){
     this.networkRequest=networkRequest
     this.localRequest=localRequest
     
     
   }
   
   private static DataRepository INSTANCE =null;
   public static DataRepository newInstance(NetworkRequestSource networkRequest,LocalRequestSource localRequest){
            if (INSTANCE == null) {
            INSTANCE = new  DataRepository( networkRequest, localRequest);
        }
        return INSTANCE;
   }
  
}
interface DataSource {
  // this is the interface which any presenter in MVP can communicate with DataRepository with 
  // eg. getting the user data from local database
  // eg. request some webservice API
  public void getCurrentUser(OnUserCallback userCallback);
  
  //check the example below
  interface OnUserCallback{
    public void onUserExists(User user);
    public void onUserError (String msg);
  }
}

// this is an example of how the callback implemented in the presenter.
mDataRepository.getCurrentUser(new OnUserCallback(){
  @override
  public void onUserExists(User user){
    
    
    
  }
   @override
  public void onUserError (String msg){
    
  }
  
} )