Composing message is now available.

This commit is contained in:
Alex Ning 2020-07-08 11:34:27 +08:00
parent 40fd7bf40f
commit 406d82ef52
6 changed files with 148 additions and 14 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -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" />
<View

View File

@ -155,10 +155,16 @@
<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_subject_hint">Subject (100 characters max)</string>
<string name="send_message_content_hint">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="message_username_required">Hey, to whom to you want to send this message?</string>
<string name="message_subject_required">Your message should have a subject</string>
<string name="message_content_required">You need to tell the recipient something</string>
<string name="sending_message">Sending</string>
<string name="send_message_success">Message sent</string>
<string name="send_message_failed">Could not send this message</string>
<string name="select_a_subreddit">Please select a subreddit first</string>
<string name="title_required">The post need a good title</string>