opensrp-anc/src/main/java/org/smartregister/anc/library/activity/BaseProfileActivity.java
package org.smartregister.anc.library.activity;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.Toolbar;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.smartregister.anc.library.AncLibrary;
import org.smartregister.anc.library.R;
import org.smartregister.anc.library.contract.ProfileContract;
import org.smartregister.anc.library.event.ClientDetailsFetchedEvent;
import org.smartregister.anc.library.event.PatientRemovedEvent;
import org.smartregister.anc.library.task.FetchProfileDataTask;
import org.smartregister.anc.library.util.ANCJsonFormUtils;
import org.smartregister.anc.library.util.ConstantsUtils;
import org.smartregister.anc.library.util.Utils;
import org.smartregister.repository.AllSharedPreferences;
import org.smartregister.view.activity.SecuredActivity;
import timber.log.Timber;
//com.google.android.material.appbar.AppBarLayout;
//import com.google.android.material.appbar.CollapsingToolbarLayout;
/**
* Created by ndegwamartin on 16/07/2018.
*/
public abstract class BaseProfileActivity extends SecuredActivity
implements AppBarLayout.OnOffsetChangedListener, View.OnClickListener {
protected CollapsingToolbarLayout collapsingToolbarLayout;
protected ActionBar actionBar;
protected String womanName;
protected AppBarLayout appBarLayout;
protected ProgressDialog progressDialog;
protected ProfileContract.Presenter mProfilePresenter;
private boolean appBarTitleIsShown = true;
private int appBarLayoutScrollRange = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getViewLayoutId());
findViewById(R.id.btn_profile_registration_info).setOnClickListener(this);
Toolbar toolbar = findViewById(R.id.collapsing_toolbar);
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
appBarLayout = findViewById(R.id.collapsing_toolbar_appbarlayout);
if (appBarLayout != null) {
// Set collapsing tool bar title.
collapsingToolbarLayout = appBarLayout.findViewById(R.id.collapsing_toolbar_layout);
appBarLayout.addOnOffsetChangedListener(this);
}
}
@Override
public void onResume() {
super.onResume();
registerEventBus();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//super.onActivityResult(requestCode, resultCode, data);
AllSharedPreferences allSharedPreferences = AncLibrary.getInstance().getContext().allSharedPreferences();
if (requestCode == ANCJsonFormUtils.REQUEST_CODE_GET_JSON && resultCode == Activity.RESULT_OK) {
mProfilePresenter.processFormDetailsSave(data, allSharedPreferences);
}
}
@Override
public void onPause() {
EventBus.getDefault().unregister(this);
super.onPause();
}
protected void registerEventBus() {
EventBus.getDefault().register(this);
}
protected abstract int getViewLayoutId();
@Override
public void onClick(View view) {
if (view.getId() == R.id.btn_profile_registration_info) {
String baseEntityId = getIntent().getStringExtra(ConstantsUtils.IntentKeyUtils.BASE_ENTITY_ID);
new FetchProfileDataTask(true).execute(baseEntityId);
}
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void startFormForEdit(ClientDetailsFetchedEvent event) {
if (event != null && event.isEditMode()) {
String formMetadata = ANCJsonFormUtils.getAutoPopulatedJsonEditRegisterFormString(this, event.getWomanClient());
try {
ANCJsonFormUtils.startFormForEdit(this, ANCJsonFormUtils.REQUEST_CODE_GET_JSON, formMetadata);
} catch (Exception e) {
Timber.e(e, "%s --> startFormForEdit()", this.getClass().getCanonicalName());
}
}
}
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void refreshProfileTopSection(ClientDetailsFetchedEvent event) {
if (event != null && !event.isEditMode()) {
Utils.removeStickyEvent(event);
mProfilePresenter.refreshProfileTopSection(event.getWomanClient());
}
}
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void removePatient(PatientRemovedEvent event) {
if (event != null) {
Utils.removeStickyEvent(event);
hideProgressDialog();
finish();
}
}
public void hideProgressDialog() {
if (progressDialog != null) {
progressDialog.dismiss();
}
}
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (appBarLayoutScrollRange == -1) {
appBarLayoutScrollRange = appBarLayout.getTotalScrollRange();
}
if (appBarLayoutScrollRange + verticalOffset == 0) {
collapsingToolbarLayout.setTitle(womanName);
appBarTitleIsShown = true;
} else if (appBarTitleIsShown) {
collapsingToolbarLayout.setTitle(" ");
appBarTitleIsShown = false;
}
}
public void showProgressDialog(int saveMessageStringIdentifier) {
if (progressDialog == null) {
progressDialog = new ProgressDialog(this);
progressDialog.setCancelable(false);
progressDialog.setTitle(getString(saveMessageStringIdentifier));
progressDialog.setMessage(getString(R.string.please_wait_message));
setProgressDialog(progressDialog);
}
if (!isFinishing()) progressDialog.show();
}
public ProgressDialog getProgressDialog() {
return progressDialog;
}
public void setProgressDialog(ProgressDialog progressDialog) {
this.progressDialog = progressDialog;
}
}