Skip to content

Client charges Offline Feature #302

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

Merged
merged 8 commits into from
Jul 5, 2016
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
10 changes: 2 additions & 8 deletions mifosng-android/src/main/java/com/mifos/api/DataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public Observable<List<OfflineCenter>> getCenterList(
/**
* Charges API
*/
//TODO Remove this Method After fixing the Charge Test
public Observable<Page<Charges>> getClientCharges(int clientId, int offset, int limit) {
return mBaseApiManager.getChargeApi().getListOfCharges(clientId, offset, limit);
}
Expand Down Expand Up @@ -179,14 +180,6 @@ public Observable<Client> getClient(int id) {
return mBaseApiManager.getClientsApi().getClient(id);
}

public Observable<ResponseBody> uploadClientImage(int id, Part file) {
return mBaseApiManager.getClientsApi().uploadClientImage(id, file);
}

public Observable<ResponseBody> deleteClientImage(int clientId) {
return mBaseApiManager.getClientsApi().deleteClientImage(clientId);
}


/**
* Identifiers API
Expand Down Expand Up @@ -276,6 +269,7 @@ public Observable<List<DataTable>> getSavingsDataTable() {
return mBaseApiManager.getDataTableApi().getTableOf("m_savings_account");
}

//TODO Remove this method after removing its usage
public Observable<List<DataTable>> getClientDataTable() {
return mBaseApiManager.getDataTableApi().getTableOf("m_client");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.mifos.api.datamanager;

import com.mifos.api.BaseApiManager;
import com.mifos.api.local.databasehelper.DatabaseHelperCharge;
import com.mifos.objects.client.Charges;
import com.mifos.objects.client.Page;
import com.mifos.utils.PrefManager;

import javax.inject.Inject;
import javax.inject.Singleton;

import rx.Observable;
import rx.functions.Func1;

/**
* This DataManager is for Managing Charge API, In which Request is going to Server
* and In Response, We are getting Charge API Observable Response using Retrofit2.
* DataManagerCharge saving response in Database and response to Presenter as accordingly.
* <p/>
* Created by Rajan Maurya on 4/7/16.
*/
@Singleton
public class DataManagerCharge {

public final BaseApiManager mBaseApiManager;
public final DatabaseHelperCharge mDatabaseHelperCharge;

@Inject
public DataManagerCharge(BaseApiManager baseApiManager,
DatabaseHelperCharge databaseHelperCharge) {
mBaseApiManager = baseApiManager;
mDatabaseHelperCharge = databaseHelperCharge;
}


/**
* This Method Request the Charge API at
* https://demo.openmf.org/fineract-provider/api/v1/clients/{clientId}/charges
* and in response get the of the Charge Page that contains Charges list.
*
* @param clientId Client Id
* @param offset Offset From Which Position Charge List user want
* @param limit Maximum Limit of the Response Charge List Size
* @return Page<Charge> Page of Charge in Which List Size is according to Limit and from
* where position is Starting according to offset</>
*/
public Observable<Page<Charges>> getClientCharges(final int clientId, int offset, int limit) {
switch (PrefManager.getUserStatus()) {
case 0:
return mBaseApiManager.getChargeApi().getListOfCharges(clientId, offset, limit)
.concatMap(new Func1<Page<Charges>, Observable<? extends Page<Charges>>>() {
@Override
public Observable<? extends Page<Charges>> call(Page<Charges>
chargesPage) {
mDatabaseHelperCharge.saveClientCharges(chargesPage, clientId);
return Observable.just(chargesPage);
}
});

case 1:
/**
* Return Client Charges from DatabaseHelperClient only one time.
*/
if (offset == 0)
return mDatabaseHelperCharge.readClientCharges(clientId);

default:
return Observable.just(new Page<Charges>());
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import javax.inject.Inject;
import javax.inject.Singleton;

import okhttp3.MultipartBody;
import okhttp3.ResponseBody;
import rx.Observable;
import rx.functions.Func1;

Expand Down Expand Up @@ -140,4 +142,32 @@ public Observable<? extends ClientAccounts> call(
}
}


/**
* This Method for removing the Client Image from his profile on server
* if its response is true the client does not have any profile Image and if
* response is false then failed to update the client image from server profile.
* There can any problem during updating the client image like Network error.
*
* @param clientId Client ID
* @return ResposeBody is the Retrofit 2 response
*/
public Observable<ResponseBody> deleteClientImage(int clientId) {
return mBaseApiManager.getClientsApi().deleteClientImage(clientId);
}


/**
* This Method will be called when ever user want to update the client profile image.
* The response of the updating the client image can be true or false its depends upon the
* network and right choice image file.
*
* @param id Client Id
* @param file MultipartBody of the Image file
* @return ResposeBody is the Retrofit 2 response
*/
public Observable<ResponseBody> uploadClientImage(int id, MultipartBody.Part file) {
return mBaseApiManager.getClientsApi().uploadClientImage(id, file);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.mifos.api.local.databasehelper;

import android.os.AsyncTask;

import com.mifos.objects.client.Charges;
import com.mifos.objects.client.Charges_Table;
import com.mifos.objects.client.ClientDate;
import com.mifos.objects.client.Page;
import com.raizlabs.android.dbflow.sql.language.SQLite;

import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Singleton;

import rx.Observable;
import rx.Subscriber;

/**
* Created by Rajan Maurya on 4/7/16.
*/
@Singleton
public class DatabaseHelperCharge {

@Inject
public DatabaseHelperCharge() {

}


/**
* This Method save the All Client Charges in Database and save the Charge Due date in the
* ClientDate as reference with Charge Id.
*
* @param chargesPage
* @param clientId
* @return null
*/
public Observable<Void> saveClientCharges(final Page<Charges> chargesPage,
final int clientId) {
AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() {
@Override
public void run() {

for (Charges charges : chargesPage.getPageItems()) {
charges.setClientId(clientId);

ClientDate clientDate = new ClientDate(0, charges.getId(),
charges.getDueDate().get(2),
charges.getDueDate().get(1),
charges.getDueDate().get(0));
charges.setChargeDueDate(clientDate);
charges.save();
}
}
});
return null;
}


/**
* This method Retrieve the Charges from Charges_Table and set the Charges Due date after
* loading the Charge due date from the ChargeDate_table as reference with charge Id.
*
* @param clientId Client ID
* @return Page of Charges
*/
public Observable<Page<Charges>> readClientCharges(final int clientId) {
return Observable.create(new Observable.OnSubscribe<Page<Charges>>() {
@Override
public void call(Subscriber<? super Page<Charges>> subscriber) {

//Loading All charges from Charges_Table as reference to client id
List<Charges> chargesList = SQLite.select()
.from(Charges.class)
.where(Charges_Table.clientId.eq(clientId))
.queryList();

//Setting the Charge Due Date
for (int i = 0; i < chargesList.size(); i++) {
chargesList.get(i).setDueDate(Arrays.asList(
chargesList.get(i).getChargeDueDate().getYear(),
chargesList.get(i).getChargeDueDate().getMonth(),
chargesList.get(i).getChargeDueDate().getDay()));
}

Page<Charges> chargePage = new Page<Charges>();
chargePage.setPageItems(chargesList);
subscriber.onNext(chargePage);

}
});

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
import rx.Subscriber;

/**
* This DatabaseHelper Managing all Database logic and staff (Saving, Update, Delete).
* Whenever DataManager send response to save or request to read from Database then this class
* save the response or read the all values from database and return as accordingly.
* <p/>
* Created by Rajan Maurya on 24/06/16.
*/
@Singleton
Expand All @@ -50,7 +54,7 @@ public void run() {

for (Client client : clientPage.getPageItems()) {

ClientDate clientDate = new ClientDate(client.getId(),
ClientDate clientDate = new ClientDate(client.getId(), 0,
client.getActivationDate().get(0),
client.getActivationDate().get(1),
client.getActivationDate().get(2));
Expand Down Expand Up @@ -113,8 +117,9 @@ public void call(Subscriber<? super Client> subscriber) {

/**
* This Method write the ClientAccount in tho DB. According to Schema Defined in Model
*
* @param clientAccounts Model of List of LoanAccount and SavingAccount
* @param clientId Client Id
* @param clientId Client Id
* @return null
*/
public Observable<Void> saveClientAccounts(final ClientAccounts clientAccounts,
Expand Down Expand Up @@ -144,6 +149,7 @@ public void run() {
/**
* This Method Read the Table of LoanAccount and SavingAccount and return the List of
* LoanAccount and SavingAccount according to clientId
*
* @param clientId Client Id
* @return Return the ClientAccount according to client Id
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

import com.mifos.api.DataManager;
import com.mifos.api.datamanager.DataManagerCenter;
import com.mifos.api.datamanager.DataManagerCharge;
import com.mifos.api.datamanager.DataManagerClient;
import com.mifos.api.datamanager.DataManagerDataTable;
import com.mifos.api.datamanager.DataManagerGroups;
import com.mifos.api.local.databasehelper.DatabaseHelperCenter;
import com.mifos.api.local.databasehelper.DatabaseHelperCharge;
import com.mifos.api.local.databasehelper.DatabaseHelperClient;
import com.mifos.api.local.databasehelper.DatabaseHelperDataTable;
import com.mifos.api.local.databasehelper.DatabaseHelperGroups;
Expand Down Expand Up @@ -39,12 +41,14 @@ public interface ApplicationComponent {
DataManagerGroups dataManagerGroups();
DataManagerCenter dataManagerCenters();
DataManagerDataTable dataManagerDataTable();
DataManagerCharge dataManagerCharge();


DatabaseHelperClient databaseHelperClient();
DatabaseHelperCenter databaseHelperCenter();
DatabaseHelperGroups databaseHelperGroup();
DatabaseHelperDataTable databaseHelperDataTable();
DatabaseHelperCharge databaseHelperCharge();

Bus eventBus();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.mifos.mifosxdroid.online.clientcharge;

import com.mifos.api.DataManager;
import com.mifos.mifosxdroid.base.Presenter;
import com.mifos.api.datamanager.DataManagerCharge;
import com.mifos.mifosxdroid.base.BasePresenter;
import com.mifos.objects.client.Charges;
import com.mifos.objects.client.Page;

Expand All @@ -15,52 +15,53 @@
/**
* Created by Rajan Maurya on 5/6/16.
*/
public class ClientChargePresenter implements Presenter<ClientChargeMvpView> {
public class ClientChargePresenter extends BasePresenter<ClientChargeMvpView> {


private final DataManager mDataManager;
private final DataManagerCharge mDataManagerCharge;
private Subscription mSubscription;
private ClientChargeMvpView mClientChargeMvpView;

@Inject
public ClientChargePresenter(DataManager dataManager) {
mDataManager = dataManager;
public ClientChargePresenter(DataManagerCharge dataManagerCharge) {
mDataManagerCharge = dataManagerCharge;
}


@Override
public void attachView(ClientChargeMvpView mvpView) {
mClientChargeMvpView = mvpView;
super.attachView(mvpView);
}


@Override
public void detachView() {
mClientChargeMvpView = null;
super.detachView();
if (mSubscription != null) mSubscription.unsubscribe();
}

public void loadCharges(int clientId, int offset, int limit) {
mClientChargeMvpView.showProgressbar(true);
getMvpView().showProgressbar(true);
if (mSubscription != null) mSubscription.unsubscribe();
mSubscription = mDataManager.getClientCharges(clientId, offset, limit)
mSubscription = mDataManagerCharge.getClientCharges(clientId, offset, limit)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Subscriber<Page<Charges>>() {
@Override
public void onCompleted() {
mClientChargeMvpView.showProgressbar(false);
getMvpView().showProgressbar(false);
}

@Override
public void onError(Throwable e) {
mClientChargeMvpView.showProgressbar(false);
mClientChargeMvpView
getMvpView().showProgressbar(false);
getMvpView()
.showFetchingErrorCharges("Failed to Load Charges");
}

@Override
public void onNext(Page<Charges> chargesPage) {
mClientChargeMvpView.showProgressbar(false);
mClientChargeMvpView.showChargesList(chargesPage);
getMvpView().showProgressbar(false);
getMvpView().showChargesList(chargesPage);
}
});
}
Expand Down
Loading