Submitting link posts is available. Fixed force stop when a user has no post in ViewUserDetailActivity.

This commit is contained in:
Alex Ning 2019-07-12 09:35:05 +08:00
parent 2b9b8fddfa
commit 68d3a7b09f
15 changed files with 386 additions and 16 deletions

Binary file not shown.

View File

@ -19,6 +19,11 @@
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">
<activity
android:name=".PostLinkActivity"
android:label="@string/post_link_activity_label"
android:parentActivityName=".MainActivity"
android:windowSoftInputMode="adjustResize" />
<activity
android:name=".SubscribedThingListingActivity"
android:label="@string/subscriptions"

View File

@ -19,4 +19,5 @@ interface AppComponent {
void inject(SubscribedThingListingActivity subscribedThingListingActivity);
void inject(PostTextActivity postTextActivity);
void inject(SubscribedSubredditsListingFragment subscribedSubredditsListingFragment);
void inject(PostLinkActivity postLinkActivity);
}

View File

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

View File

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

View File

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

View File

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

View File

@ -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<String, String> getHttpBasicAuthHeader() {
Map<String, String> params = new HashMap<>();

View File

@ -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<String> submitPostCall = api.submit(RedditUtils.getOAuthHeader(accessToken), params);

View File

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

View File

@ -0,0 +1,95 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/coordinator_layout_post_link_activity"
tools:context=".PostTextActivity">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:paddingStart="16dp"
android:paddingEnd="16dp">
<pl.droidsonroids.gif.GifImageView
android:id="@+id/subreddit_icon_gif_image_view_post_link_activity"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/subreddit_name_text_view_post_link_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf="@id/rules_button_post_link_activity"
android:layout_toEndOf="@id/subreddit_icon_gif_image_view_post_link_activity"
android:layout_centerVertical="true"
android:layout_marginStart="32dp"
android:layout_marginEnd="16dp"
android:text="@string/choose_a_subreddit" />
<Button
android:id="@+id/rules_button_post_link_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:text="@string/rules"
android:textColor="@color/colorAccent" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/dividerColor" />
<EditText
android:id="@+id/post_title_edit_text_post_link_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:padding="16dp"
android:hint="@string/post_title_hint"
android:inputType="textCapSentences|textMultiLine"
android:textSize="18sp"
android:background="#00000000"
android:textColor="@color/primaryTextColor" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/dividerColor" />
<EditText
android:id="@+id/post_link_edit_text_post_link_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
android:padding="16dp"
android:hint="@string/post_link_hint"
android:inputType="textCapSentences|textMultiLine"
android:textSize="18sp"
android:background="#00000000"
android:textColor="@color/primaryTextColor" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

View File

@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/coordinator_layout_post_detail_activity"
android:id="@+id/coordinator_layout_post_text_activity"
tools:context=".PostTextActivity">
<androidx.core.widget.NestedScrollView

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_send_post_link_activity"
android:orderInCategory="1"
android:title="@string/action_send"
android:icon="@drawable/ic_send_white_24dp"
app:showAsAction="ifRoom" />
</menu>

View File

@ -6,6 +6,7 @@
<string name="comment_activity_label_is_replying">Reply</string>
<string name="post_text_activity_label">Text Post</string>
<string name="subreddit_selection_activity_label">Select a Subreddit</string>
<string name="post_link_activity_label">Link Post</string>
<string name="navigation_drawer_open">Open navigation drawer</string>
<string name="navigation_drawer_close">Close navigation drawer</string>
@ -97,7 +98,16 @@
<string name="post_url_hint">URL</string>
<string name="choose_a_subreddit">Choose a subreddit</string>
<string name="rules">Rules</string>
<string name="post_link_hint">URL</string>
<string name="subreddits">Subreddits</string>
<string name="users">Users</string>
<string name="dialog_post_type">Post</string>
<string-array name="dialog_post_types_array">
<item>Text</item>
<item>Link</item>
<item>Image</item>
<item>Video</item>
</string-array>
</resources>