Skip to content

Commit

Permalink
Merge pull request #443 from mayank-kgp/newchargedialouge
Browse files Browse the repository at this point in the history
Add API Response Model in ChargeDialogeFragment
  • Loading branch information
therajanmaurya authored Feb 21, 2017
2 parents 58c9153 + 19295f5 commit fbd4258
Show file tree
Hide file tree
Showing 18 changed files with 1,079 additions and 115 deletions.
3 changes: 2 additions & 1 deletion mifosng-android/src/main/java/com/mifos/api/DataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.mifos.objects.organisation.Office;
import com.mifos.objects.organisation.Staff;
import com.mifos.objects.response.SaveResponse;
import com.mifos.objects.templates.clients.ChargeTemplate;
import com.mifos.objects.templates.loans.GroupLoanTemplate;
import com.mifos.services.data.ChargesPayload;
import com.mifos.services.data.GroupLoanPayload;
Expand Down Expand Up @@ -95,7 +96,7 @@ public Observable<Page<Charges>> getClientCharges(int clientId, int offset, int
return mBaseApiManager.getChargeApi().getListOfCharges(clientId, offset, limit);
}

public Observable<ResponseBody> getAllChargesV2(int clientId) {
public Observable<ChargeTemplate> getAllChargesV2(int clientId) {
return mBaseApiManager.getChargeApi().getAllChargesS(clientId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.mifos.api.model.APIEndPoint;
import com.mifos.objects.client.Charges;
import com.mifos.objects.client.Page;
import com.mifos.objects.templates.clients.ChargeTemplate;
import com.mifos.services.data.ChargesPayload;

import okhttp3.ResponseBody;
Expand All @@ -26,7 +27,7 @@ public interface ChargeService {
Observable<ResponseBody> listAllCharges();

@GET(APIEndPoint.CLIENTS + "/{clientId}/charges/template")
Observable<ResponseBody> getAllChargesS(@Path("clientId") int clientId);
Observable<ChargeTemplate> getAllChargesS(@Path("clientId") int clientId);

@GET(APIEndPoint.LOANS + "/{loanId}/charges/template")
Observable<ResponseBody> getAllChargev3(@Path("loanId") int loanId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -25,63 +24,57 @@
import com.mifos.mifosxdroid.core.util.Toaster;
import com.mifos.mifosxdroid.uihelpers.MFDatePicker;
import com.mifos.objects.client.Charges;
import com.mifos.objects.templates.clients.ChargeTemplate;
import com.mifos.services.data.ChargesPayload;
import com.mifos.utils.Constants;
import com.mifos.utils.DateHelper;
import com.mifos.utils.FragmentConstants;
import com.mifos.utils.SafeUIBlockingUtility;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import butterknife.OnClick;

/**
* Created by nellyk on 1/22/2016.
* <p/>
* Use this Dialog Fragment to Create and/or Update charges
*/
public class ChargeDialogFragment extends ProgressableDialogFragment implements
MFDatePicker.OnDatePickListener, ChargeDialogMvpView {
MFDatePicker.OnDatePickListener, ChargeDialogMvpView,
AdapterView.OnItemSelectedListener {

public final String LOG_TAG = getClass().getSimpleName();

@BindView(R.id.sp_charge_name)
Spinner sp_charge_name;
Spinner spChargeName;

@BindView(R.id.amount_due_charge)
EditText et_amout_due;
EditText etAmoutDue;

@BindView(R.id.et_date)
EditText charge_due_date;
EditText etChargeDueDate;

@BindView(R.id.et_charge_locale)
EditText charge_locale;
EditText etChargeLocale;

@BindView(R.id.bt_save_charge)
Button bt_save_charge;
Button btnSaveCharge;

@Inject
ChargeDialogPresenter mChargeDialogPresenter;

private List<String> chargeNameList = new ArrayList<>();
private ArrayAdapter<String> chargeNameAdapter;
private ChargeTemplate mChargeTemplate;
String duedateString;
private View rootView;
private SafeUIBlockingUtility safeUIBlockingUtility;
private DialogFragment mfDatePicker;
private int Id;
private int chargeId;
private int clientId;
private HashMap<String, Integer> chargeNameIdHashMap = new HashMap<String, Integer>();
private String chargeName;

public static ChargeDialogFragment newInstance(int clientId) {
ChargeDialogFragment chargeDialogFragment = new ChargeDialogFragment();
Expand Down Expand Up @@ -118,33 +111,30 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle

ButterKnife.bind(this, rootView);
mChargeDialogPresenter.attachView(this);

inflatedueDate();
inflateChargesSpinner();
inflateChargeNameSpinner();

duedateString = charge_due_date.getText().toString();
duedateString = etChargeDueDate.getText().toString();
duedateString = DateHelper.getDateAsStringUsedForCollectionSheetPayload(duedateString)
.replace("-", " ");

bt_save_charge.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

ChargesPayload chargesPayload = new ChargesPayload();
chargesPayload.setAmount(et_amout_due.getEditableText().toString());
chargesPayload.setLocale(charge_locale.getEditableText().toString());
chargesPayload.setDueDate(duedateString);
chargesPayload.setDateFormat("dd MMMM yyyy");
chargesPayload.setChargeId(Id);
initiateChargesCreation(chargesPayload);
}
});
return rootView;
}

@OnClick(R.id.bt_save_charge)
public void saveNewCharge() {
ChargesPayload chargesPayload = new ChargesPayload();
chargesPayload.setAmount(etAmoutDue.getEditableText().toString());
chargesPayload.setLocale(etChargeLocale.getEditableText().toString());
chargesPayload.setDueDate(duedateString);
chargesPayload.setDateFormat("dd MMMM yyyy");
chargesPayload.setChargeId(chargeId);
initiateChargesCreation(chargesPayload);
}

@Override
public void onDatePicked(String date) {
charge_due_date.setText(date);
etChargeDueDate.setText(date);
}

//Charges Fetching API
Expand All @@ -159,81 +149,44 @@ private void initiateChargesCreation(ChargesPayload chargesPayload) {

public void inflatedueDate() {
mfDatePicker = MFDatePicker.newInsance(this);
charge_due_date.setText(MFDatePicker.getDatePickedAsString());
charge_due_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mfDatePicker.show(getActivity().getSupportFragmentManager(), FragmentConstants
.DFRAG_DATE_PICKER);
}
});
etChargeDueDate.setText(MFDatePicker.getDatePickedAsString());
}

@Override
public void showAllChargesV2(ResponseBody result) {
/* Activity is null - Fragment has been detached; no need to do anything. */
if (getActivity() == null) return;

Log.d(LOG_TAG, "Charges Loaded Successfully");

final List<Charges> charges = new ArrayList<>();
// you can use this array to populate your spinner
final ArrayList<String> chargesNames = new ArrayList<String>();
//Try to get response body
BufferedReader reader = null;
StringBuilder sb = new StringBuilder();
try {
reader = new BufferedReader(new InputStreamReader(result.byteStream()));
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
JSONObject obj = new JSONObject(sb.toString());
if (obj.has("chargeOptions")) {
JSONArray chargesTypes = obj.getJSONArray("chargeOptions");
for (int i = 0; i < chargesTypes.length(); i++) {
JSONObject chargesObject = chargesTypes.getJSONObject(i);
Charges charge = new Charges();
charge.setId(chargesObject.optInt("id"));
charge.setName(chargesObject.optString("name"));
charges.add(charge);
chargesNames.add(chargesObject.optString("name"));
chargeNameIdHashMap.put(charge.getName(), charge.getId());
}

}
String stringResult = sb.toString();
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
final ArrayAdapter<String> chargesAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item, chargesNames);
chargesAdapter.setDropDownViewResource(android.R.layout
.simple_spinner_dropdown_item);
sp_charge_name.setAdapter(chargesAdapter);
sp_charge_name.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long
l) {
Id = chargeNameIdHashMap.get(chargesNames.get(i));
Log.d("chargesoptionss" + chargesNames.get(i), String.valueOf(Id));
if (Id != -1) {


} else {
@OnClick(R.id.et_date)
public void inflateDatePicker() {
mfDatePicker.show(getActivity().getSupportFragmentManager(), FragmentConstants
.DFRAG_DATE_PICKER);
}

Toast.makeText(getActivity(), getString(R.string.error_select_charge)
, Toast.LENGTH_SHORT).show();

}
public void inflateChargeNameSpinner() {
chargeNameAdapter = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_spinner_item, chargeNameList);
chargeNameAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spChargeName.setAdapter(chargeNameAdapter);
spChargeName.setOnItemSelectedListener(this);
}

}
@Override
public void showAllChargesV2(ChargeTemplate chargeTemplate) {
mChargeTemplate = chargeTemplate;
chargeNameList.addAll(mChargeDialogPresenter.filterChargeName
(chargeTemplate.getChargeOptions()));
chargeNameAdapter.notifyDataSetChanged();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (parent.getId()) {
case R.id.sp_charge_name:
chargeId = mChargeTemplate.getChargeOptions().get(position).getId();
break;
}
}

}
});
@Override
public void onNothingSelected(AdapterView<?> parent) {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

import com.mifos.mifosxdroid.base.MvpView;
import com.mifos.objects.client.Charges;

import okhttp3.ResponseBody;
import com.mifos.objects.templates.clients.ChargeTemplate;

/**
* Created by Rajan Maurya on 08/06/16.
*/
public interface ChargeDialogMvpView extends MvpView {

void showAllChargesV2(ResponseBody response);
void showAllChargesV2(ChargeTemplate chargeTemplate);

void showChargesCreatedSuccessfully(Charges changes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
import com.mifos.api.DataManager;
import com.mifos.mifosxdroid.base.BasePresenter;
import com.mifos.objects.client.Charges;
import com.mifos.objects.templates.clients.ChargeOptions;
import com.mifos.objects.templates.clients.ChargeTemplate;
import com.mifos.services.data.ChargesPayload;

import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;

import okhttp3.ResponseBody;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
Expand Down Expand Up @@ -44,7 +49,7 @@ public void loadAllChargesV2(int clientId) {
mSubscription = mDataManager.getAllChargesV2(clientId)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Subscriber<ResponseBody>() {
.subscribe(new Subscriber<ChargeTemplate>() {
@Override
public void onCompleted() {
getMvpView().showProgressbar(false);
Expand All @@ -57,9 +62,9 @@ public void onError(Throwable e) {
}

@Override
public void onNext(ResponseBody response) {
public void onNext(ChargeTemplate chargeTemplate) {
getMvpView().showProgressbar(false);
getMvpView().showAllChargesV2(response);
getMvpView().showAllChargesV2(chargeTemplate);
}
});
}
Expand Down Expand Up @@ -90,4 +95,17 @@ public void onNext(Charges charges) {
}
});
}

public List<String> filterChargeName(final List<ChargeOptions>
chargeOptions) {
final ArrayList<String> chargeNameList = new ArrayList<>();
Observable.from(chargeOptions)
.subscribe(new Action1<ChargeOptions>() {
@Override
public void call(ChargeOptions chargeOptions) {
chargeNameList.add(chargeOptions.getName());
}
});
return chargeNameList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.os.Parcel;
import android.os.Parcelable;

import com.google.gson.annotations.SerializedName;
import com.mifos.api.local.MifosBaseModel;
import com.mifos.api.local.MifosDatabase;
import com.raizlabs.android.dbflow.annotation.Column;
Expand All @@ -18,12 +19,15 @@
public class ChargeCalculationType extends MifosBaseModel implements Parcelable {

@PrimaryKey
@SerializedName("id")
Integer id;

@Column
@SerializedName("code")
String code;

@Column
@SerializedName("value")
String value;


Expand Down Expand Up @@ -51,6 +55,15 @@ public void setValue(String value) {
this.value = value;
}

@Override
public String toString() {
return "ChargeCalculationType{" +
"id=" + id +
", code='" + code + '\'' +
", value='" + value + '\'' +
'}';
}

@Override
public int describeContents() {
return 0;
Expand Down
Loading

0 comments on commit fbd4258

Please sign in to comment.