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

fix: Update identifiers list on adding or removing items #743

Merged
merged 1 commit into from
Mar 7, 2018
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -11,6 +11,7 @@
import com.mifos.objects.client.ClientPayload;
import com.mifos.objects.client.Page;
import com.mifos.objects.noncore.Identifier;
import com.mifos.objects.noncore.IdentifierCreationResponse;
import com.mifos.objects.noncore.IdentifierPayload;
import com.mifos.objects.noncore.IdentifierTemplate;
import com.mifos.objects.templates.clients.ClientsTemplate;
Expand Down Expand Up @@ -316,10 +317,10 @@ public Observable<List<Identifier>> getClientIdentifiers(int clientId) {
*
* @param clientId Client Id
* @param identifierPayload IdentifierPayload
* @return GenericResponse
* @return IdentifierCreationResponse
*/
public Observable<GenericResponse> createClientIdentifier(int clientId,
IdentifierPayload identifierPayload) {
public Observable<IdentifierCreationResponse> createClientIdentifier(
Copy link
Member

Choose a reason for hiding this comment

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

@tarun0 don't need to create new Model, It's generic response that's why @droidchef made it. It is making more sense.

Copy link
Member Author

Choose a reason for hiding this comment

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

@therajanmaurya We are not fetching the identifiers list again. We are retrieving the created identifier's id from the response based on the discussion with @droidchef That's why I changed it.

Copy link
Member Author

Choose a reason for hiding this comment

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

@droidchef Will it be a better approach to check if the GenericResponse got resourceId and similar values that we want to check and then proceed accordingly? You once told about the naming and readability. I created the new model following those lines.

int clientId, IdentifierPayload identifierPayload) {
return mBaseApiManager.getClientsApi().createClientIdentifier(clientId, identifierPayload);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.mifos.objects.client.ClientPayload;
import com.mifos.objects.client.Page;
import com.mifos.objects.noncore.Identifier;
import com.mifos.objects.noncore.IdentifierCreationResponse;
import com.mifos.objects.noncore.IdentifierPayload;
import com.mifos.objects.noncore.IdentifierTemplate;
import com.mifos.objects.templates.clients.ClientsTemplate;
Expand Down Expand Up @@ -92,11 +93,12 @@ Observable<ResponseBody> uploadClientImage(@Path("clientId") int clientId,
*
* @param clientId Client Id
* @param identifierPayload IdentifierPayload
* @return GenericResponse
* @return IdentifierCreationResponse
*/
@POST(APIEndPoint.CLIENTS + "/{clientId}/identifiers")
Observable<GenericResponse> createClientIdentifier(@Path("clientId") int clientId,
@Body IdentifierPayload identifierPayload);
Observable<IdentifierCreationResponse> createClientIdentifier(
Copy link
Member

Choose a reason for hiding this comment

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

same as above

@Path("clientId") int clientId,
@Body IdentifierPayload identifierPayload);


/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.mifos.mifosxdroid.dialogfragments.identifierdialog;

import com.mifos.objects.noncore.Identifier;

/**
* Created by Tarun on 07-08-17.
*/

public interface ClientIdentifierCreationListener {

void onClientIdentifierCreationSuccess(Identifier identifier);

void onClientIdentifierCreationFailure(String errorMessage);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
import com.mifos.mifosxdroid.R;
import com.mifos.mifosxdroid.core.MifosBaseActivity;
import com.mifos.mifosxdroid.core.ProgressableDialogFragment;
import com.mifos.mifosxdroid.online.clientidentifiers.ClientIdentifiersFragment;
import com.mifos.objects.noncore.DocumentType;
import com.mifos.objects.noncore.Identifier;
import com.mifos.objects.noncore.IdentifierCreationResponse;
import com.mifos.objects.noncore.IdentifierPayload;
import com.mifos.objects.noncore.IdentifierTemplate;
import com.mifos.utils.Constants;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.inject.Inject;
Expand Down Expand Up @@ -53,16 +56,21 @@ public class IdentifierDialogFragment extends ProgressableDialogFragment impleme
@Inject
IdentifierDialogPresenter mIdentifierDialogPresenter;

View rootView;
@Nullable
private ClientIdentifierCreationListener clientIdentifierCreationListener;

private View rootView;
private int clientId;
private IdentifierTemplate identifierTemplate;
private int identifierDocumentTypeId;
private String status;
private Identifier identifier;
private HashMap<String, DocumentType> documentTypeHashMap;

List<String> mListIdentifierType = new ArrayList<>();
private List<String> mListIdentifierType = new ArrayList<>();

ArrayAdapter<String> mIdentifierTypeAdapter;
ArrayAdapter<String> mIdentifierStatusAdapter;
private ArrayAdapter<String> mIdentifierTypeAdapter;
private ArrayAdapter<String> mIdentifierStatusAdapter;

public static IdentifierDialogFragment newInstance(int clientId) {
IdentifierDialogFragment documentDialogFragment = new IdentifierDialogFragment();
Expand Down Expand Up @@ -130,6 +138,14 @@ void onClickCreateIdentifier() {
identifierPayload.setDocumentKey(et_unique_id.getText().toString());
identifierPayload.setDescription(et_description.getText().toString());

// Add the values in the identifier. It'll be sent to the calling Fragment
// if the request is successful.
identifier = new Identifier();
identifier.setDescription(et_description.getText().toString());
identifier.setDocumentKey(et_unique_id.getText().toString());
identifier.setDocumentType(documentTypeHashMap
.get(sp_identifier_type.getSelectedItem().toString()));

mIdentifierDialogPresenter.createClientIdentifier(clientId, identifierPayload);
}
}
Expand All @@ -140,20 +156,31 @@ public void showClientIdentifierTemplate(IdentifierTemplate identifierTemplate)
this.identifierTemplate = identifierTemplate;
mListIdentifierType.addAll(mIdentifierDialogPresenter.getIdentifierDocumentTypeNames
(identifierTemplate.getAllowedDocumentTypes()));
documentTypeHashMap = mIdentifierDialogPresenter
.mapDocumentTypesWithName(identifierTemplate.getAllowedDocumentTypes());
mIdentifierTypeAdapter.notifyDataSetChanged();
}

@Override
public void showIdentifierCreatedSuccessfully() {
public void showIdentifierCreatedSuccessfully(
IdentifierCreationResponse identifierCreationResponse) {
Toast.makeText(getActivity(), R.string.identifier_created_successfully,
Toast.LENGTH_SHORT).show();
identifier.setClientId(identifierCreationResponse.getClientId());
identifier.setId(identifierCreationResponse.getResourceId());
if (clientIdentifierCreationListener != null) {
clientIdentifierCreationListener.onClientIdentifierCreationSuccess(identifier);
}
getDialog().dismiss();
((ClientIdentifiersFragment) getTargetFragment()).doRefresh();
}

@Override
public void showMessage(String message) {
Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
public void showErrorMessage(String message) {
if (clientIdentifierCreationListener != null) {
clientIdentifierCreationListener.onClientIdentifierCreationFailure(message);
} else {
Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
}
}

@Override
Expand Down Expand Up @@ -189,4 +216,9 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long
public void onNothingSelected(AdapterView<?> parent) {

}

public void setOnClientIdentifierCreationListener(
@Nullable ClientIdentifierCreationListener listener) {
clientIdentifierCreationListener = listener;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mifos.mifosxdroid.dialogfragments.identifierdialog;

import com.mifos.mifosxdroid.base.MvpView;
import com.mifos.objects.noncore.IdentifierCreationResponse;
import com.mifos.objects.noncore.IdentifierTemplate;

/**
Expand All @@ -13,9 +14,9 @@ public interface IdentifierDialogMvpView extends MvpView {

void showClientIdentifierTemplate(IdentifierTemplate identifierTemplate);

void showIdentifierCreatedSuccessfully();
void showIdentifierCreatedSuccessfully(IdentifierCreationResponse identifierCreationResponse);

void showMessage(String message);
void showErrorMessage(String message);

void showError(int errorMessage);
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.mifos.mifosxdroid.dialogfragments.identifierdialog;

import com.mifos.api.GenericResponse;

import com.mifos.api.datamanager.DataManagerClient;
import com.mifos.mifosxdroid.R;
import com.mifos.mifosxdroid.base.BasePresenter;
import com.mifos.objects.noncore.DocumentType;
import com.mifos.objects.noncore.IdentifierCreationResponse;
import com.mifos.objects.noncore.IdentifierPayload;
import com.mifos.objects.noncore.IdentifierTemplate;
import com.mifos.objects.noncore.IdentifierType;
import com.mifos.utils.MFErrorParser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.inject.Inject;
Expand Down Expand Up @@ -81,7 +83,7 @@ public void createClientIdentifier(int clientId, IdentifierPayload identifierPay
mSubscriptions.add(mDataManagerClient.createClientIdentifier(clientId, identifierPayload)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Subscriber<GenericResponse>() {
.subscribe(new Subscriber<IdentifierCreationResponse>() {
@Override
public void onCompleted() {
}
Expand All @@ -93,7 +95,7 @@ public void onError(Throwable e) {
if (e instanceof HttpException) {
String errorMessage = ((HttpException) e).response().errorBody()
.string();
getMvpView().showMessage(MFErrorParser.parseError(errorMessage)
getMvpView().showErrorMessage(MFErrorParser.parseError(errorMessage)
.getErrors().get(0).getDefaultUserMessage());
}
} catch (Throwable throwable) {
Expand All @@ -102,23 +104,40 @@ public void onError(Throwable e) {
}

@Override
public void onNext(GenericResponse genericResponse) {
public void onNext(IdentifierCreationResponse identifierCreationResponse) {
getMvpView().showProgressbar(false);
getMvpView().showIdentifierCreatedSuccessfully();
getMvpView().showIdentifierCreatedSuccessfully(identifierCreationResponse);
}
})
);
}

public List<String> getIdentifierDocumentTypeNames(List<IdentifierType> identifierTypes) {
final ArrayList<String> documentType = new ArrayList<>();
Observable.from(identifierTypes)
.subscribe(new Action1<IdentifierType>() {
public List<String> getIdentifierDocumentTypeNames(List<DocumentType> documentTypes) {
final ArrayList<String> documentTypeList = new ArrayList<>();
Observable.from(documentTypes)
.subscribe(new Action1<DocumentType>() {
@Override
public void call(DocumentType documentType) {
documentTypeList.add(documentType.getName());
}
});
return documentTypeList;
}

/**
* Method to map Document Type with the corresponding name.
* @param documentTypeList List of DocumentType
* @return HashMap of <Name,DocumentType>
*/
HashMap<String, DocumentType> mapDocumentTypesWithName(List<DocumentType> documentTypeList) {
final HashMap<String, DocumentType> hashMap = new HashMap<>();
Observable.from(documentTypeList)
.subscribe(new Action1<DocumentType>() {
@Override
public void call(IdentifierType identifierType) {
documentType.add(identifierType.getName());
public void call(DocumentType documentType) {
hashMap.put(documentType.getName(), documentType);
}
});
return documentType;
return hashMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import com.mifos.mifosxdroid.adapters.IdentifierListAdapter;
import com.mifos.mifosxdroid.core.MifosBaseActivity;
import com.mifos.mifosxdroid.core.MifosBaseFragment;
import com.mifos.mifosxdroid.core.util.Toaster;
import com.mifos.mifosxdroid.dialogfragments.identifierdialog.ClientIdentifierCreationListener;
import com.mifos.mifosxdroid.dialogfragments.identifierdialog.IdentifierDialogFragment;
import com.mifos.mifosxdroid.online.documentlist.DocumentListFragment;
import com.mifos.objects.noncore.Identifier;
Expand All @@ -43,7 +45,8 @@


public class ClientIdentifiersFragment extends MifosBaseFragment implements
ClientIdentifiersMvpView, IdentifierOptionsListener, SwipeRefreshLayout.OnRefreshListener {
ClientIdentifiersMvpView, IdentifierOptionsListener, SwipeRefreshLayout.OnRefreshListener,
ClientIdentifierCreationListener {

@BindView(R.id.rv_client_identifier)
RecyclerView rv_client_identifier;
Expand All @@ -55,7 +58,7 @@ public class ClientIdentifiersFragment extends MifosBaseFragment implements
TextView mNoIdentifierText;

@BindView(R.id.ll_error)
LinearLayout llError;
LinearLayout ll_error;

@BindView(R.id.noIdentifierIcon)
ImageView mNoIdentifierIcon;
Expand Down Expand Up @@ -128,20 +131,40 @@ public void showUserInterface() {

@Override
public void showClientIdentifiers(List<Identifier> identifiers) {
if (llError.getVisibility() == View.VISIBLE) {
llError.setVisibility(View.GONE);
}
this.identifiers = identifiers;
identifierListAdapter.setIdentifiers(identifiers);
identifierListAdapter.notifyDataSetChanged();

if (identifiers.isEmpty()) {
showEmptyClientIdentifier();
} else {
if (ll_error.getVisibility() == View.VISIBLE) {
ll_error.setVisibility(View.GONE);
}
}
}

@Override
public void showEmptyClientIdentifier() {
llError.setVisibility(View.VISIBLE);
private void showEmptyClientIdentifier() {
ll_error.setVisibility(View.VISIBLE);
mNoIdentifierText.setText(getResources().getString(R.string.no_identifier_to_show));
mNoIdentifierIcon.setImageResource(R.drawable.ic_assignment_turned_in_black_24dp);
}

@Override
public void onClientIdentifierCreationSuccess(Identifier identifier) {
if (identifiers.size() == 0) {
//The list is empty prior to adding the new identifier. Remove the empty list message.
ll_error.setVisibility(View.GONE);
}
identifiers.add(identifier);
identifierListAdapter.notifyItemInserted(identifiers.size() - 1);
}

@Override
public void onClientIdentifierCreationFailure(String errorMessage) {
Toaster.show(rootView, errorMessage);
}

@Override
public void showFetchingError(int errorMessage) {
Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_SHORT).show();
Expand All @@ -156,7 +179,7 @@ public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_remove_identifier:
mClientIdentifiersPresenter.deleteIdentifier(clientId,
identifiers.get(position).getId());
identifiers.get(position).getId(), position);
break;
case R.id.menu_identifier_documents:
DocumentListFragment documentListFragment =
Expand All @@ -180,10 +203,11 @@ public boolean onMenuItemClick(MenuItem item) {
}

@Override
public void identifierDeletedSuccessfully() {
public void identifierDeletedSuccessfully(int position) {
Toast.makeText(getActivity(), R.string.identifier_deleted_successfully,
Toast.LENGTH_SHORT).show();
loadIdentifiers();
identifiers.remove(position);
identifierListAdapter.notifyItemRemoved(position);
}

@Override
Expand All @@ -209,7 +233,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
case R.id.menu_add:
IdentifierDialogFragment identifierDialogFragment =
IdentifierDialogFragment.newInstance(clientId);
identifierDialogFragment.setTargetFragment(this, 1);
identifierDialogFragment.setOnClientIdentifierCreationListener(this);
FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager()
.beginTransaction();
fragmentTransaction.addToBackStack(FragmentConstants.FRAG_DOCUMENT_LIST);
Expand All @@ -225,8 +249,4 @@ public void onDestroyView() {
mClientIdentifiersPresenter.detachView();
hideMifosProgressBar();
}

public void doRefresh() {
loadIdentifiers();
}
}
Loading