Skip to content

Commit

Permalink
AC-1049: Migrate providerdashboard package from MVP to MVVM architect…
Browse files Browse the repository at this point in the history
…ure (#970)
  • Loading branch information
amrsalah3 authored Oct 28, 2022
1 parent 971ce1c commit 74ac364
Show file tree
Hide file tree
Showing 21 changed files with 351 additions and 533 deletions.
5 changes: 5 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import rx.Observable;

Expand All @@ -35,23 +33,18 @@
import androidx.work.NetworkType;
import androidx.work.OneTimeWorkRequest;

import com.openmrs.android_sdk.R;
import com.openmrs.android_sdk.library.OpenMRSLogger;
import com.openmrs.android_sdk.library.api.RestApi;
import com.openmrs.android_sdk.library.api.workers.provider.AddProviderWorker;
import com.openmrs.android_sdk.library.api.workers.provider.DeleteProviderWorker;
import com.openmrs.android_sdk.library.api.workers.provider.UpdateProviderWorker;
import com.openmrs.android_sdk.library.dao.ProviderRoomDAO;
import com.openmrs.android_sdk.library.databases.entities.LocationEntity;
import com.openmrs.android_sdk.library.listeners.retrofitcallbacks.DefaultResponseCallback;
import com.openmrs.android_sdk.library.models.Provider;
import com.openmrs.android_sdk.library.models.Resource;
import com.openmrs.android_sdk.library.models.ResultType;
import com.openmrs.android_sdk.library.models.Results;
import com.openmrs.android_sdk.utilities.NetworkUtils;
import com.openmrs.android_sdk.utilities.ToastUtil;

import org.jetbrains.annotations.NotNull;

/**
* The type Provider repository.
Expand Down Expand Up @@ -173,7 +166,7 @@ public Observable<ResultType> addProvider(Provider provider) {
}

/**
* Update exiting provider in the database.
* Update existing provider in the database.
*
* @param provider the provider
* @return Observable ResultType of the operation being locally success, all success, or fail
Expand All @@ -198,7 +191,7 @@ public Observable<ResultType> updateProvider(Provider provider) {
.build()
);

logger.e("Updated provider will be synced to the server when device gets connected to network");
logger.i("Updated provider will be synced to the server when device gets connected to network");
return ResultType.UpdateProviderLocalSuccess;
}

Expand All @@ -209,7 +202,7 @@ public Observable<ResultType> updateProvider(Provider provider) {
providerRoomDao.updateProviderByUuid(response.body().getDisplay(), provider.getId(),
response.body().getPerson(), response.body().getUuid(),
response.body().getIdentifier());
logger.e("Updating provider succeeded " + response.raw());
logger.i("Updating provider succeeded " + response.raw());
return ResultType.UpdateProviderSuccess;
} else {
logger.e("Failed to update provider. Error: " + response.message());
Expand All @@ -219,51 +212,46 @@ public Observable<ResultType> updateProvider(Provider provider) {
}

/**
* Delete providers.
* Delete an existing provider from the database.
*
* @param providerUuid the provider uuid
* @param callback the callback
* @param providerUuid the UUID of the provider to be deleted
* @return Observable ResultType of the operation being locally success, all success, or fail
*/
public void deleteProviders(String providerUuid, DefaultResponseCallback callback) {

//when callback would call onResponse successfull the UI will refresh automatically
if (NetworkUtils.isOnline()) {
restApi.deleteProvider(providerUuid).enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(@NotNull Call<ResponseBody> call, @NotNull Response<ResponseBody> response) {
if (response.isSuccessful()) {

// offline deletion
providerRoomDao.deleteByUuid(providerUuid);

ToastUtil.success(context.getString(R.string.delete_provider_success_msg));
logger.e("Deleting Provider Successful " + response.raw());

callback.onResponse();
}
}

@Override
public void onFailure(@NotNull Call<ResponseBody> call, @NotNull Throwable t) {
logger.e("Failed to delete provider. Error: " + t.getMessage());
callback.onErrorResponse(context.getString(R.string.delete_provider_failure_msg));
}
});
} else {
public Observable<ResultType> deleteProviders(String providerUuid) {
return createObservableIO(() -> {
if (!NetworkUtils.isOnline()) {
// If not online, delete provider locally.
providerRoomDao.deleteByUuid(providerUuid);

// offline deletion
Data data = new Data.Builder().putString("uuid", providerUuid).build();
providerRoomDao.deleteByUuid(providerUuid);
callback.onResponse();
// Delegate to the WorkManager.
Data data = new Data.Builder()
.putString("uuid", providerUuid)
.build();
Constraints constraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build();
workManager.enqueue(new OneTimeWorkRequest.Builder(DeleteProviderWorker.class)
.setConstraints(constraints)
.setInputData(data)
.build()
);

// enqueue the work to workManager
Constraints constraints = new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build();
workManager.enqueue(new OneTimeWorkRequest.Builder(DeleteProviderWorker.class).setConstraints(constraints).setInputData(data).build());
logger.i("Provider will be removed from the server when you're back online");
return ResultType.ProviderDeletionLocalSuccess;
}

//toast about deferred provider deletion
ToastUtil.success(context.getString(R.string.offline_provider_delete));
logger.e("Provider will be removed from the server when you're back online");
}
// Otherwise (online), delete provider remotely.
Response<ResponseBody> response = restApi.deleteProvider(providerUuid).execute();
if (response.isSuccessful()) {
// Delete provider from the database.
providerRoomDao.deleteByUuid(providerUuid);
logger.i("Deleting Provider Successful " + response.raw());
return ResultType.ProviderDeletionSuccess;
} else {
logger.e("Failed to delete provider. Error: " + response.message());
throw new Exception("Failed to delete provider. Error: " + response.message());
}
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,7 @@ enum class ResultType {
AddProviderLocalSuccess,
UpdateProviderSuccess,
UpdateProviderLocalSuccess,
ProviderDeletionSuccess,
ProviderDeletionLocalSuccess,
ProviderDeletionError,
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,6 @@ object ApplicationConstants {
}

object RequestCodes {
const val ADD_PROVIDER_REQ_CODE = 100
const val EDIT_PROVIDER_REQ_CODE = 101
const val START_SETTINGS_REQ_CODE = 102
const val IMAGE_REQUEST = 1
const val GALLERY_IMAGE_REQUEST = 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,10 +438,8 @@ private View.OnClickListener onClickActionSolver(final OnClickAction action) {
dismiss();
break;
case DELETE_PROVIDER:
ProviderDashboardActivity providerDashboardActivity = (ProviderDashboardActivity) getActivity();
providerDashboardActivity.mPresenter.deleteProvider();
((ProviderDashboardActivity) getActivity()).deleteProvider();
dismiss();
providerDashboardActivity.finish();
break;
case MULTI_DELETE_PATIENT:
SyncedPatientsActivity syncedPatientsActivity = (SyncedPatientsActivity) getActivity();
Expand Down

This file was deleted.

Loading

0 comments on commit 74ac364

Please sign in to comment.