VladMorzhanov
9/23/2017 - 2:57 PM

Android Repository class example for repository pattern.

Android Repository class example for repository pattern.

package com.dev.data.source;

import com.dev.data.models.User;
import com.dev.data.source.local.UserLocalDataSource;
import com.dev.data.source.network.UserNetworkDataSource;

import okhttp3.ResponseBody;

/**
 * Concrete implementation to load Users from the data sources into a cache.
 */
public class UserRepository implements UserDataSource {

    private UserLocalDataSource userLocalDataSource;

    private UserNetworkDataSource userNetworkDataSource;

    private static UserRepository mInstance;

    private UserRepository() {
        userNetworkDataSource = UserNetworkDataSource.getInstance();
        userLocalDataSource = new UserLocalDataSource();
    }

    public static UserRepository getInstance() {
        if (mInstance == null)
            mInstance = new UserRepository();
        return mInstance;
    }

    @Override
    public void getUser(GetUserCallback callback) {
        userLocalDataSource.getUser(new GetUserCallback() {
            @Override
            public void onUserLoaded(User user) {
                callback.onUserLoaded(user);
            }

            @Override
            public void onDataNotAvailable(Throwable err) {
                if (err.getMessage().equals("Data is null")) {
                    userNetworkDataSource.getUser(new GetUserCallback() {

                        @Override
                        public void onUserLoaded(User user) {
                            callback.onUserLoaded(user);
                            userLocalDataSource.saveUser(user);
                        }

                        @Override
                        public void onDataNotAvailable(Throwable err) {
                            callback.onDataNotAvailable(err);
                        }
                    });
                }
            }
        });
    }

    @Override
    public void saveUser(User user) {
        userLocalDataSource.saveUser(user);
    }

    @Override
    public void updateUser(User user, GetUserCallback callback) {
        userNetworkDataSource.updateUser(user, new GetUserCallback() {
            @Override
            public void onUserLoaded(User user) {
                userLocalDataSource.updateUser(user, new GetUserCallback() {
                    @Override
                    public void onUserLoaded(User user) {
                        callback.onUserLoaded(user);
                    }

                    @Override
                    public void onDataNotAvailable(Throwable err) {
                        callback.onDataNotAvailable(err);
                    }
                });
            }

            @Override
            public void onDataNotAvailable(Throwable err) {
                callback.onDataNotAvailable(err);
            }
        });
    }

    @Override
    public long getId() {
        return userLocalDataSource.getId();
    }

    @Override
    public void setId(long id) {
        userLocalDataSource.getUser(new GetUserCallback() {
            @Override
            public void onUserLoaded(User user) {
                user.setId(id);

                userNetworkDataSource.updateUser(user, new GetUserCallback() {
                    @Override
                    public void onUserLoaded(User user) {
                        userLocalDataSource.setId(id);
                    }

                    @Override
                    public void onDataNotAvailable(Throwable err) {

                    }
                });
            }

            @Override
            public void onDataNotAvailable(Throwable err) {

            }
        });
    }
}