leave / remove tasks on deleting a challenge

This commit is contained in:
Negue 2017-03-09 21:15:57 +01:00
parent 82e51292f5
commit afc0a92e37
5 changed files with 96 additions and 30 deletions

View file

@ -491,6 +491,10 @@ To start, which parts of your life do you want to improve?</string>
<string name="challenge_details">Challenge Details</string>
<string name="challenge_leave_title">Leave Challenge</string>
<string name="challenge_leave_text">Are you sure you want to leave the Challenge “%s”?</string>
<string name="challenge_remove_tasks_title">Remove tasks</string>
<string name="challenge_remove_tasks_text">Do you want to remove the tasks?</string>
<string name="remove_tasks">Remove</string>
<string name="keep_tasks">Keep</string>
<string name="my_challenges">My Challenges</string>
<string name="public_challenges">Public</string>
<string name="challenges">Challenges</string>

View file

@ -8,6 +8,7 @@ import com.habitrpg.android.habitica.ui.fragments.social.challenges.ChallegeDeta
import com.habitrpg.android.habitica.ui.fragments.social.challenges.ChallengeTasksRecyclerViewFragment;
import com.habitrpg.android.habitica.ui.helpers.MarkdownParser;
import com.magicmicky.habitrpgwrapper.lib.models.Challenge;
import com.magicmicky.habitrpgwrapper.lib.models.LeaveChallengeBody;
import com.magicmicky.habitrpgwrapper.lib.models.tasks.Task;
import com.raizlabs.android.dbflow.sql.builder.Condition;
import com.raizlabs.android.dbflow.sql.language.Select;
@ -38,6 +39,7 @@ import javax.inject.Inject;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.functions.Action1;
public class ChallengeDetailActivity extends BaseActivity {
@ -184,25 +186,8 @@ public class ChallengeDetailActivity extends BaseActivity {
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();
showChallengeLeaveDialog();
HabiticaApplication.User.resetChallengeList();
finish();
}, throwable -> {
});
})
.setNegativeButton(this.getString(R.string.no), (dialog, which) -> {
dialog.dismiss();
}).show();
return true;
default:
@ -210,6 +195,49 @@ public class ChallengeDetailActivity extends BaseActivity {
}
}
private void showChallengeLeaveDialog(){
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) -> {
dialog.dismiss();
showRemoveTasksDialog(keepTasks -> {
this.apiHelper.apiService.leaveChallenge(challenge.id, new LeaveChallengeBody(keepTasks))
.compose(apiHelper.configureApiCallObserver())
.subscribe(aVoid -> {
challenge.user_id = null;
challenge.async().save();
HabiticaApplication.User.resetChallengeList();
finish();
}, throwable -> {
});
});
})
.setNegativeButton(this.getString(R.string.no), (dialog, which) -> {
dialog.dismiss();
}).show();
}
// refactor as an UseCase later - see ChallengeDetailDialogHolder
private void showRemoveTasksDialog(Action1<String> callback){
new AlertDialog.Builder(this)
.setTitle(this.getString(R.string.challenge_remove_tasks_title))
.setMessage(this.getString(R.string.challenge_remove_tasks_text))
.setPositiveButton(this.getString(R.string.remove_tasks), (dialog, which) -> {
callback.call("remove-all");
dialog.dismiss();
})
.setNegativeButton(this.getString(R.string.keep_tasks), (dialog, which) -> {
callback.call("keep-all");
dialog.dismiss();
}).show();
}
@Override
public boolean onSupportNavigateUp() {
finish();

View file

@ -9,6 +9,7 @@ import com.habitrpg.android.habitica.ui.adapter.social.ChallengesListViewAdapter
import com.habitrpg.android.habitica.ui.helpers.MarkdownParser;
import com.magicmicky.habitrpgwrapper.lib.models.Challenge;
import com.magicmicky.habitrpgwrapper.lib.models.HabitRPGUser;
import com.magicmicky.habitrpgwrapper.lib.models.LeaveChallengeBody;
import com.magicmicky.habitrpgwrapper.lib.models.tasks.Task;
import net.pherth.android.emoji_library.EmojiParser;
@ -320,22 +321,43 @@ public class ChallegeDetailDialogHolder {
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();
.setPositiveButton(context.getString(R.string.yes), (dialog, which) ->
this.user.resetChallengeList();
showRemoveTasksDialog(keepTasks -> {
if (challengeLeftAction != null) {
challengeLeftAction.call(challenge);
}
this.apiHelper.apiService.leaveChallenge(challenge.id, new LeaveChallengeBody(keepTasks))
.compose(apiHelper.configureApiCallObserver())
.subscribe(aVoid -> {
challenge.user_id = null;
challenge.async().save();
this.dialog.dismiss();
}, throwable -> {
this.user.resetChallengeList();
if (challengeLeftAction != null) {
challengeLeftAction.call(challenge);
}
this.dialog.dismiss();
}, throwable -> {
});
})).setNegativeButton(context.getString(R.string.no), (dialog, which) -> {
dialog.dismiss();
}).show();
}
// refactor as an UseCase later - see ChallengeDetailActivity
private void showRemoveTasksDialog(Action1<String> callback){
new AlertDialog.Builder(context)
.setTitle(context.getString(R.string.challenge_remove_tasks_title))
.setMessage(context.getString(R.string.challenge_remove_tasks_text))
.setPositiveButton(context.getString(R.string.remove_tasks), (dialog, which) -> {
callback.call("remove-all");
dialog.dismiss();
})
.setNegativeButton(context.getString(R.string.keep_tasks), (dialog, which) -> {
callback.call("keep-all");
dialog.dismiss();
}).show();
}
}

View file

@ -7,6 +7,7 @@ import com.magicmicky.habitrpgwrapper.lib.models.ContentResult;
import com.magicmicky.habitrpgwrapper.lib.models.Group;
import com.magicmicky.habitrpgwrapper.lib.models.HabitRPGUser;
import com.magicmicky.habitrpgwrapper.lib.models.Items;
import com.magicmicky.habitrpgwrapper.lib.models.LeaveChallengeBody;
import com.magicmicky.habitrpgwrapper.lib.models.PostChatMessageResult;
import com.magicmicky.habitrpgwrapper.lib.models.PurchaseValidationRequest;
import com.magicmicky.habitrpgwrapper.lib.models.PurchaseValidationResult;
@ -285,7 +286,7 @@ public interface ApiService {
Observable<HabitResponse<Challenge>> joinChallenge(@Path("challengeId") String challengeId);
@POST("challenges/{challengeId}/leave")
Observable<HabitResponse<Void>> leaveChallenge(@Path("challengeId") String challengeId);
Observable<HabitResponse<Void>> leaveChallenge(@Path("challengeId") String challengeId, @Body LeaveChallengeBody body);
//DEBUG: These calls only work on a local development server

View file

@ -0,0 +1,11 @@
package com.magicmicky.habitrpgwrapper.lib.models;
public class LeaveChallengeBody {
public String keep;
public LeaveChallengeBody(String keep) {
this.keep = keep;
}
}