From 68d3a7b09f42aef6c3ec1874af3c9dee4c3eef8c Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 12 Jul 2019 09:35:05 +0800 Subject: Submitting link posts is available. Fixed force stop when a user has no post in ViewUserDetailActivity. --- .../infinityforreddit/AppComponent.java | 1 + .../infinityforreddit/MainActivity.java | 24 ++- .../infinityforreddit/PostFragment.java | 7 +- .../infinityforreddit/PostLinkActivity.java | 223 +++++++++++++++++++++ .../infinityforreddit/PostTextActivity.java | 6 +- .../infinityforreddit/RedditUtils.java | 2 + .../infinityforreddit/SubmitPost.java | 14 +- .../SubredditSelectionActivity.java | 3 - 8 files changed, 265 insertions(+), 15 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java (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 ac6eca67..93862875 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -19,4 +19,5 @@ interface AppComponent { void inject(SubscribedThingListingActivity subscribedThingListingActivity); void inject(PostTextActivity postTextActivity); void inject(SubscribedSubredditsListingFragment subscribedSubredditsListingFragment); + void inject(PostLinkActivity postLinkActivity); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index a711091c..bdcfece5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -11,9 +11,11 @@ import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBarDrawerToggle; +import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import androidx.core.view.GravityCompat; @@ -220,8 +222,26 @@ public class MainActivity extends AppCompatActivity { } fab.setOnClickListener(view -> { - Intent intent = new Intent(this, PostTextActivity.class); - this.startActivity(intent); + new AlertDialog.Builder(this).setTitle(R.string.dialog_post_type) + .setItems(R.array.dialog_post_types_array, (dialog, which) -> { + Intent intent; + switch (which) { + case 0: + intent = new Intent(this, PostTextActivity.class); + break; + case 1: + intent = new Intent(this, PostLinkActivity.class); + break; + case 2: + Toast.makeText(this, "Not implemented yet", Toast.LENGTH_SHORT).show(); + return; + default: + Toast.makeText(this, "Not implemented yet", Toast.LENGTH_SHORT).show(); + return; + } + + startActivity(intent); + }).show(); }); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java index e91d2ab9..5f6bd33c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java @@ -25,6 +25,7 @@ import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearSmoothScroller; import androidx.recyclerview.widget.RecyclerView; +import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar; @@ -55,7 +56,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { @BindView(R.id.fetch_post_info_image_view_post_fragment) ImageView mFetchPostInfoImageView; @BindView(R.id.fetch_post_info_text_view_post_fragment) TextView mFetchPostInfoTextView; - private RequestManager glide; + private RequestManager mGlide; private Activity activity; private LinearLayoutManager mLinearLayoutManager; @@ -117,6 +118,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator { window = activity.getWindow(); + mGlide = Glide.with(activity); + lazyModeRunnable = new Runnable() { @Override public void run() { @@ -255,7 +258,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { if(activity != null && isAdded()) { mFetchPostInfoLinearLayout.setVisibility(View.VISIBLE); mFetchPostInfoTextView.setText(stringResId); - glide.load(R.drawable.load_post_error_indicator).into(mFetchPostInfoImageView); + mGlide.load(R.drawable.load_post_error_indicator).into(mFetchPostInfoImageView); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java new file mode 100644 index 00000000..193fabbc --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java @@ -0,0 +1,223 @@ +package ml.docilealligator.infinityforreddit; + +import android.content.Intent; +import android.content.SharedPreferences; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.view.Menu; +import android.view.MenuItem; +import android.widget.Button; +import android.widget.EditText; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.ActionBar; +import androidx.appcompat.app.AppCompatActivity; +import androidx.coordinatorlayout.widget.CoordinatorLayout; + +import com.bumptech.glide.Glide; +import com.bumptech.glide.RequestManager; +import com.bumptech.glide.request.RequestOptions; +import com.google.android.material.snackbar.Snackbar; + +import java.util.Locale; + +import javax.inject.Inject; +import javax.inject.Named; + +import butterknife.BindView; +import butterknife.ButterKnife; +import jp.wasabeef.glide.transformations.RoundedCornersTransformation; +import pl.droidsonroids.gif.GifImageView; +import retrofit2.Retrofit; + +public class PostLinkActivity 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; + + @BindView(R.id.coordinator_layout_post_link_activity) CoordinatorLayout coordinatorLayout; + @BindView(R.id.subreddit_icon_gif_image_view_post_link_activity) GifImageView iconGifImageView; + @BindView(R.id.subreddit_name_text_view_post_link_activity) TextView subreditNameTextView; + @BindView(R.id.rules_button_post_link_activity) Button rulesButton; + @BindView(R.id.post_title_edit_text_post_link_activity) EditText titleEditText; + @BindView(R.id.post_link_edit_text_post_link_activity) EditText contentEditText; + + private String iconUrl; + private String subredditName; + private boolean subredditSelected = false; + private boolean subredditIsUser; + + private RequestManager mGlide; + private Locale mLocale; + + @Inject + @Named("oauth") + Retrofit mOauthRetrofit; + + @Inject + @Named("auth_info") + SharedPreferences sharedPreferences; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_post_link); + + ButterKnife.bind(this); + + ((Infinity) getApplication()).getmAppComponent().inject(this); + + ActionBar actionBar = getSupportActionBar(); + Drawable upArrow = getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp); + actionBar.setHomeAsUpIndicator(upArrow); + + mGlide = Glide.with(this); + mLocale = getResources().getConfiguration().locale; + + 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 { + 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); + }); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.post_link_activity, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(@NonNull MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + finish(); + return true; + case R.id.action_send_post_link_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(); + + String subredditName; + if(subredditIsUser) { + subredditName = "u_" + subreditNameTextView.getText().toString(); + } else { + subredditName = subreditNameTextView.getText().toString(); + } + + SubmitPost.submitTextOrLinkPost(mOauthRetrofit, sharedPreferences, mLocale, subredditName, + titleEditText.getText().toString(), contentEditText.getText().toString(), + false, RedditUtils.KIND_LINK, new SubmitPost.SubmitPostListener() { + @Override + public void submitSuccessful(Post post) { + Intent intent = new Intent(PostLinkActivity.this, ViewPostDetailActivity.class); + intent.putExtra(ViewPostDetailActivity.EXTRA_POST_DATA, post); + startActivity(intent); + finish(); + } + + @Override + public void submitFailed(@Nullable String errorMessage) { + postingSnackbar.dismiss(); + item.setEnabled(true); + item.getIcon().setAlpha(255); + 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; + } + + 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) { + 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); + + subreditNameTextView.setTextColor(getResources().getColor(R.color.primaryTextColor)); + subreditNameTextView.setText(subredditName); + if(!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); + } + } + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java index 75db3a73..ea6e655d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java @@ -44,7 +44,7 @@ public class PostTextActivity extends AppCompatActivity { private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0; - @BindView(R.id.coordinator_layout_post_detail_activity) CoordinatorLayout coordinatorLayout; + @BindView(R.id.coordinator_layout_post_text_activity) CoordinatorLayout coordinatorLayout; @BindView(R.id.subreddit_icon_gif_image_view_post_text_activity) GifImageView iconGifImageView; @BindView(R.id.subreddit_name_text_view_post_text_activity) TextView subreditNameTextView; @BindView(R.id.rules_button_post_text_activity) Button rulesButton; @@ -156,9 +156,9 @@ public class PostTextActivity extends AppCompatActivity { subredditName = subreditNameTextView.getText().toString(); } - SubmitPost.submitPostText(mOauthRetrofit, sharedPreferences, mLocale, subredditName, + SubmitPost.submitTextOrLinkPost(mOauthRetrofit, sharedPreferences, mLocale, subredditName, titleEditText.getText().toString(), contentEditText.getText().toString(), - false, new SubmitPost.SubmitPostListener() { + false, RedditUtils.KIND_SELF, new SubmitPost.SubmitPostListener() { @Override public void submitSuccessful(Post post) { Intent intent = new Intent(PostTextActivity.this, ViewPostDetailActivity.class); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java index faf58e4a..b3b989f0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java @@ -52,6 +52,7 @@ public class RedditUtils { static final String API_TYPE_JSON = "json"; static final String RETURN_RTJSON_KEY = "return_rtjson"; static final String TEXT_KEY = "text"; + static final String URL_KEY = "url"; static final String THING_ID_KEY = "thing_id"; static final String SR_KEY = "sr"; @@ -60,6 +61,7 @@ public class RedditUtils { static final String KIND_KEY = "kind"; static final String KIND_TEXT = "text"; static final String KIND_SELF = "self"; + static final String KIND_LINK = "link"; static Map getHttpBasicAuthHeader() { Map params = new HashMap<>(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java index d493c66a..4a03398d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java @@ -24,9 +24,9 @@ class SubmitPost { void submitFailed(@Nullable String errorMessage); } - static void submitPostText(Retrofit oauthRetrofit, SharedPreferences authInfoSharedPreferences, - Locale locale, String subredditName, String title, String text, boolean isNSFW, - SubmitPostListener submitPostListener) { + static void submitTextOrLinkPost(Retrofit oauthRetrofit, SharedPreferences authInfoSharedPreferences, + Locale locale, String subredditName, String title, String content, boolean isNSFW, + String kind, SubmitPostListener submitPostListener) { RedditAPI api = oauthRetrofit.create(RedditAPI.class); String accessToken = authInfoSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); @@ -34,8 +34,12 @@ 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_SELF); - params.put(RedditUtils.TEXT_KEY, text); + params.put(RedditUtils.KIND_KEY, kind); + if(kind.equals(RedditUtils.KIND_SELF)) { + params.put(RedditUtils.TEXT_KEY, content); + } else if(kind.equals(RedditUtils.KIND_LINK)) { + params.put(RedditUtils.URL_KEY, content); + } params.put(RedditUtils.NSFW_KEY, Boolean.toString(isNSFW)); Call submitPostCall = api.submit(RedditUtils.getOAuthHeader(accessToken), params); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java index 0411eea0..7e8e4d7d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java @@ -10,7 +10,6 @@ import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; -import SubscribedSubredditDatabase.SubscribedSubredditViewModel; import butterknife.ButterKnife; public class SubredditSelectionActivity extends AppCompatActivity { @@ -19,8 +18,6 @@ public class SubredditSelectionActivity extends AppCompatActivity { static final String EXTRA_RETURN_SUBREDDIT_ICON_URL_KEY = "ERSIURLK"; static final String EXTRA_RETURN_SUBREDDIT_IS_USER_KEY = "ERSIUK"; - private SubscribedSubredditViewModel mSubscribedSubredditViewModel; - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); -- cgit v1.2.3