Skip to content

Commit

Permalink
- Add submit to full reply editor
Browse files Browse the repository at this point in the history
- Sync reply text between editor and source
  • Loading branch information
TheKeeperOfPie committed Jul 25, 2015
1 parent 3e25c6b commit 9d38a11
Show file tree
Hide file tree
Showing 29 changed files with 412 additions and 190 deletions.
3 changes: 3 additions & 0 deletions app/src/main/java/com/winsonchiu/reader/FragmentWeb.java
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ public void onPause() {

public boolean navigateBack() {
if (isFinished) {
webView.destroy();
webView = null;
Log.d(TAG, "navigateBack finished");
return true;
}

Expand Down
77 changes: 39 additions & 38 deletions app/src/main/java/com/winsonchiu/reader/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.winsonchiu.reader.data.reddit.Comment;
import com.winsonchiu.reader.data.reddit.Link;
import com.winsonchiu.reader.data.reddit.Reddit;
import com.winsonchiu.reader.data.reddit.Replyable;
import com.winsonchiu.reader.data.reddit.Sort;
import com.winsonchiu.reader.data.reddit.Thing;
import com.winsonchiu.reader.data.reddit.Time;
Expand Down Expand Up @@ -225,7 +226,12 @@ public void onResponse(String response) {
e.printStackTrace();
}
}
}, null);
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Failed to send reply", Toast.LENGTH_LONG).show();
}
});
}

@Override
Expand Down Expand Up @@ -475,6 +481,17 @@ public void editLink(Link link) {
.commit();
}

@Override
public void showReplyEditor(Replyable replyable) {

getFragmentManager().beginTransaction()
.hide(getFragmentManager().findFragmentById(R.id.frame_fragment))
.add(R.id.frame_fragment, FragmentReply.newInstance(replyable),
FragmentReply.TAG)
.addToBackStack(null)
.commit();
}

};

eventListenerComment = new AdapterCommentList.ViewHolderComment.EventListener() {
Expand Down Expand Up @@ -525,42 +542,11 @@ public void editComment(Comment comment, String text) {
getControllerComments().editComment(comment, text);
}

@Override
public void sendComment(String name, String text) {
reddit.sendComment(name, text, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
Comment newComment = Comment.fromJson(
jsonObject.getJSONObject("json")
.getJSONObject("data")
.getJSONArray("things")
.getJSONObject(0), 0);
getControllerComments().insertComment(newComment);
}
catch (JSONException e) {
e.printStackTrace();
}
}
}, null);
}

@Override
public void jumpToParent(Comment comment) {
getControllerComments().jumpToParent(comment);
}

@Override
public void showReplyEditor(Comment comment) {
getFragmentManager().beginTransaction()
.hide(getFragmentManager().findFragmentById(R.id.frame_fragment))
.add(R.id.frame_fragment, FragmentReply.newInstance(),
FragmentReply.TAG)
.addToBackStack(null)
.commit();
}

};

if (sharedPreferences.getBoolean(AppSettings.BETA_NOTICE_0, true)) {
Expand Down Expand Up @@ -786,6 +772,7 @@ private void parseUrl(String urlString) {
getFragmentManager().beginTransaction()
.replace(R.id.frame_fragment, FragmentWeb
.newInstance(urlString), FragmentWeb.TAG)
.addToBackStack(null)
.commit();
return;
}
Expand Down Expand Up @@ -919,7 +906,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
@Override
public void onBackPressed() {
onNavigationBackClick();

}

@Override
Expand Down Expand Up @@ -949,17 +935,17 @@ else if (urlString.indexOf("reddit.com") < 20) {
super.startActivity(intentActivity);
}
else if (URLUtil.isValidUrl(urlString)) {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction()
.add(R.id.frame_fragment, FragmentWeb
.newInstance(urlString), FragmentWeb.TAG)
.addToBackStack(null);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();

Fragment fragment = getFragmentManager().findFragmentById(R.id.frame_fragment);
if (fragment != null) {
fragmentTransaction.hide(fragment);
}

fragmentTransaction.commit();
fragmentTransaction.add(R.id.frame_fragment, FragmentWeb
.newInstance(urlString), FragmentWeb.TAG)
.addToBackStack(null)
.commit();
}
}
else {
Expand Down Expand Up @@ -1047,15 +1033,30 @@ public AdapterCommentList.ViewHolderComment.EventListener getEventListenerCommen
public void onNavigationBackClick() {
if (getFragmentManager().getBackStackEntryCount() > 0) {
FragmentBase fragment = (FragmentBase) getFragmentManager().findFragmentById(R.id.frame_fragment);

if (fragment != null && !fragment.navigateBack()) {
return;
}

/*
If this is the only fragment in the stack, close out the Activity,
otherwise show the fragment
*/
// if (getFragmentManager().getBackStackEntryCount() == 1) {
// finish();
// return;
// }
getFragmentManager().popBackStackImmediate();

fragment = (FragmentBase) getFragmentManager().findFragmentById(R.id.frame_fragment);
if (fragment != null) {
getFragmentManager().beginTransaction().show(fragment).commit();
fragment.onShown();
Log.d(TAG, "Fragment shown");
}
else {
finish();
}
}
else {
if (isTaskRoot()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ public void afterTextChanged(Editable s) {
buttonReplyEditor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
eventListener.showReplyEditor(comment);
eventListenerBase.showReplyEditor(comment);
}
});

Expand All @@ -555,7 +555,7 @@ private void sendReply() {
comment.setEdited(System.currentTimeMillis());
}
else {
eventListener.sendComment(comment.getName(), editTextReply.getText().toString());
eventListenerBase.sendComment(comment.getName(), editTextReply.getText().toString());
}
comment.setReplyExpanded(!comment.isReplyExpanded());
layoutContainerReply.setVisibility(View.GONE);
Expand Down Expand Up @@ -869,11 +869,11 @@ public void toggleReply() {
buttonSendReply.setText(
comment.isEditMode() ? itemView.getContext().getString(R.string.send_edit) :
itemView.getContext().getString(R.string.send_reply));
editTextReply.setText(comment.getReplyText());
layoutContainerReply.setVisibility(
comment.isReplyExpanded() ? View.VISIBLE : View.GONE);
if (comment.isReplyExpanded()) {
replyCallback.onReplyShown();
editTextReply.setText(comment.getReplyText());
editTextReply.clearFocus();
InputMethodManager inputManager = (InputMethodManager) itemView.getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
Expand Down Expand Up @@ -1025,9 +1025,7 @@ public interface EventListener {
boolean toggleComment(int position);
void deleteComment(Comment comment);
void editComment(Comment comment, String text);
void sendComment(String name, String text);
void jumpToParent(Comment comment);
void showReplyEditor(Comment comment);
}

public interface ReplyCallback{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.VolleyError;
import com.winsonchiu.reader.data.reddit.Replyable;
import com.winsonchiu.reader.utils.ControllerListener;
import com.winsonchiu.reader.R;
import com.winsonchiu.reader.data.reddit.Sort;
Expand Down Expand Up @@ -140,7 +141,8 @@ public void loadLinkComments() {
setRefreshing(true);

reddit.loadGet(
Reddit.OAUTH_URL + "/r/" + subreddit + "/comments/" + linkId + "?depth=10&showmore=true&showedits=true&limit=100&sort=" + sort.toString(),
Reddit.OAUTH_URL + "/r/" + subreddit + "/comments/" + linkId + "?depth=10&showmore=true&showedits=true&limit=100&sort=" + sort
.toString(),
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Expand Down Expand Up @@ -795,6 +797,32 @@ public void jumpToParent(Comment child) {

}

public boolean setReplyText(String name, String text, boolean collapsed) {

if (name.equals(link.getName())) {
link.setReplyText(text);
link.setReplyExpanded(!collapsed);
for (Listener listener : listeners) {
listener.getAdapter().notifyItemChanged(0);
}
return true;
}

for (int index = 0; index < listingComments.getChildren().size(); index++) {
Thing thing = listingComments.getChildren().get(index);
if (thing.getName().equals(name)) {
((Replyable) thing).setReplyText(text);
((Replyable) thing).setReplyExpanded(!collapsed);
for (Listener listener : listeners) {
listener.getAdapter().notifyItemChanged(index + 1);
}
return true;
}
}

return false;
}

public interface Listener extends ControllerListener {
void setSort(Sort sort);
void setIsCommentThread(boolean isCommentThread);
Expand Down
Loading

0 comments on commit 9d38a11

Please sign in to comment.