openmrs-android-sdk/src/main/java/com/openmrs/android_sdk/library/api/repository/ProviderRepository.java
/**
     * Add provider.
     *
     * @param provider the provider
     * @param callback the callback
     */
Add provider.
@param provider the provider
@param callback the callback
Add provider . @param provider the provider @param callback the callback
public void addProvider(Provider provider, DefaultResponseCallback callback) {
        if (NetworkUtils.isOnline()) {
            restApi.addProvider(provider).enqueue(new Callback<Provider>() {
                @Override
                public void onResponse(@NotNull Call<Provider> call, @NotNull Response<Provider> response) {
                    if (response.isSuccessful()) {
                        providerRoomDao.addProvider(response.body());
                        ToastUtil.success(context.getString(R.string.add_provider_success_msg));
                        logger.e("Adding Provider Successful " + response.raw());
                        callback.onResponse();
                    }
                }
                @Override
                public void onFailure(@NotNull Call<Provider> call, @NotNull Throwable t) {
                    logger.e("Failed to add provider. Error:  " + t.getMessage());
                    callback.onErrorResponse(context.getString(R.string.add_provider_failure_msg));
                }
            });
        } else {
            long providerId = providerRoomDao.addProvider(provider);
            Data data = new Data.Builder().putLong("id", providerId).build();
            callback.onResponse();
            Constraints constraints = new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build();
            workManager.enqueue(new OneTimeWorkRequest.Builder(AddProviderWorker.class).setConstraints(constraints).setInputData(data).build());
            ToastUtil.notify(context.getString(R.string.offline_provider_add));
            logger.e("provider will be synced to the server when device gets connected to network");
        }
    }
public void addProvider ( Provider provider , DefaultResponseCallback callback ) { if ( NetworkUtils . isOnline ( ) ) { restApi . addProvider ( provider ) . enqueue ( new Callback < Provider > ( ) { @ Override public void onResponse ( @ NotNull Call < Provider > call , @ NotNull Response < Provider > response ) { if ( response . isSuccessful ( ) ) { providerRoomDao . addProvider ( response . body ( ) ) ; ToastUtil . success ( context . getString ( R . string . add_provider_success_msg ) ) ; logger . e ( "Adding Provider Successful " + response . raw ( ) ) ; callback . onResponse ( ) ; } } @ Override public void onFailure ( @ NotNull Call < Provider > call , @ NotNull Throwable t ) { logger . e ( "Failed to add provider. Error:  " + t . getMessage ( ) ) ; callback . onErrorResponse ( context . getString ( R . string . add_provider_failure_msg ) ) ; } } ) ; } else { long providerId = providerRoomDao . addProvider ( provider ) ; Data data = new Data . Builder ( ) . putLong ( "id" , providerId ) . build ( ) ; callback . onResponse ( ) ; Constraints constraints = new Constraints . Builder ( ) . setRequiredNetworkType ( NetworkType . CONNECTED ) . build ( ) ; workManager . enqueue ( new OneTimeWorkRequest . Builder ( AddProviderWorker . class ) . setConstraints ( constraints ) . setInputData ( data ) . build ( ) ) ; ToastUtil . notify ( context . getString ( R . string . offline_provider_add ) ) ; logger . e ( "provider will be synced to the server when device gets connected to network" ) ; } }
Add provider.
Add provider .
//offline adding provider //offline addition operation //delegate to the workManager //toast about deferred provider creation