Added a feature: Sending comments.

This commit is contained in:
Alex Ning 2019-06-14 10:30:26 +08:00
parent 42c7b316f3
commit f69ae29bb9
11 changed files with 115 additions and 92 deletions

Binary file not shown.

View File

@ -75,5 +75,5 @@ dependencies {
implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.16'
implementation 'com.github.Ferfalk:SimpleSearchView:0.1.3'
implementation 'org.greenrobot:eventbus:3.1.1'
implementation 'com.github.chinalwb:are:0.1.5'
implementation 'jp.wasabeef:richeditor-android:1.2.2'
}

View File

@ -15,4 +15,5 @@ interface AppComponent {
void inject(ViewPostDetailActivity viewPostDetailActivity);
void inject(ViewSubredditDetailActivity viewSubredditDetailActivity);
void inject(ViewUserDetailActivity viewUserDetailActivity);
void inject(CommentActivity commentActivity);
}

View File

@ -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();*/
}
}

View File

@ -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);
}

View File

@ -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, "");

View File

@ -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();
}
});
}
}

View File

@ -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);*/
}
}

View File

@ -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"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="8dp"
android:layout_weight="1"
android:gravity="top"
android:hint="@string/write_comment_hint"
android:inputType="textMultiLine|textCapSentences"
android:textSize="18sp"
android:textColor="@color/primaryTextColor"/>
<com.chinalwb.are.styles.toolbar.ARE_ToolbarDefault
android:id="@+id/areToolbar"
<EditText
android:id="@+id/comment_edit_text_comment_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical" />
android:hint="@string/write_comment_hint"
android:inputType="textCapSentences|textMultiLine"
android:textSize="18sp"
android:background="#00000000" />
</LinearLayout>

View File

@ -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>