VladMorzhanov
9/23/2017 - 2:59 PM

Android NetworkDataSource example for repository pattern.

Android NetworkDataSource example for repository pattern.

package com.dev.data.source.network;

import android.os.Environment;

import com.mevics.dev.data.models.User;
import com.mevics.dev.data.source.SettingsRepository;
import com.mevics.dev.data.source.UserDataSource;
import com.mevics.dev.data.source.UserRepository;

import java.io.File;

import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Implementation of the User's data source that works with Retrofit API.
 */
public class UserNetworkDataSource implements UserDataSource {

    private APIService mAPIService;

    private UserRepository userRepository;

    private static UserNetworkDataSource mInstance;

    private UserNetworkDataSource() {
        mAPIService = RetrofitProvider.getMevicsAPIService();
        if (userRepository != null)
            userRepository = UserRepository.getInstance();
    }

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

    @Override
    public void getUser(GetUserCallback callback) {
        mAPIService.getUser(userRepository.getToken()).observeOn(Schedulers.io())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(
                        callback::onUserLoaded,
                        callback::onDataNotAvailable
                );
    }

    @Override
    public void addPassword(String pwd, ResponseCallback responseCallback) {
        mAPIService.addUserPwd(userRepository.getToken(), pwd).observeOn(Schedulers.io())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(
                        responseCallback::onResponseLoaded,
                        responseCallback::onResponseNotAvailable
                );
    }


    @Override
    public void saveUser(User user) {

    }

    @Override
    public void updateUser(User user, GetUserCallback callback) {
        mAPIService.updateUser(userRepository.getToken(), user).observeOn(Schedulers.io())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(
                        callback::onUserLoaded,
                        callback::onDataNotAvailable
                );
    }

    @Override
    public void addUserCheckPwd(String pwd, ResponseCallback callback) {
        mAPIService.addUserCheckPwd(userRepository.getToken(), pwd).observeOn(Schedulers.io())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(
                        callback::onResponseLoaded,
                        callback::onResponseNotAvailable
                );
    }

    @Override
    public void updateUserEmail(String email, ResponseCallback callback) {
        mAPIService.updateUserEmail(userRepository.getToken(), userRepository.getEmail()).observeOn(Schedulers.io())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(
                        callback::onResponseLoaded,
                        callback::onResponseNotAvailable
                );
    }

    @Override
    public void getPwdAvailable(String pwd, ResponseCallback responseCallback) {
        mAPIService.getPwdAvailable(pwd, userRepository.getToken()).observeOn(Schedulers.io())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(
                        responseCallback::onResponseLoaded,
                        responseCallback::onResponseNotAvailable
                );
    }

    @Override
    public void updateUserPwd(String pwd, ResponseCallback responseCallback) {
        mAPIService.updateUserPwd(pwd, userRepository.getToken()).observeOn(Schedulers.io())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(
                        responseCallback::onResponseLoaded,
                        responseCallback::onResponseNotAvailable
                );
    }
}