Prepare to add composing private message feature.

This commit is contained in:
Alex Ning 2020-07-07 23:35:18 +08:00
parent ba2a219168
commit 40fd7bf40f
13 changed files with 327 additions and 5 deletions

View File

@ -87,6 +87,10 @@ dependencies {
implementation 'com.google.code.gson:gson:2.8.6' implementation 'com.google.code.gson:gson:2.8.6'
implementation 'me.zhanghai.android.fastscroll:library:1.1.2' implementation 'me.zhanghai.android.fastscroll:library:1.1.2'
implementation "com.thefuntasty.hauler:core:3.1.0" 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' def toroVersion = '3.7.0.2010003'
implementation "im.ene.toro3:toro:$toroVersion" implementation "im.ene.toro3:toro:$toroVersion"

View File

@ -23,13 +23,19 @@
android:theme="@style/AppTheme" android:theme="@style/AppTheme"
android:usesCleartextTraffic="true" android:usesCleartextTraffic="true"
tools:replace="android:label"> tools:replace="android:label">
<activity android:name=".Activity.SendPrivateMessageActivity"
android:label="@string/send_private_message_activity_label"
android:parentActivityName=".Activity.MainActivity"
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="adjustResize" />
<service <service
android:name=".Service.DownloadRedditVideoService" android:name=".Service.DownloadRedditVideoService"
android:enabled="true" android:enabled="true"
android:exported="false" /> android:exported="false" />
<activity android:name=".Activity.ViewPrivateMessagesActivity" <activity
android:name=".Activity.ViewPrivateMessagesActivity"
android:parentActivityName=".Activity.MainActivity" android:parentActivityName=".Activity.MainActivity"
android:theme="@style/AppTheme.Slidable" android:theme="@style/AppTheme.Slidable"
android:windowSoftInputMode="adjustResize" /> android:windowSoftInputMode="adjustResize" />

View File

@ -308,4 +308,8 @@ public interface RedditAPI {
@FormUrlEncoded @FormUrlEncoded
@POST("/api/report") @POST("/api/report")
Call<String> report(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> params); Call<String> report(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> params);
@FormUrlEncoded
@POST("/api/compose")
Call<String> composePrivateMessage(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> params);
} }

View File

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

View File

@ -652,6 +652,11 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele
Toast.makeText(this, R.string.no_app, Toast.LENGTH_SHORT).show(); Toast.makeText(this, R.string.no_app, Toast.LENGTH_SHORT).show();
} }
return true; 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; return false;
} }

View File

@ -27,6 +27,7 @@ import ml.docilealligator.infinityforreddit.Activity.SearchActivity;
import ml.docilealligator.infinityforreddit.Activity.SearchResultActivity; import ml.docilealligator.infinityforreddit.Activity.SearchResultActivity;
import ml.docilealligator.infinityforreddit.Activity.SearchSubredditsResultActivity; import ml.docilealligator.infinityforreddit.Activity.SearchSubredditsResultActivity;
import ml.docilealligator.infinityforreddit.Activity.SelectedSubredditsActivity; import ml.docilealligator.infinityforreddit.Activity.SelectedSubredditsActivity;
import ml.docilealligator.infinityforreddit.Activity.SendPrivateMessageActivity;
import ml.docilealligator.infinityforreddit.Activity.SettingsActivity; import ml.docilealligator.infinityforreddit.Activity.SettingsActivity;
import ml.docilealligator.infinityforreddit.Activity.SubredditMultiselectionActivity; import ml.docilealligator.infinityforreddit.Activity.SubredditMultiselectionActivity;
import ml.docilealligator.infinityforreddit.Activity.SubredditSelectionActivity; import ml.docilealligator.infinityforreddit.Activity.SubredditSelectionActivity;
@ -179,4 +180,6 @@ public interface AppComponent {
void inject(InboxFragment inboxFragment); void inject(InboxFragment inboxFragment);
void inject(ViewPrivateMessagesActivity viewPrivateMessagesActivity); void inject(ViewPrivateMessagesActivity viewPrivateMessagesActivity);
void inject(SendPrivateMessageActivity sendPrivateMessageActivity);
} }

View File

@ -19,17 +19,14 @@ public class SendComment {
public static void sendComment(String commentMarkdown, String thingFullname, int parentDepth, public static void sendComment(String commentMarkdown, String thingFullname, int parentDepth,
Locale locale, Retrofit oauthRetrofit, String accessToken, Locale locale, Retrofit oauthRetrofit, String accessToken,
SendCommentListener sendCommentListener) { SendCommentListener sendCommentListener) {
RedditAPI api = oauthRetrofit.create(RedditAPI.class);
Map<String, String> headers = APIUtils.getOAuthHeader(accessToken); Map<String, String> headers = APIUtils.getOAuthHeader(accessToken);
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put(APIUtils.API_TYPE_KEY, "json"); params.put(APIUtils.API_TYPE_KEY, "json");
params.put(APIUtils.RETURN_RTJSON_KEY, "true"); params.put(APIUtils.RETURN_RTJSON_KEY, "true");
params.put(APIUtils.TEXT_KEY, commentMarkdown); params.put(APIUtils.TEXT_KEY, commentMarkdown);
params.put(APIUtils.THING_ID_KEY, thingFullname); params.put(APIUtils.THING_ID_KEY, thingFullname);
api.sendCommentOrReplyToMessage(headers, params);
Call<String> sendCommentCall = api.sendCommentOrReplyToMessage(headers, params); oauthRetrofit.create(RedditAPI.class).sendCommentOrReplyToMessage(headers, params).enqueue(new Callback<String>() {
sendCommentCall.enqueue(new Callback<String>() {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {

View File

@ -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<String, String> headers = APIUtils.getOAuthHeader(accessToken);
Map<String, String> 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<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> 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<String> call, @NonNull Throwable t) {
composeMessageListener.composeMessageFailed(t.getMessage());
}
});
}
public interface ComposeMessageListener {
void composeMessageSuccess(Message message);
void composeMessageFailed(String errorMessage);
}
}

View File

@ -92,6 +92,9 @@ public class APIUtils {
public static final String REASON_KEY = "reason"; public static final String REASON_KEY = "reason";
public static final String SUBJECT_KEY = "subject";
public static final String TO_KEY = "to";
public static Map<String, String> getHttpBasicAuthHeader() { public static Map<String, String> getHttpBasicAuthHeader() {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
String credentials = String.format("%s:%s", APIUtils.CLIENT_ID, ""); String credentials = String.format("%s:%s", APIUtils.CLIENT_ID, "");

View File

@ -0,0 +1,98 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/coordinator_layout_send_private_message_activity"
tools:context=".Activity.SendPrivateMessageActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar_layout_send_private_message_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar_send_private_message_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:navigationIcon="?attr/homeAsUpIndicator"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/username_edit_text_send_private_message_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000"
android:gravity="top"
android:hint="@string/send_message_username_hint"
android:inputType="textCapSentences|textMultiLine"
android:paddingTop="16dp"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:paddingBottom="16dp"
android:textColor="?attr/primaryTextColor"
android:textSize="?attr/content_font_18"
android:fontFamily="?attr/content_font_family" />
<View
android:id="@+id/divider_1_send_private_message_activity"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="16dp" />
<EditText
android:id="@+id/subjet_edit_text_send_private_message_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000"
android:gravity="top"
android:hint="@string/send_message_subject_hint"
android:inputType="textCapSentences|textMultiLine"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:paddingBottom="16dp"
android:textColor="?attr/primaryTextColor"
android:textSize="?attr/content_font_18"
android:fontFamily="?attr/content_font_family" />
<View
android:id="@+id/divider_2_send_private_message_activity"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="16dp" />
<EditText
android:id="@+id/content_edit_text_send_private_message_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000"
android:gravity="top"
android:hint="@string/send_message_content_hint"
android:inputType="textCapSentences|textMultiLine"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:paddingBottom="16dp"
android:textColor="?attr/primaryTextColor"
android:textSize="?attr/content_font_18"
android:fontFamily="?attr/content_font_family" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_send_send_private_message_activity"
android:orderInCategory="1"
android:title="@string/action_send"
android:icon="@drawable/ic_send_toolbar_24dp"
app:showAsAction="ifRoom" />
</menu>

View File

@ -37,4 +37,10 @@
android:orderInCategory="6" android:orderInCategory="6"
android:title="@string/action_share" android:title="@string/action_share"
app:showAsAction="never" /> app:showAsAction="never" />
<item
android:id="@+id/action_send_private_message_view_user_detail_activity"
android:orderInCategory="6"
android:title="@string/action_send_private_message"
app:showAsAction="never" />
</menu> </menu>

View File

@ -28,6 +28,7 @@
<string name="report_activity_label">Report</string> <string name="report_activity_label">Report</string>
<string name="view_imgur_media_activity_image_label">Image %1$d/%2$d</string> <string name="view_imgur_media_activity_image_label">Image %1$d/%2$d</string>
<string name="view_imgur_media_activity_video_label">Video %1$d/%2$d</string> <string name="view_imgur_media_activity_video_label">Video %1$d/%2$d</string>
<string name="send_private_message_activity_label">Send PM</string>
<string name="navigation_drawer_open">Open navigation drawer</string> <string name="navigation_drawer_open">Open navigation drawer</string>
<string name="navigation_drawer_close">Close navigation drawer</string> <string name="navigation_drawer_close">Close navigation drawer</string>
@ -63,6 +64,7 @@
<string name="action_report">Report</string> <string name="action_report">Report</string>
<string name="action_see_removed">See Removed</string> <string name="action_see_removed">See Removed</string>
<string name="action_set_wallpaper">Set as Wallpaper</string> <string name="action_set_wallpaper">Set as Wallpaper</string>
<string name="action_send_private_message">Send Private Message</string>
<string name="parse_json_response_error">Error occurred when parsing the JSON response</string> <string name="parse_json_response_error">Error occurred when parsing the JSON response</string>
<string name="retrieve_token_error">Error Retrieving the token</string> <string name="retrieve_token_error">Error Retrieving the token</string>
@ -152,6 +154,9 @@
<string name="send_comment_failed">Could not send this comment</string> <string name="send_comment_failed">Could not send this comment</string>
<string name="parse_sent_comment_failed">The comment is sent but unable to get the sent comment</string> <string name="parse_sent_comment_failed">The comment is sent but unable to get the sent comment</string>
<string name="send_message_username_hint">User</string>
<string name="send_message_subject_hint">Subject</string>
<string name="send_message_content_hint">Message</string>
<string name="reply_message_failed">Could not reply to this message</string> <string name="reply_message_failed">Could not reply to this message</string>
<string name="error_getting_message">Error getting this message</string> <string name="error_getting_message">Error getting this message</string>