diff options
author | Alex Ning <chineseperson5@gmail.com> | 2019-06-14 02:30:26 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2019-06-14 02:30:26 +0000 |
commit | f69ae29bb93a113bad77bd258c92398bd45cd902 (patch) | |
tree | c091c9bc761c05cc4b928d9986a1f2e4caf4ff99 /app/src/main | |
parent | 42c7b316f37b17178518cbf9649a788fcdf0895c (diff) | |
download | infinity-for-reddit-f69ae29bb93a113bad77bd258c92398bd45cd902.tar infinity-for-reddit-f69ae29bb93a113bad77bd258c92398bd45cd902.tar.gz infinity-for-reddit-f69ae29bb93a113bad77bd258c92398bd45cd902.tar.bz2 infinity-for-reddit-f69ae29bb93a113bad77bd258c92398bd45cd902.tar.lz infinity-for-reddit-f69ae29bb93a113bad77bd258c92398bd45cd902.tar.xz infinity-for-reddit-f69ae29bb93a113bad77bd258c92398bd45cd902.tar.zst infinity-for-reddit-f69ae29bb93a113bad77bd258c92398bd45cd902.zip |
Added a feature: Sending comments.
Diffstat (limited to '')
8 files changed, 113 insertions, 90 deletions
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<String> searchPostsInSpecificSubreddit(@Query("q") String query, @Query("after") String after, @HeaderMap Map<String, String> headers); + + @FormUrlEncoded + @POST("/api/comment") + Call<String> sendComment(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> 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<String, String> getHttpBasicAuthHeader() { Map<String, String> 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<String, String> headers = RedditUtils.getOAuthHeader(accessToken); + Map<String, String> 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<String> sendCommentCall = api.sendComment(headers, params); + sendCommentCall.enqueue(new Callback<String>() { + @Override + public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { + if(response.isSuccessful()) { + Log.i("asdfasdfaf", response.body()); + sendCommentListener.sendCommentSuccess(); + } else { + sendCommentListener.sendCommentFailed(); + } + } + + @Override + public void onFailure(@NonNull Call<String> 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);*/ } } diff --git a/app/src/main/res/layout/activity_comment.xml b/app/src/main/res/layout/activity_comment.xml index 3beff433..c494cf3f 100644 --- a/app/src/main/res/layout/activity_comment.xml +++ b/app/src/main/res/layout/activity_comment.xml @@ -4,6 +4,7 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" + android:id="@+id/coordinator_layout_comment_activity" tools:context=".CommentActivity"> <com.google.android.material.appbar.AppBarLayout @@ -24,32 +25,24 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" + android:padding="16dp" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <TextView android:id="@+id/comment_parent_text_view_comment_activity" android:layout_width="match_parent" android:layout_height="wrap_content" - android:padding="16dp" + android:layout_marginBottom="16dp" android:textSize="16sp" /> - <com.chinalwb.are.AREditText - android:id="@+id/arEditText" + <EditText + android:id="@+id/comment_edit_text_comment_activity" android:layout_width="match_parent" - android:layout_height="0dp" - android:layout_margin="8dp" - android:layout_weight="1" - android:gravity="top" + android:layout_height="wrap_content" android:hint="@string/write_comment_hint" - android:inputType="textMultiLine|textCapSentences" + android:inputType="textCapSentences|textMultiLine" android:textSize="18sp" - android:textColor="@color/primaryTextColor"/> - - <com.chinalwb.are.styles.toolbar.ARE_ToolbarDefault - android:id="@+id/areToolbar" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:gravity="center_vertical" /> + android:background="#00000000" /> </LinearLayout> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7a3985eb..1b4968b0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -72,5 +72,6 @@ <string name="lazy_mode_stop">Lazy Mode stopped</string> <string name="write_comment_hint">Your interesting thoughts here</string> + <string name="send_comment_failed">Could not send this comment</string> </resources> |