Show More-Button / Leave in ChallengeDetails Activity / refactoring

This commit is contained in:
Negue 2017-01-04 20:18:50 +01:00
parent 44d2486e8d
commit c4de096a00
14 changed files with 383 additions and 251 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 580 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 436 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 658 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 928 B

View file

@ -49,10 +49,7 @@
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"
android:layout_marginLeft="26dp"
android:layout_marginStart="26dp"
android:layout_marginRight="23dp"
android:layout_marginEnd="23dp"
android:layout_marginTop="?attr/actionBarSize"
android:adjustViewBounds="true"
android:fitsSystemWindows="true"
@ -61,82 +58,115 @@
android:orientation="vertical"
app:layout_collapseMode="parallax">
<net.pherth.android.emoji_library.EmojiTextView
android:id="@+id/challenge_name"
style="@style/Headline"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="2"
android:textColor="@color/textColorDark" />
android:layout_marginEnd="23dp"
android:layout_marginLeft="26dp"
android:layout_marginRight="23dp"
android:layout_marginStart="26dp"
android:orientation="vertical">
<net.pherth.android.emoji_library.EmojiTextView
android:id="@+id/challenge_description"
style="@style/Body2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="3"
android:textColor="@color/textColorDark" />
<net.pherth.android.emoji_library.EmojiTextView
android:id="@+id/challenge_name"
style="@style/Headline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="2"
android:textColor="@color/textColorDark" />
<net.pherth.android.emoji_library.EmojiTextView
android:id="@+id/challenge_description"
style="@style/Body2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="4"
android:textColor="@color/textColorDark" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:weightSum="1.0">
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_gravity="right"
android:layout_marginEnd="23dp"
android:layout_marginRight="23dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="10dp"
android:orientation="horizontal">
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/gem_prize_layout">
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="36dp"
android:src="@drawable/ic_header_gem" />
<TextView
android:id="@+id/gem_amount"
style="@style/Body1"
<Button
android:id="@+id/btn_show_more"
style="@style/Body1_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="15"
android:textColor="@color/textColorDark" />
android:gravity="left|center"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:text="Show more"
android:textAllCaps="false"
android:textColor="#d0c1ff" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="17dp"
android:layout_marginStart="17dp"
android:gravity="center">
android:layout_weight="1"
android:gravity="right"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="36dp"
android:src="@drawable/participants_light" />
<TextView
android:id="@+id/challenge_member_count"
style="@style/Body1"
<LinearLayout
android:id="@+id/gem_prize_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="15"
android:textColor="@color/textColorDark" />
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="36dp"
android:src="@drawable/ic_header_gem" />
<TextView
android:id="@+id/gem_amount"
style="@style/Body1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/textColorDark"
tools:text="15" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="17dp"
android:layout_marginStart="17dp"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="36dp"
android:src="@drawable/participants_light" />
<TextView
android:id="@+id/challenge_member_count"
style="@style/Body1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/textColorDark"
tools:text="15" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<android.support.v7.widget.Toolbar

View file

@ -0,0 +1,13 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.habitrpg.android.habitica.TaskActivity">
<item
android:id="@+id/action_leave"
android:icon="@drawable/leave_light"
android:title="@string/leave"
app:showAsAction="always"
android:actionViewClass="android.widget.ImageButton"/>
</menu>

View file

@ -38,6 +38,14 @@
<item name="android:letterSpacing" tools:targetApi="lollipop">0.035</item>
</style>
<style name="Body1_Button" parent="@style/Widget.AppCompat.Button.Borderless">
<item name="android:fontFamily" tools:targetApi="jelly_bean">
@string/font_family_medium
</item>
<item name="android:textSize">14sp</item>
<item name="android:letterSpacing" tools:targetApi="lollipop">0.035</item>
</style>
<style name="Title2">
<item name="android:fontFamily" tools:targetApi="jelly_bean">
@string/font_family_condensed

View file

@ -0,0 +1,9 @@
package com.habitrpg.android.habitica.events.commands;
public class ShowChallengeDetailActivityCommand {
public String challengeId;
public ShowChallengeDetailActivityCommand(String challengeId){
this.challengeId = challengeId;
}
}

View file

@ -1,9 +1,11 @@
package com.habitrpg.android.habitica.events.commands;
public class ShowChallengeTasksCommand {
public class ShowChallengeDetailDialogCommand {
public String challengeId;
public ShowChallengeTasksCommand(String challengeId){
public ShowChallengeDetailDialogCommand(String challengeId){
this.challengeId = challengeId;
}
}

View file

@ -1,10 +1,14 @@
package com.habitrpg.android.habitica.ui.activities;
import android.app.AlertDialog;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
@ -13,9 +17,12 @@ import com.habitrpg.android.habitica.APIHelper;
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.fragments.social.challenges.ChallegeDetailDialogHolder;
import com.habitrpg.android.habitica.ui.fragments.social.challenges.ChallengeTasksFragment;
import com.habitrpg.android.habitica.ui.helpers.MarkdownParser;
import com.magicmicky.habitrpgwrapper.lib.models.Challenge;
import com.raizlabs.android.dbflow.sql.builder.Condition;
import com.raizlabs.android.dbflow.sql.language.Select;
import net.pherth.android.emoji_library.EmojiParser;
import net.pherth.android.emoji_library.EmojiTextView;
@ -24,6 +31,7 @@ import javax.inject.Inject;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class ChallengeDetailActivity extends BaseActivity {
@ -40,11 +48,20 @@ public class ChallengeDetailActivity extends BaseActivity {
private ChallengeViewHolder challengeViewHolder;
private Challenge challenge;
@Override
protected int getLayoutResId() {
return R.layout.activity_challenge_detail;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_challenge_details, menu);
return true;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -53,6 +70,7 @@ public class ChallengeDetailActivity extends BaseActivity {
getSupportActionBar().setTitle(R.string.challenge_details);
Bundle extras = getIntent().getExtras();
String challengeId = extras.getString(CHALLENGE_ID);
@ -70,12 +88,10 @@ public class ChallengeDetailActivity extends BaseActivity {
transaction.replace(R.id.fragment_container, fragment).addToBackStack(null).commitAllowingStateLoss();
}
apiHelper.apiService.getChallenge(challengeId)
.compose(apiHelper.configureApiCallObserver())
.subscribe(challenge -> challengeViewHolder.bind(challenge), throwable -> {
});
challenge = new Select().from(Challenge.class).where(Condition.column("id").is(challengeId)).querySingle();
challengeViewHolder = new ChallengeViewHolder(findViewById(R.id.challenge_header));
challengeViewHolder.bind(challenge);
}
@Override
@ -83,7 +99,7 @@ public class ChallengeDetailActivity extends BaseActivity {
component.inject(this);
}
public static class ChallengeViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public class ChallengeViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.challenge_name)
EmojiTextView challengeName;
@ -123,9 +139,43 @@ public class ChallengeDetailActivity extends BaseActivity {
}
}
@Override
public void onClick(View view) {
@OnClick(R.id.btn_show_more)
public void onShowMore() {
ChallegeDetailDialogHolder.showDialog(ChallengeDetailActivity.this, ChallengeDetailActivity.this.apiHelper,
HabiticaApplication.User, challenge,
challenge1 -> {
},
challenge1 -> ChallengeDetailActivity.this.onBackPressed());
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_leave:
new AlertDialog.Builder(this)
.setTitle(this.getString(R.string.challenge_leave_title))
.setMessage(String.format(this.getString(R.string.challenge_leave_text), challenge.name))
.setPositiveButton(this.getString(R.string.yes), (dialog, which) -> {
this.apiHelper.apiService.leaveChallenge(challenge.id)
.compose(apiHelper.configureApiCallObserver())
.subscribe(aVoid -> {
challenge.user_id = null;
challenge.async().save();
HabiticaApplication.User.resetChallengeList();
}, throwable -> {
});
})
.setNegativeButton(this.getString(R.string.no), (dialog, which) -> {
dialog.dismiss();
}).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

View file

@ -1,24 +1,18 @@
package com.habitrpg.android.habitica.ui.adapter.social;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.habitrpg.android.habitica.R;
import com.habitrpg.android.habitica.events.commands.JoinChallengeCommand;
import com.habitrpg.android.habitica.events.commands.LeaveChallengeCommand;
import com.habitrpg.android.habitica.events.commands.OpenFullProfileCommand;
import com.habitrpg.android.habitica.events.commands.ShowChallengeTasksCommand;
import com.habitrpg.android.habitica.events.commands.ShowChallengeDetailDialogCommand;
import com.habitrpg.android.habitica.events.commands.ShowChallengeDetailActivityCommand;
import com.magicmicky.habitrpgwrapper.lib.models.Challenge;
import net.pherth.android.emoji_library.EmojiParser;
@ -79,9 +73,9 @@ public class ChallengesListViewAdapter extends RecyclerView.Adapter<ChallengesLi
public void replaceChallenge(Challenge challenge) {
int index = challenges.indexOf(challenge);
if(index == -1){
if (index == -1) {
for (int i = 0; i < challenges.size(); i++) {
if(challenges.get(i).id.equals(challenge.id)){
if (challenges.get(i).id.equals(challenge.id)) {
index = i;
break;
@ -89,7 +83,7 @@ public class ChallengesListViewAdapter extends RecyclerView.Adapter<ChallengesLi
}
}
if(index != -1) {
if (index != -1) {
challenges.set(index, challenge);
notifyItemChanged(index);
}
@ -132,7 +126,7 @@ public class ChallengesListViewAdapter extends RecyclerView.Adapter<ChallengesLi
itemView.setOnClickListener(this);
if(!viewUserChallengesOnly){
if (!viewUserChallengesOnly) {
challengeName.setTextColor(ContextCompat.getColor(getContext(), R.color.brand_200));
}
}
@ -189,16 +183,12 @@ public class ChallengesListViewAdapter extends RecyclerView.Adapter<ChallengesLi
@Override
public void onClick(View view) {
/*if (view == leaderLayout) {
EventBus.getDefault().post(new OpenFullProfileCommand(challenge.leaderId));
} else if (view == joinButton) {
EventBus.getDefault().post(new JoinChallengeCommand(challenge.id));
} else if (view == leaveButton) {
} else*/
if (challenge != null) {
// Card tapped
EventBus.getDefault().post(new ShowChallengeTasksCommand(challenge.id));
if (viewUserChallengesOnly) {
EventBus.getDefault().post(new ShowChallengeDetailActivityCommand(challenge.id));
} else {
EventBus.getDefault().post(new ShowChallengeDetailDialogCommand(challenge.id));
}
}
}
}

View file

@ -0,0 +1,175 @@
package com.habitrpg.android.habitica.ui.fragments.social.challenges;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.habitrpg.android.habitica.APIHelper;
import com.habitrpg.android.habitica.HabiticaApplication;
import com.habitrpg.android.habitica.R;
import com.habitrpg.android.habitica.events.commands.OpenFullProfileCommand;
import com.habitrpg.android.habitica.ui.activities.ChallengeDetailActivity;
import com.habitrpg.android.habitica.ui.helpers.MarkdownParser;
import com.magicmicky.habitrpgwrapper.lib.models.Challenge;
import com.magicmicky.habitrpgwrapper.lib.models.HabitRPGUser;
import net.pherth.android.emoji_library.EmojiParser;
import net.pherth.android.emoji_library.EmojiTextView;
import org.greenrobot.eventbus.EventBus;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.functions.Action1;
public class ChallegeDetailDialogHolder {
@BindView(R.id.challenge_not_joined_header)
LinearLayout notJoinedHeader;
@BindView(R.id.challenge_joined_header)
LinearLayout joinedHeader;
@BindView(R.id.challenge_join_btn)
Button joinButton;
@BindView(R.id.challenge_leave_btn)
Button leaveButton;
@BindView(R.id.challenge_name)
EmojiTextView challengeName;
@BindView(R.id.challenge_description)
EmojiTextView challengeDescription;
@BindView(R.id.challenge_leader)
TextView challengeLeader;
@BindView(R.id.gem_amount)
TextView gem_amount;
@BindView(R.id.challenge_member_count)
TextView member_count;
private AlertDialog dialog;
private APIHelper apiHelper;
private HabitRPGUser user;
private Challenge challenge;
private Action1<Challenge> challengeJoinedAction;
private Action1<Challenge> challengeLeftAction;
private Activity context;
protected ChallegeDetailDialogHolder(View view, Activity context) {
this.context = context;
ButterKnife.bind(this, view);
}
public void bind(AlertDialog dialog, APIHelper apiHelper, HabitRPGUser user, Challenge challenge,
Action1<Challenge> challengeJoinedAction, Action1<Challenge> challengeLeftAction) {
this.dialog = dialog;
this.apiHelper = apiHelper;
this.user = user;
this.challenge = challenge;
this.challengeJoinedAction = challengeJoinedAction;
this.challengeLeftAction = challengeLeftAction;
changeViewsByChallenge(challenge);
}
public void changeViewsByChallenge(Challenge challenge) {
setJoined(challenge.user_id != null && !challenge.user_id.isEmpty());
challengeName.setText(EmojiParser.parseEmojis(challenge.name));
challengeDescription.setText(MarkdownParser.parseMarkdown(challenge.description));
challengeLeader.setText(challenge.leaderName);
gem_amount.setText(challenge.prize + "");
member_count.setText(challenge.memberCount + "");
}
public void setJoined(boolean joined) {
joinedHeader.setVisibility(joined ? View.VISIBLE : View.GONE);
leaveButton.setVisibility(joined ? View.VISIBLE : View.GONE);
notJoinedHeader.setVisibility(joined ? View.GONE : View.VISIBLE);
joinButton.setVisibility(joined ? View.GONE : View.VISIBLE);
}
@OnClick(R.id.challenge_leader)
public void openLeaderProfile() {
EventBus.getDefault().post(new OpenFullProfileCommand(challenge.leaderId));
}
@OnClick(R.id.challenge_go_to_btn)
public void openChallengeActivity() {
Bundle bundle = new Bundle();
bundle.putString(ChallengeDetailActivity.CHALLENGE_ID, challenge.id);
Intent intent = new Intent(HabiticaApplication.currentActivity, ChallengeDetailActivity.class);
intent.putExtras(bundle);
//intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
context.startActivity(intent);
this.dialog.dismiss();
}
@OnClick(R.id.challenge_join_btn)
public void joinChallenge() {
this.apiHelper.apiService.joinChallenge(challenge.id)
.compose(apiHelper.configureApiCallObserver())
.subscribe(challenge -> {
challenge.user_id = this.user.getId();
challenge.async().save();
if (challengeJoinedAction != null) {
challengeJoinedAction.call(challenge);
}
changeViewsByChallenge(challenge);
}, throwable -> {
});
}
@OnClick(R.id.challenge_leave_btn)
public void leaveChallenge() {
new AlertDialog.Builder(context)
.setTitle(context.getString(R.string.challenge_leave_title))
.setMessage(String.format(context.getString(R.string.challenge_leave_text), challenge.name))
.setPositiveButton(context.getString(R.string.yes), (dialog, which) -> {
this.apiHelper.apiService.leaveChallenge(challenge.id)
.compose(apiHelper.configureApiCallObserver())
.subscribe(aVoid -> {
challenge.user_id = null;
challenge.async().save();
this.user.resetChallengeList();
this.dialog.dismiss();
}, throwable -> {
});
}).setNegativeButton(context.getString(R.string.no), (dialog, which) -> {
dialog.dismiss();
}).show();
}
public static void showDialog(Activity activity, APIHelper apiHelper, HabitRPGUser user, Challenge challenge,
Action1<Challenge> challengeJoinedAction, Action1<Challenge> challengeLeftAction) {
View dialogLayout = activity.getLayoutInflater().inflate(R.layout.dialog_challenge_detail, null);
ChallegeDetailDialogHolder challegeDetailDialogHolder = new ChallegeDetailDialogHolder(dialogLayout, activity);
AlertDialog.Builder builder = new AlertDialog.Builder(activity)
.setView(dialogLayout);
challegeDetailDialogHolder.bind(builder.show(), apiHelper, user, challenge, challengeJoinedAction, challengeLeftAction);
}
}

View file

@ -24,7 +24,7 @@ import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class ChallengeListFragment extends BaseMainFragment implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {
public class ChallengeListFragment extends BaseMainFragment implements SwipeRefreshLayout.OnRefreshListener {
@BindView(R.id.challenges_refresh_layout)
SwipeRefreshLayout swipeRefreshLayout;
@ -143,11 +143,6 @@ public class ChallengeListFragment extends BaseMainFragment implements View.OnCl
challengeAdapter.setChallenges(challenges);
}
@Override
public void onClick(View v) {
}
public void addItem(Challenge challenge) {
challengeAdapter.addChallenge(challenge);
}

View file

@ -1,49 +1,29 @@
package com.habitrpg.android.habitica.ui.fragments.social.challenges;
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;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.habitrpg.android.habitica.APIHelper;
import com.habitrpg.android.habitica.HabiticaApplication;
import com.habitrpg.android.habitica.R;
import com.habitrpg.android.habitica.components.AppComponent;
import com.habitrpg.android.habitica.events.commands.OpenFullProfileCommand;
import com.habitrpg.android.habitica.events.commands.ShowChallengeTasksCommand;
import com.habitrpg.android.habitica.events.commands.ShowChallengeDetailActivityCommand;
import com.habitrpg.android.habitica.events.commands.ShowChallengeDetailDialogCommand;
import com.habitrpg.android.habitica.ui.activities.ChallengeDetailActivity;
import com.habitrpg.android.habitica.ui.fragments.BaseMainFragment;
import com.habitrpg.android.habitica.ui.helpers.MarkdownParser;
import com.magicmicky.habitrpgwrapper.lib.models.Challenge;
import com.magicmicky.habitrpgwrapper.lib.models.HabitRPGUser;
import com.raizlabs.android.dbflow.sql.builder.Condition;
import com.raizlabs.android.dbflow.sql.language.Select;
import net.pherth.android.emoji_library.EmojiParser;
import net.pherth.android.emoji_library.EmojiTextView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.util.Stack;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class ChallengesOverviewFragment extends BaseMainFragment {
public ViewPager viewPager;
@ -146,18 +126,27 @@ public class ChallengesOverviewFragment extends BaseMainFragment {
}
@Subscribe
public void onEvent(ShowChallengeTasksCommand cmd) {
View dialogLayout = HabiticaApplication.currentActivity.getLayoutInflater().inflate(R.layout.dialog_challenge_detail, null);
public void onEvent(ShowChallengeDetailDialogCommand cmd) {
Challenge challenge = new Select().from(Challenge.class).where(Condition.column("id").is(cmd.challengeId)).querySingle();
ChallegeDetailDialogHolder challegeDetailDialogHolder = new ChallegeDetailDialogHolder(dialogLayout, activity);
ChallegeDetailDialogHolder.showDialog(HabiticaApplication.currentActivity, apiHelper, user, challenge, challenge1 -> {
userChallengesFragment.addItem(challenge1);
availableChallengesFragment.updateItem(challenge1);
}, challenge1 -> {
userChallengesFragment.onRefresh();
availableChallengesFragment.onRefresh();
});
}
AlertDialog.Builder builder = new AlertDialog.Builder(HabiticaApplication.currentActivity)
.setView(dialogLayout);
@Subscribe
public void onEvent(ShowChallengeDetailActivityCommand cmd) {
Bundle bundle = new Bundle();
bundle.putString(ChallengeDetailActivity.CHALLENGE_ID, cmd.challengeId);
challegeDetailDialogHolder.bind(builder.show(), apiHelper, user, challenge);
Intent intent = new Intent(HabiticaApplication.currentActivity, ChallengeDetailActivity.class);
intent.putExtras(bundle);
//intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
HabiticaApplication.currentActivity.startActivity(intent);
}
public boolean onHandleBackPressed() {
@ -172,135 +161,6 @@ public class ChallengesOverviewFragment extends BaseMainFragment {
return false;
}
public class ChallegeDetailDialogHolder {
@BindView(R.id.challenge_not_joined_header)
LinearLayout notJoinedHeader;
@BindView(R.id.challenge_joined_header)
LinearLayout joinedHeader;
@BindView(R.id.challenge_join_btn)
Button joinButton;
@BindView(R.id.challenge_leave_btn)
Button leaveButton;
@BindView(R.id.challenge_name)
EmojiTextView challengeName;
@BindView(R.id.challenge_description)
EmojiTextView challengeDescription;
@BindView(R.id.challenge_leader)
TextView challengeLeader;
@BindView(R.id.gem_amount)
TextView gem_amount;
@BindView(R.id.challenge_member_count)
TextView member_count;
private AlertDialog dialog;
private APIHelper apiHelper;
private HabitRPGUser user;
private Challenge challenge;
private Context context;
protected ChallegeDetailDialogHolder(View view, Context context) {
this.context = context;
ButterKnife.bind(this, view);
}
public void bind(AlertDialog dialog, APIHelper apiHelper, HabitRPGUser user, Challenge challenge) {
this.dialog = dialog;
this.apiHelper = apiHelper;
this.user = user;
this.challenge = challenge;
changeViewsByChallenge(challenge);
}
public void changeViewsByChallenge(Challenge challenge){
setJoined(challenge.user_id != null && !challenge.user_id.isEmpty());
challengeName.setText(EmojiParser.parseEmojis(challenge.name));
challengeDescription.setText( MarkdownParser.parseMarkdown(challenge.description));
challengeLeader.setText(challenge.leaderName);
gem_amount.setText(challenge.prize + "");
member_count.setText(challenge.memberCount + "");
}
public void setJoined(boolean joined){
joinedHeader.setVisibility(joined ? View.VISIBLE : View.GONE);
leaveButton.setVisibility(joined ? View.VISIBLE : View.GONE);
notJoinedHeader.setVisibility(joined ? View.GONE : View.VISIBLE);
joinButton.setVisibility(joined ? View.GONE : View.VISIBLE);
}
@OnClick(R.id.challenge_leader)
public void openLeaderProfile() {
EventBus.getDefault().post(new OpenFullProfileCommand(challenge.leaderId));
}
@OnClick(R.id.challenge_go_to_btn)
public void openChallengeActivity() {
Bundle bundle = new Bundle();
bundle.putString(ChallengeDetailActivity.CHALLENGE_ID, challenge.id);
Intent intent = new Intent(HabiticaApplication.currentActivity, ChallengeDetailActivity.class);
intent.putExtras(bundle);
//intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
this.dialog.dismiss();
}
@OnClick(R.id.challenge_join_btn)
public void joinChallenge() {
this.apiHelper.apiService.joinChallenge(challenge.id)
.compose(apiHelper.configureApiCallObserver())
.subscribe(challenge -> {
challenge.user_id = this.user.getId();
challenge.async().save();
userChallengesFragment.addItem(challenge);
availableChallengesFragment.updateItem(challenge);
changeViewsByChallenge(challenge);
}, throwable -> {
});
}
@OnClick(R.id.challenge_leave_btn)
public void leaveChallenge() {
new AlertDialog.Builder(context)
.setTitle(context.getString(R.string.challenge_leave_title))
.setMessage(String.format(context.getString(R.string.challenge_leave_text), challenge.name))
.setPositiveButton(context.getString(R.string.yes), (dialog, which) -> {
this.apiHelper.apiService.leaveChallenge(challenge.id)
.compose(apiHelper.configureApiCallObserver())
.subscribe(aVoid -> {
challenge.user_id = null;
challenge.async().save();
this.user.resetChallengeList();
userChallengesFragment.onRefresh();
availableChallengesFragment.onRefresh();
this.dialog.dismiss();
}, throwable -> {
});
}).setNegativeButton(context.getString(R.string.no), (dialog, which) -> {
dialog.dismiss();
}).show();
}
}
@Override
public String customTitle() {
return getString(R.string.challenges);