mirror of
https://github.com/sudoxnym/habitica-android.git
synced 2026-05-25 07:06:01 +00:00
Challenges - back button history on tablayout
This commit is contained in:
parent
a5cee4fbbe
commit
98554a7a8b
3 changed files with 61 additions and 18 deletions
|
|
@ -11,6 +11,7 @@ import com.habitrpg.android.habitica.HabiticaApplication;
|
|||
import com.habitrpg.android.habitica.R;
|
||||
import com.habitrpg.android.habitica.components.AppComponent;
|
||||
import com.habitrpg.android.habitica.ui.AvatarWithBarsViewModel;
|
||||
import com.habitrpg.android.habitica.ui.fragments.BaseFragment;
|
||||
import com.habitrpg.android.habitica.ui.fragments.social.challenges.ChallengesOverviewFragment;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
|
@ -31,6 +32,7 @@ public class ChallengeOverviewActivity extends BaseActivity {
|
|||
@Inject
|
||||
public APIHelper apiHelper;
|
||||
|
||||
private ChallengesOverviewFragment overviewFragment;
|
||||
|
||||
@Override
|
||||
protected int getLayoutResId() {
|
||||
|
|
@ -45,19 +47,19 @@ public class ChallengeOverviewActivity extends BaseActivity {
|
|||
|
||||
getSupportActionBar().setTitle(R.string.challenges);
|
||||
|
||||
ChallengesOverviewFragment fragment = new ChallengesOverviewFragment();
|
||||
fragment.setTabLayout(detail_tabs);
|
||||
fragment.setUser(HabiticaApplication.User);
|
||||
overviewFragment = new ChallengesOverviewFragment();
|
||||
overviewFragment.setTabLayout(detail_tabs);
|
||||
overviewFragment.setUser(HabiticaApplication.User);
|
||||
|
||||
AvatarWithBarsViewModel avatarInHeader = new AvatarWithBarsViewModel(this, avatar_with_bars);
|
||||
avatarInHeader.updateData(HabiticaApplication.User);
|
||||
|
||||
if (getSupportFragmentManager().getFragments() == null) {
|
||||
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, fragment).commitAllowingStateLoss();
|
||||
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, overviewFragment).commitAllowingStateLoss();
|
||||
} else {
|
||||
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
|
||||
transaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out, android.R.anim.fade_in, android.R.anim.fade_out);
|
||||
transaction.replace(R.id.fragment_container, fragment).addToBackStack(null).commitAllowingStateLoss();
|
||||
transaction.replace(R.id.fragment_container, overviewFragment).addToBackStack(null).commitAllowingStateLoss();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -74,6 +76,8 @@ public class ChallengeOverviewActivity extends BaseActivity {
|
|||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
finish();
|
||||
if(!overviewFragment.onHandleBackPressed()){
|
||||
finish();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,7 +43,6 @@ public class ChallengeListFragment extends BaseMainFragment implements View.OnCl
|
|||
super.onCreate(savedInstance);
|
||||
|
||||
challengeAdapter = new ChallengesListViewAdapter(viewUserChallengesOnly);
|
||||
|
||||
}
|
||||
|
||||
public void setViewUserChallengesOnly(boolean only) {
|
||||
|
|
|
|||
|
|
@ -4,8 +4,12 @@ import android.app.AlertDialog;
|
|||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentPagerAdapter;
|
||||
import android.support.v4.app.FragmentStatePagerAdapter;
|
||||
import android.support.v4.view.PagerAdapter;
|
||||
import android.support.v4.view.ViewPager;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
|
|
@ -30,6 +34,8 @@ import com.raizlabs.android.dbflow.sql.language.Select;
|
|||
import org.greenrobot.eventbus.EventBus;
|
||||
import org.greenrobot.eventbus.Subscribe;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.OnClick;
|
||||
|
|
@ -37,6 +43,10 @@ import butterknife.OnClick;
|
|||
public class ChallengesOverviewFragment extends BaseMainFragment {
|
||||
|
||||
public ViewPager viewPager;
|
||||
public FragmentStatePagerAdapter statePagerAdapter;
|
||||
private Stack<Integer> pageHistory;
|
||||
private boolean saveToHistory;
|
||||
int currentPage;
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
|
|
@ -57,6 +67,8 @@ public class ChallengesOverviewFragment extends BaseMainFragment {
|
|||
availableChallengesFragment.setUser(this.user);
|
||||
availableChallengesFragment.setViewUserChallengesOnly(false);
|
||||
|
||||
pageHistory = new Stack<Integer>();
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
|
|
@ -71,7 +83,7 @@ public class ChallengesOverviewFragment extends BaseMainFragment {
|
|||
public void setViewPagerAdapter() {
|
||||
android.support.v4.app.FragmentManager fragmentManager = getChildFragmentManager();
|
||||
|
||||
viewPager.setAdapter(new FragmentPagerAdapter(fragmentManager) {
|
||||
statePagerAdapter = new FragmentStatePagerAdapter(fragmentManager) {
|
||||
|
||||
@Override
|
||||
public Fragment getItem(int position) {
|
||||
|
|
@ -102,7 +114,27 @@ public class ChallengesOverviewFragment extends BaseMainFragment {
|
|||
}
|
||||
return "";
|
||||
}
|
||||
};
|
||||
viewPager.setAdapter(statePagerAdapter);
|
||||
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
|
||||
|
||||
@Override
|
||||
public void onPageSelected(int newPageId) {
|
||||
if (saveToHistory)
|
||||
pageHistory.push(Integer.valueOf(currentPage));
|
||||
|
||||
currentPage = newPageId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageScrolled(int arg0, float arg1, int arg2) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageScrollStateChanged(int arg0) {
|
||||
}
|
||||
});
|
||||
saveToHistory = true;
|
||||
|
||||
if (tabLayout != null && viewPager != null) {
|
||||
tabLayout.setupWithViewPager(viewPager);
|
||||
|
|
@ -110,7 +142,7 @@ public class ChallengesOverviewFragment extends BaseMainFragment {
|
|||
}
|
||||
|
||||
@Subscribe
|
||||
public void onEvent(ShowChallengeTasksCommand cmd){
|
||||
public void onEvent(ShowChallengeTasksCommand cmd) {
|
||||
|
||||
View dialogLayout = HabiticaApplication.currentActivity.getLayoutInflater().inflate(R.layout.dialog_challenge_detail, null);
|
||||
|
||||
|
|
@ -124,6 +156,18 @@ public class ChallengesOverviewFragment extends BaseMainFragment {
|
|||
challegeDetailDialogHolder.bind(builder.show(), apiHelper, user, challenge);
|
||||
}
|
||||
|
||||
public boolean onHandleBackPressed() {
|
||||
if (!pageHistory.empty()) {
|
||||
saveToHistory = false;
|
||||
viewPager.setCurrentItem(pageHistory.pop().intValue());
|
||||
saveToHistory = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public class ChallegeDetailDialogHolder {
|
||||
|
||||
@BindView(R.id.challenge_not_joined_header)
|
||||
|
|
@ -166,19 +210,16 @@ public class ChallengesOverviewFragment extends BaseMainFragment {
|
|||
ButterKnife.bind(this, view);
|
||||
}
|
||||
|
||||
public void bind(AlertDialog dialog, APIHelper apiHelper, HabitRPGUser user, Challenge challenge){
|
||||
public void bind(AlertDialog dialog, APIHelper apiHelper, HabitRPGUser user, Challenge challenge) {
|
||||
this.dialog = dialog;
|
||||
this.apiHelper = apiHelper;
|
||||
this.user = user;
|
||||
this.challenge = challenge;
|
||||
|
||||
if(challenge.user_id == null || challenge.user_id.isEmpty())
|
||||
{
|
||||
if (challenge.user_id == null || challenge.user_id.isEmpty()) {
|
||||
notJoinedHeader.setVisibility(View.VISIBLE);
|
||||
joinButton.setVisibility(View.VISIBLE);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
joinedHeader.setVisibility(View.VISIBLE);
|
||||
leaveButton.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
|
@ -187,8 +228,8 @@ public class ChallengesOverviewFragment extends BaseMainFragment {
|
|||
challengeDescription.setText(challenge.description);
|
||||
challengeLeader.setText(challenge.leaderName);
|
||||
|
||||
gem_amount.setText(challenge.prize+"");
|
||||
member_count.setText(challenge.memberCount+"");
|
||||
gem_amount.setText(challenge.prize + "");
|
||||
member_count.setText(challenge.memberCount + "");
|
||||
}
|
||||
|
||||
@OnClick(R.id.challenge_leader)
|
||||
|
|
@ -248,7 +289,6 @@ public class ChallengesOverviewFragment extends BaseMainFragment {
|
|||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public String customTitle() {
|
||||
return getString(R.string.challenges);
|
||||
|
|
|
|||
Loading…
Reference in a new issue