From 2b9b8fddfabc40c41152db529ec9311226db7669 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 11 Jul 2019 22:56:15 +0800 Subject: Submitting posts to user account is available. --- .../infinityforreddit/AppComponent.java | 1 + .../infinityforreddit/PostTextActivity.java | 90 +++++++++++++++++++--- .../infinityforreddit/SubmitPost.java | 42 +++++++--- .../SubredditSelectionActivity.java | 6 +- .../SubscribedSubredditsListingFragment.java | 18 ++++- .../SubscribedSubredditsRecyclerViewAdapter.java | 56 ++++++++++---- 6 files changed, 173 insertions(+), 40 deletions(-) (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index db457e0c..ac6eca67 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -18,4 +18,5 @@ interface AppComponent { void inject(CommentActivity commentActivity); void inject(SubscribedThingListingActivity subscribedThingListingActivity); void inject(PostTextActivity postTextActivity); + void inject(SubscribedSubredditsListingFragment subscribedSubredditsListingFragment); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java index cc3f1358..75db3a73 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java @@ -35,6 +35,12 @@ import retrofit2.Retrofit; public class PostTextActivity extends AppCompatActivity { static final String EXTRA_SUBREDDIT_NAME = "ESN"; + static final String EXTRA_SUBREDDIT_ICON = "ESI"; + + private static final String SUBREDDIT_NAME_STATE = "SNS"; + private static final String SUBREDDIT_ICON_STATE = "SIS"; + private static final String SUBREDDIT_SELECTED_STATE = "SSS"; + private static final String SUBREDDIT_IS_USER_STATE = "SIUS"; private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0; @@ -45,6 +51,11 @@ public class PostTextActivity extends AppCompatActivity { @BindView(R.id.post_title_edit_text_post_text_activity) EditText titleEditText; @BindView(R.id.post_text_content_edit_text_post_text_activity) EditText contentEditText; + private String iconUrl; + private String subredditName; + private boolean subredditSelected = false; + private boolean subredditIsUser; + private RequestManager mGlide; private Locale mLocale; @@ -72,14 +83,43 @@ public class PostTextActivity extends AppCompatActivity { mGlide = Glide.with(this); mLocale = getResources().getConfiguration().locale; - if(getIntent().hasExtra(EXTRA_SUBREDDIT_NAME)) { - subreditNameTextView.setText(getIntent().getExtras().getString(EXTRA_SUBREDDIT_NAME)); + if(savedInstanceState != null) { + subredditName = savedInstanceState.getString(SUBREDDIT_NAME_STATE); + iconUrl = savedInstanceState.getString(SUBREDDIT_ICON_STATE); + subredditSelected = savedInstanceState.getBoolean(SUBREDDIT_SELECTED_STATE); + subredditIsUser = savedInstanceState.getBoolean(SUBREDDIT_IS_USER_STATE); + + if(subredditName != null) { + subreditNameTextView.setText(subredditName); + } + if(iconUrl != null && !iconUrl.equals("")) { + mGlide.load(iconUrl) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .error(mGlide.load(R.drawable.subreddit_default_icon) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))) + .into(iconGifImageView); + } else { + mGlide.load(R.drawable.subreddit_default_icon) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .into(iconGifImageView); + } } else { - mGlide.load(R.drawable.subreddit_default_icon) - .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) - .into(iconGifImageView); + if(getIntent().hasExtra(EXTRA_SUBREDDIT_NAME)) { + subredditName = getIntent().getExtras().getString(EXTRA_SUBREDDIT_NAME); + iconUrl = getIntent().getExtras().getString(EXTRA_SUBREDDIT_ICON); + subreditNameTextView.setText(subredditName); + } else { + mGlide.load(R.drawable.subreddit_default_icon) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .into(iconGifImageView); + } } + iconGifImageView.setOnClickListener(view -> { + Intent intent = new Intent(this, SubredditSelectionActivity.class); + startActivityForResult(intent, SUBREDDIT_SELECTION_REQUEST_CODE); + }); + subreditNameTextView.setOnClickListener(view -> { Intent intent = new Intent(this, SubredditSelectionActivity.class); startActivityForResult(intent, SUBREDDIT_SELECTION_REQUEST_CODE); @@ -99,12 +139,24 @@ public class PostTextActivity extends AppCompatActivity { finish(); return true; case R.id.action_send_post_text_activity: + if(!subredditSelected) { + Snackbar.make(coordinatorLayout, R.string.select_a_subreddit, Snackbar.LENGTH_SHORT).show(); + return true; + } + item.setEnabled(false); item.getIcon().setAlpha(130); Snackbar postingSnackbar = Snackbar.make(coordinatorLayout, R.string.posting, Snackbar.LENGTH_INDEFINITE); postingSnackbar.show(); - SubmitPost.submitPostText(mOauthRetrofit, sharedPreferences, mLocale, subreditNameTextView.getText().toString(), + String subredditName; + if(subredditIsUser) { + subredditName = "u_" + subreditNameTextView.getText().toString(); + } else { + subredditName = subreditNameTextView.getText().toString(); + } + + SubmitPost.submitPostText(mOauthRetrofit, sharedPreferences, mLocale, subredditName, titleEditText.getText().toString(), contentEditText.getText().toString(), false, new SubmitPost.SubmitPostListener() { @Override @@ -116,11 +168,15 @@ public class PostTextActivity extends AppCompatActivity { } @Override - public void submitFailed() { + public void submitFailed(@Nullable String errorMessage) { postingSnackbar.dismiss(); item.setEnabled(true); item.getIcon().setAlpha(255); - Snackbar.make(coordinatorLayout, R.string.post_failed, Snackbar.LENGTH_SHORT); + if(errorMessage == null) { + Snackbar.make(coordinatorLayout, R.string.post_failed, Snackbar.LENGTH_SHORT).show(); + } else { + Snackbar.make(coordinatorLayout, errorMessage, Snackbar.LENGTH_SHORT).show(); + } } }); return true; @@ -129,15 +185,27 @@ public class PostTextActivity extends AppCompatActivity { return false; } + @Override + protected void onSaveInstanceState(@NonNull Bundle outState) { + super.onSaveInstanceState(outState); + outState.putString(SUBREDDIT_NAME_STATE, subredditName); + outState.putString(SUBREDDIT_ICON_STATE, iconUrl); + outState.putBoolean(SUBREDDIT_SELECTED_STATE, subredditSelected); + outState.putBoolean(SUBREDDIT_IS_USER_STATE, subredditIsUser); + } + @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == SUBREDDIT_SELECTION_REQUEST_CODE) { if(resultCode == RESULT_OK) { - subreditNameTextView.setTextColor(getResources().getColor(R.color.primaryTextColor)); - subreditNameTextView.setText(data.getExtras().getString(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_NAME_KEY)); + subredditName = data.getExtras().getString(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_NAME_KEY); + iconUrl = data.getExtras().getString(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_ICON_URL_KEY); + subredditSelected = true; + subredditIsUser = data.getExtras().getBoolean(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_IS_USER_KEY); - String iconUrl = data.getExtras().getString(SubredditSelectionActivity.EXTRA_RETURN_SUBREDDIT_ICON_URL_KEY); + subreditNameTextView.setTextColor(getResources().getColor(R.color.primaryTextColor)); + subreditNameTextView.setText(subredditName); if(!iconUrl.equals("")) { mGlide.load(iconUrl) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java index 7de16719..d493c66a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java @@ -4,7 +4,9 @@ import android.content.SharedPreferences; import android.util.Log; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -19,7 +21,7 @@ import retrofit2.Retrofit; class SubmitPost { interface SubmitPostListener { void submitSuccessful(Post post); - void submitFailed(); + void submitFailed(@Nullable String errorMessage); } static void submitPostText(Retrofit oauthRetrofit, SharedPreferences authInfoSharedPreferences, @@ -32,7 +34,7 @@ class SubmitPost { params.put(RedditUtils.API_TYPE_KEY, RedditUtils.API_TYPE_JSON); params.put(RedditUtils.SR_KEY, subredditName); params.put(RedditUtils.TITLE_KEY, title); - params.put(RedditUtils.KIND_KEY, RedditUtils.KIND_TEXT); + params.put(RedditUtils.KIND_KEY, RedditUtils.KIND_SELF); params.put(RedditUtils.TEXT_KEY, text); params.put(RedditUtils.NSFW_KEY, Boolean.toString(isNSFW)); @@ -47,18 +49,18 @@ class SubmitPost { submitPostListener); } catch (JSONException e) { e.printStackTrace(); - submitPostListener.submitFailed(); + submitPostListener.submitFailed(null); } } else { Log.i("call_failed", response.message()); - submitPostListener.submitFailed(); + submitPostListener.submitFailed(null); } } @Override public void onFailure(@NonNull Call call, @NonNull Throwable t) { Log.i("call_failed", call.request().url().toString()); - submitPostListener.submitFailed(); + submitPostListener.submitFailed(null); } }); } @@ -66,13 +68,29 @@ class SubmitPost { private static void getSubmittedPost(String response, Retrofit oauthRetrofit, SharedPreferences authInfoSharedPreferences, Locale locale, SubmitPostListener submitPostListener) throws JSONException { - JSONObject responseObject = new JSONObject(response); - if(responseObject.getJSONObject(JSONUtils.JSON_KEY).getJSONArray(JSONUtils.ERRORS_KEY).length() != 0) { - submitPostListener.submitFailed(); + JSONObject responseObject = new JSONObject(response).getJSONObject(JSONUtils.JSON_KEY); + if(responseObject.getJSONArray(JSONUtils.ERRORS_KEY).length() != 0) { + JSONArray error = responseObject.getJSONArray(JSONUtils.ERRORS_KEY) + .getJSONArray(responseObject.getJSONArray(JSONUtils.ERRORS_KEY).length() - 1); + if(error.length() != 0) { + String errorString; + if(error.length() >= 2) { + errorString = error.getString(1); + errorString = errorString.substring(0, 1).toUpperCase() + errorString.substring(1); + submitPostListener.submitFailed(errorString); + } else { + errorString = error.getString(0); + errorString = errorString.substring(0, 1).toUpperCase() + errorString.substring(1); + submitPostListener.submitFailed(errorString); + } + } else { + submitPostListener.submitFailed(null); + } + return; } - String postId = responseObject.getJSONObject(JSONUtils.JSON_KEY).getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.ID_KEY); + String postId = responseObject.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.ID_KEY); RedditAPI api = oauthRetrofit.create(RedditAPI.class); String accessToken = authInfoSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); @@ -90,19 +108,19 @@ class SubmitPost { @Override public void onParsePostFail() { - submitPostListener.submitFailed(); + submitPostListener.submitFailed(null); } }); } else { Log.i("call_failed", response.message()); - submitPostListener.submitFailed(); + submitPostListener.submitFailed(response.message()); } } @Override public void onFailure(@NonNull Call call, @NonNull Throwable t) { Log.i("call_failed", call.request().url().toString()); - submitPostListener.submitFailed(); + submitPostListener.submitFailed(t.getMessage()); } }); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java index eece6d5c..0411eea0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java @@ -16,7 +16,8 @@ import butterknife.ButterKnife; public class SubredditSelectionActivity extends AppCompatActivity { static final String EXTRA_RETURN_SUBREDDIT_NAME_KEY = "ERSNK"; - static final String EXTRA_RETURN_SUBREDDIT_ICON_URL_KEY = "ERSIUK"; + static final String EXTRA_RETURN_SUBREDDIT_ICON_URL_KEY = "ERSIURLK"; + static final String EXTRA_RETURN_SUBREDDIT_IS_USER_KEY = "ERSIUK"; private SubscribedSubredditViewModel mSubscribedSubredditViewModel; @@ -49,10 +50,11 @@ public class SubredditSelectionActivity extends AppCompatActivity { return false; } - void getSelectedSubreddit(String name, String iconUrl) { + void getSelectedSubreddit(String name, String iconUrl, boolean subredditIsUser) { Intent returnIntent = new Intent(); returnIntent.putExtra(EXTRA_RETURN_SUBREDDIT_NAME_KEY, name); returnIntent.putExtra(EXTRA_RETURN_SUBREDDIT_ICON_URL_KEY, iconUrl); + returnIntent.putExtra(EXTRA_RETURN_SUBREDDIT_IS_USER_KEY, subredditIsUser); setResult(Activity.RESULT_OK, returnIntent); finish(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java index 2950d88c..c6e840a9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java @@ -2,6 +2,7 @@ package ml.docilealligator.infinityforreddit; import android.app.Activity; +import android.content.SharedPreferences; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -17,6 +18,9 @@ import androidx.recyclerview.widget.RecyclerView; import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; +import javax.inject.Inject; +import javax.inject.Named; + import SubscribedSubredditDatabase.SubscribedSubredditViewModel; import butterknife.BindView; import butterknife.ButterKnife; @@ -41,6 +45,10 @@ public class SubscribedSubredditsListingFragment extends Fragment { private boolean mInsertSuccess = false; + @Inject + @Named("user_info") + SharedPreferences sharedPreferences; + public SubscribedSubredditsListingFragment() { // Required empty public constructor } @@ -54,18 +62,23 @@ public class SubscribedSubredditsListingFragment extends Fragment { mActivity = getActivity(); + ((Infinity) mActivity.getApplication()).getmAppComponent().inject(this); + + String username = sharedPreferences.getString(SharedPreferencesUtils.USER_KEY, ""); + String userIconUrl = sharedPreferences.getString(SharedPreferencesUtils.PROFILE_IMAGE_URL_KEY, ""); + mGlide = Glide.with(this); mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity)); SubscribedSubredditsRecyclerViewAdapter adapter; if(getArguments().getBoolean(EXTRA_IS_SUBREDDIT_SELECTION)) { - adapter = new SubscribedSubredditsRecyclerViewAdapter(mActivity, (name, iconUrl) -> ((SubredditSelectionActivity) mActivity).getSelectedSubreddit(name, iconUrl)); + adapter = new SubscribedSubredditsRecyclerViewAdapter(mActivity, + (name, iconUrl, subredditIsUser) -> ((SubredditSelectionActivity) mActivity).getSelectedSubreddit(name, iconUrl, subredditIsUser)); } else { adapter = new SubscribedSubredditsRecyclerViewAdapter(mActivity); } - mRecyclerView.setAdapter(adapter); mSubscribedSubredditViewModel = ViewModelProviders.of(this).get(SubscribedSubredditViewModel.class); @@ -79,6 +92,7 @@ public class SubscribedSubredditsListingFragment extends Fragment { mRecyclerView.setVisibility(View.VISIBLE); } + adapter.addUser(username, userIconUrl); adapter.setSubscribedSubreddits(subscribedSubredditData); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsRecyclerViewAdapter.java index d9f37e73..7126213d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsRecyclerViewAdapter.java @@ -26,8 +26,11 @@ class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapter { - if(viewHolder.getAdapterPosition() >= 0) { - if(itemClickListener != null) { - itemClickListener.onClick(mSubscribedSubredditData.get(i).getName(), mSubscribedSubredditData.get(i).getIconUrl()); + String name; + String iconUrl; + + if(itemClickListener != null) { + if(viewHolder.getAdapterPosition() == 0) { + name = username; + iconUrl = userIconUrl; + } else { + name = mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 1).getName(); + iconUrl = mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 1).getIconUrl(); + } + + viewHolder.itemView.setOnClickListener(view -> { + if(viewHolder.getAdapterPosition() == 0) { + itemClickListener.onClick(name, iconUrl, true); } else { - Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class); - intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, mSubscribedSubredditData.get(viewHolder.getAdapterPosition()).getName()); - mContext.startActivity(intent); + itemClickListener.onClick(name, iconUrl, false); } - } - }); - if(!mSubscribedSubredditData.get(i).getIconUrl().equals("")) { - glide.load(mSubscribedSubredditData.get(i).getIconUrl()) + }); + } else { + name = mSubscribedSubredditData.get(viewHolder.getAdapterPosition()).getName(); + iconUrl = mSubscribedSubredditData.get(viewHolder.getAdapterPosition()).getIconUrl(); + + viewHolder.itemView.setOnClickListener(view -> { + Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class); + intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, name); + mContext.startActivity(intent); + }); + } + + if(!iconUrl.equals("")) { + glide.load(iconUrl) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) .error(glide.load(R.drawable.subreddit_default_icon) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))) @@ -71,12 +93,16 @@ class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapter