From 40fd7bf40f698b7670003cd28bc8db484c8615dc Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 7 Jul 2020 23:35:18 +0800 Subject: [PATCH] Prepare to add composing private message feature. --- app/build.gradle | 4 + app/src/main/AndroidManifest.xml | 8 +- .../infinityforreddit/API/RedditAPI.java | 4 + .../Activity/SendPrivateMessageActivity.java | 123 ++++++++++++++++++ .../Activity/ViewUserDetailActivity.java | 5 + .../infinityforreddit/AppComponent.java | 3 + .../Comment/SendComment.java | 5 +- .../Message/ComposeMessage.java | 58 +++++++++ .../infinityforreddit/Utils/APIUtils.java | 3 + .../layout/activity_send_private_message.xml | 98 ++++++++++++++ .../menu/send_private_message_activity.xml | 10 ++ .../res/menu/view_user_detail_activity.xml | 6 + app/src/main/res/values/strings.xml | 5 + 13 files changed, 327 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SendPrivateMessageActivity.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Message/ComposeMessage.java create mode 100644 app/src/main/res/layout/activity_send_private_message.xml create mode 100644 app/src/main/res/menu/send_private_message_activity.xml diff --git a/app/build.gradle b/app/build.gradle index 76f05962..e70eeda9 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -87,6 +87,10 @@ dependencies { implementation 'com.google.code.gson:gson:2.8.6' implementation 'me.zhanghai.android.fastscroll:library:1.1.2' implementation "com.thefuntasty.hauler:core:3.1.0" + // androidX startup for auto-init + implementation "androidx.startup:startup-runtime:1.0.0-alpha01" + //crashy + implementation 'com.github.CraZyLegenD:Crashy:1.0.3' def toroVersion = '3.7.0.2010003' implementation "im.ene.toro3:toro:$toroVersion" diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index d92c8ba0..4d30b837 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -23,13 +23,19 @@ android:theme="@style/AppTheme" android:usesCleartextTraffic="true" tools:replace="android:label"> + - diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/API/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/API/RedditAPI.java index fd6e71c7..fbfdd32a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/API/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/API/RedditAPI.java @@ -308,4 +308,8 @@ public interface RedditAPI { @FormUrlEncoded @POST("/api/report") Call report(@HeaderMap Map headers, @FieldMap Map params); + + @FormUrlEncoded + @POST("/api/compose") + Call composePrivateMessage(@HeaderMap Map headers, @FieldMap Map params); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SendPrivateMessageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SendPrivateMessageActivity.java new file mode 100644 index 00000000..9fa56478 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SendPrivateMessageActivity.java @@ -0,0 +1,123 @@ +package ml.docilealligator.infinityforreddit.Activity; + +import android.content.SharedPreferences; +import android.os.Build; +import android.os.Bundle; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; +import android.widget.EditText; + +import androidx.annotation.NonNull; +import androidx.appcompat.widget.Toolbar; +import androidx.coordinatorlayout.widget.CoordinatorLayout; + +import com.google.android.material.appbar.AppBarLayout; + +import javax.inject.Inject; +import javax.inject.Named; + +import butterknife.BindView; +import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import retrofit2.Retrofit; + +public class SendPrivateMessageActivity extends BaseActivity { + public static final String EXTRA_RECIPIENT_USERNAME = "ERU"; + @BindView(R.id.coordinator_layout_send_private_message_activity) + CoordinatorLayout coordinatorLayout; + @BindView(R.id.appbar_layout_send_private_message_activity) + AppBarLayout appBarLayout; + @BindView(R.id.toolbar_send_private_message_activity) + Toolbar toolbar; + @BindView(R.id.username_edit_text_send_private_message_activity) + EditText usernameEditText; + @BindView(R.id.divider_1_send_private_message_activity) + View divider1; + @BindView(R.id.subjet_edit_text_send_private_message_activity) + EditText subjectEditText; + @BindView(R.id.divider_2_send_private_message_activity) + View divider2; + @BindView(R.id.content_edit_text_send_private_message_activity) + EditText messageEditText; + @Inject + @Named("oauth") + Retrofit mOauthRetrofit; + @Inject + RedditDataRoomDatabase mRedditDataRoomDatabase; + @Inject + @Named("default") + SharedPreferences mSharedPreferences; + @Inject + CustomThemeWrapper mCustomThemeWrapper; + + @Override + protected void onCreate(Bundle savedInstanceState) { + ((Infinity) getApplication()).getAppComponent().inject(this); + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_send_private_message); + + ButterKnife.bind(this); + + applyCustomTheme(); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && isChangeStatusBarIconColor()) { + addOnOffsetChangedListener(appBarLayout); + } + + setSupportActionBar(toolbar); + + String username = getIntent().getStringExtra(EXTRA_RECIPIENT_USERNAME); + if (username != null) { + usernameEditText.setText(username); + } + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.send_private_message_activity, menu); + applyMenuItemTheme(menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(@NonNull MenuItem item) { + if (item.getItemId() == android.R.id.home) { + finish(); + return true; + } else if (item.getItemId() == R.id.action_send_send_private_message_activity) { + + } + return false; + } + + @Override + protected SharedPreferences getDefaultSharedPreferences() { + return mSharedPreferences; + } + + @Override + protected CustomThemeWrapper getCustomThemeWrapper() { + return mCustomThemeWrapper; + } + + @Override + protected void applyCustomTheme() { + coordinatorLayout.setBackgroundColor(mCustomThemeWrapper.getBackgroundColor()); + applyAppBarLayoutAndToolbarTheme(appBarLayout, toolbar); + int primaryTextColor = mCustomThemeWrapper.getPrimaryTextColor(); + usernameEditText.setTextColor(primaryTextColor); + subjectEditText.setTextColor(primaryTextColor); + messageEditText.setTextColor(primaryTextColor); + int secondaryTextColor = mCustomThemeWrapper.getSecondaryTextColor(); + usernameEditText.setHintTextColor(secondaryTextColor); + subjectEditText.setHintTextColor(secondaryTextColor); + messageEditText.setHintTextColor(secondaryTextColor); + int dividerColor = mCustomThemeWrapper.getDividerColor(); + divider1.setBackgroundColor(dividerColor); + divider2.setBackgroundColor(dividerColor); + } +} \ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java index 30891f38..04847b7f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java @@ -652,6 +652,11 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele Toast.makeText(this, R.string.no_app, Toast.LENGTH_SHORT).show(); } return true; + case R.id.action_send_private_message_view_user_detail_activity: + Intent pmIntent = new Intent(this, SendPrivateMessageActivity.class); + pmIntent.putExtra(SendPrivateMessageActivity.EXTRA_RECIPIENT_USERNAME, username); + startActivity(pmIntent); + return true; } return false; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index db9b4ddb..6bbc049a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -27,6 +27,7 @@ import ml.docilealligator.infinityforreddit.Activity.SearchActivity; import ml.docilealligator.infinityforreddit.Activity.SearchResultActivity; import ml.docilealligator.infinityforreddit.Activity.SearchSubredditsResultActivity; import ml.docilealligator.infinityforreddit.Activity.SelectedSubredditsActivity; +import ml.docilealligator.infinityforreddit.Activity.SendPrivateMessageActivity; import ml.docilealligator.infinityforreddit.Activity.SettingsActivity; import ml.docilealligator.infinityforreddit.Activity.SubredditMultiselectionActivity; import ml.docilealligator.infinityforreddit.Activity.SubredditSelectionActivity; @@ -179,4 +180,6 @@ public interface AppComponent { void inject(InboxFragment inboxFragment); void inject(ViewPrivateMessagesActivity viewPrivateMessagesActivity); + + void inject(SendPrivateMessageActivity sendPrivateMessageActivity); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Comment/SendComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Comment/SendComment.java index 1ce8fac5..6991364f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Comment/SendComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Comment/SendComment.java @@ -19,17 +19,14 @@ public class SendComment { public static void sendComment(String commentMarkdown, String thingFullname, int parentDepth, Locale locale, Retrofit oauthRetrofit, String accessToken, SendCommentListener sendCommentListener) { - RedditAPI api = oauthRetrofit.create(RedditAPI.class); Map headers = APIUtils.getOAuthHeader(accessToken); Map params = new HashMap<>(); params.put(APIUtils.API_TYPE_KEY, "json"); params.put(APIUtils.RETURN_RTJSON_KEY, "true"); params.put(APIUtils.TEXT_KEY, commentMarkdown); params.put(APIUtils.THING_ID_KEY, thingFullname); - api.sendCommentOrReplyToMessage(headers, params); - Call sendCommentCall = api.sendCommentOrReplyToMessage(headers, params); - sendCommentCall.enqueue(new Callback() { + oauthRetrofit.create(RedditAPI.class).sendCommentOrReplyToMessage(headers, params).enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.isSuccessful()) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Message/ComposeMessage.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Message/ComposeMessage.java new file mode 100644 index 00000000..f0697a57 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Message/ComposeMessage.java @@ -0,0 +1,58 @@ +package ml.docilealligator.infinityforreddit.Message; + +import androidx.annotation.NonNull; + +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + +import ml.docilealligator.infinityforreddit.API.RedditAPI; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; + +public class ComposeMessage { + public static void composeMessage(Retrofit oauthRetrofit, String accessToken, Locale locale, String username, + String subject, String message, ComposeMessageListener composeMessageListener) { + Map headers = APIUtils.getOAuthHeader(accessToken); + Map params = new HashMap<>(); + params.put(APIUtils.API_TYPE_KEY, "json"); + params.put(APIUtils.SUBJECT_KEY, subject); + params.put(APIUtils.TEXT_KEY, message); + params.put(APIUtils.TO_KEY, username); + + oauthRetrofit.create(RedditAPI.class).composePrivateMessage(headers, params).enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful()) { + ParseMessage.parseRepliedMessage(response.body(), locale, + new ParseMessage.ParseSentMessageAsyncTaskListener() { + @Override + public void parseSuccess(Message message) { + composeMessageListener.composeMessageSuccess(message); + } + + @Override + public void parseFailed(String errorMessage) { + composeMessageListener.composeMessageFailed(errorMessage); + } + }); + } else { + composeMessageListener.composeMessageFailed(response.message()); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + composeMessageListener.composeMessageFailed(t.getMessage()); + } + }); + } + + public interface ComposeMessageListener { + void composeMessageSuccess(Message message); + void composeMessageFailed(String errorMessage); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/APIUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/APIUtils.java index 89b85bbe..1b00e57a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/APIUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/APIUtils.java @@ -92,6 +92,9 @@ public class APIUtils { public static final String REASON_KEY = "reason"; + public static final String SUBJECT_KEY = "subject"; + public static final String TO_KEY = "to"; + public static Map getHttpBasicAuthHeader() { Map params = new HashMap<>(); String credentials = String.format("%s:%s", APIUtils.CLIENT_ID, ""); diff --git a/app/src/main/res/layout/activity_send_private_message.xml b/app/src/main/res/layout/activity_send_private_message.xml new file mode 100644 index 00000000..3938e90d --- /dev/null +++ b/app/src/main/res/layout/activity_send_private_message.xml @@ -0,0 +1,98 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/send_private_message_activity.xml b/app/src/main/res/menu/send_private_message_activity.xml new file mode 100644 index 00000000..ba7f4a4f --- /dev/null +++ b/app/src/main/res/menu/send_private_message_activity.xml @@ -0,0 +1,10 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/view_user_detail_activity.xml b/app/src/main/res/menu/view_user_detail_activity.xml index 18c3ef16..50170f31 100644 --- a/app/src/main/res/menu/view_user_detail_activity.xml +++ b/app/src/main/res/menu/view_user_detail_activity.xml @@ -37,4 +37,10 @@ android:orderInCategory="6" android:title="@string/action_share" app:showAsAction="never" /> + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 72f64124..9a7ed547 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -28,6 +28,7 @@ Report Image %1$d/%2$d Video %1$d/%2$d + Send PM Open navigation drawer Close navigation drawer @@ -63,6 +64,7 @@ Report See Removed Set as Wallpaper + Send Private Message Error occurred when parsing the JSON response Error Retrieving the token @@ -152,6 +154,9 @@ Could not send this comment The comment is sent but unable to get the sent comment + User + Subject + Message Could not reply to this message Error getting this message