diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CommentActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CommentActivity.java index 8eeb0a07..684b9d6d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CommentActivity.java @@ -316,6 +316,7 @@ public class CommentActivity extends BaseActivity { if (!isSubmitting) { isSubmitting = true; if (commentEditText.getText() == null || commentEditText.getText().toString().equals("")) { + isSubmitting = false; Snackbar.make(coordinatorLayout, R.string.comment_content_required, Snackbar.LENGTH_SHORT).show(); return true; } @@ -352,7 +353,7 @@ public class CommentActivity extends BaseActivity { item.setEnabled(true); item.getIcon().setAlpha(255); - if (errorMessage == null) { + if (errorMessage == null || !errorMessage.equals("")) { Snackbar.make(coordinatorLayout, R.string.send_comment_failed, Snackbar.LENGTH_SHORT).show(); } else { Snackbar.make(coordinatorLayout, errorMessage, Snackbar.LENGTH_SHORT).show(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SendPrivateMessageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SendPrivateMessageActivity.java index 9fa56478..c000079d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SendPrivateMessageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SendPrivateMessageActivity.java @@ -7,26 +7,32 @@ import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.EditText; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.widget.Toolbar; import androidx.coordinatorlayout.widget.CoordinatorLayout; import com.google.android.material.appbar.AppBarLayout; +import com.google.android.material.snackbar.Snackbar; import javax.inject.Inject; import javax.inject.Named; import butterknife.BindView; import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.AsyncTask.GetCurrentAccountAsyncTask; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.Message.ComposeMessage; 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"; + private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; + private static final String ACCESS_TOKEN_STATE = "ATS"; @BindView(R.id.coordinator_layout_send_private_message_activity) CoordinatorLayout coordinatorLayout; @BindView(R.id.appbar_layout_send_private_message_activity) @@ -53,6 +59,9 @@ public class SendPrivateMessageActivity extends BaseActivity { SharedPreferences mSharedPreferences; @Inject CustomThemeWrapper mCustomThemeWrapper; + private boolean mNullAccessToken = false; + private String mAccessToken; + private boolean isSubmitting = false; @Override protected void onCreate(Bundle savedInstanceState) { @@ -68,6 +77,16 @@ public class SendPrivateMessageActivity extends BaseActivity { addOnOffsetChangedListener(appBarLayout); } + if (savedInstanceState == null) { + getCurrentAccount(); + } else { + mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); + mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE); + if (!mNullAccessToken && mAccessToken == null) { + getCurrentAccount(); + } + } + setSupportActionBar(toolbar); String username = getIntent().getStringExtra(EXTRA_RECIPIENT_USERNAME); @@ -76,6 +95,16 @@ public class SendPrivateMessageActivity extends BaseActivity { } } + private void getCurrentAccount() { + new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> { + if (account == null) { + mNullAccessToken = true; + } else { + mAccessToken = account.getAccessToken(); + } + }).execute(); + } + @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.send_private_message_activity, menu); @@ -89,11 +118,69 @@ public class SendPrivateMessageActivity extends BaseActivity { finish(); return true; } else if (item.getItemId() == R.id.action_send_send_private_message_activity) { + if (!isSubmitting) { + isSubmitting = true; + if (usernameEditText.getText() == null || usernameEditText.getText().toString().equals("")) { + isSubmitting = false; + Snackbar.make(coordinatorLayout, R.string.message_username_required, Snackbar.LENGTH_LONG).show(); + 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(); + return true; + } + + item.setEnabled(false); + item.getIcon().setAlpha(130); + 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(); + } + + @Override + public void composeMessageFailed(String errorMessage) { + 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(); + } + } + }); + } } return false; } + @Override + protected void onSaveInstanceState(@NonNull Bundle outState) { + super.onSaveInstanceState(outState); + outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); + outState.putString(ACCESS_TOKEN_STATE, mAccessToken); + } + @Override protected SharedPreferences getDefaultSharedPreferences() { return mSharedPreferences; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Message/ComposeMessage.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Message/ComposeMessage.java index f0697a57..019e1207 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Message/ComposeMessage.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Message/ComposeMessage.java @@ -1,5 +1,7 @@ package ml.docilealligator.infinityforreddit.Message; +import android.util.Log; + import androidx.annotation.NonNull; import java.util.HashMap; @@ -19,6 +21,7 @@ public class ComposeMessage { 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.SUBJECT_KEY, subject); params.put(APIUtils.TEXT_KEY, message); params.put(APIUtils.TO_KEY, username); @@ -27,18 +30,18 @@ public class ComposeMessage { @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); - } + ParseMessage.parseComposedMessageError(response.body(), new ParseMessage.ParseComposedMessageErrorListener() { + @Override + public void noError() { + composeMessageListener.composeMessageSuccess(); + } - @Override - public void parseFailed(String errorMessage) { - composeMessageListener.composeMessageFailed(errorMessage); - } - }); + @Override + public void error(String errorMessage) { + composeMessageListener.composeMessageFailed(errorMessage); + } + }); + Log.i("asfsdaf", "s " + response.body()); } else { composeMessageListener.composeMessageFailed(response.message()); } @@ -52,7 +55,7 @@ public class ComposeMessage { } public interface ComposeMessageListener { - void composeMessageSuccess(Message message); + void composeMessageSuccess(); void composeMessageFailed(String errorMessage); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Message/ParseMessage.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Message/ParseMessage.java index 6b4048e4..6d489703 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Message/ParseMessage.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Message/ParseMessage.java @@ -41,6 +41,10 @@ public class ParseMessage { new ParseSentMessageAsnycTask(response, locale, parseSentMessageAsyncTaskListener).execute(); } + public static void parseComposedMessageError(String response, ParseComposedMessageErrorListener parseComposedMessageErrorListener) { + new ParseComposedMessageErrorAsncTask(response, parseComposedMessageErrorListener).execute(); + } + @Nullable private static Message parseSingleMessage(JSONObject messageJSON, Locale locale, int messageType) throws JSONException { String kind = messageJSON.getString(JSONUtils.KIND_KEY); @@ -165,6 +169,33 @@ public class ParseMessage { } } + private static class ParseComposedMessageErrorAsncTask extends AsyncTask { + private String response; + private ParseComposedMessageErrorListener parseComposedMessageErrorListener; + private String errorMessage; + + ParseComposedMessageErrorAsncTask(String response, ParseComposedMessageErrorListener parseComposedMessageErrorListener) { + this.response = response; + this.parseComposedMessageErrorListener = parseComposedMessageErrorListener; + } + + @Override + protected Void doInBackground(Void... voids) { + errorMessage = parseRepliedMessageErrorMessage(response); + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + if (errorMessage == null) { + parseComposedMessageErrorListener.noError(); + } else { + parseComposedMessageErrorListener.error(errorMessage); + } + } + } + @Nullable private static String parseRepliedMessageErrorMessage(String response) { try { @@ -202,4 +233,9 @@ public class ParseMessage { void parseSuccess(Message message); void parseFailed(String errorMessage); } + + public interface ParseComposedMessageErrorListener { + void noError(); + void error(String errorMessage); + } } 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 3938e90d..537ff6ef 100644 --- a/app/src/main/res/layout/activity_send_private_message.xml +++ b/app/src/main/res/layout/activity_send_private_message.xml @@ -68,6 +68,7 @@ android:paddingBottom="16dp" android:textColor="?attr/primaryTextColor" android:textSize="?attr/content_font_18" + android:maxLength="100" android:fontFamily="?attr/content_font_family" /> The comment is sent but unable to get the sent comment User - Subject + Subject (100 characters max) Message Could not reply to this message Error getting this message + Hey, to whom to you want to send this message? + Your message should have a subject + You need to tell the recipient something + Sending + Message sent + Could not send this message Please select a subreddit first The post need a good title