diff options
Diffstat (limited to 'app/src/main/java/ml')
7 files changed, 197 insertions, 4 deletions
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<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); } 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<String, String> headers = APIUtils.getOAuthHeader(accessToken); Map<String, String> 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<String> sendCommentCall = api.sendCommentOrReplyToMessage(headers, params); - sendCommentCall.enqueue(new Callback<String>() { + oauthRetrofit.create(RedditAPI.class).sendCommentOrReplyToMessage(headers, params).enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> 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<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); + } +} 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<String, String> getHttpBasicAuthHeader() { Map<String, String> params = new HashMap<>(); String credentials = String.format("%s:%s", APIUtils.CLIENT_ID, ""); |