| package org.apache.fineract.ui.base; |
| |
| import android.app.ProgressDialog; |
| import android.content.Context; |
| import android.os.Bundle; |
| import android.support.v4.app.Fragment; |
| import android.support.v4.app.FragmentManager; |
| import android.support.v4.app.FragmentTransaction; |
| import android.support.v7.app.AppCompatActivity; |
| import android.support.v7.widget.Toolbar; |
| import android.view.MenuItem; |
| import android.view.View; |
| import android.view.inputmethod.InputMethodManager; |
| |
| import org.apache.fineract.MifosApplication; |
| import org.apache.fineract.R; |
| import org.apache.fineract.injection.component.ActivityComponent; |
| import org.apache.fineract.injection.component.ConfigPersistentComponent; |
| import org.apache.fineract.injection.component.DaggerConfigPersistentComponent; |
| import org.apache.fineract.injection.module.ActivityModule; |
| |
| import java.util.HashMap; |
| import java.util.Map; |
| import java.util.concurrent.atomic.AtomicLong; |
| |
| |
| /** |
| * Abstract activity that every other Activity in this application must implement. It handles |
| * creation of Dagger components and makes sure that instances of ConfigPersistentComponent survive |
| * across configuration changes. |
| */ |
| public class MifosBaseActivity extends AppCompatActivity implements BaseActivityCallback { |
| |
| private static final String KEY_ACTIVITY_ID = "KEY_ACTIVITY_ID"; |
| private static final AtomicLong NEXT_ID = new AtomicLong(0); |
| private static final Map<Long, ConfigPersistentComponent> sComponentsMap = new HashMap<>(); |
| |
| protected Toolbar toolbar; |
| private ActivityComponent activityComponent; |
| private ProgressDialog progress; |
| private long activityId; |
| |
| @Override |
| public void setContentView(int layoutResID) { |
| super.setContentView(layoutResID); |
| toolbar = (Toolbar) findViewById(R.id.toolbar); |
| if (toolbar != null) { |
| setSupportActionBar(toolbar); |
| } |
| } |
| |
| @Override |
| protected void onCreate(Bundle savedInstanceState) { |
| super.onCreate(savedInstanceState); |
| |
| // Create the ActivityComponent and reuses cached ConfigPersistentComponent if this is |
| // being called after a configuration change. |
| activityId = savedInstanceState != null ? |
| savedInstanceState.getLong(KEY_ACTIVITY_ID) : NEXT_ID.getAndIncrement(); |
| ConfigPersistentComponent configPersistentComponent; |
| if (!sComponentsMap.containsKey(activityId)) { |
| configPersistentComponent = DaggerConfigPersistentComponent.builder() |
| .applicationComponent(MifosApplication.get(this).getComponent()) |
| .build(); |
| sComponentsMap.put(activityId, configPersistentComponent); |
| } else { |
| configPersistentComponent = sComponentsMap.get(activityId); |
| } |
| activityComponent = configPersistentComponent.activityComponent(new ActivityModule(this)); |
| } |
| |
| @Override |
| protected void onSaveInstanceState(Bundle outState) { |
| super.onSaveInstanceState(outState); |
| outState.putLong(KEY_ACTIVITY_ID, activityId); |
| } |
| |
| @Override |
| protected void onDestroy() { |
| if (!isChangingConfigurations()) { |
| sComponentsMap.remove(activityId); |
| } |
| super.onDestroy(); |
| } |
| |
| public ActivityComponent getActivityComponent() { |
| return activityComponent; |
| } |
| |
| public void setActionBarTitle(String title) { |
| if (getSupportActionBar() != null && getTitle() != null) { |
| setTitle(title); |
| } |
| } |
| |
| public void showBackButton() { |
| if (getSupportActionBar() != null) { |
| getSupportActionBar().setHomeButtonEnabled(true); |
| getSupportActionBar().setDisplayHomeAsUpEnabled(true); |
| } |
| } |
| |
| public void setActionBarTitle(int title) { |
| setActionBarTitle(getResources().getString(title)); |
| } |
| |
| public Toolbar getToolbar() { |
| return toolbar; |
| } |
| |
| @Override |
| public boolean onOptionsItemSelected(MenuItem item) { |
| switch (item.getItemId()) { |
| case android.R.id.home: |
| onBackPressed(); |
| return true; |
| default: |
| return super.onOptionsItemSelected(item); |
| } |
| } |
| |
| @Override |
| public void showMifosProgressDialog(String message) { |
| if (progress == null) { |
| progress = new ProgressDialog(this, ProgressDialog.STYLE_SPINNER); |
| progress.setCancelable(false); |
| } |
| progress.setMessage(message); |
| progress.show(); |
| } |
| |
| @Override |
| public void setToolbarTitle(String title) { |
| setActionBarTitle(title); |
| } |
| |
| public void hideKeyboard(View view) { |
| InputMethodManager inputManager = (InputMethodManager) this |
| .getSystemService(Context.INPUT_METHOD_SERVICE); |
| inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager |
| .RESULT_UNCHANGED_SHOWN); |
| } |
| |
| @Override |
| public void hideMifosProgressDialog() { |
| if (progress != null && progress.isShowing()) |
| progress.dismiss(); |
| } |
| |
| @Override |
| public void logout() { |
| |
| } |
| |
| /** |
| * Replace Fragment in FrameLayout Container. |
| * |
| * @param fragment Fragment |
| * @param addToBackStack Add to BackStack |
| * @param containerId Container Id |
| */ |
| public void replaceFragment(Fragment fragment, boolean addToBackStack, int containerId) { |
| invalidateOptionsMenu(); |
| String backStateName = fragment.getClass().getName(); |
| boolean fragmentPopped = getSupportFragmentManager().popBackStackImmediate(backStateName, |
| 0); |
| |
| if (!fragmentPopped && getSupportFragmentManager().findFragmentByTag(backStateName) == |
| null) { |
| FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); |
| transaction.replace(containerId, fragment, backStateName); |
| if (addToBackStack) { |
| transaction.addToBackStack(backStateName); |
| } |
| transaction.commit(); |
| } |
| } |
| |
| public void clearFragmentBackStack() { |
| FragmentManager fm = getSupportFragmentManager(); |
| int backStackCount = getSupportFragmentManager().getBackStackEntryCount(); |
| for (int i = 0; i < backStackCount; i++) { |
| int backStackId = getSupportFragmentManager().getBackStackEntryAt(i).getId(); |
| fm.popBackStack(backStackId, FragmentManager.POP_BACK_STACK_INCLUSIVE); |
| } |
| } |
| public int stackCount() { |
| return getSupportFragmentManager().getBackStackEntryCount(); |
| } |
| } |