Skip to content

Commit

Permalink
[AN-1183] The Comment Dialog only dissmiss's on submite comment succe…
Browse files Browse the repository at this point in the history
…ss so you don't loose the message.
  • Loading branch information
jdandrade committed Feb 9, 2017
1 parent 85b1302 commit ccb4bd1
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@
import cm.aptoide.pt.dataprovider.util.CommentType;
import cm.aptoide.pt.dataprovider.ws.v7.BaseRequestWithStore;
import cm.aptoide.pt.dataprovider.ws.v7.ListCommentsRequest;
import cm.aptoide.pt.dataprovider.ws.v7.PostCommentForReview;
import cm.aptoide.pt.dataprovider.ws.v7.PostCommentForTimelineArticle;
import cm.aptoide.pt.dataprovider.ws.v7.store.PostCommentForStore;
import cm.aptoide.pt.interfaces.AptoideClientUUID;
import cm.aptoide.pt.logger.Logger;
import cm.aptoide.pt.model.v7.BaseV7Response;
import cm.aptoide.pt.model.v7.Comment;
import cm.aptoide.pt.model.v7.ListComments;
Expand All @@ -51,7 +47,6 @@
import java.util.Date;
import java.util.List;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

// TODO: 21/12/2016 sithengineer refactor and split in multiple classes to list comments
Expand Down Expand Up @@ -299,7 +294,7 @@ public Observable<Void> createNewCommentFragment(final String timelineArticleId,
return commentDialogFragment.lifecycle()
.doOnSubscribe(() -> commentDialogFragment.show(fm, "fragment_comment_dialog"))
.filter(event -> event.equals(FragmentEvent.DESTROY_VIEW))
.flatMap(event -> reloadComments());
.flatMap(event -> Observable.empty());
}

return showSignInMessage();
Expand All @@ -322,7 +317,7 @@ private Observable<Void> createNewCommentFragment(long storeId, long previousCom
return commentDialogFragment.lifecycle()
.doOnSubscribe(() -> commentDialogFragment.show(fm, "fragment_comment_dialog"))
.filter(event -> event.equals(FragmentEvent.DESTROY_VIEW))
.flatMap(event -> reloadComments());
.flatMap(event -> Observable.empty());
}

return showSignInMessage();
Expand Down Expand Up @@ -390,7 +385,7 @@ public Observable<Void> createNewCommentFragment(String timelineArticleId) {
commentDialogFragment.show(fm, "fragment_comment_dialog");
})
.filter(event -> event.equals(FragmentEvent.DESTROY_VIEW))
.flatMap(event -> reloadComments());
.flatMap(event -> Observable.empty());
}

return showSignInMessage();
Expand All @@ -413,43 +408,30 @@ public Observable<Void> createNewCommentFragment(long storeCommentId, String sto
commentDialogFragment.show(fm, "fragment_comment_dialog");
})
.filter(event -> event.equals(FragmentEvent.DESTROY_VIEW))
.flatMap(event -> reloadComments());
.flatMap(event -> Observable.empty());
}

return showSignInMessage();
});
}

@Override public void okSelected(String inputText, long longAsId, Long previousCommentId,
@Override public void okSelected(BaseV7Response response, long longAsId, Long previousCommentId,
String idAsString) {
submitComment(inputText, longAsId, previousCommentId, idAsString).observeOn(
//AndroidSchedulers.mainThread()).map(wsResponse -> wsResponse.isOk()).doOnError(e -> {
AndroidSchedulers.mainThread()).doOnError(e -> {
CrashReport.getInstance().log(e);
ShowMessage.asSnack(this, R.string.error_occured);
}).retry().compose(bindUntilEvent(FragmentEvent.DESTROY_VIEW)).subscribe(response -> {
if (response.isOk()) {
if (response instanceof SetComment) {
ComplexComment complexComment = getComplexComment(inputText, previousCommentId,
if (response instanceof SetComment) {
ComplexComment complexComment =
getComplexComment(((SetComment) response).getData().getBody(), previousCommentId,
((SetComment) response).getData().getId());

CommentDisplayable commentDisplayable = new CommentDisplayable(complexComment);
CommentDisplayable commentDisplayable = new CommentDisplayable(complexComment);

if (complexComment.getParent() != null) {
insertChildCommentInsideParent(complexComment);
} else {
addDisplayable(0, commentDisplayable, true);
}

ManagerPreferences.setForceServerRefreshFlag(true);
ShowMessage.asSnack(this.getActivity(), R.string.comment_submitted);
}
return;
if (complexComment.getParent() != null) {
insertChildCommentInsideParent(complexComment);
} else {
addDisplayable(0, commentDisplayable, true);
}
ShowMessage.asSnack(this, R.string.error_occured);
}, err -> {
CrashReport.getInstance().log(err);
});
ManagerPreferences.setForceServerRefreshFlag(true);
ShowMessage.asSnack(this.getActivity(), R.string.comment_submitted);
}
}

private void insertChildCommentInsideParent(ComplexComment complexComment) {
Expand All @@ -470,42 +452,6 @@ private void insertChildCommentInsideParent(ComplexComment complexComment) {
addDisplayables(this.displayables);
}

private Observable<? extends BaseV7Response> submitComment(String inputText, long idAsLong,
Long previousCommentId, String idAsString) {
switch (commentType) {
case REVIEW:
// new comment on a review
return PostCommentForReview.of(idAsLong, inputText, AptoideAccountManager.getAccessToken(),
aptoideClientUUID.getAptoideClientUUID()).observe();

case STORE:
// check if this is a new comment on a store or a reply to a previous one
if (previousCommentId == null) {
return PostCommentForStore.of(idAsLong, inputText, AptoideAccountManager.getAccessToken(),
aptoideClientUUID.getAptoideClientUUID()).observe();
}

return PostCommentForStore.of(idAsLong, previousCommentId, inputText,
AptoideAccountManager.getAccessToken(), aptoideClientUUID.getAptoideClientUUID())
.observe();

case TIMELINE:
// check if this is a new comment on a article or a reply to a previous one
if (previousCommentId == null) {
return PostCommentForTimelineArticle.of(idAsString, inputText,
AptoideAccountManager.getAccessToken(), aptoideClientUUID.getAptoideClientUUID())
.observe();
}

return PostCommentForTimelineArticle.of(idAsString, previousCommentId, inputText,
AptoideAccountManager.getAccessToken(), aptoideClientUUID.getAptoideClientUUID())
.observe();
}
// default case
Logger.e(this.getTag(), "Unable to create reply due to missing comment type");
return Observable.empty();
}

@NonNull
private ComplexComment getComplexComment(String inputText, Long previousCommentId, long id) {
Comment comment = new Comment();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package cm.aptoide.pt.v8engine.interfaces;

import cm.aptoide.pt.model.v7.BaseV7Response;

/**
* Created by jdandrade on 03/02/2017.
*/
public interface CommentDialogCallbackContract {
void okSelected(String text, long idAsLong, Long previousCommentId, String inputText);
void okSelected(BaseV7Response inputText, long longAsId, Long previousCommentId, String idAsString);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,27 @@
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import cm.aptoide.accountmanager.AptoideAccountManager;
import cm.aptoide.pt.crashreports.CrashReport;
import cm.aptoide.pt.dataprovider.DataProvider;
import cm.aptoide.pt.dataprovider.repository.IdsRepositoryImpl;
import cm.aptoide.pt.dataprovider.util.CommentType;
import cm.aptoide.pt.dataprovider.ws.v7.PostCommentForReview;
import cm.aptoide.pt.dataprovider.ws.v7.PostCommentForTimelineArticle;
import cm.aptoide.pt.dataprovider.ws.v7.store.PostCommentForStore;
import cm.aptoide.pt.interfaces.AptoideClientUUID;
import cm.aptoide.pt.logger.Logger;
import cm.aptoide.pt.model.v7.BaseV7Response;
import cm.aptoide.pt.preferences.secure.SecurePreferencesImplementation;
import cm.aptoide.pt.utils.AptoideUtils;
import cm.aptoide.pt.utils.design.ShowMessage;
import cm.aptoide.pt.v8engine.R;
import cm.aptoide.pt.v8engine.fragment.CommentListFragment;
import com.jakewharton.rxbinding.view.RxView;
import com.trello.rxlifecycle.android.FragmentEvent;
import com.trello.rxlifecycle.components.RxDialogFragment;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;

public class CommentDialogFragment extends RxDialogFragment {

Expand Down Expand Up @@ -185,19 +194,29 @@ public void onTextChanged(CharSequence charSequence, int start, int count, int a
}
});

RxView.clicks(commentButton).flatMap(a -> Observable.just(getText())).filter(inputText -> {
if (TextUtils.isEmpty(inputText)) {
enableError(onEmptyTextError);
return false;
}
disableError();
return true;
}).subscribe(inputText -> {
this.dismiss();
commentDialogCallbackContract.okSelected(inputText, idAsLong, previousCommentId, idAsString);
}, err -> {
CrashReport.getInstance().log(err);
});
RxView.clicks(commentButton)
.flatMap(a -> Observable.just(getText()))
.filter(inputText -> {
if (TextUtils.isEmpty(inputText)) {
enableError(onEmptyTextError);
return false;
}
disableError();
return true;
})
.flatMap(inputText -> submitComment(inputText, idAsLong, previousCommentId,
idAsString).observeOn(AndroidSchedulers.mainThread()).doOnError(e -> {
CrashReport.getInstance().log(e);
ShowMessage.asSnack(this, R.string.error_occured);
}).retry().compose(bindUntilEvent(FragmentEvent.DESTROY_VIEW)))
.subscribe(resp -> {
if (resp.isOk()) {
this.dismiss();
commentDialogCallbackContract.okSelected(resp, idAsLong, previousCommentId, idAsString);
} else {
ShowMessage.asSnack(this, R.string.error_occured);
}
}, throwable -> CrashReport.getInstance().log(throwable));
}

private void disableError() {
Expand All @@ -215,6 +234,42 @@ private void enableError(String error) {
textInputLayout.setError(error);
}

private Observable<? extends BaseV7Response> submitComment(String inputText, long idAsLong,
Long previousCommentId, String idAsString) {
switch (commentType) {
case REVIEW:
// new comment on a review
return PostCommentForReview.of(idAsLong, inputText, AptoideAccountManager.getAccessToken(),
aptoideClientUUID.getAptoideClientUUID()).observe();

case STORE:
// check if this is a new comment on a store or a reply to a previous one
if (previousCommentId == null) {
return PostCommentForStore.of(idAsLong, inputText, AptoideAccountManager.getAccessToken(),
aptoideClientUUID.getAptoideClientUUID()).observe();
}

return PostCommentForStore.of(idAsLong, previousCommentId, inputText,
AptoideAccountManager.getAccessToken(), aptoideClientUUID.getAptoideClientUUID())
.observe();

case TIMELINE:
// check if this is a new comment on a article or a reply to a previous one
if (previousCommentId == null) {
return PostCommentForTimelineArticle.of(idAsString, inputText,
AptoideAccountManager.getAccessToken(), aptoideClientUUID.getAptoideClientUUID())
.observe();
}

return PostCommentForTimelineArticle.of(idAsString, previousCommentId, inputText,
AptoideAccountManager.getAccessToken(), aptoideClientUUID.getAptoideClientUUID())
.observe();
}
// default case
Logger.e(this.getTag(), "Unable to create reply due to missing comment type");
return Observable.empty();
}

public void setCommentDialogCallbackContract(CommentListFragment commentDialogCallbackContract) {
this.commentDialogCallbackContract = commentDialogCallbackContract;
}
Expand Down

0 comments on commit ccb4bd1

Please sign in to comment.