From 0e31e40ef1809508e390cd7349806e2647c9219e Mon Sep 17 00:00:00 2001 From: Balazs Toldi Date: Fri, 18 Aug 2023 15:39:34 +0200 Subject: [PATCH] Send Private messages from ViewUserDetailActivity --- .../activities/InboxActivity.java | 6 +- .../SendPrivateMessageActivity.java | 201 ++++++++++++++---- .../ViewSubredditDetailActivity.java | 4 +- .../activities/ViewUserDetailActivity.java | 3 +- .../layout/activity_send_private_message.xml | 114 +++++----- 5 files changed, 218 insertions(+), 110 deletions(-) diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/activities/InboxActivity.java b/app/src/main/java/eu/toldi/infinityforlemmy/activities/InboxActivity.java index e5006498..f5f2cf29 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/activities/InboxActivity.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/activities/InboxActivity.java @@ -181,7 +181,7 @@ public class InboxActivity extends BaseActivity implements ActivityToolbarInterf if (i == EditorInfo.IME_ACTION_DONE) { Utils.hideKeyboard(this); Intent pmIntent = new Intent(this, SendPrivateMessageActivity.class); - pmIntent.putExtra(SendPrivateMessageActivity.EXTRA_RECIPIENT_USERNAME, thingEditText.getText().toString()); + //pmIntent.putExtra(SendPrivateMessageActivity.EXTRA_RECIPIENT_USERNAME, thingEditText.getText().toString()); startActivity(pmIntent); return true; } @@ -194,7 +194,7 @@ public class InboxActivity extends BaseActivity implements ActivityToolbarInterf -> { Utils.hideKeyboard(this); Intent pmIntent = new Intent(this, SendPrivateMessageActivity.class); - pmIntent.putExtra(SendPrivateMessageActivity.EXTRA_RECIPIENT_USERNAME, thingEditText.getText().toString()); + //pmIntent.putExtra(SendPrivateMessageActivity.EXTRA_RECIPIENT_USERNAME, thingEditText.getText().toString()); startActivity(pmIntent); }) .setNegativeButton(R.string.cancel, null) @@ -331,7 +331,7 @@ public class InboxActivity extends BaseActivity implements ActivityToolbarInterf if (resultCode == RESULT_OK && requestCode == SEARCH_USER_REQUEST_CODE && data != null) { String username = data.getStringExtra(SearchActivity.EXTRA_RETURN_USER_NAME); Intent intent = new Intent(this, SendPrivateMessageActivity.class); - intent.putExtra(SendPrivateMessageActivity.EXTRA_RECIPIENT_USERNAME, username); + //intent.putExtra(SendPrivateMessageActivity.EXTRA_RECIPIENT_USERNAME, username); startActivity(intent); } } diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/activities/SendPrivateMessageActivity.java b/app/src/main/java/eu/toldi/infinityforlemmy/activities/SendPrivateMessageActivity.java index 9f3e3b27..281bb229 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/activities/SendPrivateMessageActivity.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/activities/SendPrivateMessageActivity.java @@ -1,8 +1,13 @@ package eu.toldi.infinityforlemmy.activities; +import android.content.ActivityNotFoundException; +import android.content.Intent; import android.content.SharedPreferences; +import android.net.Uri; import android.os.Build; import android.os.Bundle; +import android.os.Environment; +import android.provider.MediaStore; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -10,12 +15,20 @@ import android.widget.EditText; import android.widget.Toast; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.appcompat.widget.Toolbar; import androidx.coordinatorlayout.widget.CoordinatorLayout; +import androidx.core.content.FileProvider; +import androidx.recyclerview.widget.RecyclerView; import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.snackbar.Snackbar; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.concurrent.Executor; + import javax.inject.Inject; import javax.inject.Named; @@ -23,13 +36,24 @@ import butterknife.BindView; import butterknife.ButterKnife; import eu.toldi.infinityforlemmy.Infinity; import eu.toldi.infinityforlemmy.R; +import eu.toldi.infinityforlemmy.RetrofitHolder; +import eu.toldi.infinityforlemmy.UploadImageEnabledActivity; +import eu.toldi.infinityforlemmy.UploadedImage; +import eu.toldi.infinityforlemmy.adapters.MarkdownBottomBarRecyclerViewAdapter; +import eu.toldi.infinityforlemmy.bottomsheetfragments.UploadedImagesBottomSheetFragment; import eu.toldi.infinityforlemmy.customtheme.CustomThemeWrapper; -import eu.toldi.infinityforlemmy.message.ComposeMessage; +import eu.toldi.infinityforlemmy.customviews.LinearLayoutManagerBugFixed; +import eu.toldi.infinityforlemmy.privatemessage.LemmyPrivateMessageAPI; +import eu.toldi.infinityforlemmy.privatemessage.PrivateMessage; +import eu.toldi.infinityforlemmy.user.BasicUserInfo; import eu.toldi.infinityforlemmy.utils.SharedPreferencesUtils; -import retrofit2.Retrofit; +import eu.toldi.infinityforlemmy.utils.Utils; -public class SendPrivateMessageActivity extends BaseActivity { - public static final String EXTRA_RECIPIENT_USERNAME = "ERU"; +public class SendPrivateMessageActivity extends BaseActivity implements UploadImageEnabledActivity { + public static final String EXTRA_RECIPIENT_USER_INFO = "ERUI"; + + private static final int PICK_IMAGE_REQUEST_CODE = 100; + private static final int CAPTURE_IMAGE_REQUEST_CODE = 200; @BindView(R.id.coordinator_layout_send_private_message_activity) CoordinatorLayout coordinatorLayout; @BindView(R.id.appbar_layout_send_private_message_activity) @@ -40,15 +64,20 @@ public class SendPrivateMessageActivity extends BaseActivity { 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; + + @BindView(R.id.markdown_bottom_bar_recycler_view_send_private_message_activity) + RecyclerView markdownBottomBarRecyclerView; @Inject - @Named("oauth") - Retrofit mOauthRetrofit; + @Named("no_oauth") + RetrofitHolder mRetrofit; + + @Inject + Executor mExecutor; + @Inject @Named("default") SharedPreferences mSharedPreferences; @@ -57,15 +86,24 @@ public class SendPrivateMessageActivity extends BaseActivity { SharedPreferences mCurrentAccountSharedPreferences; @Inject CustomThemeWrapper mCustomThemeWrapper; + + @Inject + LemmyPrivateMessageAPI mLemmyPrivateMessageAPI; private String mAccessToken; + + private BasicUserInfo mRecipientBasicUserInfo; private boolean isSubmitting = false; + private ArrayList uploadedImages = new ArrayList<>(); + + private Uri capturedImageUri; + @Override protected void onCreate(Bundle savedInstanceState) { ((Infinity) getApplication()).getAppComponent().inject(this); setImmersiveModeNotApplicable(); - + super.onCreate(savedInstanceState); setContentView(R.layout.activity_send_private_message); @@ -77,14 +115,66 @@ public class SendPrivateMessageActivity extends BaseActivity { addOnOffsetChangedListener(appBarLayout); } + MarkdownBottomBarRecyclerViewAdapter adapter = new MarkdownBottomBarRecyclerViewAdapter( + mCustomThemeWrapper, new MarkdownBottomBarRecyclerViewAdapter.ItemClickListener() { + @Override + public void onClick(int item) { + MarkdownBottomBarRecyclerViewAdapter.bindEditTextWithItemClickListener( + SendPrivateMessageActivity.this, messageEditText, item); + } + + @Override + public void onUploadImage() { + Utils.hideKeyboard(SendPrivateMessageActivity.this); + UploadedImagesBottomSheetFragment fragment = new UploadedImagesBottomSheetFragment(); + Bundle arguments = new Bundle(); + arguments.putParcelableArrayList(UploadedImagesBottomSheetFragment.EXTRA_UPLOADED_IMAGES, + uploadedImages); + fragment.setArguments(arguments); + fragment.show(getSupportFragmentManager(), fragment.getTag()); + } + }); + + markdownBottomBarRecyclerView.setLayoutManager(new LinearLayoutManagerBugFixed(this, + LinearLayoutManagerBugFixed.HORIZONTAL, false)); + markdownBottomBarRecyclerView.setAdapter(adapter); + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); setSupportActionBar(toolbar); - - String username = getIntent().getStringExtra(EXTRA_RECIPIENT_USERNAME); - if (username != null) { - usernameEditText.setText(username); + if (savedInstanceState != null) { + mRecipientBasicUserInfo = savedInstanceState.getParcelable(EXTRA_RECIPIENT_USER_INFO); + } else { + mRecipientBasicUserInfo = getIntent().getParcelableExtra(EXTRA_RECIPIENT_USER_INFO); } + + if (mRecipientBasicUserInfo != null) { + usernameEditText.setText(mRecipientBasicUserInfo.getQualifiedName()); + usernameEditText.setEnabled(false); + } else { + finish(); + } + + + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { + super.onActivityResult(requestCode, resultCode, data); + if (resultCode == RESULT_OK) { + if (requestCode == PICK_IMAGE_REQUEST_CODE) { + if (data == null) { + Toast.makeText(SendPrivateMessageActivity.this, R.string.error_getting_image, Toast.LENGTH_LONG).show(); + return; + } + Utils.uploadImageToReddit(this, mExecutor, mRetrofit, + mAccessToken, messageEditText, coordinatorLayout, data.getData(), uploadedImages); + } else if (requestCode == CAPTURE_IMAGE_REQUEST_CODE) { + Utils.uploadImageToReddit(this, mExecutor, mRetrofit, + mAccessToken, messageEditText, coordinatorLayout, capturedImageUri, uploadedImages); + } + } + } @Override @@ -108,12 +198,6 @@ public class SendPrivateMessageActivity extends BaseActivity { return true; } - if (subjectEditText.getText() == null || subjectEditText.getText().toString().equals("")) { - isSubmitting = false; - Snackbar.make(coordinatorLayout, R.string.message_subject_required, Snackbar.LENGTH_LONG).show(); - return true; - } - if (messageEditText.getText() == null || messageEditText.getText().toString().equals("")) { isSubmitting = false; Snackbar.make(coordinatorLayout, R.string.message_content_required, Snackbar.LENGTH_LONG).show(); @@ -125,32 +209,26 @@ public class SendPrivateMessageActivity extends BaseActivity { Snackbar sendingSnackbar = Snackbar.make(coordinatorLayout, R.string.sending_message, Snackbar.LENGTH_INDEFINITE); sendingSnackbar.show(); - ComposeMessage.composeMessage(mOauthRetrofit, mAccessToken, getResources().getConfiguration().locale, - usernameEditText.getText().toString(), subjectEditText.getText().toString(), - messageEditText.getText().toString(), new ComposeMessage.ComposeMessageListener() { - @Override - public void composeMessageSuccess() { - isSubmitting = false; - item.setEnabled(true); - item.getIcon().setAlpha(255); - Toast.makeText(SendPrivateMessageActivity.this, R.string.send_message_success, Toast.LENGTH_SHORT).show(); - finish(); - } + mLemmyPrivateMessageAPI.sendPrivateMessage(mAccessToken, mRecipientBasicUserInfo.getId(), messageEditText.getText().toString(), new LemmyPrivateMessageAPI.PrivateMessageSentListener() { + @Override + public void onPrivateMessageSentSuccess(@NonNull PrivateMessage privateMessage) { + isSubmitting = false; + item.setEnabled(true); + item.getIcon().setAlpha(255); + Toast.makeText(SendPrivateMessageActivity.this, R.string.send_message_success, Toast.LENGTH_SHORT).show(); + finish(); + } - @Override - public void composeMessageFailed(String errorMessage) { - isSubmitting = false; - sendingSnackbar.dismiss(); - item.setEnabled(true); - item.getIcon().setAlpha(255); + @Override + public void onPrivateMessageSentError() { + isSubmitting = false; + sendingSnackbar.dismiss(); + item.setEnabled(true); + item.getIcon().setAlpha(255); - if (errorMessage == null || errorMessage.equals("")) { - Snackbar.make(coordinatorLayout, R.string.send_message_failed, Snackbar.LENGTH_LONG).show(); - } else { - Snackbar.make(coordinatorLayout, errorMessage, Snackbar.LENGTH_LONG).show(); - } - } - }); + Snackbar.make(coordinatorLayout, R.string.send_message_failed, Snackbar.LENGTH_LONG).show(); + } + }); } } return false; @@ -159,6 +237,7 @@ public class SendPrivateMessageActivity extends BaseActivity { @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); + outState.putParcelable(EXTRA_RECIPIENT_USER_INFO, mRecipientBasicUserInfo); } @Override @@ -177,19 +256,49 @@ public class SendPrivateMessageActivity extends BaseActivity { applyAppBarLayoutAndCollapsingToolbarLayoutAndToolbarTheme(appBarLayout, null, 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); if (typeface != null) { usernameEditText.setTypeface(typeface); - subjectEditText.setTypeface(typeface); messageEditText.setTypeface(typeface); } } + + @Override + public void uploadImage() { + Intent intent = new Intent(); + intent.setType("image/*"); + intent.setAction(Intent.ACTION_GET_CONTENT); + startActivityForResult(Intent.createChooser(intent, + getResources().getString(R.string.select_from_gallery)), PICK_IMAGE_REQUEST_CODE); + } + + @Override + public void captureImage() { + Intent pictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); + try { + capturedImageUri = FileProvider.getUriForFile(this, "eu.toldi.infinityforlemmy.provider", + File.createTempFile("captured_image", ".jpg", getExternalFilesDir(Environment.DIRECTORY_PICTURES))); + pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri); + startActivityForResult(pictureIntent, CAPTURE_IMAGE_REQUEST_CODE); + } catch (IOException ex) { + Toast.makeText(this, R.string.error_creating_temp_file, Toast.LENGTH_SHORT).show(); + } catch (ActivityNotFoundException e) { + Toast.makeText(this, R.string.no_camera_available, Toast.LENGTH_SHORT).show(); + } + } + + @Override + public void insertImageUrl(UploadedImage uploadedImage) { + int start = Math.max(messageEditText.getSelectionStart(), 0); + int end = Math.max(messageEditText.getSelectionEnd(), 0); + messageEditText.getText().replace(Math.min(start, end), Math.max(start, end), + "![" + uploadedImage.imageName + "](" + uploadedImage.imageUrl + ")", + 0, "![]()".length() + uploadedImage.imageName.length() + uploadedImage.imageUrl.length()); + } } \ No newline at end of file diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewSubredditDetailActivity.java b/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewSubredditDetailActivity.java index 4b0f645a..47d6143e 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewSubredditDetailActivity.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewSubredditDetailActivity.java @@ -1227,8 +1227,8 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp } return true; } else if (itemId == R.id.action_contact_mods_view_subreddit_detail_activity) { - Intent intent = new Intent(this, SendPrivateMessageActivity.class); - intent.putExtra(SendPrivateMessageActivity.EXTRA_RECIPIENT_USERNAME, "r/" + communityName); + /* Intent intent = new Intent(this, SendPrivateMessageActivity.class); + intent.putExtra(SendPrivateMessageActivity.EXTRA_RECIPIENT_USERNAME, "r/" + communityName);*/ //startActivity(intent); return true; } else if (itemId == R.id.block_community_view_subreddit_detail_activity) { diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewUserDetailActivity.java b/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewUserDetailActivity.java index 84c9f9b3..dfced343 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewUserDetailActivity.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewUserDetailActivity.java @@ -108,6 +108,7 @@ import eu.toldi.infinityforlemmy.post.PostPagingSource; import eu.toldi.infinityforlemmy.readpost.InsertReadPost; import eu.toldi.infinityforlemmy.subreddit.ParseSubredditData; import eu.toldi.infinityforlemmy.subreddit.SubredditData; +import eu.toldi.infinityforlemmy.user.BasicUserInfo; import eu.toldi.infinityforlemmy.user.BlockUser; import eu.toldi.infinityforlemmy.user.FetchUserData; import eu.toldi.infinityforlemmy.user.UserDao; @@ -1268,7 +1269,7 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele } Intent pmIntent = new Intent(this, SendPrivateMessageActivity.class); - pmIntent.putExtra(SendPrivateMessageActivity.EXTRA_RECIPIENT_USERNAME, username); + pmIntent.putExtra(SendPrivateMessageActivity.EXTRA_RECIPIENT_USER_INFO, new BasicUserInfo(mUserData.getId(), username, qualifiedName, mUserData.getAvatar(), mUserData.getDisplayName())); startActivity(pmIntent); return true; } else if (itemId == R.id.action_add_to_post_filter_view_user_detail_activity) { diff --git a/app/src/main/res/layout/activity_send_private_message.xml b/app/src/main/res/layout/activity_send_private_message.xml index b70f857b..64fc58d4 100644 --- a/app/src/main/res/layout/activity_send_private_message.xml +++ b/app/src/main/res/layout/activity_send_private_message.xml @@ -23,77 +23,75 @@ - - + android:layout_height="0dp" + android:layout_weight="1"> - + android:orientation="vertical"> - + - + - + - + - + - + + + + \ No newline at end of file