Skip to content

Instantly share code, notes, and snippets.

@mnisbet13
Last active November 28, 2018 15:22
Show Gist options
  • Save mnisbet13/a4c3942d4c07a026483e to your computer and use it in GitHub Desktop.
Save mnisbet13/a4c3942d4c07a026483e to your computer and use it in GitHub Desktop.
package com.thredup.android.fragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.Toolbar;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.StyleSpan;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import com.thredup.android.R;
import com.thredup.android.domain.Constants;
import com.thredup.android.layout.CustomTypefaceSpan;
import com.thredup.android.util.ThredUpUtil;
public class EmbeddedDialogFragment extends DialogFragment {
private Toolbar mToolbar;
public static EmbeddedDialogFragment newInstance() {
EmbeddedDialogFragment frag = new EmbeddedDialogFragment();
return frag;
}
@Override
public int getLayoutResources() {
return R.layout.embedded_dialog_layout;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(android.support.v4.app.DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_DeviceDefault_Light_Dialog);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mToolbar = (Toolbar) getView().findViewById(R.id.toolbar);
mToolbar = ThredUpUtil.makeWhiteToolbar(getContext(), mToolbar);
mToolbar.hideOverflowMenu();
mToolbar.setTitleTextColor(ContextCompat.getColor(getContext(), R.color.black));
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
if (savedInstanceState != null && TextUtils.isEmpty(savedInstanceState.getString("current_fragment"))) {
String title = savedInstanceState.getString("current_fragment");
mToolbar.setTitle(title);
mToolbar.setNavigationIcon(R.drawable.ic_nav_back_green_selector);
} else {
String title = getString(R.string.account_info);
mToolbar.setTitle(title);
mToolbar.setNavigationIcon(R.drawable.ic_close_selector);
BaseDialogFragment fragment = AccountInfoFragment.newInstance();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_content, fragment, "account_info");
transaction.commit();
}
if (getDialog() != null) {
getDialog().setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
onBackPressed();
}
return true;
}
});
}
}
@Override
public void onResume() {
super.onResume();
getDialog().getWindow()
.setLayout(getResources().getDimensionPixelSize(R.dimen.acct_dialog_width),
getResources().getDimensionPixelSize(R.dimen.acct_dialog_height));
}
public void replaceFragment(DialogFragment replacementFragment, String tag, String pageTitle) {
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.slide_left_from_right, android.R.anim.fade_out, android.R.anim.fade_in,
R.anim.slide_out_right_from_left);
transaction.replace(R.id.fragment_content, replacementFragment, tag);
transaction.addToBackStack(tag);
transaction.commit();
if (!TextUtils.isEmpty(pageTitle)) {
mToolbar.setTitle(pageTitle.toString());
mToolbar.setNavigationIcon(R.drawable.ic_nav_back_green_selector);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
FragmentManager fragmentManager = getChildFragmentManager();
String fragmentName;
if (fragmentManager.getBackStackEntryCount() >= 1) {
fragmentName = fragmentManager.getBackStackEntryAt(fragmentManager.getBackStackEntryCount() - 1).getName();
outState.putString("current_fragment", fragmentName);
}
}
public void onBackPressed() {
if (getChildFragmentManager().popBackStackImmediate()) {
BaseDialogFragment bdf = (BaseDialogFragment) getChildFragmentManager().getFragments()
.get(getChildFragmentManager().getFragments().size() - 1);
String title = getString(R.string.account_info);
if (bdf == null && getChildFragmentManager().findFragmentByTag(getString(R.string.order_history)) != null
&& getChildFragmentManager().findFragmentByTag(getString(R.string.order_history)).isVisible()) {
title = getString(R.string.order_history);
}
if (bdf != null) {
title = getNewTitle(bdf.getTitle());
}
mToolbar.setTitle(title);
if (title.equals(getString(R.string.account_info))) {
mToolbar.setNavigationIcon(R.drawable.ic_close_selector);
}
} else {
EmbeddedDialogFragment.this.dismiss();
}
}
private String getNewTitle(String title) {
// Maps the old title to the new title based on the structure of the view heirarchy
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment