Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AC-810 Allergy can be deleted in online/offline mode #793

Merged
merged 4 commits into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"formatVersion": 1,
"database": {
"version": 1,
"identityHash": "9ce04f6ec616f483e7380864d70335b6",
"identityHash": "a7ac209f015d121f1c97a404ea9ce58e",
"entities": [
{
"tableName": "concepts",
Expand Down Expand Up @@ -698,14 +698,20 @@
},
{
"tableName": "allergy",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`_id` INTEGER PRIMARY KEY AUTOINCREMENT, `patientId` TEXT, `comment` TEXT, `severity_display` TEXT, `severity_uuid` TEXT, `allergen_display` TEXT, `allergen_uuid` TEXT, `allergy_reactions` TEXT)",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`_id` INTEGER PRIMARY KEY AUTOINCREMENT, `uuid` TEXT, `patientId` TEXT, `comment` TEXT, `severity_display` TEXT, `severity_uuid` TEXT, `allergen_display` TEXT, `allergen_uuid` TEXT, `allergy_reactions` TEXT)",
"fields": [
{
"fieldPath": "id",
"columnName": "_id",
"affinity": "INTEGER",
"notNull": false
},
{
"fieldPath": "uuid",
"columnName": "uuid",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "patientId",
"columnName": "patientId",
Expand Down Expand Up @@ -762,7 +768,7 @@
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"9ce04f6ec616f483e7380864d70335b6\")"
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"a7ac209f015d121f1c97a404ea9ce58e\")"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public class AllergyRoomDAOTest {
@Rule
public InstantTaskExecutorRule instantTaskExecutorRule = new InstantTaskExecutorRule();

private AllergyEntity allergyEntity1 = createAllergyEntity(10L, "1", "comment 1", "severity display 1", "severity uuid 1", "allergen display 1", "allergen uuid 1");
private AllergyEntity allergyEntity2 = createAllergyEntity(20L, "2", "comment 2", "severity display 2", "severity uuid 2", "allergen display 2", "allergen uuid 2");
private AllergyEntity allergyEntity1 = createAllergyEntity(10L, "uuid 1", "1", "comment 1", "severity display 1", "severity uuid 1", "allergen display 1", "allergen uuid 1");
private AllergyEntity allergyEntity2 = createAllergyEntity(20L, "uuid 2", "2", "comment 2", "severity display 2", "severity uuid 2", "allergen display 2", "allergen uuid 2");
private AppDatabase mDatabase;
private AllergyRoomDAO allergyRoomDAO;

Expand Down Expand Up @@ -82,9 +82,19 @@ public void getAllAllergies_shouldGetAllAllergiesCorrectly() {
Assert.assertEquals(renderAllergyString(allergyRoomDAO.getAllAllergiesByPatientID("2").get(0)), renderAllergyString(allergyEntity2));
}

private AllergyEntity createAllergyEntity(long id, String patientID, String comment, String severityDisplay, String severityUUID, String allergenDisplay, String allergenUUID) {
@Test
public void deleteAllergyByUUID_shouldCorrectlyDeleteAllergyByUUID() {
allergyRoomDAO.saveAllergy(allergyEntity1);
allergyRoomDAO.saveAllergy(allergyEntity2);
allergyRoomDAO.deleteAllergyByUUID(allergyEntity1.getUuid());
Assert.assertEquals(allergyRoomDAO.getAllAllergiesByPatientID("1").size(), 0L);
Assert.assertEquals(allergyRoomDAO.getAllAllergiesByPatientID("2").size(), 1L);
}

private AllergyEntity createAllergyEntity(long id, String uuid, String patientID, String comment, String severityDisplay, String severityUUID, String allergenDisplay, String allergenUUID) {
AllergyEntity allergyEntity = new AllergyEntity();
allergyEntity.setId(id);
allergyEntity.setUuid(uuid);
allergyEntity.setPatientId(patientID);
allergyEntity.setComment(comment);
allergyEntity.setSeverityDisplay(severityDisplay);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,7 @@ interface PatientChartsPresenter extends PatientDashboardMainPresenter {

interface PatientAllergyPresenter extends PatientDashboardMainPresenter {
void getAllergy(Fragment fragment);

void deleteAllergy(String uuid);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,22 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.LinearLayoutManager;

import org.jetbrains.annotations.NotNull;
import org.openmrs.mobile.R;
import org.openmrs.mobile.activities.patientdashboard.PatientDashboardActivity;
import org.openmrs.mobile.activities.patientdashboard.PatientDashboardContract;
import org.openmrs.mobile.activities.patientdashboard.PatientDashboardFragment;
import org.openmrs.mobile.databinding.FragmentPatientAllergyBinding;
import org.openmrs.mobile.models.Allergy;
import org.openmrs.mobile.utilities.ToastUtil;

import java.util.List;

public class PatientAllergyFragment extends PatientDashboardFragment implements PatientDashboardContract.ViewPatientAllergy {
public class PatientAllergyFragment extends PatientDashboardFragment implements PatientDashboardContract.ViewPatientAllergy, PatientAllergyRecyclerViewAdapter.OnLongPressListener {
AlertDialog alertDialog;
private PatientDashboardActivity mPatientDashboardActivity;
private FragmentPatientAllergyBinding binding;

Expand Down Expand Up @@ -88,10 +92,29 @@ public void showAllergyList(List<Allergy> allergies) {
binding.emptyAllergyList.setVisibility(View.VISIBLE);
} else {
binding.emptyAllergyList.setVisibility(View.GONE);
PatientAllergyRecyclerViewAdapter adapter = new PatientAllergyRecyclerViewAdapter(getContext(), allergies);
PatientAllergyRecyclerViewAdapter adapter = new PatientAllergyRecyclerViewAdapter(getContext(), allergies, this);
binding.recyclerViewAllergy.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
}

@Override
public void showDialogueBox(Allergy allergy) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getContext());
alertDialogBuilder.setTitle(R.string.delete_allergy_title);
alertDialogBuilder
.setMessage(R.string.delete_allergy_description)
.setCancelable(false)
.setPositiveButton(R.string.mark_patient_deceased_proceed, (dialog, id) -> {
dialog.cancel();
// Code to delete
((PatientDashboardAllergyPresenter) mPresenter).deleteAllergy(allergy.getUuid());
})
.setNegativeButton(R.string.dialog_button_cancel, (dialog, id) -> {
alertDialog.cancel();
});
alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import org.openmrs.mobile.R;
Expand All @@ -32,17 +34,19 @@
public class PatientAllergyRecyclerViewAdapter extends RecyclerView.Adapter<PatientAllergyRecyclerViewAdapter.ViewHolder> {
private Context context;
private List<Allergy> list;
private OnLongPressListener longPressListener;

public PatientAllergyRecyclerViewAdapter(Context context, List<Allergy> list) {
public PatientAllergyRecyclerViewAdapter(Context context, List<Allergy> list, OnLongPressListener longPressListener) {
this.context = context;
this.list = list;
this.longPressListener = longPressListener;
}

@NonNull
@Override
public PatientAllergyRecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(context).inflate(R.layout.list_patient_allergy, parent, false);
return new ViewHolder(itemView);
return new ViewHolder(itemView, longPressListener);
}

@Override
Expand Down Expand Up @@ -71,6 +75,11 @@ public void onBindViewHolder(@NonNull PatientAllergyRecyclerViewAdapter.ViewHold
} else {
holder.severity.setText(allergy.getSeverity().getDisplay());
}

holder.cardView.setOnLongClickListener(view -> {
longPressListener.showDialogueBox(allergy);
return false;
});
}

@Override
Expand All @@ -83,13 +92,19 @@ public class ViewHolder extends RecyclerView.ViewHolder {
private TextView reaction;
private TextView severity;
private TextView comment;
private CardView cardView;

public ViewHolder(@NonNull View itemView) {
public ViewHolder(@NonNull View itemView, OnLongPressListener longPressListener) {
super(itemView);
allergen = itemView.findViewById(R.id.allergy_allergen);
reaction = itemView.findViewById(R.id.allergy_reaction);
severity = itemView.findViewById(R.id.allergy_severity);
comment = itemView.findViewById(R.id.allergy_comment);
cardView = itemView.findViewById(R.id.allergy_cardView);
}
}

interface OnLongPressListener {
void showDialogueBox(Allergy allergy);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.openmrs.mobile.activities.patientdashboard.PatientDashboardContract;
import org.openmrs.mobile.activities.patientdashboard.PatientDashboardMainPresenterImpl;
import org.openmrs.mobile.api.CustomApiCallback;
import org.openmrs.mobile.api.RestApi;
import org.openmrs.mobile.api.RestServiceBuilder;
import org.openmrs.mobile.api.repository.AllergyRepository;
Expand All @@ -27,7 +28,7 @@

import java.util.List;

public class PatientDashboardAllergyPresenter extends PatientDashboardMainPresenterImpl implements PatientDashboardContract.PatientAllergyPresenter {
public class PatientDashboardAllergyPresenter extends PatientDashboardMainPresenterImpl implements PatientDashboardContract.PatientAllergyPresenter, CustomApiCallback {
private PatientDashboardContract.ViewPatientAllergy mPatientAllergyView;
private String patientId;
private PatientDAO patientDAO;
Expand Down Expand Up @@ -62,7 +63,27 @@ public void getAllergy(Fragment fragment) {
allergyRepository.getAllergies(restApi, mPatient.getUuid()).observe(fragment, this::updateViews);
}

public void getAllergyFromDatabase() {
List<Allergy> allergyList = allergyRepository.getAllergyFromDatabase(mPatient.getId().toString());
updateViews(allergyList);
}

@Override
public void deleteAllergy(String allergyUuid) {
allergyRepository.deleteAllergy(restApi, mPatient.getUuid(), allergyUuid, this);
}

public void updateViews(List<Allergy> allergies) {
mPatientAllergyView.showAllergyList(allergies);
}

@Override
public void onSuccess() {
getAllergyFromDatabase();
}

@Override
public void onFailure() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,8 @@ Call<Provider> UpdateProvider(@Path("uuid") String uuid,

@GET("patient/{uuid}/allergy")
Call<Results<Allergy>> getAllergies(@Path("uuid") String uuid);

@DELETE("patient/{patientUuid}/allergy/{allergyUuid}")
Call<ResponseBody> deleteAllergy(@Path("patientUuid") String patientUuid,
@Path("allergyUuid") String allergyUuid);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.work.Constraints;
import androidx.work.Data;
import androidx.work.NetworkType;
import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkManager;

import org.jetbrains.annotations.NotNull;
import org.openmrs.mobile.R;
import org.openmrs.mobile.api.CustomApiCallback;
import org.openmrs.mobile.api.RestApi;
import org.openmrs.mobile.api.workers.allergy.DeleteAllergyWorker;
import org.openmrs.mobile.application.OpenMRS;
import org.openmrs.mobile.dao.AllergyRoomDAO;
import org.openmrs.mobile.databases.AppDatabase;
Expand All @@ -33,19 +40,22 @@
import java.util.ArrayList;
import java.util.List;

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

public class AllergyRepository {
AllergyRoomDAO allergyRoomDAO;
WorkManager workManager;
String patientID;
List<Allergy> allergyList = new ArrayList<>();
List<AllergyEntity> allergyEntitiesOffline = new ArrayList<>();

public AllergyRepository(String patientID) {
this.patientID = patientID;
allergyRoomDAO = AppDatabase.getDatabase(OpenMRS.getInstance()).allergyRoomDAO();
workManager = WorkManager.getInstance(OpenMRS.getInstance());
}

public AllergyRepository(String id, AllergyRoomDAO allergyRoomDAO) {
Expand Down Expand Up @@ -82,4 +92,45 @@ public void onFailure(@NotNull Call<Results<Allergy>> call, @NotNull Throwable t
}
return allergyLiveData;
}

public List<Allergy> getAllergyFromDatabase(String patientID) {
allergyEntitiesOffline = allergyRoomDAO.getAllAllergiesByPatientID(patientID);
allergyList = AppDatabaseHelper.convertTo(allergyEntitiesOffline);
return allergyList;
}

public void deleteAllergy(RestApi restApi, String patientUuid, String allergyUuid, CustomApiCallback callback) {
if (NetworkUtils.isOnline()) {
restApi.deleteAllergy(patientUuid, allergyUuid).enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
allergyRoomDAO.deleteAllergyByUUID(allergyUuid);
ToastUtil.success(OpenMRS.getInstance().getString(R.string.delete_allergy_success));
callback.onSuccess();
}
}

@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rishabh-997 instead of using customAPI callback use default response callback and try to use toast utils in the presenter by using these callbacks

Copy link
Collaborator Author

@rishabh-997 rishabh-997 Aug 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that mean I need to create another Listener extending DefaultResponseCallback ??? Because I need to send ToastType also as three different Toasts are being used here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rishabh-997 Maybe we can have an additional method other than OnSuccess and On failure just extend from default response callback and add the additional method for notify does it sounds good ?

ToastUtil.error(OpenMRS.getInstance().getString(R.string.delete_allergy_failure));
callback.onFailure();
}
});
} else {
// offline deletion
Data data = new Data.Builder()
.putString("patient_uuid", patientUuid)
.putString("allergy_uuid", allergyUuid)
.build();
allergyRoomDAO.deleteAllergyByUUID(allergyUuid);
callback.onSuccess();

// enqueue the work to workManager
Constraints constraints = new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build();
workManager.enqueue(new OneTimeWorkRequest.Builder(DeleteAllergyWorker.class).setConstraints(constraints).setInputData(data).build());

ToastUtil.success(OpenMRS.getInstance().getString(R.string.delete_allergy_offline));
}
}
}
Loading