diff options
author | Alex Ning <chineseperson5@gmail.com> | 2019-07-11 08:32:27 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2019-07-11 08:32:27 +0000 |
commit | d8669c94e88345e1ed55468ba60aa5570bb1b745 (patch) | |
tree | 49556a508720a46c86324660e9c6d66769596b39 /app | |
parent | 36d24523b68e4ebecab02e7afff044b1625b80c9 (diff) | |
download | infinity-for-reddit-d8669c94e88345e1ed55468ba60aa5570bb1b745.tar infinity-for-reddit-d8669c94e88345e1ed55468ba60aa5570bb1b745.tar.gz infinity-for-reddit-d8669c94e88345e1ed55468ba60aa5570bb1b745.tar.bz2 infinity-for-reddit-d8669c94e88345e1ed55468ba60aa5570bb1b745.tar.lz infinity-for-reddit-d8669c94e88345e1ed55468ba60aa5570bb1b745.tar.xz infinity-for-reddit-d8669c94e88345e1ed55468ba60aa5570bb1b745.tar.zst infinity-for-reddit-d8669c94e88345e1ed55468ba60aa5570bb1b745.zip |
Submitting posts to subreddit is available now. Fixed User's posts cannot be all loaded.
Diffstat (limited to 'app')
17 files changed, 249 insertions, 78 deletions
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c0b81dbf..55c5dcb6 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -19,7 +19,8 @@ android:supportsRtl="true" android:theme="@style/AppTheme" android:usesCleartextTraffic="true"> - <activity android:name=".SubscribedThingListingActivity" + <activity + android:name=".SubscribedThingListingActivity" android:label="@string/subscriptions" android:parentActivityName=".MainActivity" android:theme="@style/AppTheme.NoActionBar" /> @@ -30,7 +31,8 @@ <activity android:name=".PostTextActivity" android:label="@string/post_text_activity_label" - android:parentActivityName=".MainActivity" /> + android:parentActivityName=".MainActivity" + android:windowSoftInputMode="adjustResize" /> <activity android:name=".CommentActivity" android:label="@string/comment_activity_label" diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index aa906d2a..db457e0c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -17,4 +17,5 @@ interface AppComponent { void inject(ViewUserDetailActivity viewUserDetailActivity); void inject(CommentActivity commentActivity); void inject(SubscribedThingListingActivity subscribedThingListingActivity); + void inject(PostTextActivity postTextActivity); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java index e05d4d17..b4560802 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java @@ -121,7 +121,9 @@ public class CommentActivity extends AppCompatActivity { finish(); } }); + return true; } - return super.onOptionsItemSelected(item); + + return false; } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java index 8b2f3374..433f22d0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java @@ -70,4 +70,5 @@ public class JSONUtils { static final String JSON_KEY = "json"; static final String THINGS_KEY = "things"; static final String PARENT_ID_KEY = "parent_id"; + static final String ERRORS_KEY = "errors"; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java index 4aa84343..88101048 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java @@ -2,12 +2,13 @@ package ml.docilealligator.infinityforreddit; import android.util.Log; -import java.util.ArrayList; -import java.util.Locale; - import androidx.annotation.NonNull; import androidx.lifecycle.MutableLiveData; import androidx.paging.PageKeyedDataSource; + +import java.util.ArrayList; +import java.util.Locale; + import retrofit2.Call; import retrofit2.Callback; import retrofit2.Retrofit; @@ -114,7 +115,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { loadSubredditPostsAfter(params, callback); break; case TYPE_USER: - loadUserPostsAfter(params, callback); + loadUserPostsAfter(params, callback, null); break; case TYPE_SEARCH: loadSearchPostsAfter(params, callback); @@ -157,8 +158,8 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { } @Override - public void onFailure(Call<String> call, Throwable t) { - String errorMessage = t == null ? "unknown error" : t.getMessage(); + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + String errorMessage = t.getMessage(); initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage)); } }); @@ -170,7 +171,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { bestPost.enqueue(new Callback<String>() { @Override - public void onResponse(Call<String> call, retrofit2.Response<String> response) { + public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if(response.isSuccessful()) { ParsePost.parsePosts(response.body(), locale, new ParsePost.ParsePostsListingListener() { @Override @@ -192,8 +193,8 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { } @Override - public void onFailure(Call<String> call, Throwable t) { - String errorMessage = t == null ? "unknown error" : t.getMessage(); + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + String errorMessage = t.getMessage(); paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage)); } }); @@ -204,7 +205,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { Call<String> getPost = api.getSubredditBestPosts(name, null, RedditUtils.getOAuthHeader(accessToken)); getPost.enqueue(new Callback<String>() { @Override - public void onResponse(Call<String> call, retrofit2.Response<String> response) { + public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if(response.isSuccessful()) { ParsePost.parsePosts(response.body(), locale, new ParsePost.ParsePostsListingListener() { @@ -233,8 +234,8 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { } @Override - public void onFailure(Call<String> call, Throwable t) { - String errorMessage = t == null ? "unknown error" : t.getMessage(); + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + String errorMessage = t.getMessage(); initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage)); } }); @@ -245,7 +246,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { Call<String> getPost = api.getSubredditBestPosts(name, params.key, RedditUtils.getOAuthHeader(accessToken)); getPost.enqueue(new Callback<String>() { @Override - public void onResponse(Call<String> call, retrofit2.Response<String> response) { + public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if(response.isSuccessful()) { ParsePost.parsePosts(response.body(), locale, new ParsePost.ParsePostsListingListener() { @Override @@ -267,8 +268,8 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { } @Override - public void onFailure(Call<String> call, Throwable t) { - String errorMessage = t == null ? "unknown error" : t.getMessage(); + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + String errorMessage = t.getMessage(); paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage)); } }); @@ -279,7 +280,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { Call<String> getPost = api.getUserBestPosts(name, lastItem, RedditUtils.getOAuthHeader(accessToken)); getPost.enqueue(new Callback<String>() { @Override - public void onResponse(Call<String> call, retrofit2.Response<String> response) { + public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if(response.isSuccessful()) { ParsePost.parsePosts(response.body(), locale, new ParsePost.ParsePostsListingListener() { @@ -311,42 +312,48 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { } @Override - public void onFailure(Call<String> call, Throwable t) { - String errorMessage = t == null ? "unknown error" : t.getMessage(); + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + String errorMessage = t.getMessage(); initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage)); } }); } - private void loadUserPostsAfter(@NonNull LoadParams<String> params, @NonNull final LoadCallback<String, Post> callback) { + private void loadUserPostsAfter(@NonNull LoadParams<String> params, @NonNull final LoadCallback<String, Post> callback, String lastItem) { + String after = lastItem == null ? params.key : lastItem; + RedditAPI api = retrofit.create(RedditAPI.class); - Call<String> getPost = api.getUserBestPosts(name, params.key, RedditUtils.getOAuthHeader(accessToken)); + Call<String> getPost = api.getUserBestPosts(name, after, RedditUtils.getOAuthHeader(accessToken)); getPost.enqueue(new Callback<String>() { @Override - public void onResponse(Call<String> call, retrofit2.Response<String> response) { + public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if(response.isSuccessful()) { ParsePost.parsePosts(response.body(), locale, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) { - callback.onResult(newPosts, lastItem); - paginationNetworkStateLiveData.postValue(NetworkState.LOADED); + if(newPosts.size() == 0 && !lastItem.equals("null")) { + loadUserPostsAfter(params, callback, lastItem); + } else { + callback.onResult(newPosts, lastItem); + paginationNetworkStateLiveData.postValue(NetworkState.LOADED); + } } @Override public void onParsePostsListingFail() { - Log.i("Best post", "Error parsing data"); + Log.i("User posts", "Error parsing data"); paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data")); } }); } else { - Log.i("Best post", response.message()); + Log.i("User posts", response.message()); paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, response.message())); } } @Override - public void onFailure(Call<String> call, Throwable t) { - String errorMessage = t == null ? "unknown error" : t.getMessage(); + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + String errorMessage = t.getMessage(); paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage)); } }); @@ -357,7 +364,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { Call<String> getPost = api.searchPosts(name, null, RedditUtils.getOAuthHeader(accessToken)); getPost.enqueue(new Callback<String>() { @Override - public void onResponse(Call<String> call, retrofit2.Response<String> response) { + public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if(response.isSuccessful()) { ParsePost.parsePosts(response.body(), locale, new ParsePost.ParsePostsListingListener() { @@ -386,8 +393,8 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { } @Override - public void onFailure(Call<String> call, Throwable t) { - String errorMessage = t == null ? "unknown error" : t.getMessage(); + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + String errorMessage = t.getMessage(); initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage)); } }); @@ -398,7 +405,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { Call<String> getPost = api.searchPosts(name, params.key, RedditUtils.getOAuthHeader(accessToken)); getPost.enqueue(new Callback<String>() { @Override - public void onResponse(Call<String> call, retrofit2.Response<String> response) { + public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if(response.isSuccessful()) { ParsePost.parsePosts(response.body(), locale, new ParsePost.ParsePostsListingListener() { @Override @@ -409,19 +416,19 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { @Override public void onParsePostsListingFail() { - Log.i("Best post", "Error parsing data"); + Log.i("Search post", "Error parsing data"); paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data")); } }); } else { - Log.i("Best post", response.message()); + Log.i("Search post", response.message()); paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, response.message())); } } @Override - public void onFailure(Call<String> call, Throwable t) { - String errorMessage = t == null ? "unknown error" : t.getMessage(); + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + String errorMessage = t.getMessage(); paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage)); } }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java index 1541a233..bb63a905 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java @@ -145,7 +145,6 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo canStartActivity = false; Intent intent = new Intent(mContext, ViewPostDetailActivity.class); - intent.putExtra(ViewPostDetailActivity.EXTRA_TITLE, title); intent.putExtra(ViewPostDetailActivity.EXTRA_POST_DATA, post); intent.putExtra(ViewPostDetailActivity.EXTRA_POST_LIST_POSITION, position); mContext.startActivity(intent); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java index 26ece355..cc3f1358 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java @@ -1,8 +1,10 @@ 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; @@ -12,15 +14,23 @@ 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 PostTextActivity extends AppCompatActivity { @@ -28,6 +38,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.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; @@ -35,6 +46,15 @@ public class PostTextActivity extends AppCompatActivity { @BindView(R.id.post_text_content_edit_text_post_text_activity) EditText contentEditText; private RequestManager mGlide; + private Locale mLocale; + + @Inject + @Named("oauth") + Retrofit mOauthRetrofit; + + @Inject + @Named("auth_info") + SharedPreferences sharedPreferences; @Override protected void onCreate(Bundle savedInstanceState) { @@ -43,11 +63,14 @@ public class PostTextActivity extends AppCompatActivity { 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(getIntent().hasExtra(EXTRA_SUBREDDIT_NAME)) { subreditNameTextView.setText(getIntent().getExtras().getString(EXTRA_SUBREDDIT_NAME)); @@ -64,11 +87,43 @@ public class PostTextActivity extends AppCompatActivity { } @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.post_text_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_text_activity: + 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(), + titleEditText.getText().toString(), contentEditText.getText().toString(), + false, new SubmitPost.SubmitPostListener() { + @Override + public void submitSuccessful(Post post) { + Intent intent = new Intent(PostTextActivity.this, ViewPostDetailActivity.class); + intent.putExtra(ViewPostDetailActivity.EXTRA_POST_DATA, post); + startActivity(intent); + finish(); + } + + @Override + public void submitFailed() { + postingSnackbar.dismiss(); + item.setEnabled(true); + item.getIcon().setAlpha(255); + Snackbar.make(coordinatorLayout, R.string.post_failed, Snackbar.LENGTH_SHORT); + } + }); + return true; } return false; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java index a10579c1..e1f044c4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java @@ -78,4 +78,8 @@ public interface RedditAPI { @FormUrlEncoded @POST("/api/del") Call<String> delete(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> params); + + @FormUrlEncoded + @POST("/api/submit") + Call<String> submit(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> params); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java index c6964764..faf58e4a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java @@ -49,10 +49,18 @@ public class RedditUtils { static final String SR_NAME_KEY = "sr_name"; static final String API_TYPE_KEY = "api_type"; + static final String API_TYPE_JSON = "json"; static final String RETURN_RTJSON_KEY = "return_rtjson"; static final String TEXT_KEY = "text"; static final String THING_ID_KEY = "thing_id"; + static final String SR_KEY = "sr"; + static final String TITLE_KEY = "title"; + static final String NSFW_KEY = "nsfw"; + static final String KIND_KEY = "kind"; + static final String KIND_TEXT = "text"; + static final String KIND_SELF = "self"; + static Map<String, String> getHttpBasicAuthHeader() { Map<String, String> params = new HashMap<>(); String credentials = String.format("%s:%s", RedditUtils.CLIENT_ID, ""); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java new file mode 100644 index 00000000..7de16719 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java @@ -0,0 +1,109 @@ +package ml.docilealligator.infinityforreddit; + +import android.content.SharedPreferences; +import android.util.Log; + +import androidx.annotation.NonNull; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Retrofit; + +class SubmitPost { + interface SubmitPostListener { + void submitSuccessful(Post post); + void submitFailed(); + } + + static void submitPostText(Retrofit oauthRetrofit, SharedPreferences authInfoSharedPreferences, + Locale locale, String subredditName, String title, String text, boolean isNSFW, + SubmitPostListener submitPostListener) { + RedditAPI api = oauthRetrofit.create(RedditAPI.class); + String accessToken = authInfoSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); + + Map<String, String> params = new HashMap<>(); + 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.TEXT_KEY, text); + params.put(RedditUtils.NSFW_KEY, Boolean.toString(isNSFW)); + + Call<String> submitPostCall = api.submit(RedditUtils.getOAuthHeader(accessToken), params); + submitPostCall.enqueue(new Callback<String>() { + @Override + public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { + Log.i("code", "asfd" + response.body()); + if(response.isSuccessful()) { + try { + getSubmittedPost(response.body(), oauthRetrofit, authInfoSharedPreferences, locale, + submitPostListener); + } catch (JSONException e) { + e.printStackTrace(); + submitPostListener.submitFailed(); + } + } else { + Log.i("call_failed", response.message()); + submitPostListener.submitFailed(); + } + } + + @Override + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + Log.i("call_failed", call.request().url().toString()); + submitPostListener.submitFailed(); + } + }); + } + + 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(); + return; + } + + String postId = responseObject.getJSONObject(JSONUtils.JSON_KEY).getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.ID_KEY); + + RedditAPI api = oauthRetrofit.create(RedditAPI.class); + String accessToken = authInfoSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); + + Call<String> getPostCall = api.getPost(postId, RedditUtils.getOAuthHeader(accessToken)); + getPostCall.enqueue(new Callback<String>() { + @Override + public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { + if(response.isSuccessful()) { + ParsePost.parsePost(response.body(), locale, new ParsePost.ParsePostListener() { + @Override + public void onParsePostSuccess(Post post) { + submitPostListener.submitSuccessful(post); + } + + @Override + public void onParsePostFail() { + submitPostListener.submitFailed(); + } + }); + } else { + Log.i("call_failed", response.message()); + submitPostListener.submitFailed(); + } + } + + @Override + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + Log.i("call_failed", call.request().url().toString()); + submitPostListener.submitFailed(); + } + }); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java index 3d565f3c..eece6d5c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java @@ -33,7 +33,7 @@ public class SubredditSelectionActivity extends AppCompatActivity { SubscribedSubredditsListingFragment fragment = new SubscribedSubredditsListingFragment(); Bundle bundle = new Bundle(); - bundle.putInt(PostFragment.POST_TYPE_KEY, PostDataSource.TYPE_FRONT_PAGE); + bundle.putBoolean(SubscribedSubredditsListingFragment.EXTRA_IS_SUBREDDIT_SELECTION, true); fragment.setArguments(bundle); getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_subreddit_selection_activity, fragment).commit(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java index 476782cb..2950d88c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditsListingFragment.java @@ -60,11 +60,9 @@ public class SubscribedSubredditsListingFragment extends Fragment { SubscribedSubredditsRecyclerViewAdapter adapter; if(getArguments().getBoolean(EXTRA_IS_SUBREDDIT_SELECTION)) { - adapter = new SubscribedSubredditsRecyclerViewAdapter(mActivity); + adapter = new SubscribedSubredditsRecyclerViewAdapter(mActivity, (name, iconUrl) -> ((SubredditSelectionActivity) mActivity).getSelectedSubreddit(name, iconUrl)); } else { - adapter = new SubscribedSubredditsRecyclerViewAdapter(mActivity, (name, iconUrl) -> { - ((SubredditSelectionActivity) mActivity).getSelectedSubreddit(name, iconUrl); - }); + adapter = new SubscribedSubredditsRecyclerViewAdapter(mActivity); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedThingListingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedThingListingActivity.java index ce3a8b72..92568a9f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedThingListingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedThingListingActivity.java @@ -129,7 +129,11 @@ public class SubscribedThingListingActivity extends AppCompatActivity { public Fragment getItem(int position) { switch (position) { case 0: { - return new SubscribedSubredditsListingFragment(); + SubscribedSubredditsListingFragment fragment = new SubscribedSubredditsListingFragment(); + Bundle bundle = new Bundle(); + bundle.putBoolean(SubscribedSubredditsListingFragment.EXTRA_IS_SUBREDDIT_SELECTION, false); + fragment.setArguments(bundle); + return fragment; } default: { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index 96084fc6..9b8529ed 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -38,7 +38,6 @@ import static ml.docilealligator.infinityforreddit.CommentActivity.WRITE_COMMENT public class ViewPostDetailActivity extends AppCompatActivity { - static final String EXTRA_TITLE = "ET"; static final String EXTRA_POST_DATA = "EPD"; static final String EXTRA_POST_LIST_POSITION = "EPLI"; @@ -185,38 +184,6 @@ public class ViewPostDetailActivity extends AppCompatActivity { } }); } - - /*mCommentProgressbar.setVisibility(View.GONE); - - if (expandedComments.size() > 0) { - if(mAdapter == null) { - mNestedScrollView.getViewTreeObserver().addOnScrollChangedListener(() -> { - if(!isLoadingMoreChildren) { - View view = mNestedScrollView.getChildAt(mNestedScrollView.getChildCount() - 1); - int diff = view.getBottom() - (mNestedScrollView.getHeight() + - mNestedScrollView.getScrollY()); - if(diff == 0) { - fetchMoreComments(mChildrenStartingIndex); - } - } - }); - } - - mAdapter = new CommentRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit, - mOauthRetrofit, mGlide, mSharedPreferences, mPost, - mPost.getSubredditNamePrefixed(), mLocale, new CommentRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() { - @Override - public void updatePost(Post post) { - EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition)); - } - }); - mRecyclerView.setAdapter(mAdapter); - - //mCommentCardView.setVisibility(View.VISIBLE); - } else { - mNoCommentWrapperLinearLayout.setVisibility(View.VISIBLE); - mGlide.load(R.drawable.no_comment_placeholder).into(mNoCommentImageView); - }*/ } @Override diff --git a/app/src/main/res/layout/activity_post_text.xml b/app/src/main/res/layout/activity_post_text.xml index 9bc76c10..2b16fc33 100644 --- a/app/src/main/res/layout/activity_post_text.xml +++ b/app/src/main/res/layout/activity_post_text.xml @@ -4,6 +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" tools:context=".PostTextActivity"> <androidx.core.widget.NestedScrollView diff --git a/app/src/main/res/menu/post_text_activity.xml b/app/src/main/res/menu/post_text_activity.xml new file mode 100644 index 00000000..94650918 --- /dev/null +++ b/app/src/main/res/menu/post_text_activity.xml @@ -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_text_activity" + android:orderInCategory="1" + android:title="@string/action_send" + android:icon="@drawable/ic_send_white_24dp" + app:showAsAction="ifRoom" /> +</menu>
\ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index effd81dc..298f02f2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -80,6 +80,9 @@ <string name="send_comment_failed">Could not send this comment</string> <string name="parse_sent_comment_failed">The comment is sent but unable to get the sent comment</string> + <string name="posting">Posting</string> + <string name="post_failed">Could not post it</string> + <string name="download_completed">Download completed</string> <string name="download_failed">Download Failed</string> |