mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2024-12-25 10:28:22 +01:00
Submitting image posts is now available.
This commit is contained in:
parent
de4b53ddf0
commit
9d1e53b585
@ -40,6 +40,15 @@ class AppModule {
|
||||
.build();
|
||||
}
|
||||
|
||||
@Provides @Named("upload_media")
|
||||
@Singleton
|
||||
Retrofit provideUploadMediaRetrofit() {
|
||||
return new Retrofit.Builder()
|
||||
.baseUrl(RedditUtils.API_UPLOAD_MEDIA_URI)
|
||||
.addConverterFactory(ScalarsConverterFactory.create())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
OkHttpClient provideOkHttpClient(@Named("no_oauth") Retrofit retrofit, @Named("auth_info") SharedPreferences sharedPreferences) {
|
||||
|
@ -71,4 +71,7 @@ public class JSONUtils {
|
||||
static final String THINGS_KEY = "things";
|
||||
static final String PARENT_ID_KEY = "parent_id";
|
||||
static final String ERRORS_KEY = "errors";
|
||||
static final String ARGS_KEY = "args";
|
||||
static final String FIELDS_KEY = "fields";
|
||||
static final String VALUE_KEY = "value";
|
||||
}
|
||||
|
@ -197,10 +197,10 @@ public class MainActivity extends AppCompatActivity {
|
||||
|
||||
loadUserData();
|
||||
|
||||
mName = getSharedPreferences(SharedPreferencesUtils.USER_INFO_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.USER_KEY, "");
|
||||
mProfileImageUrl = getSharedPreferences(SharedPreferencesUtils.USER_INFO_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.PROFILE_IMAGE_URL_KEY, "");
|
||||
mBannerImageUrl = getSharedPreferences(SharedPreferencesUtils.USER_INFO_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.BANNER_IMAGE_URL_KEY, "");
|
||||
mKarma = getSharedPreferences(SharedPreferencesUtils.USER_INFO_FILE_KEY, Context.MODE_PRIVATE).getString(SharedPreferencesUtils.KARMA_KEY, "");
|
||||
mName = mUserInfoSharedPreferences.getString(SharedPreferencesUtils.USER_KEY, "");
|
||||
mProfileImageUrl = mUserInfoSharedPreferences.getString(SharedPreferencesUtils.PROFILE_IMAGE_URL_KEY, "");
|
||||
mBannerImageUrl = mUserInfoSharedPreferences.getString(SharedPreferencesUtils.BANNER_IMAGE_URL_KEY, "");
|
||||
mKarma = mUserInfoSharedPreferences.getString(SharedPreferencesUtils.KARMA_KEY, "");
|
||||
|
||||
mNameTextView.setText(mName);
|
||||
mKarmaTextView.setText(mKarma);
|
||||
@ -242,16 +242,19 @@ public class MainActivity extends AppCompatActivity {
|
||||
startActivity(intent);
|
||||
dialog.dismiss();
|
||||
});
|
||||
|
||||
linkTypeLinearLayout.setOnClickListener(view -> {
|
||||
Intent intent = new Intent(MainActivity.this, PostLinkActivity.class);
|
||||
startActivity(intent);
|
||||
dialog.dismiss();
|
||||
});
|
||||
|
||||
imageTypeLinearLayout.setOnClickListener(view -> {
|
||||
Intent intent = new Intent(MainActivity.this, PostImageActivity.class);
|
||||
startActivity(intent);
|
||||
dialog.dismiss();
|
||||
});
|
||||
|
||||
videoTypeLinearLayout.setOnClickListener(view -> {
|
||||
Toast.makeText(this, "Not implemented yet", Toast.LENGTH_SHORT).show();
|
||||
dialog.dismiss();
|
||||
|
@ -27,8 +27,8 @@ class ParsePost {
|
||||
void onParsePostFail();
|
||||
}
|
||||
|
||||
static void parsePosts(String response, Locale locale, ParsePostsListingListener parsePostsListingListener) {
|
||||
new ParsePostDataAsyncTask(response, locale, parsePostsListingListener).execute();
|
||||
static void parsePosts(String response, Locale locale, int nPosts, ParsePostsListingListener parsePostsListingListener) {
|
||||
new ParsePostDataAsyncTask(response, locale, nPosts, parsePostsListingListener).execute();
|
||||
}
|
||||
|
||||
static void parsePost(String response, Locale locale, ParsePostListener parsePostListener) {
|
||||
@ -38,6 +38,7 @@ class ParsePost {
|
||||
private static class ParsePostDataAsyncTask extends AsyncTask<Void, Void, Void> {
|
||||
private JSONArray allData;
|
||||
private Locale locale;
|
||||
private int nPosts;
|
||||
private ParsePostsListingListener parsePostsListingListener;
|
||||
private ParsePostListener parsePostListener;
|
||||
private ArrayList<Post> newPosts;
|
||||
@ -45,7 +46,7 @@ class ParsePost {
|
||||
private String lastItem;
|
||||
private boolean parseFailed;
|
||||
|
||||
ParsePostDataAsyncTask(String response, Locale locale,
|
||||
ParsePostDataAsyncTask(String response, Locale locale, int nPosts,
|
||||
ParsePostsListingListener parsePostsListingListener) {
|
||||
this.parsePostsListingListener = parsePostsListingListener;
|
||||
try {
|
||||
@ -53,6 +54,7 @@ class ParsePost {
|
||||
allData = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
|
||||
lastItem = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY);
|
||||
this.locale = locale;
|
||||
this.nPosts = nPosts;
|
||||
newPosts = new ArrayList<>();
|
||||
parseFailed = false;
|
||||
} catch (JSONException e) {
|
||||
@ -99,7 +101,14 @@ class ParsePost {
|
||||
}
|
||||
} else {
|
||||
//Posts listing
|
||||
for(int i = 0; i < allData.length(); i++) {
|
||||
int size;
|
||||
if(nPosts < 0 || nPosts > allData.length()) {
|
||||
size = allData.length();
|
||||
} else {
|
||||
size = nPosts;
|
||||
}
|
||||
|
||||
for(int i = 0; i < size; i++) {
|
||||
String kind = allData.getJSONObject(i).getString(JSONUtils.KIND_KEY);
|
||||
if(kind.equals("t3")) {
|
||||
//It's a post
|
||||
|
@ -131,7 +131,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
|
||||
if (response.isSuccessful()) {
|
||||
ParsePost.parsePosts(response.body(), locale,
|
||||
ParsePost.parsePosts(response.body(), locale, -1,
|
||||
new ParsePost.ParsePostsListingListener() {
|
||||
@Override
|
||||
public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) {
|
||||
@ -173,7 +173,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
|
||||
if(response.isSuccessful()) {
|
||||
ParsePost.parsePosts(response.body(), locale, new ParsePost.ParsePostsListingListener() {
|
||||
ParsePost.parsePosts(response.body(), locale, -1, new ParsePost.ParsePostsListingListener() {
|
||||
@Override
|
||||
public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) {
|
||||
callback.onResult(newPosts, lastItem);
|
||||
@ -207,7 +207,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
|
||||
if(response.isSuccessful()) {
|
||||
ParsePost.parsePosts(response.body(), locale,
|
||||
ParsePost.parsePosts(response.body(), locale, -1,
|
||||
new ParsePost.ParsePostsListingListener() {
|
||||
@Override
|
||||
public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) {
|
||||
@ -248,7 +248,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
|
||||
if(response.isSuccessful()) {
|
||||
ParsePost.parsePosts(response.body(), locale, new ParsePost.ParsePostsListingListener() {
|
||||
ParsePost.parsePosts(response.body(), locale, -1, new ParsePost.ParsePostsListingListener() {
|
||||
@Override
|
||||
public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) {
|
||||
callback.onResult(newPosts, lastItem);
|
||||
@ -282,7 +282,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
|
||||
if(response.isSuccessful()) {
|
||||
ParsePost.parsePosts(response.body(), locale,
|
||||
ParsePost.parsePosts(response.body(), locale, -1,
|
||||
new ParsePost.ParsePostsListingListener() {
|
||||
@Override
|
||||
public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) {
|
||||
@ -328,7 +328,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
|
||||
if(response.isSuccessful()) {
|
||||
ParsePost.parsePosts(response.body(), locale, new ParsePost.ParsePostsListingListener() {
|
||||
ParsePost.parsePosts(response.body(), locale, -1, new ParsePost.ParsePostsListingListener() {
|
||||
@Override
|
||||
public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) {
|
||||
if(newPosts.size() == 0 && !lastItem.equals("null")) {
|
||||
@ -366,7 +366,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
|
||||
if(response.isSuccessful()) {
|
||||
ParsePost.parsePosts(response.body(), locale,
|
||||
ParsePost.parsePosts(response.body(), locale, -1,
|
||||
new ParsePost.ParsePostsListingListener() {
|
||||
@Override
|
||||
public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) {
|
||||
@ -407,7 +407,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
|
||||
if(response.isSuccessful()) {
|
||||
ParsePost.parsePosts(response.body(), locale, new ParsePost.ParsePostsListingListener() {
|
||||
ParsePost.parsePosts(response.body(), locale, -1, new ParsePost.ParsePostsListingListener() {
|
||||
@Override
|
||||
public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) {
|
||||
callback.onResult(newPosts, lastItem);
|
||||
|
@ -2,6 +2,7 @@ package ml.docilealligator.infinityforreddit;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
@ -23,9 +24,12 @@ import androidx.coordinatorlayout.widget.CoordinatorLayout;
|
||||
import com.bumptech.glide.Glide;
|
||||
import com.bumptech.glide.RequestManager;
|
||||
import com.bumptech.glide.request.RequestOptions;
|
||||
import com.bumptech.glide.request.target.CustomTarget;
|
||||
import com.bumptech.glide.request.transition.Transition;
|
||||
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
||||
import com.google.android.material.snackbar.Snackbar;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Locale;
|
||||
|
||||
import javax.inject.Inject;
|
||||
@ -35,6 +39,8 @@ import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
|
||||
import pl.droidsonroids.gif.GifImageView;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
public class PostImageActivity extends AppCompatActivity {
|
||||
@ -75,6 +81,14 @@ public class PostImageActivity extends AppCompatActivity {
|
||||
@Named("oauth")
|
||||
Retrofit mOauthRetrofit;
|
||||
|
||||
@Inject
|
||||
@Named("upload_media")
|
||||
Retrofit mUploadMediaRetrofit;
|
||||
|
||||
@Inject
|
||||
@Named("user_info")
|
||||
SharedPreferences mUserInfoSharedPreferences;
|
||||
|
||||
@Inject
|
||||
@Named("auth_info")
|
||||
SharedPreferences sharedPreferences;
|
||||
@ -149,11 +163,12 @@ public class PostImageActivity extends AppCompatActivity {
|
||||
selectFromLibraryFab.setOnClickListener(view -> {
|
||||
Intent intent = new Intent();
|
||||
intent.setType("image/*");
|
||||
intent.setAction(Intent.ACTION_GET_CONTENT);
|
||||
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
|
||||
startActivityForResult(Intent.createChooser(intent,getResources().getString(R.string.select_from_gallery)), PICK_IMAGE_REQUEST_CODE);
|
||||
});
|
||||
|
||||
selectAgainTextView.setOnClickListener(view -> {
|
||||
imageUri = null;
|
||||
selectAgainTextView.setVisibility(View.GONE);
|
||||
mGlide.clear(imageView);
|
||||
constraintLayout.setVisibility(View.VISIBLE);
|
||||
@ -185,6 +200,11 @@ public class PostImageActivity extends AppCompatActivity {
|
||||
return true;
|
||||
}
|
||||
|
||||
if(imageUri == null) {
|
||||
Snackbar.make(coordinatorLayout, R.string.select_an_image, Snackbar.LENGTH_SHORT).show();
|
||||
return true;
|
||||
}
|
||||
|
||||
item.setEnabled(false);
|
||||
item.getIcon().setAlpha(130);
|
||||
Snackbar postingSnackbar = Snackbar.make(coordinatorLayout, R.string.posting, Snackbar.LENGTH_INDEFINITE);
|
||||
@ -197,35 +217,85 @@ public class PostImageActivity extends AppCompatActivity {
|
||||
subredditName = subreditNameTextView.getText().toString();
|
||||
}
|
||||
|
||||
/*SubmitPost.submitTextOrLinkPost(mOauthRetrofit, sharedPreferences, mLocale, subredditName,
|
||||
titleEditText.getText().toString(), contentEditText.getText().toString(),
|
||||
false, RedditUtils.KIND_LINK, new SubmitPost.SubmitPostListener() {
|
||||
Glide.with(this)
|
||||
.asBitmap()
|
||||
.load(imageUri)
|
||||
.into(new CustomTarget<Bitmap>() {
|
||||
|
||||
@Override
|
||||
public void submitSuccessful(Post post) {
|
||||
Intent intent = new Intent(PostLinkActivity.this, ViewPostDetailActivity.class);
|
||||
intent.putExtra(ViewPostDetailActivity.EXTRA_POST_DATA, post);
|
||||
startActivity(intent);
|
||||
finish();
|
||||
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
|
||||
SubmitPost.submitImagePost(mOauthRetrofit, mUploadMediaRetrofit, sharedPreferences,
|
||||
mLocale, subredditName, titleEditText.getText().toString(), resource,
|
||||
false, new SubmitPost.SubmitPostListener() {
|
||||
@Override
|
||||
public void submitSuccessful(Post post) {
|
||||
RedditAPI api = mOauthRetrofit.create(RedditAPI.class);
|
||||
Call<String> getPost = api.getUserBestPosts(mUserInfoSharedPreferences.getString(SharedPreferencesUtils.USER_KEY, ""), null,
|
||||
RedditUtils.getOAuthHeader(sharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "")));
|
||||
getPost.enqueue(new Callback<String>() {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
|
||||
if(response.isSuccessful()) {
|
||||
ParsePost.parsePosts(response.body(), mLocale, 1,
|
||||
new ParsePost.ParsePostsListingListener() {
|
||||
@Override
|
||||
public void onParsePostsListingSuccess(ArrayList<Post> newPostData, String lastItem) {
|
||||
Intent intent = new Intent(PostImageActivity.this, ViewPostDetailActivity.class);
|
||||
intent.putExtra(ViewPostDetailActivity.EXTRA_POST_DATA, newPostData.get(0));
|
||||
startActivity(intent);
|
||||
finish();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onParsePostsListingFail() {
|
||||
startViewUserDetailActivity();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
startViewUserDetailActivity();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
|
||||
startViewUserDetailActivity();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
public void onLoadCleared(@Nullable Drawable placeholder) {
|
||||
|
||||
}
|
||||
});*/
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void startViewUserDetailActivity() {
|
||||
Intent intent = new Intent(PostImageActivity.this, ViewUserDetailActivity.class);
|
||||
intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY,
|
||||
mUserInfoSharedPreferences.getString(SharedPreferencesUtils.USER_KEY, ""));
|
||||
startActivity(intent);
|
||||
finish();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSaveInstanceState(@NonNull Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
|
@ -2,12 +2,17 @@ package ml.docilealligator.infinityforreddit;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import okhttp3.MultipartBody;
|
||||
import okhttp3.RequestBody;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.http.FieldMap;
|
||||
import retrofit2.http.FormUrlEncoded;
|
||||
import retrofit2.http.GET;
|
||||
import retrofit2.http.HeaderMap;
|
||||
import retrofit2.http.Multipart;
|
||||
import retrofit2.http.POST;
|
||||
import retrofit2.http.Part;
|
||||
import retrofit2.http.PartMap;
|
||||
import retrofit2.http.Path;
|
||||
import retrofit2.http.Query;
|
||||
|
||||
@ -82,4 +87,12 @@ public interface RedditAPI {
|
||||
@FormUrlEncoded
|
||||
@POST("/api/submit")
|
||||
Call<String> submit(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> params);
|
||||
|
||||
@FormUrlEncoded
|
||||
@POST("/api/media/asset.json?raw_json=1&gilding_detail=1")
|
||||
Call<String> uploadImage(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> params);
|
||||
|
||||
@Multipart
|
||||
@POST(".")
|
||||
Call<String> uploadMediaToAWS(@PartMap()Map<String, RequestBody> params, @Part() MultipartBody.Part file);
|
||||
}
|
||||
|
@ -5,6 +5,9 @@ import android.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.RequestBody;
|
||||
|
||||
/**
|
||||
* Created by alex on 2/23/18.
|
||||
*/
|
||||
@ -13,6 +16,7 @@ public class RedditUtils {
|
||||
static final String OAUTH_URL ="https://www.reddit.com/api/v1/authorize.compact";
|
||||
static final String OAUTH_API_BASE_URI = "https://oauth.reddit.com";
|
||||
static final String API_BASE_URI = "https://www.reddit.com";
|
||||
static final String API_UPLOAD_MEDIA_URI = "https://reddit-uploaded-media.s3-accelerate.amazonaws.com";
|
||||
|
||||
static final String CLIENT_ID_KEY = "client_id";
|
||||
static final String CLIENT_ID = "";
|
||||
@ -62,6 +66,10 @@ public class RedditUtils {
|
||||
static final String KIND_TEXT = "text";
|
||||
static final String KIND_SELF = "self";
|
||||
static final String KIND_LINK = "link";
|
||||
static final String KIND_IMAGE = "image";
|
||||
|
||||
static final String FILEPATH_KEY = "filepath";
|
||||
static final String MIMETYPE_KEY = "mimetype";
|
||||
|
||||
static Map<String, String> getHttpBasicAuthHeader() {
|
||||
Map<String, String> params = new HashMap<>();
|
||||
@ -77,4 +85,8 @@ public class RedditUtils {
|
||||
params.put(RedditUtils.USER_AGENT_KEY, RedditUtils.USER_AGENT);
|
||||
return params;
|
||||
}
|
||||
|
||||
static RequestBody getRequestBody(String s) {
|
||||
return RequestBody.create(MediaType.parse("text/plain"), s);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
package ml.docilealligator.infinityforreddit;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
import android.graphics.Bitmap;
|
||||
import android.os.AsyncTask;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
@ -9,13 +11,23 @@ import androidx.annotation.Nullable;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
import org.xmlpull.v1.XmlPullParserFactory;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.MultipartBody;
|
||||
import okhttp3.RequestBody;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
class SubmitPost {
|
||||
@ -37,7 +49,7 @@ class SubmitPost {
|
||||
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)) {
|
||||
} else if(kind.equals(RedditUtils.KIND_LINK) || kind.equals(RedditUtils.KIND_IMAGE)) {
|
||||
params.put(RedditUtils.URL_KEY, content);
|
||||
}
|
||||
params.put(RedditUtils.NSFW_KEY, Boolean.toString(isNSFW));
|
||||
@ -49,8 +61,8 @@ class SubmitPost {
|
||||
Log.i("code", "asfd" + response.body());
|
||||
if(response.isSuccessful()) {
|
||||
try {
|
||||
getSubmittedPost(response.body(), oauthRetrofit, authInfoSharedPreferences, locale,
|
||||
submitPostListener);
|
||||
getSubmittedPost(response.body(), kind, oauthRetrofit, authInfoSharedPreferences,
|
||||
locale, submitPostListener);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
submitPostListener.submitFailed(null);
|
||||
@ -69,7 +81,194 @@ class SubmitPost {
|
||||
});
|
||||
}
|
||||
|
||||
private static void getSubmittedPost(String response, Retrofit oauthRetrofit,
|
||||
static void submitImagePost(Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit,
|
||||
SharedPreferences authInfoSharedPreferences, Locale locale,
|
||||
String subredditName, String title, Bitmap image, boolean isNSFW,
|
||||
SubmitPostListener submitPostListener) {
|
||||
RedditAPI api = oauthRetrofit.create(RedditAPI.class);
|
||||
String accessToken = authInfoSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "");
|
||||
|
||||
Map<String, String> uploadImageParams = new HashMap<>();
|
||||
uploadImageParams.put(RedditUtils.FILEPATH_KEY, "tetestst.jpg");
|
||||
uploadImageParams.put(RedditUtils.MIMETYPE_KEY, "image/jpeg");
|
||||
|
||||
Log.i("map", RedditUtils.getOAuthHeader(accessToken).toString());
|
||||
Call<String> uploadImageCall = api.uploadImage(RedditUtils.getOAuthHeader(accessToken), uploadImageParams);
|
||||
uploadImageCall.enqueue(new Callback<String>() {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
|
||||
if(response.isSuccessful()) {
|
||||
new ParseJSONResponseFromAWSAsyncTask(response.body(), new ParseJSONResponseFromAWSAsyncTask.ParseJSONResponseFromAWSListener() {
|
||||
@Override
|
||||
public void parseSuccessful(Map<String, RequestBody> nameValuePairsMap) {
|
||||
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
|
||||
byte[] byteArray = stream.toByteArray();
|
||||
|
||||
RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), byteArray);
|
||||
MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("file", "testing.jpg", fileBody);
|
||||
|
||||
RedditAPI uploadMediaToAWSApi = uploadMediaRetrofit.create(RedditAPI.class);
|
||||
Call<String> uploadMediaToAWS = uploadMediaToAWSApi.uploadMediaToAWS(nameValuePairsMap, fileToUpload);
|
||||
|
||||
uploadMediaToAWS.enqueue(new Callback<String>() {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
|
||||
Log.i("responsesese", "aws" + response.body());
|
||||
if(response.isSuccessful()) {
|
||||
new ParseXMLReponseFromAWSAsyncTask(response.body(), new ParseXMLReponseFromAWSAsyncTask.ParseXMLResponseFromAWSListener() {
|
||||
@Override
|
||||
public void parseSuccessful(String imageUrl) {
|
||||
submitTextOrLinkPost(oauthRetrofit, authInfoSharedPreferences, locale,
|
||||
subredditName, title, imageUrl, isNSFW, RedditUtils.KIND_IMAGE,
|
||||
submitPostListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void parseFailed() {
|
||||
submitPostListener.submitFailed(null);
|
||||
}
|
||||
}).execute();
|
||||
} else {
|
||||
Log.i("asfasdfsd", "failedddddddddd" + response.code());
|
||||
submitPostListener.submitFailed("Error: " + response.code());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
|
||||
Log.i("asfasdfsd", "failedddddddddd" + t.getMessage());
|
||||
submitPostListener.submitFailed(t.getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void parseFailed() {
|
||||
submitPostListener.submitFailed("Parse from aws failed");
|
||||
}
|
||||
}).execute();
|
||||
} else {
|
||||
submitPostListener.submitFailed(response.message());
|
||||
}
|
||||
Log.i("image", "dddd" + response.body());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
|
||||
submitPostListener.submitFailed(t.getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static class ParseJSONResponseFromAWSAsyncTask extends AsyncTask<Void, Void, Void> {
|
||||
interface ParseJSONResponseFromAWSListener {
|
||||
void parseSuccessful(Map<String, RequestBody> nameValuePairsMap);
|
||||
void parseFailed();
|
||||
}
|
||||
|
||||
private String response;
|
||||
private ParseJSONResponseFromAWSListener parseJSONResponseFromAWSListener;
|
||||
private Map<String, RequestBody> nameValuePairsMap;
|
||||
private boolean successful;
|
||||
|
||||
ParseJSONResponseFromAWSAsyncTask(String response, ParseJSONResponseFromAWSListener parseJSONResponseFromAWSListener) {
|
||||
this.response = response;
|
||||
this.parseJSONResponseFromAWSListener = parseJSONResponseFromAWSListener;
|
||||
nameValuePairsMap = new HashMap<>();
|
||||
successful = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(Void... voids) {
|
||||
try {
|
||||
JSONObject responseObject = new JSONObject(response);
|
||||
JSONArray nameValuePairs = responseObject.getJSONObject(JSONUtils.ARGS_KEY).getJSONArray(JSONUtils.FIELDS_KEY);
|
||||
|
||||
nameValuePairsMap = new HashMap<>();
|
||||
for(int i = 0; i < nameValuePairs.length(); i++) {
|
||||
nameValuePairsMap.put(nameValuePairs.getJSONObject(i).getString(JSONUtils.NAME_KEY),
|
||||
RedditUtils.getRequestBody(nameValuePairs.getJSONObject(i).getString(JSONUtils.VALUE_KEY)));
|
||||
}
|
||||
|
||||
successful = true;
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
successful = false;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void aVoid) {
|
||||
if(successful) {
|
||||
parseJSONResponseFromAWSListener.parseSuccessful(nameValuePairsMap);
|
||||
} else {
|
||||
parseJSONResponseFromAWSListener.parseFailed();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class ParseXMLReponseFromAWSAsyncTask extends AsyncTask<Void, Void, Void> {
|
||||
interface ParseXMLResponseFromAWSListener {
|
||||
void parseSuccessful(String imageUrl);
|
||||
void parseFailed();
|
||||
}
|
||||
|
||||
private String response;
|
||||
private ParseXMLResponseFromAWSListener parseXMLResponseFromAWSListener;
|
||||
private String imageUrl;
|
||||
private boolean successful;
|
||||
|
||||
ParseXMLReponseFromAWSAsyncTask(String response, ParseXMLResponseFromAWSListener parseXMLResponseFromAWSListener) {
|
||||
this.response = response;
|
||||
this.parseXMLResponseFromAWSListener = parseXMLResponseFromAWSListener;
|
||||
successful = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(Void... voids) {
|
||||
try {
|
||||
XmlPullParser xmlPullParser = XmlPullParserFactory.newInstance().newPullParser();
|
||||
xmlPullParser.setInput(new StringReader(response));
|
||||
|
||||
boolean isLocationTag = false;
|
||||
int eventType = xmlPullParser.getEventType();
|
||||
while(eventType != XmlPullParser.END_DOCUMENT) {
|
||||
if(eventType == XmlPullParser.START_TAG) {
|
||||
if(xmlPullParser.getName().equals("Location")) {
|
||||
isLocationTag = true;
|
||||
}
|
||||
} else if(eventType == XmlPullParser.TEXT) {
|
||||
if(isLocationTag) {
|
||||
imageUrl = xmlPullParser.getText();
|
||||
successful = true;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
eventType = xmlPullParser.next();
|
||||
}
|
||||
} catch (XmlPullParserException | IOException e) {
|
||||
e.printStackTrace();
|
||||
successful = false;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void aVoid) {
|
||||
if(successful) {
|
||||
parseXMLResponseFromAWSListener.parseSuccessful(imageUrl);
|
||||
} else {
|
||||
parseXMLResponseFromAWSListener.parseFailed();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void getSubmittedPost(String response, String kind, Retrofit oauthRetrofit,
|
||||
SharedPreferences authInfoSharedPreferences, Locale locale,
|
||||
SubmitPostListener submitPostListener) throws JSONException {
|
||||
JSONObject responseObject = new JSONObject(response).getJSONObject(JSONUtils.JSON_KEY);
|
||||
@ -94,38 +293,42 @@ class SubmitPost {
|
||||
return;
|
||||
}
|
||||
|
||||
String postId = responseObject.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.ID_KEY);
|
||||
if(!kind.equals(RedditUtils.KIND_IMAGE)) {
|
||||
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, "");
|
||||
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);
|
||||
}
|
||||
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(null);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
Log.i("call_failed", response.message());
|
||||
submitPostListener.submitFailed(response.message());
|
||||
@Override
|
||||
public void onParsePostFail() {
|
||||
submitPostListener.submitFailed(null);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
Log.i("call_failed", response.message());
|
||||
submitPostListener.submitFailed(response.message());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
|
||||
Log.i("call_failed", call.request().url().toString());
|
||||
submitPostListener.submitFailed(t.getMessage());
|
||||
}
|
||||
});
|
||||
@Override
|
||||
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
|
||||
Log.i("call_failed", call.request().url().toString());
|
||||
submitPostListener.submitFailed(t.getMessage());
|
||||
}
|
||||
});
|
||||
} else {
|
||||
submitPostListener.submitSuccessful(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -83,6 +83,7 @@
|
||||
<string name="parse_sent_comment_failed">The comment is sent but unable to get the sent comment</string>
|
||||
|
||||
<string name="select_a_subreddit">Please select a subreddit first</string>
|
||||
<string name="select_an_image">Please select an image first</string>
|
||||
<string name="posting">Posting</string>
|
||||
<string name="post_failed">Could not post it</string>
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user