diff --git a/mifosng-android/src/main/AndroidManifest.xml b/mifosng-android/src/main/AndroidManifest.xml index f7513112b55..b3798e825bc 100755 --- a/mifosng-android/src/main/AndroidManifest.xml +++ b/mifosng-android/src/main/AndroidManifest.xml @@ -112,16 +112,20 @@ android:screenOrientation="portrait" /> + android:label="@string/sync_clients" + android:screenOrientation="portrait" /> + android:label="@string/sync_groups" + android:screenOrientation="portrait" /> + + + android:label="@string/sync_loanrepayments" + android:screenOrientation="portrait" /> + android:exported="false"/> - + \ No newline at end of file diff --git a/mifosng-android/src/main/java/com/mifos/api/datamanager/DataManagerCenter.java b/mifosng-android/src/main/java/com/mifos/api/datamanager/DataManagerCenter.java index bd69cda8bd2..cea506033a8 100644 --- a/mifosng-android/src/main/java/com/mifos/api/datamanager/DataManagerCenter.java +++ b/mifosng-android/src/main/java/com/mifos/api/datamanager/DataManagerCenter.java @@ -63,7 +63,7 @@ public Observable> getCenters(boolean paged, int offset, int limit) /** * Return All Centers List from DatabaseHelperCenter only one time. * If offset is zero this means this is first request and - * return all clients from DatabaseHelperCenter + * return all centers from DatabaseHelperCenter */ if (offset == 0) return mDatabaseHelperCenter.readAllCenters(); @@ -88,13 +88,55 @@ public Observable getCentersGroupAndMeeting(int id) { } public Observable createCenter(CenterPayload centerPayload) { - return mBaseApiManager.getCenterApi().createCenter(centerPayload); + switch (PrefManager.getUserStatus()) { + case 0: + return mBaseApiManager.getCenterApi().createCenter(centerPayload); + case 1: + /** + * Save CenterPayload in Database table. + */ + return mDatabaseHelperCenter.saveCenterPayload(centerPayload); + + default: + return Observable.just(new SaveResponse()); + } } public Observable> getOffices() { return mBaseApiManager.getOfficeApi().getAllOffices(); } + /** + * This method loading the all CenterPayloads from the Database. + * + * @return List + */ + public Observable> getAllDatabaseCenterPayload() { + return mDatabaseHelperCenter.readAllCenterPayload(); + } + + /** + * This method will called when user is syncing the Database center. + * whenever a center is synced then request goes to Database to delete that center form + * Database and reload the list from Database and update the list in UI + * + * @param id of the centerPayload in Database + * @return List + */ + public Observable> deleteAndUpdateCenterPayloads(int id) { + return mDatabaseHelperCenter.deleteAndUpdateCenterPayloads(id); + } + + /** + * This Method updating the CenterPayload in Database and return the same CenterPayload + * + * @param centerPayload CenterPayload + * @return CenterPayload + */ + public Observable updateCenterPayload(CenterPayload centerPayload) { + return mDatabaseHelperCenter.updateDatabaseCenterPayload(centerPayload); + } + /** * This method is activating the center * diff --git a/mifosng-android/src/main/java/com/mifos/api/local/MifosDatabase.java b/mifosng-android/src/main/java/com/mifos/api/local/MifosDatabase.java index 04a04b992f8..05da465ed65 100644 --- a/mifosng-android/src/main/java/com/mifos/api/local/MifosDatabase.java +++ b/mifosng-android/src/main/java/com/mifos/api/local/MifosDatabase.java @@ -12,5 +12,5 @@ public class MifosDatabase { public static final String NAME = "Mifos"; //Always Increase the Version Number - public static final int VERSION = 1; + public static final int VERSION = 2; } diff --git a/mifosng-android/src/main/java/com/mifos/api/local/databasehelper/DatabaseHelperCenter.java b/mifosng-android/src/main/java/com/mifos/api/local/databasehelper/DatabaseHelperCenter.java index 9b78d4ea1e2..b5e729080de 100644 --- a/mifosng-android/src/main/java/com/mifos/api/local/databasehelper/DatabaseHelperCenter.java +++ b/mifosng-android/src/main/java/com/mifos/api/local/databasehelper/DatabaseHelperCenter.java @@ -5,13 +5,20 @@ import com.mifos.objects.client.Page; import com.mifos.objects.group.Center; +import com.mifos.objects.response.SaveResponse; +import com.mifos.services.data.CenterPayload; +import com.mifos.services.data.CenterPayload_Table; +import com.raizlabs.android.dbflow.sql.language.Delete; import com.raizlabs.android.dbflow.sql.language.SQLite; +import java.util.List; + import javax.inject.Inject; import javax.inject.Singleton; import rx.Observable; import rx.Subscriber; +import rx.functions.Func0; /** * Created by Rajan Maurya on 28/6/16. @@ -69,4 +76,56 @@ public void call(Subscriber> subscriber) { } + public Observable saveCenterPayload(final CenterPayload centerPayload) { + return Observable.defer(new Func0>() { + @Override + public Observable call() { + centerPayload.save(); + return Observable.just(new SaveResponse()); + } + }); + } + + public Observable> readAllCenterPayload() { + return Observable.defer(new Func0>>() { + @Override + public Observable> call() { + List centerPayloads = SQLite.select() + .from(CenterPayload.class) + .queryList(); + return Observable.just(centerPayloads); + } + }); + } + + /** + * This Method for deleting the center payload from the Database according to Id and + * again fetch the center List from the Database CenterPayload_Table + * @param id is Id of the Center Payload in which reference center was saved into Database + * @return List + */ + public Observable> deleteAndUpdateCenterPayloads(final int id) { + return Observable.defer(new Func0>>() { + @Override + public Observable> call() { + Delete.table(CenterPayload.class, CenterPayload_Table.id.eq(id)); + + List groupPayloads = SQLite.select() + .from(CenterPayload.class) + .queryList(); + return Observable.just(groupPayloads); + } + }); + } + + public Observable updateDatabaseCenterPayload( + final CenterPayload centerPayload) { + return Observable.defer(new Func0>() { + @Override + public Observable call() { + centerPayload.update(); + return Observable.just(centerPayload); + } + }); + } } diff --git a/mifosng-android/src/main/java/com/mifos/mifosxdroid/adapters/SyncCenterPayloadAdapter.java b/mifosng-android/src/main/java/com/mifos/mifosxdroid/adapters/SyncCenterPayloadAdapter.java new file mode 100644 index 00000000000..a38817a23ec --- /dev/null +++ b/mifosng-android/src/main/java/com/mifos/mifosxdroid/adapters/SyncCenterPayloadAdapter.java @@ -0,0 +1,91 @@ +package com.mifos.mifosxdroid.adapters; + +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import com.mifos.mifosxdroid.R; +import com.mifos.services.data.CenterPayload; + +import java.util.ArrayList; +import java.util.List; + +import javax.inject.Inject; + +import butterknife.BindView; +import butterknife.ButterKnife; + +/** + * Created by mayankjindal on 04/07/17. + */ + +public class SyncCenterPayloadAdapter extends + RecyclerView.Adapter { + + private List mCenterPayloads; + + @Inject + public SyncCenterPayloadAdapter() { + mCenterPayloads = new ArrayList<>(); + } + + @Override + public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + View view = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.item_sync_center, parent, false); + return new ViewHolder(view); + } + + @Override + public void onBindViewHolder(ViewHolder holder, int position) { + CenterPayload centerPayload = mCenterPayloads.get(position); + + holder.tvName.setText(centerPayload.getName()); + holder.tvOfficeId.setText(String.valueOf(centerPayload.getOfficeId())); + holder.tvActivationDate.setText(centerPayload.getActivationDate()); + if (centerPayload.isActive()) { + holder.tvActiveStatus.setText(String.valueOf(true)); + } else { + holder.tvActiveStatus.setText(String.valueOf(false)); + } + if (mCenterPayloads.get(position).getErrorMessage() != null) { + holder.tvErrorMessage.setText(centerPayload.getErrorMessage()); + holder.tvErrorMessage.setVisibility(View.VISIBLE); + } + } + + @Override + public int getItemCount() { + return mCenterPayloads.size(); + } + + public void setCenterPayload(List centerPayload) { + mCenterPayloads = centerPayload; + notifyDataSetChanged(); + } + + public static class ViewHolder extends RecyclerView.ViewHolder { + + @BindView(R.id.tv_db_name) + TextView tvName; + + @BindView(R.id.tv_db_office_id) + TextView tvOfficeId; + + @BindView(R.id.tv_db_activation_date) + TextView tvActivationDate; + + @BindView(R.id.tv_db_active_status) + TextView tvActiveStatus; + + @BindView(R.id.tv_error_message) + TextView tvErrorMessage; + + public ViewHolder(View v) { + super(v); + ButterKnife.bind(this, v); + } + } +} diff --git a/mifosng-android/src/main/java/com/mifos/mifosxdroid/injection/component/ActivityComponent.java b/mifosng-android/src/main/java/com/mifos/mifosxdroid/injection/component/ActivityComponent.java index fa0b10956a4..b0290c153e2 100644 --- a/mifosng-android/src/main/java/com/mifos/mifosxdroid/injection/component/ActivityComponent.java +++ b/mifosng-android/src/main/java/com/mifos/mifosxdroid/injection/component/ActivityComponent.java @@ -13,6 +13,7 @@ import com.mifos.mifosxdroid.injection.module.ActivityModule; import com.mifos.mifosxdroid.login.LoginActivity; import com.mifos.mifosxdroid.offline.offlinedashbarod.OfflineDashboardFragment; +import com.mifos.mifosxdroid.offline.synccenterpayloads.SyncCenterPayloadsFragment; import com.mifos.mifosxdroid.offline.syncclientpayloads.SyncClientPayloadsFragment; import com.mifos.mifosxdroid.offline.syncgrouppayloads.SyncGroupPayloadsFragment; import com.mifos.mifosxdroid.offline.syncloanrepaymenttransacition.SyncLoanRepaymentTransactionFragment; @@ -145,6 +146,8 @@ public interface ActivityComponent { void inject(SyncGroupPayloadsFragment syncGroupPayloadsFragment); + void inject(SyncCenterPayloadsFragment syncCenterPayloadsFragment); + void inject(OfflineDashboardFragment offlineDashboardFragment); void inject(SyncLoanRepaymentTransactionFragment syncLoanRepaymentTransactionFragment); diff --git a/mifosng-android/src/main/java/com/mifos/mifosxdroid/offline/offlinedashbarod/OfflineDashboardFragment.java b/mifosng-android/src/main/java/com/mifos/mifosxdroid/offline/offlinedashbarod/OfflineDashboardFragment.java index 781133edc1d..d1c961d8b99 100644 --- a/mifosng-android/src/main/java/com/mifos/mifosxdroid/offline/offlinedashbarod/OfflineDashboardFragment.java +++ b/mifosng-android/src/main/java/com/mifos/mifosxdroid/offline/offlinedashbarod/OfflineDashboardFragment.java @@ -20,6 +20,7 @@ import com.mifos.mifosxdroid.core.MifosBaseFragment; import com.mifos.mifosxdroid.core.RecyclerItemClickListener; import com.mifos.mifosxdroid.core.util.Toaster; +import com.mifos.mifosxdroid.offline.synccenterpayloads.SyncCenterPayloadActivity; import com.mifos.mifosxdroid.offline.syncclientpayloads.SyncClientPayloadActivity; import com.mifos.mifosxdroid.offline.syncgrouppayloads.SyncGroupPayloadsActivity; import com.mifos.mifosxdroid.offline.syncloanrepaymenttransacition.SyncLoanRepaymentTransactionActivity; @@ -30,6 +31,7 @@ import com.mifos.objects.accounts.savings.SavingsAccountTransactionRequest; import com.mifos.objects.client.ClientPayload; import com.mifos.objects.group.GroupPayload; +import com.mifos.services.data.CenterPayload; import com.mifos.utils.ItemOffsetDecoration; import java.util.ArrayList; @@ -90,14 +92,16 @@ public class OfflineDashboardFragment extends MifosBaseFragment implements OfflineDashboardAdapter mOfflineDashboardAdapter; // update mPayloadIndex to number of request is going to fetch data in Presenter; - private int mPayloadIndex = 4; + private int mPayloadIndex = 5; private static final int GRID_COUNT = 2; private List mPayloadClasses; public static final int[] SYNC_CARD_UI_NAMES = {R.string.sync_clients, - R.string.sync_groups, R.string.sync_loanrepayments, + R.string.sync_groups, + R.string.sync_centers, + R.string.sync_loanrepayments, R.string.sync_savingsaccounttransactions}; @@ -155,10 +159,11 @@ public void onStart() { super.onStart(); mOfflineDashboardAdapter.removeAllCards(); mPayloadClasses.clear(); - mPayloadIndex = 4; + mPayloadIndex = 5; mOfflineDashboardPresenter.loadDatabaseClientPayload(); mOfflineDashboardPresenter.loadDatabaseGroupPayload(); + mOfflineDashboardPresenter.loadDatabaseCenterPayload(); mOfflineDashboardPresenter.loadDatabaseLoanRepaymentTransactions(); mOfflineDashboardPresenter.loadDatabaseSavingsAccountTransactions(); } @@ -203,6 +208,25 @@ public void showGroups(List groupPayloads) { } } + /** + * This method set the response of DataManager from DatabaseHelper that if List + * Size is zero, then decrease the value of mPayloadIndex by 1 and if size is not equal to zero + * the update the adapter and add the Card UI name and size() of the List to sync. + * + * @param centerPayloads List from DatabaseHelperGroup + */ + @Override + public void showCenters(List centerPayloads) { + if (centerPayloads.size() != 0) { + mOfflineDashboardAdapter.showCard(getActivity() + .getResources().getString(R.string.payloads_count) + + centerPayloads.size(), SYNC_CARD_UI_NAMES[2]); + mPayloadClasses.add(SyncCenterPayloadActivity.class); + } else { + mPayloadIndex = mPayloadIndex - 1; + showNoPayloadToShow(); + } + } /** * This method set the response of DataManager from DatabaseHelper that if @@ -217,7 +241,7 @@ public void showLoanRepaymentTransactions(List loanRepayme if (loanRepaymentRequests.size() != 0) { mOfflineDashboardAdapter.showCard(getActivity().getResources() .getString(R.string.transactions_count) + - loanRepaymentRequests.size(), SYNC_CARD_UI_NAMES[2]); + loanRepaymentRequests.size(), SYNC_CARD_UI_NAMES[3]); mPayloadClasses.add(SyncLoanRepaymentTransactionActivity.class); } else { mPayloadIndex = mPayloadIndex - 1; @@ -238,7 +262,7 @@ public void showSavingsAccountTransaction(List if (transactions.size() != 0) { mOfflineDashboardAdapter.showCard(getActivity().getResources() .getString(R.string.transactions_count) + - transactions.size(), SYNC_CARD_UI_NAMES[3]); + transactions.size(), SYNC_CARD_UI_NAMES[4]); mPayloadClasses.add(SyncSavingsAccountTransactionActivity.class); } else { mPayloadIndex = mPayloadIndex - 1; diff --git a/mifosng-android/src/main/java/com/mifos/mifosxdroid/offline/offlinedashbarod/OfflineDashboardMvpView.java b/mifosng-android/src/main/java/com/mifos/mifosxdroid/offline/offlinedashbarod/OfflineDashboardMvpView.java index 0ad3006768c..3a576daac43 100644 --- a/mifosng-android/src/main/java/com/mifos/mifosxdroid/offline/offlinedashbarod/OfflineDashboardMvpView.java +++ b/mifosng-android/src/main/java/com/mifos/mifosxdroid/offline/offlinedashbarod/OfflineDashboardMvpView.java @@ -5,6 +5,7 @@ import com.mifos.objects.accounts.savings.SavingsAccountTransactionRequest; import com.mifos.objects.client.ClientPayload; import com.mifos.objects.group.GroupPayload; +import com.mifos.services.data.CenterPayload; import java.util.List; @@ -17,6 +18,8 @@ public interface OfflineDashboardMvpView extends MvpView { void showGroups(List groupPayloads); + void showCenters(List centerPayloads); + void showLoanRepaymentTransactions(List loanRepaymentRequests); void showSavingsAccountTransaction(List transactions); diff --git a/mifosng-android/src/main/java/com/mifos/mifosxdroid/offline/offlinedashbarod/OfflineDashboardPresenter.java b/mifosng-android/src/main/java/com/mifos/mifosxdroid/offline/offlinedashbarod/OfflineDashboardPresenter.java index cc8fa9cb743..842de0bcbe7 100644 --- a/mifosng-android/src/main/java/com/mifos/mifosxdroid/offline/offlinedashbarod/OfflineDashboardPresenter.java +++ b/mifosng-android/src/main/java/com/mifos/mifosxdroid/offline/offlinedashbarod/OfflineDashboardPresenter.java @@ -1,5 +1,6 @@ package com.mifos.mifosxdroid.offline.offlinedashbarod; +import com.mifos.api.datamanager.DataManagerCenter; import com.mifos.api.datamanager.DataManagerClient; import com.mifos.api.datamanager.DataManagerGroups; import com.mifos.api.datamanager.DataManagerLoan; @@ -10,6 +11,7 @@ import com.mifos.objects.accounts.savings.SavingsAccountTransactionRequest; import com.mifos.objects.client.ClientPayload; import com.mifos.objects.group.GroupPayload; +import com.mifos.services.data.CenterPayload; import java.util.List; @@ -29,17 +31,20 @@ public class OfflineDashboardPresenter extends BasePresenter>() { @Override public void onCompleted() { - } @Override @@ -90,7 +94,6 @@ public void loadDatabaseGroupPayload() { .subscribe(new Subscriber>() { @Override public void onCompleted() { - } @Override @@ -107,6 +110,31 @@ public void onNext(List groupPayloads) { })); } + public void loadDatabaseCenterPayload() { + checkViewAttached(); + getMvpView().showProgressbar(true); + mSubscriptions.add(mDataManagerCenters.getAllDatabaseCenterPayload() + .observeOn(AndroidSchedulers.mainThread()) + .subscribeOn(Schedulers.io()) + .subscribe(new Subscriber>() { + @Override + public void onCompleted() { + } + + @Override + public void onError(Throwable e) { + getMvpView().showProgressbar(false); + getMvpView().showError(R.string.failed_to_load_centerpayload); + } + + @Override + public void onNext(List centerPayloads) { + getMvpView().showCenters(centerPayloads); + getMvpView().showProgressbar(false); + } + })); + } + public void loadDatabaseLoanRepaymentTransactions() { checkViewAttached(); getMvpView().showProgressbar(true); @@ -116,7 +144,6 @@ public void loadDatabaseLoanRepaymentTransactions() { .subscribe(new Subscriber>() { @Override public void onCompleted() { - } @Override @@ -142,7 +169,6 @@ public void loadDatabaseSavingsAccountTransactions() { .subscribe(new Subscriber>() { @Override public void onCompleted() { - } @Override @@ -160,5 +186,4 @@ public void onNext(List }) ); } - } diff --git a/mifosng-android/src/main/java/com/mifos/mifosxdroid/offline/synccenterpayloads/SyncCenterPayloadActivity.java b/mifosng-android/src/main/java/com/mifos/mifosxdroid/offline/synccenterpayloads/SyncCenterPayloadActivity.java new file mode 100644 index 00000000000..81b42595bed --- /dev/null +++ b/mifosng-android/src/main/java/com/mifos/mifosxdroid/offline/synccenterpayloads/SyncCenterPayloadActivity.java @@ -0,0 +1,18 @@ +package com.mifos.mifosxdroid.offline.synccenterpayloads; + +import android.os.Bundle; +import android.support.annotation.Nullable; + +import com.mifos.mifosxdroid.R; +import com.mifos.mifosxdroid.core.MifosBaseActivity; + +public class SyncCenterPayloadActivity extends MifosBaseActivity { + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_toolbar_container); + showBackButton(); + replaceFragment(SyncCenterPayloadsFragment.newInstance(), false, R.id.container); + } +} \ No newline at end of file diff --git a/mifosng-android/src/main/java/com/mifos/mifosxdroid/offline/synccenterpayloads/SyncCenterPayloadsFragment.java b/mifosng-android/src/main/java/com/mifos/mifosxdroid/offline/synccenterpayloads/SyncCenterPayloadsFragment.java new file mode 100644 index 00000000000..0e9d7cc3f59 --- /dev/null +++ b/mifosng-android/src/main/java/com/mifos/mifosxdroid/offline/synccenterpayloads/SyncCenterPayloadsFragment.java @@ -0,0 +1,294 @@ +package com.mifos.mifosxdroid.offline.synccenterpayloads; + +import android.content.DialogInterface; +import android.os.Bundle; +import android.support.v4.widget.SwipeRefreshLayout; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.TextView; + +import com.mifos.mifosxdroid.R; +import com.mifos.mifosxdroid.adapters.SyncCenterPayloadAdapter; +import com.mifos.mifosxdroid.core.MaterialDialog; +import com.mifos.mifosxdroid.core.MifosBaseActivity; +import com.mifos.mifosxdroid.core.MifosBaseFragment; +import com.mifos.mifosxdroid.core.util.Toaster; +import com.mifos.services.data.CenterPayload; +import com.mifos.utils.Constants; +import com.mifos.utils.PrefManager; + +import java.util.ArrayList; +import java.util.List; + +import javax.inject.Inject; + +import butterknife.BindView; +import butterknife.ButterKnife; +import butterknife.OnClick; + +public class SyncCenterPayloadsFragment extends MifosBaseFragment implements + SyncCenterPayloadsMvpView, DialogInterface.OnClickListener { + + public final String LOG_TAG = getClass().getSimpleName(); + + @BindView(R.id.rv_sync_payload) + RecyclerView rvPayloadCenter; + + @BindView(R.id.swipe_container) + SwipeRefreshLayout swipeRefreshLayout; + + @BindView(R.id.noPayloadText) + TextView mNoPayloadText; + + @BindView(R.id.noPayloadIcon) + ImageView mNoPayloadIcon; + + @BindView(R.id.ll_error) + LinearLayout llError; + + @Inject + SyncCenterPayloadsPresenter mSyncCenterPayloadsPresenter; + + @Inject + SyncCenterPayloadAdapter mSyncCenterPayloadAdapter; + + View rootView; + + List centerPayloads; + + DialogInterface.OnCancelListener onCancelListener; + + int mCenterSyncIndex = 0; + + public static SyncCenterPayloadsFragment newInstance() { + Bundle arguments = new Bundle(); + SyncCenterPayloadsFragment fragment = new SyncCenterPayloadsFragment(); + fragment.setArguments(arguments); + return fragment; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + ((MifosBaseActivity) getActivity()).getActivityComponent().inject(this); + centerPayloads = new ArrayList<>(); + setHasOptionsMenu(true); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + rootView = inflater.inflate(R.layout.fragment_syncpayload, container, false); + + ButterKnife.bind(this, rootView); + mSyncCenterPayloadsPresenter.attachView(this); + + LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity()); + mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); + rvPayloadCenter.setLayoutManager(mLayoutManager); + rvPayloadCenter.setHasFixedSize(true); + rvPayloadCenter.setAdapter(mSyncCenterPayloadAdapter); + + + /** + * Loading All Center Payloads from Database + */ + swipeRefreshLayout.setColorSchemeColors(getActivity() + .getResources().getIntArray(R.array.swipeRefreshColors)); + swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { + @Override + public void onRefresh() { + + mSyncCenterPayloadsPresenter.loadDatabaseCenterPayload(); + + if (swipeRefreshLayout.isRefreshing()) + swipeRefreshLayout.setRefreshing(false); + } + }); + + mSyncCenterPayloadsPresenter.loadDatabaseCenterPayload(); + + return rootView; + } + + /** + * Show when Database response is null or failed to fetch the center payload + * Onclick Send Fresh Request for Center Payload. + */ + @OnClick(R.id.noPayloadIcon) + public void reloadOnError() { + llError.setVisibility(View.GONE); + mSyncCenterPayloadsPresenter.loadDatabaseCenterPayload(); + } + + @Override + public void showCenterSyncResponse() { + mSyncCenterPayloadsPresenter.deleteAndUpdateCenterPayload(centerPayloads + .get(mCenterSyncIndex).getId()); + } + + @Override + public void showOfflineModeDialog() { + new MaterialDialog.Builder().init(getActivity()) + .setTitle(R.string.offline_mode) + .setMessage(R.string.dialog_message_offline_sync_alert) + .setPositiveButton(R.string.dialog_action_go_online, this) + .setNegativeButton(R.string.dialog_action_cancel, this) + .createMaterialDialog() + .show(); + } + + @Override + public void onClick(DialogInterface dialog, int which) { + switch (which) { + case DialogInterface.BUTTON_NEGATIVE: + break; + case DialogInterface.BUTTON_POSITIVE: + PrefManager.setUserStatus(Constants.USER_ONLINE); + if (centerPayloads.size() != 0) { + mCenterSyncIndex = 0; + syncCenterPayload(); + } else { + Toaster.show(rootView, + getActivity().getResources().getString(R.string.nothing_to_sync)); + } + break; + default: + break; + } + } + + @Override + public void showError(int stringId) { + llError.setVisibility(View.VISIBLE); + String message = stringId + getResources().getString(R.string.click_to_refresh); + mNoPayloadText.setText(message); + Toaster.show(rootView, stringId); + } + + /** + * This method is showing the center payload in the recyclerView. + * If Database Table have no entry then showing make recyclerView visibility gone and + * visible to the noPayloadIcon and noPayloadText to alert the user there is nothing + * to show. + * + * @param centerPayload + */ + @Override + public void showCenters(List centerPayload) { + centerPayloads = centerPayload; + if (centerPayload.size() == 0) { + llError.setVisibility(View.VISIBLE); + mNoPayloadText.setText(getActivity() + .getResources().getString(R.string.no_center_payload_to_sync)); + mNoPayloadIcon.setImageResource(R.drawable.ic_assignment_turned_in_black_24dp); + } else { + mSyncCenterPayloadAdapter.setCenterPayload(centerPayloads); + } + + } + + @Override + public void showCenterSyncFailed(String errorMessage) { + CenterPayload centerPayload = centerPayloads.get(mCenterSyncIndex); + centerPayload.setErrorMessage(errorMessage); + mSyncCenterPayloadsPresenter.updateCenterPayload(centerPayload); + + } + + @Override + public void showPayloadDeletedAndUpdatePayloads(List centers) { + mCenterSyncIndex = 0; + this.centerPayloads = centers; + mSyncCenterPayloadAdapter.setCenterPayload(centerPayloads); + if (centerPayloads.size() != 0) { + syncCenterPayload(); + } else { + llError.setVisibility(View.VISIBLE); + mNoPayloadText.setText(getActivity() + .getResources().getString(R.string.all_centers_synced)); + mNoPayloadIcon.setImageResource(R.drawable.ic_assignment_turned_in_black_24dp); + } + } + + @Override + public void showCenterPayloadUpdated(CenterPayload centerPayload) { + centerPayloads.set(mCenterSyncIndex, centerPayload); + mSyncCenterPayloadAdapter.notifyDataSetChanged(); + + mCenterSyncIndex = mCenterSyncIndex + 1; + if (centerPayloads.size() != mCenterSyncIndex) { + syncCenterPayload(); + } + } + + @Override + public void showProgressbar(boolean show) { + swipeRefreshLayout.setRefreshing(show); + if (show && mSyncCenterPayloadAdapter.getItemCount() == 0) { + showMifosProgressBar(); + swipeRefreshLayout.setRefreshing(false); + } else { + hideMifosProgressBar(); + } + } + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + inflater.inflate(R.menu.menu_sync, menu); + super.onCreateOptionsMenu(menu, inflater); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == R.id.action_sync) { + switch (PrefManager.getUserStatus()) { + case 0: + if (centerPayloads.size() != 0) { + mCenterSyncIndex = 0; + syncCenterPayload(); + } else { + Toaster.show(rootView, + getActivity().getResources().getString(R.string.nothing_to_sync)); + } + break; + case 1: + showOfflineModeDialog(); + break; + default: + break; + } + } + return super.onOptionsItemSelected(item); + } + + + public void syncCenterPayload() { + for (int i = 0; i < centerPayloads.size(); ++i) { + if (centerPayloads.get(i).getErrorMessage() == null) { + mSyncCenterPayloadsPresenter.syncCenterPayload(centerPayloads.get(i)); + mCenterSyncIndex = i; + break; + } else { + Log.d(LOG_TAG, + getActivity().getResources().getString(R.string.error_fix_before_sync) + + centerPayloads.get(i).getErrorMessage()); + } + } + } + + @Override + public void onDestroyView() { + super.onDestroyView(); + mSyncCenterPayloadsPresenter.detachView(); + } +} diff --git a/mifosng-android/src/main/java/com/mifos/mifosxdroid/offline/synccenterpayloads/SyncCenterPayloadsMvpView.java b/mifosng-android/src/main/java/com/mifos/mifosxdroid/offline/synccenterpayloads/SyncCenterPayloadsMvpView.java new file mode 100644 index 00000000000..f8b1094f5d9 --- /dev/null +++ b/mifosng-android/src/main/java/com/mifos/mifosxdroid/offline/synccenterpayloads/SyncCenterPayloadsMvpView.java @@ -0,0 +1,27 @@ +package com.mifos.mifosxdroid.offline.synccenterpayloads; + +import com.mifos.mifosxdroid.base.MvpView; +import com.mifos.services.data.CenterPayload; + +import java.util.List; + +/** + * Created by mayankjindal on 04/07/17. + */ + +public interface SyncCenterPayloadsMvpView extends MvpView { + + void showOfflineModeDialog(); + + void showCenters(List centerPayloads); + + void showCenterSyncResponse(); + + void showCenterSyncFailed(String errorMessage); + + void showPayloadDeletedAndUpdatePayloads(List centerPayloads); + + void showCenterPayloadUpdated(CenterPayload centerPayload); + + void showError(int stringId); +} diff --git a/mifosng-android/src/main/java/com/mifos/mifosxdroid/offline/synccenterpayloads/SyncCenterPayloadsPresenter.java b/mifosng-android/src/main/java/com/mifos/mifosxdroid/offline/synccenterpayloads/SyncCenterPayloadsPresenter.java new file mode 100644 index 00000000000..bb31a2e71f9 --- /dev/null +++ b/mifosng-android/src/main/java/com/mifos/mifosxdroid/offline/synccenterpayloads/SyncCenterPayloadsPresenter.java @@ -0,0 +1,146 @@ +package com.mifos.mifosxdroid.offline.synccenterpayloads; + +import com.mifos.api.datamanager.DataManagerCenter; +import com.mifos.mifosxdroid.R; +import com.mifos.mifosxdroid.base.BasePresenter; +import com.mifos.objects.response.SaveResponse; +import com.mifos.services.data.CenterPayload; +import com.mifos.utils.MFErrorParser; + +import java.util.List; + +import javax.inject.Inject; + +import rx.Observer; +import rx.Subscriber; +import rx.android.schedulers.AndroidSchedulers; +import rx.schedulers.Schedulers; +import rx.subscriptions.CompositeSubscription; + +/** + * Created by mayankjindal on 04/07/17. + */ + +public class SyncCenterPayloadsPresenter extends BasePresenter { + + private final DataManagerCenter mDataManagerCenter; + private CompositeSubscription mSubscriptions; + + @Inject + public SyncCenterPayloadsPresenter(DataManagerCenter dataManagerCenter) { + mDataManagerCenter = dataManagerCenter; + mSubscriptions = new CompositeSubscription(); + } + + @Override + public void attachView(SyncCenterPayloadsMvpView mvpView) { + super.attachView(mvpView); + } + + @Override + public void detachView() { + super.detachView(); + mSubscriptions.unsubscribe(); + } + + public void loadDatabaseCenterPayload() { + checkViewAttached(); + getMvpView().showProgressbar(true); + mSubscriptions.add(mDataManagerCenter.getAllDatabaseCenterPayload() + .observeOn(AndroidSchedulers.mainThread()) + .subscribeOn(Schedulers.io()) + .subscribe(new Subscriber>() { + @Override + public void onCompleted() { + } + + @Override + public void onError(Throwable e) { + getMvpView().showProgressbar(false); + getMvpView().showError(R.string.failed_to_load_centerpayload); + } + + @Override + public void onNext(List centerPayloads) { + getMvpView().showProgressbar(false); + getMvpView().showCenters(centerPayloads); + } + })); + } + + public void syncCenterPayload(CenterPayload centerPayload) { + checkViewAttached(); + getMvpView().showProgressbar(true);; + mSubscriptions.add(mDataManagerCenter.createCenter(centerPayload) + .observeOn(AndroidSchedulers.mainThread()) + .subscribeOn(Schedulers.io()) + .subscribe(new Observer() { + @Override + public void onCompleted() { + } + + @Override + public void onError(Throwable e) { + getMvpView().showProgressbar(false); + getMvpView().showCenterSyncFailed(MFErrorParser.errorMessage(e)); + } + + @Override + public void onNext(SaveResponse center) { + getMvpView().showProgressbar(false); + getMvpView().showCenterSyncResponse(); + } + })); + } + + public void deleteAndUpdateCenterPayload(int id) { + checkViewAttached(); + getMvpView().showProgressbar(true); + mSubscriptions.add(mDataManagerCenter.deleteAndUpdateCenterPayloads(id) + .observeOn(AndroidSchedulers.mainThread()) + .subscribeOn(Schedulers.io()) + .subscribe(new Observer>() { + @Override + public void onCompleted() { + } + + @Override + public void onError(Throwable e) { + getMvpView().showProgressbar(false); + getMvpView().showError(R.string.failed_to_update_list); + } + + @Override + public void onNext(List centerPayloads) { + getMvpView().showProgressbar(false); + getMvpView().showPayloadDeletedAndUpdatePayloads(centerPayloads); + } + })); + } + + public void updateCenterPayload(CenterPayload centerPayload) { + checkViewAttached(); + getMvpView().showProgressbar(true); + mSubscriptions.add(mDataManagerCenter.updateCenterPayload(centerPayload) + .observeOn(AndroidSchedulers.mainThread()) + .subscribeOn(Schedulers.io()) + .subscribe(new Subscriber() { + @Override + public void onCompleted() { + } + + @Override + public void onError(Throwable e) { + getMvpView().showProgressbar(false); + getMvpView().showError(R.string.failed_to_update_list); + } + + @Override + public void onNext(CenterPayload centerPayload) { + getMvpView().showProgressbar(false); + getMvpView().showCenterPayloadUpdated(centerPayload); + } + }) + ); + } +} diff --git a/mifosng-android/src/main/java/com/mifos/mifosxdroid/offline/syncclientpayloads/SyncClientPayloadActivity.java b/mifosng-android/src/main/java/com/mifos/mifosxdroid/offline/syncclientpayloads/SyncClientPayloadActivity.java index 6e68c8d1881..d13e3593def 100644 --- a/mifosng-android/src/main/java/com/mifos/mifosxdroid/offline/syncclientpayloads/SyncClientPayloadActivity.java +++ b/mifosng-android/src/main/java/com/mifos/mifosxdroid/offline/syncclientpayloads/SyncClientPayloadActivity.java @@ -18,4 +18,4 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { showBackButton(); replaceFragment(SyncClientPayloadsFragment.newInstance(), false, R.id.container); } -} +} \ No newline at end of file diff --git a/mifosng-android/src/main/java/com/mifos/mifosxdroid/online/createnewcenter/CreateNewCenterFragment.java b/mifosng-android/src/main/java/com/mifos/mifosxdroid/online/createnewcenter/CreateNewCenterFragment.java index 6e65fbfe878..83ad5e6447b 100755 --- a/mifosng-android/src/main/java/com/mifos/mifosxdroid/online/createnewcenter/CreateNewCenterFragment.java +++ b/mifosng-android/src/main/java/com/mifos/mifosxdroid/online/createnewcenter/CreateNewCenterFragment.java @@ -10,7 +10,6 @@ */ import android.app.Activity; -import android.content.Intent; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.DialogFragment; @@ -36,14 +35,13 @@ import com.mifos.mifosxdroid.R; import com.mifos.mifosxdroid.core.MifosBaseActivity; import com.mifos.mifosxdroid.core.MifosBaseFragment; -import com.mifos.mifosxdroid.online.CentersActivity; import com.mifos.mifosxdroid.uihelpers.MFDatePicker; import com.mifos.objects.organisation.Office; import com.mifos.objects.response.SaveResponse; import com.mifos.services.data.CenterPayload; -import com.mifos.utils.Constants; import com.mifos.utils.DateHelper; import com.mifos.utils.FragmentConstants; +import com.mifos.utils.MifosResponseHandler; import com.mifos.utils.ValidationUtil; import java.util.ArrayList; @@ -253,12 +251,9 @@ public void onNothingSelected(AdapterView parent) { @Override public void centerCreatedSuccessfully(SaveResponse saveResponse) { - Toast.makeText(getActivity(), R.string.center_created_successfully, + Toast.makeText(getActivity(), "Center " + MifosResponseHandler.getResponse(), Toast.LENGTH_LONG).show(); getActivity().getSupportFragmentManager().popBackStack(); - Intent intent = new Intent(getActivity(), CentersActivity.class); - intent.putExtra(Constants.CENTER_ID, saveResponse.getGroupId()); - startActivity(intent); } @Override diff --git a/mifosng-android/src/main/java/com/mifos/services/data/CenterPayload.java b/mifosng-android/src/main/java/com/mifos/services/data/CenterPayload.java index c0c4582d131..f9ff76cf246 100755 --- a/mifosng-android/src/main/java/com/mifos/services/data/CenterPayload.java +++ b/mifosng-android/src/main/java/com/mifos/services/data/CenterPayload.java @@ -4,18 +4,63 @@ */ package com.mifos.services.data; +import android.os.Parcel; +import android.os.Parcelable; + +import com.mifos.api.local.MifosBaseModel; +import com.mifos.api.local.MifosDatabase; +import com.raizlabs.android.dbflow.annotation.Column; +import com.raizlabs.android.dbflow.annotation.ModelContainer; +import com.raizlabs.android.dbflow.annotation.PrimaryKey; +import com.raizlabs.android.dbflow.annotation.Table; + /** * Created by nellyk on 1/22/2016. */ -public class CenterPayload { +@Table(database = MifosDatabase.class) +@ModelContainer +public class CenterPayload extends MifosBaseModel implements Parcelable { + + @PrimaryKey(autoincrement = true) + transient int id; + + @Column + transient String errorMessage; + @Column String dateFormat; + + @Column String locale; + + @Column private String name; + + @Column private int officeId; + + @Column private boolean active; + + @Column private String activationDate; + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + public String getDateFormat() { return dateFormat; } @@ -65,5 +110,45 @@ public void setActivationDate(String activationDate) { this.activationDate = activationDate; } + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeString(this.dateFormat); + dest.writeString(this.locale); + dest.writeString(this.name); + dest.writeValue(this.officeId); + dest.writeValue(this.active); + dest.writeString(this.activationDate); + } + + public CenterPayload() { + } + + protected CenterPayload(Parcel in) { + this.dateFormat = in.readString(); + this.locale = in.readString(); + this.name = in.readString(); + this.officeId = (Integer) in.readValue(Integer.class.getClassLoader()); + this.active = (Boolean) in.readValue(Boolean.class.getClassLoader()); + this.activationDate = in.readString(); + } + + public static final Parcelable.Creator CREATOR = + new Parcelable.Creator() { + @Override + public CenterPayload createFromParcel(Parcel source) { + return new CenterPayload(source); + } + + @Override + public CenterPayload[] newArray(int size) { + return new CenterPayload[size]; + } + }; + } diff --git a/mifosng-android/src/main/res/layout/item_sync_center.xml b/mifosng-android/src/main/res/layout/item_sync_center.xml new file mode 100644 index 00000000000..0b0b7105421 --- /dev/null +++ b/mifosng-android/src/main/res/layout/item_sync_center.xml @@ -0,0 +1,155 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/mifosng-android/src/main/res/values/strings.xml b/mifosng-android/src/main/res/values/strings.xml index 7261737cdc4..eb9b13a1529 100755 --- a/mifosng-android/src/main/res/values/strings.xml +++ b/mifosng-android/src/main/res/values/strings.xml @@ -470,6 +470,7 @@ Server Sync Error Clients Groups + Centers LoanRepayments SavingsAccountTransaction Payloads : @@ -478,10 +479,12 @@ There is No LoanRepayment to Sync There is No Group Payload to Sync There is No Client Payload to Sync + There is No Center Payload to Sync here is No Transaction to Sync No More Clients To Sync All LoanRepayment have been Sync All Groups have been Sync + All Centers have been Sync All Clients have been Sync \n Click to Refresh Syncing Client @@ -543,6 +546,7 @@ Failed to load ClientPayload Failed to load GroupPayload + Failed to load CenterPayload Failed to load LoanRepayment Failed Update List Failed to Load PaymentTypeOptions @@ -653,6 +657,7 @@ Notes Empty Notes Failed to Fetch Notes + SyncClientPayloadActivity \ No newline at end of file