From f69ae29bb93a113bad77bd258c92398bd45cd902 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 14 Jun 2019 10:30:26 +0800 Subject: Added a feature: Sending comments. --- .../infinityforreddit/AppComponent.java | 1 + .../infinityforreddit/CommentActivity.java | 112 ++++++++------------- .../infinityforreddit/RedditAPI.java | 4 + .../infinityforreddit/RedditUtils.java | 5 + .../infinityforreddit/SendComment.java | 51 ++++++++++ .../infinityforreddit/ViewPostDetailActivity.java | 6 +- 6 files changed, 104 insertions(+), 75 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SendComment.java (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index 99a5ff3f..e496140a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -15,4 +15,5 @@ interface AppComponent { void inject(ViewPostDetailActivity viewPostDetailActivity); void inject(ViewSubredditDetailActivity viewSubredditDetailActivity); void inject(ViewUserDetailActivity viewUserDetailActivity); + void inject(CommentActivity commentActivity); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java index 0e885b80..8f4a6958 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java @@ -1,49 +1,48 @@ package ml.docilealligator.infinityforreddit; import android.content.Intent; +import android.content.SharedPreferences; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; +import android.widget.EditText; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; +import androidx.coordinatorlayout.widget.CoordinatorLayout; -import com.chinalwb.are.AREditText; -import com.chinalwb.are.styles.toolbar.IARE_Toolbar; -import com.chinalwb.are.styles.toolitems.ARE_ToolItem_AlignmentCenter; -import com.chinalwb.are.styles.toolitems.ARE_ToolItem_AlignmentLeft; -import com.chinalwb.are.styles.toolitems.ARE_ToolItem_AlignmentRight; -import com.chinalwb.are.styles.toolitems.ARE_ToolItem_At; -import com.chinalwb.are.styles.toolitems.ARE_ToolItem_Bold; -import com.chinalwb.are.styles.toolitems.ARE_ToolItem_Hr; -import com.chinalwb.are.styles.toolitems.ARE_ToolItem_Image; -import com.chinalwb.are.styles.toolitems.ARE_ToolItem_Italic; -import com.chinalwb.are.styles.toolitems.ARE_ToolItem_Link; -import com.chinalwb.are.styles.toolitems.ARE_ToolItem_ListBullet; -import com.chinalwb.are.styles.toolitems.ARE_ToolItem_ListNumber; -import com.chinalwb.are.styles.toolitems.ARE_ToolItem_Quote; -import com.chinalwb.are.styles.toolitems.ARE_ToolItem_Strikethrough; -import com.chinalwb.are.styles.toolitems.ARE_ToolItem_Subscript; -import com.chinalwb.are.styles.toolitems.ARE_ToolItem_Superscript; -import com.chinalwb.are.styles.toolitems.ARE_ToolItem_Underline; -import com.chinalwb.are.styles.toolitems.ARE_ToolItem_Video; -import com.chinalwb.are.styles.toolitems.IARE_ToolItem; +import com.google.android.material.snackbar.Snackbar; + +import javax.inject.Inject; +import javax.inject.Named; import butterknife.BindView; import butterknife.ButterKnife; +import retrofit2.Retrofit; public class CommentActivity extends AppCompatActivity { static final String COMMENT_PARENT_TEXT = "CPT"; + static final String PARENT_FULLNAME = "PF"; static final String EXTRA_COMMENT_DATA = "ECD"; static final int WRITE_COMMENT_REQUEST_CODE = 1; + @BindView(R.id.coordinator_layout_comment_activity) CoordinatorLayout coordinatorLayout; @BindView(R.id.toolbar_comment_activity) Toolbar toolbar; @BindView(R.id.comment_parent_text_view_comment_activity) TextView commentParentTextView; - @BindView(R.id.arEditText) AREditText commentEditor; - @BindView(R.id.areToolbar) IARE_Toolbar editorToolbar; + @BindView(R.id.comment_edit_text_comment_activity) EditText commentEditText; + + private String parentFullname; + + @Inject + @Named("oauth") + Retrofit mOauthRetrofit; + + @Inject + @Named("auth_info") + SharedPreferences sharedPreferences; @Override protected void onCreate(Bundle savedInstanceState) { @@ -52,13 +51,12 @@ public class CommentActivity extends AppCompatActivity { ButterKnife.bind(this); + ((Infinity) getApplication()).getmAppComponent().inject(this); + setSupportActionBar(toolbar); commentParentTextView.setText(getIntent().getExtras().getString(COMMENT_PARENT_TEXT)); - - commentEditor.setBackgroundColor(getResources().getColor(android.R.color.transparent)); - - initToolbar(); + parentFullname = getIntent().getExtras().getString(PARENT_FULLNAME); } @Override @@ -75,53 +73,23 @@ public class CommentActivity extends AppCompatActivity { return true; case R.id.action_send_comment_activity: CommentData commentData = null; - Intent returnIntent = new Intent(); - returnIntent.putExtra(EXTRA_COMMENT_DATA, commentData); - setResult(RESULT_OK, returnIntent); + SendComment.sendComment(commentEditText.getText().toString(), parentFullname, mOauthRetrofit, + sharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""), + new SendComment.SendCommentListener() { + @Override + public void sendCommentSuccess() { + Intent returnIntent = new Intent(); + returnIntent.putExtra(EXTRA_COMMENT_DATA, commentData); + setResult(RESULT_OK, returnIntent); + finish(); + } + + @Override + public void sendCommentFailed() { + Snackbar.make(coordinatorLayout, R.string.send_comment_failed, Snackbar.LENGTH_SHORT).show(); + } + }); } return super.onOptionsItemSelected(item); } - - private void initToolbar() { - IARE_ToolItem bold = new ARE_ToolItem_Bold(); - IARE_ToolItem italic = new ARE_ToolItem_Italic(); - IARE_ToolItem underline = new ARE_ToolItem_Underline(); - IARE_ToolItem strikethrough = new ARE_ToolItem_Strikethrough(); - IARE_ToolItem quote = new ARE_ToolItem_Quote(); - IARE_ToolItem listNumber = new ARE_ToolItem_ListNumber(); - IARE_ToolItem listBullet = new ARE_ToolItem_ListBullet(); - IARE_ToolItem hr = new ARE_ToolItem_Hr(); - IARE_ToolItem link = new ARE_ToolItem_Link(); - IARE_ToolItem subscript = new ARE_ToolItem_Subscript(); - IARE_ToolItem superscript = new ARE_ToolItem_Superscript(); - IARE_ToolItem left = new ARE_ToolItem_AlignmentLeft(); - IARE_ToolItem center = new ARE_ToolItem_AlignmentCenter(); - IARE_ToolItem right = new ARE_ToolItem_AlignmentRight(); - IARE_ToolItem image = new ARE_ToolItem_Image(); - IARE_ToolItem video = new ARE_ToolItem_Video(); - IARE_ToolItem at = new ARE_ToolItem_At(); - editorToolbar.addToolbarItem(bold); - editorToolbar.addToolbarItem(italic); - editorToolbar.addToolbarItem(underline); - editorToolbar.addToolbarItem(strikethrough); - editorToolbar.addToolbarItem(quote); - editorToolbar.addToolbarItem(listNumber); - editorToolbar.addToolbarItem(listBullet); - editorToolbar.addToolbarItem(hr); - editorToolbar.addToolbarItem(link); - editorToolbar.addToolbarItem(subscript); - editorToolbar.addToolbarItem(superscript); - editorToolbar.addToolbarItem(left); - editorToolbar.addToolbarItem(center); - editorToolbar.addToolbarItem(right); - editorToolbar.addToolbarItem(image); - editorToolbar.addToolbarItem(video); - editorToolbar.addToolbarItem(at); - - commentEditor.setToolbar(editorToolbar); - - /*setHtml(); - - initToolbarArrow();*/ - } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java index 00feeb58..682d4fb3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java @@ -73,4 +73,8 @@ public interface RedditAPI { @GET("r/{subredditName}/search.json?raw_json=1&type=link&restrict_sr=true") Call searchPostsInSpecificSubreddit(@Query("q") String query, @Query("after") String after, @HeaderMap Map headers); + + @FormUrlEncoded + @POST("/api/comment") + Call sendComment(@HeaderMap Map headers, @FieldMap Map params); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java index d598f684..c6964764 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java @@ -48,6 +48,11 @@ public class RedditUtils { static final String ACTION_KEY = "action"; static final String SR_NAME_KEY = "sr_name"; + static final String API_TYPE_KEY = "api_type"; + static final String RETURN_RTJSON_KEY = "return_rtjson"; + static final String TEXT_KEY = "text"; + static final String THING_ID_KEY = "thing_id"; + static Map getHttpBasicAuthHeader() { Map params = new HashMap<>(); String credentials = String.format("%s:%s", RedditUtils.CLIENT_ID, ""); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SendComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SendComment.java new file mode 100644 index 00000000..5f7da2d7 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SendComment.java @@ -0,0 +1,51 @@ +package ml.docilealligator.infinityforreddit; + +import android.util.Log; + +import androidx.annotation.NonNull; + +import java.util.HashMap; +import java.util.Map; + +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; + +class SendComment { + + interface SendCommentListener { + void sendCommentSuccess(); + void sendCommentFailed(); + } + + static void sendComment(String commentMarkdown, String thingFullname, Retrofit oauthRetrofit, + String accessToken, SendCommentListener sendCommentListener) { + RedditAPI api = oauthRetrofit.create(RedditAPI.class); + Map headers = RedditUtils.getOAuthHeader(accessToken); + Map params = new HashMap<>(); + params.put(RedditUtils.API_TYPE_KEY, "json"); + params.put(RedditUtils.RETURN_RTJSON_KEY, "true"); + params.put(RedditUtils.TEXT_KEY, commentMarkdown); + params.put(RedditUtils.THING_ID_KEY, thingFullname); + api.sendComment(headers, params); + + Call sendCommentCall = api.sendComment(headers, params); + sendCommentCall.enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if(response.isSuccessful()) { + Log.i("asdfasdfaf", response.body()); + sendCommentListener.sendCommentSuccess(); + } else { + sendCommentListener.sendCommentFailed(); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + sendCommentListener.sendCommentFailed(); + } + }); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index ea8e8b4c..ff9d3dd4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -64,7 +64,6 @@ import retrofit2.Retrofit; import ru.noties.markwon.SpannableConfiguration; import ru.noties.markwon.view.MarkwonView; -import static ml.docilealligator.infinityforreddit.CommentActivity.EXTRA_COMMENT_DATA; import static ml.docilealligator.infinityforreddit.CommentActivity.WRITE_COMMENT_REQUEST_CODE; public class ViewPostDetailActivity extends AppCompatActivity { @@ -686,6 +685,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { case R.id.action_comment_view_post_detail_activity: Intent intent = new Intent(this, CommentActivity.class); intent.putExtra(CommentActivity.COMMENT_PARENT_TEXT, mPost.getTitle()); + intent.putExtra(CommentActivity.PARENT_FULLNAME, mPost.getFullName()); startActivityForResult(intent, WRITE_COMMENT_REQUEST_CODE); return true; case android.R.id.home: @@ -699,8 +699,8 @@ public class ViewPostDetailActivity extends AppCompatActivity { protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if(resultCode == RESULT_OK && requestCode == WRITE_COMMENT_REQUEST_CODE) { - CommentData comment = data.getExtras().getParcelable(EXTRA_COMMENT_DATA); - mAdapter.addComment(comment); + /*CommentData comment = data.getExtras().getParcelable(EXTRA_COMMENT_DATA); + mAdapter.addComment(comment);*/ } } -- cgit v1.2.3