diff options
author | Alex Ning <chineseperson5@gmail.com> | 2020-06-03 11:02:46 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2020-06-03 11:02:46 +0000 |
commit | 13d64eda146f8fcfe0572274b42db83e4ede7534 (patch) | |
tree | e759a2d965ac87e4ec95129d5919feb1e9a83977 /app/src/main/java | |
parent | 066956971a802d1456716141ee798033e0c1d60f (diff) | |
download | infinity-for-reddit-13d64eda146f8fcfe0572274b42db83e4ede7534.tar infinity-for-reddit-13d64eda146f8fcfe0572274b42db83e4ede7534.tar.gz infinity-for-reddit-13d64eda146f8fcfe0572274b42db83e4ede7534.tar.bz2 infinity-for-reddit-13d64eda146f8fcfe0572274b42db83e4ede7534.tar.lz infinity-for-reddit-13d64eda146f8fcfe0572274b42db83e4ede7534.tar.xz infinity-for-reddit-13d64eda146f8fcfe0572274b42db83e4ede7534.tar.zst infinity-for-reddit-13d64eda146f8fcfe0572274b42db83e4ede7534.zip |
Support viewing imgur albums in-app.
Diffstat (limited to '')
55 files changed, 1210 insertions, 284 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/GfycatAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/API/GfycatAPI.java index 0ea675e3..56eafa4d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/GfycatAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/API/GfycatAPI.java @@ -1,4 +1,4 @@ -package ml.docilealligator.infinityforreddit; +package ml.docilealligator.infinityforreddit.API; import retrofit2.Call; import retrofit2.http.GET; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/API/ImgurAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/API/ImgurAPI.java new file mode 100644 index 00000000..b6534ea5 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/API/ImgurAPI.java @@ -0,0 +1,18 @@ +package ml.docilealligator.infinityforreddit.API; + +import ml.docilealligator.infinityforreddit.Utils.APIUtils; +import retrofit2.Call; +import retrofit2.http.GET; +import retrofit2.http.Header; +import retrofit2.http.Path; + +public interface ImgurAPI { + @GET("gallery/{id}") + Call<String> getGalleryImages(@Header(APIUtils.AUTHORIZATION_KEY) String clientId, @Path("id") String id); + + @GET("/album/{id}") + Call<String> getAlbumImages(@Header(APIUtils.AUTHORIZATION_KEY) String clientId, @Path("id") String id); + + @GET("/image/{id}") + Call<String> getImage(@Header(APIUtils.AUTHORIZATION_KEY) String clientId, @Path("id") String id); +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/API/RedditAPI.java index 113e1604..e7922897 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/API/RedditAPI.java @@ -1,4 +1,4 @@ -package ml.docilealligator.infinityforreddit; +package ml.docilealligator.infinityforreddit.API; import java.util.Map; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java index 8440fec7..709a8cd5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java @@ -10,8 +10,9 @@ import java.io.IOException; import java.util.HashMap; import java.util.Map; +import ml.docilealligator.infinityforreddit.API.RedditAPI; import ml.docilealligator.infinityforreddit.Account.Account; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import okhttp3.Authenticator; import okhttp3.Headers; import okhttp3.Request; @@ -33,7 +34,7 @@ class AccessTokenAuthenticator implements Authenticator { @Override public Request authenticate(Route route, @NonNull Response response) { if (response.code() == 401) { - String accessToken = response.request().header(RedditUtils.AUTHORIZATION_KEY).substring(RedditUtils.AUTHORIZATION_BASE.length()); + String accessToken = response.request().header(APIUtils.AUTHORIZATION_KEY).substring(APIUtils.AUTHORIZATION_BASE.length()); synchronized (this) { Account account = mRedditDataRoomDatabase.accountDao().getCurrentAccount(); if (account == null) { @@ -43,12 +44,12 @@ class AccessTokenAuthenticator implements Authenticator { if (accessToken.equals(accessTokenFromDatabase)) { String newAccessToken = refreshAccessToken(account); if (!newAccessToken.equals("")) { - return response.request().newBuilder().headers(Headers.of(RedditUtils.getOAuthHeader(newAccessToken))).build(); + return response.request().newBuilder().headers(Headers.of(APIUtils.getOAuthHeader(newAccessToken))).build(); } else { return null; } } else { - return response.request().newBuilder().headers(Headers.of(RedditUtils.getOAuthHeader(accessTokenFromDatabase))).build(); + return response.request().newBuilder().headers(Headers.of(APIUtils.getOAuthHeader(accessTokenFromDatabase))).build(); } } } @@ -61,15 +62,15 @@ class AccessTokenAuthenticator implements Authenticator { RedditAPI api = mRetrofit.create(RedditAPI.class); Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.GRANT_TYPE_KEY, RedditUtils.GRANT_TYPE_REFRESH_TOKEN); - params.put(RedditUtils.REFRESH_TOKEN_KEY, refreshToken); + params.put(APIUtils.GRANT_TYPE_KEY, APIUtils.GRANT_TYPE_REFRESH_TOKEN); + params.put(APIUtils.REFRESH_TOKEN_KEY, refreshToken); - Call<String> accessTokenCall = api.getAccessToken(RedditUtils.getHttpBasicAuthHeader(), params); + Call<String> accessTokenCall = api.getAccessToken(APIUtils.getHttpBasicAuthHeader(), params); try { retrofit2.Response response = accessTokenCall.execute(); if (response.isSuccessful() && response.body() != null) { JSONObject jsonObject = new JSONObject((String) response.body()); - String newAccessToken = jsonObject.getString(RedditUtils.ACCESS_TOKEN_KEY); + String newAccessToken = jsonObject.getString(APIUtils.ACCESS_TOKEN_KEY); mRedditDataRoomDatabase.accountDao().changeAccessToken(account.getUsername(), newAccessToken); return newAccessToken; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/EditCommentActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/EditCommentActivity.java index fac5af22..307f5e13 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/EditCommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/EditCommentActivity.java @@ -34,8 +34,8 @@ import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.Event.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; -import ml.docilealligator.infinityforreddit.RedditAPI; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.API.RedditAPI; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import ml.docilealligator.infinityforreddit.Utils.Utils; import retrofit2.Call; import retrofit2.Callback; @@ -150,11 +150,11 @@ public class EditCommentActivity extends BaseActivity { String content = contentEditText.getText().toString(); Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.THING_ID_KEY, mFullName); - params.put(RedditUtils.TEXT_KEY, content); + params.put(APIUtils.THING_ID_KEY, mFullName); + params.put(APIUtils.TEXT_KEY, content); mOauthRetrofit.create(RedditAPI.class) - .editPostOrComment(RedditUtils.getOAuthHeader(mAccessToken), params) + .editPostOrComment(APIUtils.getOAuthHeader(mAccessToken), params) .enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/EditPostActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/EditPostActivity.java index bdc92af4..aaf5d2f5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/EditPostActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/EditPostActivity.java @@ -36,8 +36,8 @@ import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.Event.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; -import ml.docilealligator.infinityforreddit.RedditAPI; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.API.RedditAPI; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -153,11 +153,11 @@ public class EditPostActivity extends BaseActivity { Snackbar.make(coordinatorLayout, R.string.posting, Snackbar.LENGTH_SHORT).show(); Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.THING_ID_KEY, mFullName); - params.put(RedditUtils.TEXT_KEY, contentEditText.getText().toString()); + params.put(APIUtils.THING_ID_KEY, mFullName); + params.put(APIUtils.TEXT_KEY, contentEditText.getText().toString()); mOauthRetrofit.create(RedditAPI.class) - .editPostOrComment(RedditUtils.getOAuthHeader(mAccessToken), params) + .editPostOrComment(APIUtils.getOAuthHeader(mAccessToken), params) .enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LinkResolverActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LinkResolverActivity.java index 52c6ddc3..1a042e65 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LinkResolverActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LinkResolverActivity.java @@ -39,6 +39,9 @@ public class LinkResolverActivity extends AppCompatActivity { private static final String MULTIREDDIT_PATTERN_2 = "/[rR]/(\\w+\\+?)+/?"; private static final String REDD_IT_POST_PATTERN = "/\\w+/?"; private static final String GFYCAT_PATTERN = "/[\\w-]+$"; + private static final String IMGUR_GALLERY_PATTERN = "/gallery/\\w+/?"; + private static final String IMGUR_ALBUM_PATTERN = "/(album|a)/\\w+/?"; + private static final String IMGUR_IMAGE_PATTERN = "/\\w+/?"; @Inject @Named("default") @@ -162,6 +165,27 @@ public class LinkResolverActivity extends AppCompatActivity { } else { deepLinkError(uri); } + } else if (authority.contains("imgur.com")) { + if (path.matches(IMGUR_GALLERY_PATTERN)) { + Intent intent = new Intent(this, ViewImgurMediaActivity.class); + intent.putExtra(ViewImgurMediaActivity.EXTRA_IMGUR_TYPE, ViewImgurMediaActivity.IMGUR_TYPE_GALLERY); + intent.putExtra(ViewImgurMediaActivity.EXTRA_IMGUR_ID, segments.get(1)); + startActivity(intent); + } else if (path.matches(IMGUR_ALBUM_PATTERN)) { + Intent intent = new Intent(this, ViewImgurMediaActivity.class); + intent.putExtra(ViewImgurMediaActivity.EXTRA_IMGUR_TYPE, ViewImgurMediaActivity.IMGUR_TYPE_ALBUM); + intent.putExtra(ViewImgurMediaActivity.EXTRA_IMGUR_ID, segments.get(1)); + startActivity(intent); + } else if (path.matches(IMGUR_IMAGE_PATTERN)) { + Intent intent = new Intent(this, ViewImgurMediaActivity.class); + intent.putExtra(ViewImgurMediaActivity.EXTRA_IMGUR_TYPE, ViewImgurMediaActivity.IMGUR_TYPE_IMAGE); + intent.putExtra(ViewImgurMediaActivity.EXTRA_IMGUR_ID, path.substring(1)); + startActivity(intent); + } else { + deepLinkError(uri); + } + } else { + deepLinkError(uri); } } else { deepLinkError(uri); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LoginActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LoginActivity.java index 8ed1df56..7f03f302 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LoginActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/LoginActivity.java @@ -34,9 +34,9 @@ import ml.docilealligator.infinityforreddit.FetchMyInfo; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.ParseAndSaveAccountInfo; import ml.docilealligator.infinityforreddit.R; -import ml.docilealligator.infinityforreddit.RedditAPI; +import ml.docilealligator.infinityforreddit.API.RedditAPI; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -83,14 +83,14 @@ public class LoginActivity extends BaseActivity { webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setDomStorageEnabled(true); - Uri baseUri = Uri.parse(RedditUtils.OAUTH_URL); + Uri baseUri = Uri.parse(APIUtils.OAUTH_URL); Uri.Builder uriBuilder = baseUri.buildUpon(); - uriBuilder.appendQueryParameter(RedditUtils.CLIENT_ID_KEY, RedditUtils.CLIENT_ID); - uriBuilder.appendQueryParameter(RedditUtils.RESPONSE_TYPE_KEY, RedditUtils.RESPONSE_TYPE); - uriBuilder.appendQueryParameter(RedditUtils.STATE_KEY, RedditUtils.STATE); - uriBuilder.appendQueryParameter(RedditUtils.REDIRECT_URI_KEY, RedditUtils.REDIRECT_URI); - uriBuilder.appendQueryParameter(RedditUtils.DURATION_KEY, RedditUtils.DURATION); - uriBuilder.appendQueryParameter(RedditUtils.SCOPE_KEY, RedditUtils.SCOPE); + uriBuilder.appendQueryParameter(APIUtils.CLIENT_ID_KEY, APIUtils.CLIENT_ID); + uriBuilder.appendQueryParameter(APIUtils.RESPONSE_TYPE_KEY, APIUtils.RESPONSE_TYPE); + uriBuilder.appendQueryParameter(APIUtils.STATE_KEY, APIUtils.STATE); + uriBuilder.appendQueryParameter(APIUtils.REDIRECT_URI_KEY, APIUtils.REDIRECT_URI); + uriBuilder.appendQueryParameter(APIUtils.DURATION_KEY, APIUtils.DURATION); + uriBuilder.appendQueryParameter(APIUtils.SCOPE_KEY, APIUtils.SCOPE); String url = uriBuilder.toString(); @@ -104,16 +104,16 @@ public class LoginActivity extends BaseActivity { if (url.contains("&code=") || url.contains("?code=")) { Uri uri = Uri.parse(url); String state = uri.getQueryParameter("state"); - if (state.equals(RedditUtils.STATE)) { + if (state.equals(APIUtils.STATE)) { authCode = uri.getQueryParameter("code"); Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.GRANT_TYPE_KEY, "authorization_code"); + params.put(APIUtils.GRANT_TYPE_KEY, "authorization_code"); params.put("code", authCode); - params.put("redirect_uri", RedditUtils.REDIRECT_URI); + params.put("redirect_uri", APIUtils.REDIRECT_URI); RedditAPI api = mRetrofit.create(RedditAPI.class); - Call<String> accessTokenCall = api.getAccessToken(RedditUtils.getHttpBasicAuthHeader(), params); + Call<String> accessTokenCall = api.getAccessToken(APIUtils.getHttpBasicAuthHeader(), params); accessTokenCall.enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { @@ -126,8 +126,8 @@ public class LoginActivity extends BaseActivity { } JSONObject responseJSON = new JSONObject(accountResponse); - String accessToken = responseJSON.getString(RedditUtils.ACCESS_TOKEN_KEY); - String refreshToken = responseJSON.getString(RedditUtils.REFRESH_TOKEN_KEY); + String accessToken = responseJSON.getString(APIUtils.ACCESS_TOKEN_KEY); + String refreshToken = responseJSON.getString(APIUtils.REFRESH_TOKEN_KEY); FetchMyInfo.fetchAccountInfo(mOauthRetrofit, accessToken, new FetchMyInfo.FetchUserMyListener() { @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostLinkActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostLinkActivity.java index 133d4f7d..f59e807b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostLinkActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostLinkActivity.java @@ -46,7 +46,7 @@ import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.Service.SubmitPostService; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import pl.droidsonroids.gif.GifImageView; import retrofit2.Retrofit; @@ -444,7 +444,7 @@ public class PostLinkActivity extends BaseActivity implements FlairBottomSheetFr intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName); intent.putExtra(SubmitPostService.EXTRA_TITLE, titleEditText.getText().toString()); intent.putExtra(SubmitPostService.EXTRA_CONTENT, linkEditText.getText().toString()); - intent.putExtra(SubmitPostService.EXTRA_KIND, RedditUtils.KIND_LINK); + intent.putExtra(SubmitPostService.EXTRA_KIND, APIUtils.KIND_LINK); intent.putExtra(SubmitPostService.EXTRA_FLAIR, flair); intent.putExtra(SubmitPostService.EXTRA_IS_SPOILER, isSpoiler); intent.putExtra(SubmitPostService.EXTRA_IS_NSFW, isNSFW); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostTextActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostTextActivity.java index 75debb2c..a448759f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostTextActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostTextActivity.java @@ -46,7 +46,7 @@ import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.Service.SubmitPostService; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import pl.droidsonroids.gif.GifImageView; import retrofit2.Retrofit; @@ -444,7 +444,7 @@ public class PostTextActivity extends BaseActivity implements FlairBottomSheetFr intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName); intent.putExtra(SubmitPostService.EXTRA_TITLE, titleEditText.getText().toString()); intent.putExtra(SubmitPostService.EXTRA_CONTENT, contentEditText.getText().toString()); - intent.putExtra(SubmitPostService.EXTRA_KIND, RedditUtils.KIND_SELF); + intent.putExtra(SubmitPostService.EXTRA_KIND, APIUtils.KIND_SELF); intent.putExtra(SubmitPostService.EXTRA_FLAIR, flair); intent.putExtra(SubmitPostService.EXTRA_IS_SPOILER, isSpoiler); intent.putExtra(SubmitPostService.EXTRA_IS_NSFW, isNSFW); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImgurMediaActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImgurMediaActivity.java new file mode 100644 index 00000000..cf335696 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImgurMediaActivity.java @@ -0,0 +1,307 @@ +package ml.docilealligator.infinityforreddit.Activity; + +import android.content.SharedPreferences; +import android.graphics.drawable.ColorDrawable; +import android.graphics.drawable.Drawable; +import android.os.AsyncTask; +import android.os.Bundle; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; +import android.widget.ProgressBar; +import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.appcompat.app.ActionBar; +import androidx.appcompat.app.AppCompatActivity; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; +import androidx.fragment.app.FragmentStatePagerAdapter; +import androidx.viewpager.widget.ViewPager; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.ArrayList; + +import javax.inject.Inject; +import javax.inject.Named; + +import butterknife.BindView; +import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.API.ImgurAPI; +import ml.docilealligator.infinityforreddit.ContentFontStyle; +import ml.docilealligator.infinityforreddit.FontStyle; +import ml.docilealligator.infinityforreddit.Fragment.ViewImgurGifFragment; +import ml.docilealligator.infinityforreddit.Fragment.ViewImgurImageFragment; +import ml.docilealligator.infinityforreddit.Fragment.ViewImgurVideoFragment; +import ml.docilealligator.infinityforreddit.ImgurMedia; +import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.TitleFontStyle; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; +import ml.docilealligator.infinityforreddit.Utils.JSONUtils; +import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; + +public class ViewImgurMediaActivity extends AppCompatActivity { + + public static final String EXTRA_IMGUR_TYPE = "EIT"; + public static final String EXTRA_IMGUR_ID = "EII"; + public static final int IMGUR_TYPE_GALLERY = 0; + public static final int IMGUR_TYPE_ALBUM = 1; + public static final int IMGUR_TYPE_IMAGE = 2; + private static final String IMGUR_IMAGES_STATE = "IIS"; + + @BindView(R.id.progress_bar_view_imgur_media_activity) + ProgressBar progressBar; + @BindView(R.id.view_pager_view_imgur_media_activity) + ViewPager viewPager; + private SectionsPagerAdapter sectionsPagerAdapter; + private ArrayList<ImgurMedia> images; + @Inject + @Named("imgur") + Retrofit imgurRetrofit; + @Inject + @Named("default") + SharedPreferences sharedPreferences; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + ((Infinity) getApplication()).getAppComponent().inject(this); + + getTheme().applyStyle(R.style.Theme_Normal, true); + + getTheme().applyStyle(FontStyle.valueOf(sharedPreferences + .getString(SharedPreferencesUtils.FONT_SIZE_KEY, FontStyle.Normal.name())).getResId(), true); + + getTheme().applyStyle(TitleFontStyle.valueOf(sharedPreferences + .getString(SharedPreferencesUtils.TITLE_FONT_SIZE_KEY, TitleFontStyle.Normal.name())).getResId(), true); + + getTheme().applyStyle(ContentFontStyle.valueOf(sharedPreferences + .getString(SharedPreferencesUtils.CONTENT_FONT_SIZE_KEY, ContentFontStyle.Normal.name())).getResId(), true); + + setContentView(R.layout.activity_view_imgur_media); + + ButterKnife.bind(this); + + ActionBar actionBar = getSupportActionBar(); + Drawable upArrow = getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp); + actionBar.setHomeAsUpIndicator(upArrow); + actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.transparentActionBarAndExoPlayerControllerColor))); + + setTitle(" "); + + String imgurId = getIntent().getStringExtra(EXTRA_IMGUR_ID); + if (imgurId == null) { + finish(); + return; + } + + if (savedInstanceState != null) { + images = savedInstanceState.getParcelableArrayList(IMGUR_IMAGES_STATE); + } + + if (images == null) { + switch (getIntent().getIntExtra(EXTRA_IMGUR_TYPE, IMGUR_TYPE_IMAGE)) { + case IMGUR_TYPE_GALLERY: + imgurRetrofit.create(ImgurAPI.class).getGalleryImages(APIUtils.IMGUR_CLIENT_ID, imgurId) + .enqueue(new Callback<String>() { + @Override + public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { + if (response.isSuccessful()) { + new ParseImgurImagesAsyncTask(response.body(), new ParseImgurImagesAsyncTask.ParseImgurImagesAsyncTaskListener() { + @Override + public void success(ArrayList<ImgurMedia> images) { + ViewImgurMediaActivity.this.images = images; + progressBar.setVisibility(View.GONE); + setupViewPager(); + } + + @Override + public void failed() { + progressBar.setVisibility(View.GONE); + Toast.makeText(ViewImgurMediaActivity.this, R.string.error_fetching_imgur_media, Toast.LENGTH_SHORT).show(); + } + }).execute(); + } else { + progressBar.setVisibility(View.GONE); + Toast.makeText(ViewImgurMediaActivity.this, R.string.error_fetching_imgur_media, Toast.LENGTH_SHORT).show(); + } + } + + @Override + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + progressBar.setVisibility(View.GONE); + Toast.makeText(ViewImgurMediaActivity.this, R.string.error_fetching_imgur_media, Toast.LENGTH_SHORT).show(); + } + }); + break; + case IMGUR_TYPE_ALBUM: + break; + case IMGUR_TYPE_IMAGE: + break; + } + } else { + progressBar.setVisibility(View.GONE); + setupViewPager(); + } + } + + private void setupViewPager() { + setToolbarTitle(0); + sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); + viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { + @Override + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { + + } + + @Override + public void onPageSelected(int position) { + switch (images.get(position).getType()) { + case ImgurMedia.TYPE_GIF: + setTitle("GIF " + (position + 1) + "/" + images.size()); + break; + case ImgurMedia.TYPE_VIDEO: + setTitle("Video "+ (position + 1) + "/" + images.size()); + break; + default: + setTitle("Image " + (position + 1) + "/" + images.size()); + + } + } + + @Override + public void onPageScrollStateChanged(int state) { + + } + }); + viewPager.setAdapter(sectionsPagerAdapter); + } + + private void setToolbarTitle(int position) { + switch (images.get(position).getType()) { + case ImgurMedia.TYPE_GIF: + setTitle("GIF " + (position + 1) + "/" + images.size()); + break; + case ImgurMedia.TYPE_VIDEO: + setTitle("Video "+ (position + 1) + "/" + images.size()); + break; + default: + setTitle("Image " + (position + 1) + "/" + images.size()); + + } + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + return true; + } + + @Override + public boolean onOptionsItemSelected(@NonNull MenuItem item) { + if (item.getItemId() == android.R.id.home) { + finish(); + return true; + } + + return false; + } + + @Override + protected void onSaveInstanceState(@NonNull Bundle outState) { + super.onSaveInstanceState(outState); + outState.putParcelableArrayList(IMGUR_IMAGES_STATE, images); + } + + private class SectionsPagerAdapter extends FragmentStatePagerAdapter { + + SectionsPagerAdapter(@NonNull FragmentManager fm) { + super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT); + } + + @NonNull + @Override + public Fragment getItem(int position) { + ImgurMedia imgurMedia = images.get(position); + if (imgurMedia.getType() == ImgurMedia.TYPE_VIDEO) { + ViewImgurVideoFragment fragment = new ViewImgurVideoFragment(); + Bundle bundle = new Bundle(); + bundle.putParcelable(ViewImgurVideoFragment.EXTRA_IMGUR_VIDEO, imgurMedia); + fragment.setArguments(bundle); + return fragment; + } else if (imgurMedia.getType() == ImgurMedia.TYPE_GIF) { + ViewImgurGifFragment fragment = new ViewImgurGifFragment(); + Bundle bundle = new Bundle(); + bundle.putParcelable(ViewImgurGifFragment.EXTRA_IMGUR_GIF, imgurMedia); + fragment.setArguments(bundle); + return fragment; + } else { + ViewImgurImageFragment fragment = new ViewImgurImageFragment(); + Bundle bundle = new Bundle(); + bundle.putParcelable(ViewImgurImageFragment.EXTRA_IMGUR_IMAGES, imgurMedia); + fragment.setArguments(bundle); + return fragment; + } + } + + @Override + public int getCount() { + return images.size(); + } + } + + private static class ParseImgurImagesAsyncTask extends AsyncTask<Void, Void, Void> { + + private String response; + private ArrayList<ImgurMedia> images; + private boolean parseFailed = false; + private ParseImgurImagesAsyncTaskListener parseImgurImagesAsyncTaskListener; + + interface ParseImgurImagesAsyncTaskListener { + void success(ArrayList<ImgurMedia> images); + void failed(); + } + + ParseImgurImagesAsyncTask(String response, ParseImgurImagesAsyncTaskListener parseImgurImagesAsyncTaskListener) { + this.response = response; + this.parseImgurImagesAsyncTaskListener = parseImgurImagesAsyncTaskListener; + } + + @Override + protected Void doInBackground(Void... voids) { + try { + JSONArray jsonArray = new JSONObject(response).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.IMAGES_KEY); + images = new ArrayList<>(); + for (int i = 0; i < jsonArray.length(); i++) { + JSONObject image = jsonArray.getJSONObject(i); + images.add(new ImgurMedia(image.getString(JSONUtils.ID_KEY), + image.getString(JSONUtils.TITLE_KEY), image.getString(JSONUtils.DESCRIPTION_KEY), + image.getString(JSONUtils.TYPE_KEY), image.getString(JSONUtils.LINK_KEY))); + } + } catch (JSONException e) { + e.printStackTrace(); + parseFailed = true; + } + + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + if (parseFailed) { + parseImgurImagesAsyncTaskListener.failed(); + } else { + parseImgurImagesAsyncTaskListener.success(images); + } + } + } +}
\ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java index f7068f0b..e258124b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java @@ -80,12 +80,12 @@ import ml.docilealligator.infinityforreddit.Post.ParsePost; import ml.docilealligator.infinityforreddit.Post.Post; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.ReadMessage; -import ml.docilealligator.infinityforreddit.RedditAPI; +import ml.docilealligator.infinityforreddit.API.RedditAPI; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SaveThing; import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import retrofit2.Call; import retrofit2.Callback; @@ -582,10 +582,10 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS } else { if (isSingleCommentThreadMode && mSingleCommentId != null) { postAndComments = mOauthRetrofit.create(RedditAPI.class).getPostAndCommentsSingleThreadByIdOauth(subredditId, - mSingleCommentId, sortType, RedditUtils.getOAuthHeader(mAccessToken)); + mSingleCommentId, sortType, APIUtils.getOAuthHeader(mAccessToken)); } else { postAndComments = mOauthRetrofit.create(RedditAPI.class).getPostAndCommentsByIdOauth(subredditId, - sortType, RedditUtils.getOAuthHeader(mAccessToken)); + sortType, APIUtils.getOAuthHeader(mAccessToken)); } } postAndComments.enqueue(new Callback<String>() { @@ -976,8 +976,8 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS } Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.ID_KEY, mPost.getFullName()); - mOauthRetrofit.create(RedditAPI.class).markNSFW(RedditUtils.getOAuthHeader(mAccessToken), params) + params.put(APIUtils.ID_KEY, mPost.getFullName()); + mOauthRetrofit.create(RedditAPI.class).markNSFW(APIUtils.getOAuthHeader(mAccessToken), params) .enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { @@ -1014,8 +1014,8 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS } Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.ID_KEY, mPost.getFullName()); - mOauthRetrofit.create(RedditAPI.class).unmarkNSFW(RedditUtils.getOAuthHeader(mAccessToken), params) + params.put(APIUtils.ID_KEY, mPost.getFullName()); + mOauthRetrofit.create(RedditAPI.class).unmarkNSFW(APIUtils.getOAuthHeader(mAccessToken), params) .enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { @@ -1052,8 +1052,8 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS } Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.ID_KEY, mPost.getFullName()); - mOauthRetrofit.create(RedditAPI.class).markSpoiler(RedditUtils.getOAuthHeader(mAccessToken), params) + params.put(APIUtils.ID_KEY, mPost.getFullName()); + mOauthRetrofit.create(RedditAPI.class).markSpoiler(APIUtils.getOAuthHeader(mAccessToken), params) .enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { @@ -1090,8 +1090,8 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS } Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.ID_KEY, mPost.getFullName()); - mOauthRetrofit.create(RedditAPI.class).unmarkSpoiler(RedditUtils.getOAuthHeader(mAccessToken), params) + params.put(APIUtils.ID_KEY, mPost.getFullName()); + mOauthRetrofit.create(RedditAPI.class).unmarkSpoiler(APIUtils.getOAuthHeader(mAccessToken), params) .enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { @@ -1582,13 +1582,13 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS @Override public void flairSelected(Flair flair) { Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.API_TYPE_KEY, RedditUtils.API_TYPE_JSON); - params.put(RedditUtils.FLAIR_TEMPLATE_ID_KEY, flair.getId()); - params.put(RedditUtils.LINK_KEY, mPost.getFullName()); - params.put(RedditUtils.TEXT_KEY, flair.getText()); + params.put(APIUtils.API_TYPE_KEY, APIUtils.API_TYPE_JSON); + params.put(APIUtils.FLAIR_TEMPLATE_ID_KEY, flair.getId()); + params.put(APIUtils.LINK_KEY, mPost.getFullName()); + params.put(APIUtils.TEXT_KEY, flair.getText()); mOauthRetrofit.create(RedditAPI.class).selectFlair(mPost.getSubredditNamePrefixed(), - RedditUtils.getOAuthHeader(mAccessToken), params).enqueue(new Callback<String>() { + APIUtils.getOAuthHeader(mAccessToken), params).enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { if (response.isSuccessful()) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java index 64ab34ff..4f638d8b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java @@ -103,7 +103,7 @@ import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SaveThing; import ml.docilealligator.infinityforreddit.Utils.GlideImageGetter; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.Utils.Utils; import ml.docilealligator.infinityforreddit.VoteThing; @@ -1681,13 +1681,13 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy if (previousVoteType != 1) { //Not upvoted before mPost.setVoteType(1); - newVoteType = RedditUtils.DIR_UPVOTE; + newVoteType = APIUtils.DIR_UPVOTE; mUpvoteButton.setColorFilter(mUpvotedColor, android.graphics.PorterDuff.Mode.SRC_IN); mScoreTextView.setTextColor(mUpvotedColor); } else { //Upvoted before mPost.setVoteType(0); - newVoteType = RedditUtils.DIR_UNVOTE; + newVoteType = APIUtils.DIR_UNVOTE; mUpvoteButton.setColorFilter(mPostIconAndInfoColor, android.graphics.PorterDuff.Mode.SRC_IN); mScoreTextView.setTextColor(mPostIconAndInfoColor); } @@ -1700,7 +1700,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy VoteThing.voteThing(mActivity, mOauthRetrofit, mAccessToken, new VoteThing.VoteThingWithoutPositionListener() { @Override public void onVoteThingSuccess() { - if (newVoteType.equals(RedditUtils.DIR_UPVOTE)) { + if (newVoteType.equals(APIUtils.DIR_UPVOTE)) { mPost.setVoteType(1); mUpvoteButton.setColorFilter(mUpvotedColor, android.graphics.PorterDuff.Mode.SRC_IN); mScoreTextView.setTextColor(mUpvotedColor); @@ -1755,13 +1755,13 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy if (previousVoteType != -1) { //Not upvoted before mPost.setVoteType(-1); - newVoteType = RedditUtils.DIR_DOWNVOTE; + newVoteType = APIUtils.DIR_DOWNVOTE; mDownvoteButton.setColorFilter(mDownvotedColor, android.graphics.PorterDuff.Mode.SRC_IN); mScoreTextView.setTextColor(mDownvotedColor); } else { //Upvoted before mPost.setVoteType(0); - newVoteType = RedditUtils.DIR_UNVOTE; + newVoteType = APIUtils.DIR_UNVOTE; mDownvoteButton.setColorFilter(mPostIconAndInfoColor, android.graphics.PorterDuff.Mode.SRC_IN); mScoreTextView.setTextColor(mPostIconAndInfoColor); } @@ -1774,7 +1774,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy VoteThing.voteThing(mActivity, mOauthRetrofit, mAccessToken, new VoteThing.VoteThingWithoutPositionListener() { @Override public void onVoteThingSuccess() { - if (newVoteType.equals(RedditUtils.DIR_DOWNVOTE)) { + if (newVoteType.equals(APIUtils.DIR_DOWNVOTE)) { mPost.setVoteType(-1); mDownvoteButton.setColorFilter(mDownvotedColor, android.graphics.PorterDuff.Mode.SRC_IN); mScoreTextView.setTextColor(mDownvotedColor); @@ -2772,13 +2772,13 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy if (previousVoteType != CommentData.VOTE_TYPE_UPVOTE) { //Not upvoted before comment.setVoteType(CommentData.VOTE_TYPE_UPVOTE); - newVoteType = RedditUtils.DIR_UPVOTE; + newVoteType = APIUtils.DIR_UPVOTE; upvoteButton.setColorFilter(mUpvotedColor, android.graphics.PorterDuff.Mode.SRC_IN); scoreTextView.setTextColor(mUpvotedColor); } else { //Upvoted before comment.setVoteType(CommentData.VOTE_TYPE_NO_VOTE); - newVoteType = RedditUtils.DIR_UNVOTE; + newVoteType = APIUtils.DIR_UNVOTE; upvoteButton.setColorFilter(mCommentIconAndInfoColor, android.graphics.PorterDuff.Mode.SRC_IN); scoreTextView.setTextColor(mCommentIconAndInfoColor); } @@ -2791,7 +2791,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy VoteThing.voteThing(mActivity, mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() { @Override public void onVoteThingSuccess(int position) { - if (newVoteType.equals(RedditUtils.DIR_UPVOTE)) { + if (newVoteType.equals(APIUtils.DIR_UPVOTE)) { comment.setVoteType(CommentData.VOTE_TYPE_UPVOTE); upvoteButton.setColorFilter(mUpvotedColor, android.graphics.PorterDuff.Mode.SRC_IN); scoreTextView.setTextColor(mUpvotedColor); @@ -2834,13 +2834,13 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy if (previousVoteType != CommentData.VOTE_TYPE_DOWNVOTE) { //Not downvoted before comment.setVoteType(CommentData.VOTE_TYPE_DOWNVOTE); - newVoteType = RedditUtils.DIR_DOWNVOTE; + newVoteType = APIUtils.DIR_DOWNVOTE; downvoteButton.setColorFilter(mDownvotedColor, android.graphics.PorterDuff.Mode.SRC_IN); scoreTextView.setTextColor(mDownvotedColor); } else { //Downvoted before comment.setVoteType(CommentData.VOTE_TYPE_NO_VOTE); - newVoteType = RedditUtils.DIR_UNVOTE; + newVoteType = APIUtils.DIR_UNVOTE; downvoteButton.setColorFilter(mCommentIconAndInfoColor, android.graphics.PorterDuff.Mode.SRC_IN); scoreTextView.setTextColor(mCommentIconAndInfoColor); } @@ -2853,7 +2853,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy VoteThing.voteThing(mActivity, mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() { @Override public void onVoteThingSuccess(int position1) { - if (newVoteType.equals(RedditUtils.DIR_DOWNVOTE)) { + if (newVoteType.equals(APIUtils.DIR_DOWNVOTE)) { comment.setVoteType(CommentData.VOTE_TYPE_DOWNVOTE); downvoteButton.setColorFilter(mDownvotedColor, android.graphics.PorterDuff.Mode.SRC_IN); scoreTextView.setTextColor(mDownvotedColor); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentsListingRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentsListingRecyclerViewAdapter.java index ce0d30eb..3c32b201 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentsListingRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentsListingRecyclerViewAdapter.java @@ -43,7 +43,7 @@ import ml.docilealligator.infinityforreddit.Fragment.CommentMoreBottomSheetFragm import ml.docilealligator.infinityforreddit.NetworkState; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.SaveThing; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import ml.docilealligator.infinityforreddit.Utils.Utils; import ml.docilealligator.infinityforreddit.VoteThing; import retrofit2.Retrofit; @@ -256,14 +256,14 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment if (previousVoteType != CommentData.VOTE_TYPE_UPVOTE) { //Not upvoted before comment.setVoteType(CommentData.VOTE_TYPE_UPVOTE); - newVoteType = RedditUtils.DIR_UPVOTE; + newVoteType = APIUtils.DIR_UPVOTE; ((CommentViewHolder) holder).upvoteButton .setColorFilter(mUpvotedColor, android.graphics.PorterDuff.Mode.SRC_IN); ((CommentViewHolder) holder).scoreTextView.setTextColor(mUpvotedColor); } else { //Upvoted before comment.setVoteType(CommentData.VOTE_TYPE_NO_VOTE); - newVoteType = RedditUtils.DIR_UNVOTE; + newVoteType = APIUtils.DIR_UNVOTE; ((CommentViewHolder) holder).upvoteButton.setColorFilter(mCommentIconAndInfoColor, android.graphics.PorterDuff.Mode.SRC_IN); ((CommentViewHolder) holder).scoreTextView.setTextColor(mCommentIconAndInfoColor); } @@ -274,7 +274,7 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment VoteThing.voteThing(mContext, mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() { @Override public void onVoteThingSuccess(int position) { - if (newVoteType.equals(RedditUtils.DIR_UPVOTE)) { + if (newVoteType.equals(APIUtils.DIR_UPVOTE)) { comment.setVoteType(CommentData.VOTE_TYPE_UPVOTE); ((CommentViewHolder) holder).upvoteButton.setColorFilter(mUpvotedColor, android.graphics.PorterDuff.Mode.SRC_IN); ((CommentViewHolder) holder).scoreTextView.setTextColor(mUpvotedColor); @@ -309,13 +309,13 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment if (previousVoteType != CommentData.VOTE_TYPE_DOWNVOTE) { //Not downvoted before comment.setVoteType(CommentData.VOTE_TYPE_DOWNVOTE); - newVoteType = RedditUtils.DIR_DOWNVOTE; + newVoteType = APIUtils.DIR_DOWNVOTE; ((CommentViewHolder) holder).downvoteButton.setColorFilter(mDownvotedColor, android.graphics.PorterDuff.Mode.SRC_IN); ((CommentViewHolder) holder).scoreTextView.setTextColor(mDownvotedColor); } else { //Downvoted before comment.setVoteType(CommentData.VOTE_TYPE_NO_VOTE); - newVoteType = RedditUtils.DIR_UNVOTE; + newVoteType = APIUtils.DIR_UNVOTE; ((CommentViewHolder) holder).downvoteButton.setColorFilter(mCommentIconAndInfoColor, android.graphics.PorterDuff.Mode.SRC_IN); ((CommentViewHolder) holder).scoreTextView.setTextColor(mCommentIconAndInfoColor); } @@ -326,7 +326,7 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment VoteThing.voteThing(mContext, mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() { @Override public void onVoteThingSuccess(int position1) { - if (newVoteType.equals(RedditUtils.DIR_DOWNVOTE)) { + if (newVoteType.equals(APIUtils.DIR_DOWNVOTE)) { comment.setVoteType(CommentData.VOTE_TYPE_DOWNVOTE); ((CommentViewHolder) holder).downvoteButton.setColorFilter(mDownvotedColor, android.graphics.PorterDuff.Mode.SRC_IN); ((CommentViewHolder) holder).scoreTextView.setTextColor(mDownvotedColor); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java index 9840d050..7a1b6a78 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java @@ -83,7 +83,7 @@ import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SaveThing; import ml.docilealligator.infinityforreddit.User.UserDao; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.Utils.Utils; import ml.docilealligator.infinityforreddit.VoteThing; @@ -923,14 +923,14 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView if (previousVoteType != 1) { //Not upvoted before post.setVoteType(1); - newVoteType = RedditUtils.DIR_UPVOTE; + newVoteType = APIUtils.DIR_UPVOTE; ((PostCompactViewHolder) holder).upvoteButton .setColorFilter(mUpvotedColor, android.graphics.PorterDuff.Mode.SRC_IN); ((PostCompactViewHolder) holder).scoreTextView.setTextColor(mUpvotedColor); } else { //Upvoted before post.setVoteType(0); - newVoteType = RedditUtils.DIR_UNVOTE; + newVoteType = APIUtils.DIR_UNVOTE; ((PostCompactViewHolder) holder).upvoteButton.setColorFilter(mPostIconAndInfoColor, android.graphics.PorterDuff.Mode.SRC_IN); ((PostCompactViewHolder) holder).scoreTextView.setTextColor(mPostIconAndInfoColor); } @@ -940,7 +940,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView VoteThing.voteThing(mActivity, mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() { @Override public void onVoteThingSuccess(int position1) { - if (newVoteType.equals(RedditUtils.DIR_UPVOTE)) { + if (newVoteType.equals(APIUtils.DIR_UPVOTE)) { post.setVoteType(1); ((PostCompactViewHolder) holder).upvoteButton .setColorFilter(mUpvotedColor, android.graphics.PorterDuff.Mode.SRC_IN); @@ -994,14 +994,14 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView if (previousVoteType != -1) { //Not downvoted before post.setVoteType(-1); - newVoteType = RedditUtils.DIR_DOWNVOTE; + newVoteType = APIUtils.DIR_DOWNVOTE; ((PostCompactViewHolder) holder).downvoteButton .setColorFilter(mDownvotedColor, android.graphics.PorterDuff.Mode.SRC_IN); ((PostCompactViewHolder) holder).scoreTextView.setTextColor(mDownvotedColor); } else { //Downvoted before post.setVoteType(0); - newVoteType = RedditUtils.DIR_UNVOTE; + newVoteType = APIUtils.DIR_UNVOTE; ((PostCompactViewHolder) holder).downvoteButton.setColorFilter(mPostIconAndInfoColor, android.graphics.PorterDuff.Mode.SRC_IN); ((PostCompactViewHolder) holder).scoreTextView.setTextColor(mPostIconAndInfoColor); } @@ -1011,7 +1011,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView VoteThing.voteThing(mActivity, mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() { @Override public void onVoteThingSuccess(int position1) { - if (newVoteType.equals(RedditUtils.DIR_DOWNVOTE)) { + if (newVoteType.equals(APIUtils.DIR_DOWNVOTE)) { post.setVoteType(-1); ((PostCompactViewHolder) holder).downvoteButton .setColorFilter(mDownvotedColor, android.graphics.PorterDuff.Mode.SRC_IN); @@ -1632,14 +1632,14 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView if (previousVoteType != 1) { //Not upvoted before post.setVoteType(1); - newVoteType = RedditUtils.DIR_UPVOTE; + newVoteType = APIUtils.DIR_UPVOTE; upvoteButton .setColorFilter(mUpvotedColor, android.graphics.PorterDuff.Mode.SRC_IN); scoreTextView.setTextColor(mUpvotedColor); } else { //Upvoted before post.setVoteType(0); - newVoteType = RedditUtils.DIR_UNVOTE; + newVoteType = APIUtils.DIR_UNVOTE; upvoteButton.setColorFilter(mPostIconAndInfoColor, android.graphics.PorterDuff.Mode.SRC_IN); scoreTextView.setTextColor(mPostIconAndInfoColor); } @@ -1649,7 +1649,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView VoteThing.voteThing(mActivity, mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() { @Override public void onVoteThingSuccess(int position1) { - if (newVoteType.equals(RedditUtils.DIR_UPVOTE)) { + if (newVoteType.equals(APIUtils.DIR_UPVOTE)) { post.setVoteType(1); upvoteButton .setColorFilter(mUpvotedColor, android.graphics.PorterDuff.Mode.SRC_IN); @@ -1706,14 +1706,14 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView if (previousVoteType != -1) { //Not downvoted before post.setVoteType(-1); - newVoteType = RedditUtils.DIR_DOWNVOTE; + newVoteType = APIUtils.DIR_DOWNVOTE; downvoteButton .setColorFilter(mDownvotedColor, android.graphics.PorterDuff.Mode.SRC_IN); scoreTextView.setTextColor(mDownvotedColor); } else { //Downvoted before post.setVoteType(0); - newVoteType = RedditUtils.DIR_UNVOTE; + newVoteType = APIUtils.DIR_UNVOTE; downvoteButton.setColorFilter(mPostIconAndInfoColor, android.graphics.PorterDuff.Mode.SRC_IN); scoreTextView.setTextColor(mPostIconAndInfoColor); } @@ -1723,7 +1723,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView VoteThing.voteThing(mActivity, mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() { @Override public void onVoteThingSuccess(int position1) { - if (newVoteType.equals(RedditUtils.DIR_DOWNVOTE)) { + if (newVoteType.equals(APIUtils.DIR_DOWNVOTE)) { post.setVoteType(-1); downvoteButton .setColorFilter(mDownvotedColor, android.graphics.PorterDuff.Mode.SRC_IN); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index 5368f6f4..86274fd6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -34,6 +34,7 @@ import ml.docilealligator.infinityforreddit.Activity.SubscribedThingListingActiv import ml.docilealligator.infinityforreddit.Activity.ThemePreviewActivity; import ml.docilealligator.infinityforreddit.Activity.ViewGIFActivity; import ml.docilealligator.infinityforreddit.Activity.ViewImageActivity; +import ml.docilealligator.infinityforreddit.Activity.ViewImgurMediaActivity; import ml.docilealligator.infinityforreddit.Activity.ViewMessageActivity; import ml.docilealligator.infinityforreddit.Activity.ViewMultiRedditDetailActivity; import ml.docilealligator.infinityforreddit.Activity.ViewPostDetailActivity; @@ -49,6 +50,7 @@ import ml.docilealligator.infinityforreddit.Fragment.SidebarFragment; import ml.docilealligator.infinityforreddit.Fragment.SubredditListingFragment; import ml.docilealligator.infinityforreddit.Fragment.SubscribedSubredditsListingFragment; import ml.docilealligator.infinityforreddit.Fragment.UserListingFragment; +import ml.docilealligator.infinityforreddit.Fragment.ViewImgurVideoFragment; import ml.docilealligator.infinityforreddit.Service.SubmitPostService; import ml.docilealligator.infinityforreddit.Settings.AdvancedPreferenceFragment; import ml.docilealligator.infinityforreddit.Settings.CustomizeMainPageTabsFragment; @@ -167,4 +169,8 @@ public interface AppComponent { void inject(ReportActivity reportActivity); void inject(CustomizeMainPageTabsFragment customizeMainPageTabsFragment); + + void inject(ViewImgurMediaActivity viewImgurMediaActivity); + + void inject(ViewImgurVideoFragment viewImgurVideoFragment); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java index e65d435e..bf64a37a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java @@ -24,7 +24,7 @@ import im.ene.toro.exoplayer.MediaSourceBuilder; import im.ene.toro.exoplayer.ToroExo; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.Utils.CustomThemeSharedPreferencesUtils; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import okhttp3.ConnectionPool; import okhttp3.OkHttpClient; @@ -44,7 +44,7 @@ class AppModule { @Singleton Retrofit provideOauthRetrofit(OkHttpClient okHttpClient) { return new Retrofit.Builder() - .baseUrl(RedditUtils.OAUTH_API_BASE_URI) + .baseUrl(APIUtils.OAUTH_API_BASE_URI) .client(okHttpClient) .addConverterFactory(ScalarsConverterFactory.create()) .build(); @@ -55,7 +55,7 @@ class AppModule { @Singleton Retrofit provideOauthWithoutAuthenticatorRetrofit() { return new Retrofit.Builder() - .baseUrl(RedditUtils.OAUTH_API_BASE_URI) + .baseUrl(APIUtils.OAUTH_API_BASE_URI) .addConverterFactory(ScalarsConverterFactory.create()) .build(); } @@ -65,7 +65,7 @@ class AppModule { @Singleton Retrofit provideRetrofit() { return new Retrofit.Builder() - .baseUrl(RedditUtils.API_BASE_URI) + .baseUrl(APIUtils.API_BASE_URI) .addConverterFactory(ScalarsConverterFactory.create()) .build(); } @@ -75,7 +75,7 @@ class AppModule { @Singleton Retrofit provideUploadMediaRetrofit() { return new Retrofit.Builder() - .baseUrl(RedditUtils.API_UPLOAD_MEDIA_URI) + .baseUrl(APIUtils.API_UPLOAD_MEDIA_URI) .addConverterFactory(ScalarsConverterFactory.create()) .build(); } @@ -85,7 +85,7 @@ class AppModule { @Singleton Retrofit provideUploadVideoRetrofit() { return new Retrofit.Builder() - .baseUrl(RedditUtils.API_UPLOAD_VIDEO_URI) + .baseUrl(APIUtils.API_UPLOAD_VIDEO_URI) .addConverterFactory(ScalarsConverterFactory.create()) .build(); } @@ -95,7 +95,17 @@ class AppModule { @Singleton Retrofit provideGfycatRetrofit() { return new Retrofit.Builder() - .baseUrl(RedditUtils.GFYCAT_API_BASE_URI) + .baseUrl(APIUtils.GFYCAT_API_BASE_URI) + .addConverterFactory(ScalarsConverterFactory.create()) + .build(); + } + + @Provides + @Named("imgur") + @Singleton + Retrofit provideImgurRetrofit() { + return new Retrofit.Builder() + .baseUrl(APIUtils.IMGUR_API_BASE_URI) .addConverterFactory(ScalarsConverterFactory.create()) .build(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentData.java index 3cfa8a27..5c01f3fe 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentData.java @@ -5,7 +5,7 @@ import android.os.Parcelable; import java.util.ArrayList; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; public class CommentData implements Parcelable { public static final int VOTE_TYPE_NO_VOTE = 0; @@ -78,7 +78,7 @@ public class CommentData implements Parcelable { this.voteType = voteType; this.isSubmitter = isSubmitter; this.distinguished = distinguished; - this.permalink = RedditUtils.API_BASE_URI + permalink; + this.permalink = APIUtils.API_BASE_URI + permalink; this.awards = awards; this.depth = depth; this.collapsed = collapsed; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentDataSource.java index e82115ec..598579df 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentDataSource.java @@ -14,9 +14,10 @@ import org.json.JSONObject; import java.util.ArrayList; import java.util.Locale; +import ml.docilealligator.infinityforreddit.API.RedditAPI; import ml.docilealligator.infinityforreddit.Post.PostDataSource; import ml.docilealligator.infinityforreddit.Utils.JSONUtils; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -78,10 +79,10 @@ public class CommentDataSource extends PageKeyedDataSource<String, CommentData> if (sortType.getTime() != null) { commentsCall = api.getUserSavedCommentsOauth(username, PostDataSource.USER_WHERE_SAVED, null, sortType.getType().value, sortType.getTime().value, - RedditUtils.getOAuthHeader(accessToken)); + APIUtils.getOAuthHeader(accessToken)); } else { commentsCall = api.getUserSavedCommentsOauth(username, PostDataSource.USER_WHERE_SAVED, - null, sortType.getType().value, RedditUtils.getOAuthHeader(accessToken)); + null, sortType.getType().value, APIUtils.getOAuthHeader(accessToken)); } } else { if (accessToken == null) { @@ -93,10 +94,10 @@ public class CommentDataSource extends PageKeyedDataSource<String, CommentData> } } else { if (sortType.getTime() != null) { - commentsCall = api.getUserCommentsOauth(RedditUtils.getOAuthHeader(accessToken), username, + commentsCall = api.getUserCommentsOauth(APIUtils.getOAuthHeader(accessToken), username, null, sortType.getType().value, sortType.getTime().value); } else { - commentsCall = api.getUserCommentsOauth(RedditUtils.getOAuthHeader(accessToken), username, + commentsCall = api.getUserCommentsOauth(APIUtils.getOAuthHeader(accessToken), username, null, sortType.getType().value); } } @@ -156,10 +157,10 @@ public class CommentDataSource extends PageKeyedDataSource<String, CommentData> if (areSavedComments) { if (sortType.getTime() != null) { commentsCall = api.getUserSavedCommentsOauth(username, PostDataSource.USER_WHERE_SAVED, params.key, - sortType.getType().value, sortType.getTime().value, RedditUtils.getOAuthHeader(accessToken)); + sortType.getType().value, sortType.getTime().value, APIUtils.getOAuthHeader(accessToken)); } else { commentsCall = api.getUserSavedCommentsOauth(username, PostDataSource.USER_WHERE_SAVED, params.key, - sortType.getType().value, RedditUtils.getOAuthHeader(accessToken)); + sortType.getType().value, APIUtils.getOAuthHeader(accessToken)); } } else { if (accessToken == null) { @@ -171,10 +172,10 @@ public class CommentDataSource extends PageKeyedDataSource<String, CommentData> } } else { if (sortType.getTime() != null) { - commentsCall = api.getUserCommentsOauth(RedditUtils.getOAuthHeader(accessToken), + commentsCall = api.getUserCommentsOauth(APIUtils.getOAuthHeader(accessToken), username, params.key, sortType.getType().value, sortType.getTime().value); } else { - commentsCall = api.getUserCommentsOauth(RedditUtils.getOAuthHeader(accessToken), + commentsCall = api.getUserCommentsOauth(APIUtils.getOAuthHeader(accessToken), username, params.key, sortType.getType().value); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/DeleteThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/DeleteThing.java index 7ca89ff9..9e06de80 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/DeleteThing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/DeleteThing.java @@ -5,7 +5,8 @@ import androidx.annotation.NonNull; import java.util.HashMap; import java.util.Map; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.API.RedditAPI; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -14,8 +15,8 @@ import retrofit2.Retrofit; public class DeleteThing { public static void delete(Retrofit oauthRetrofit, String fullname, String accessToken, DeleteThingListener deleteThingListener) { Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.ID_KEY, fullname); - oauthRetrofit.create(RedditAPI.class).delete(RedditUtils.getOAuthHeader(accessToken), params).enqueue(new Callback<String>() { + params.put(APIUtils.ID_KEY, fullname); + oauthRetrofit.create(RedditAPI.class).delete(APIUtils.getOAuthHeader(accessToken), params).enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { if (response.isSuccessful()) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FavoriteThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FavoriteThing.java index 4183b276..c0d429f7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FavoriteThing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FavoriteThing.java @@ -5,10 +5,11 @@ import androidx.annotation.NonNull; import java.util.HashMap; import java.util.Map; +import ml.docilealligator.infinityforreddit.API.RedditAPI; import ml.docilealligator.infinityforreddit.AsyncTask.InsertSubscribedThingsAsyncTask; import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData; import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserData; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -19,9 +20,9 @@ public class FavoriteThing { String accessToken, SubscribedSubredditData subscribedSubredditData, FavoriteThingListener favoriteThingListener) { Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.SR_NAME_KEY, subscribedSubredditData.getName()); - params.put(RedditUtils.MAKE_FAVORITE_KEY, "true"); - oauthRetrofit.create(RedditAPI.class).favoriteThing(RedditUtils.getOAuthHeader(accessToken), params).enqueue(new Callback<String>() { + params.put(APIUtils.SR_NAME_KEY, subscribedSubredditData.getName()); + params.put(APIUtils.MAKE_FAVORITE_KEY, "true"); + oauthRetrofit.create(RedditAPI.class).favoriteThing(APIUtils.getOAuthHeader(accessToken), params).enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { if (response.isSuccessful()) { @@ -43,9 +44,9 @@ public class FavoriteThing { String accessToken, SubscribedSubredditData subscribedSubredditData, FavoriteThingListener favoriteThingListener) { Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.SR_NAME_KEY, subscribedSubredditData.getName()); - params.put(RedditUtils.MAKE_FAVORITE_KEY, "false"); - oauthRetrofit.create(RedditAPI.class).favoriteThing(RedditUtils.getOAuthHeader(accessToken), params).enqueue(new Callback<String>() { + params.put(APIUtils.SR_NAME_KEY, subscribedSubredditData.getName()); + params.put(APIUtils.MAKE_FAVORITE_KEY, "false"); + oauthRetrofit.create(RedditAPI.class).favoriteThing(APIUtils.getOAuthHeader(accessToken), params).enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { if (response.isSuccessful()) { @@ -67,9 +68,9 @@ public class FavoriteThing { String accessToken, SubscribedUserData subscribedUserData, FavoriteThingListener favoriteThingListener) { Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.SR_NAME_KEY, "u_" + subscribedUserData.getName()); - params.put(RedditUtils.MAKE_FAVORITE_KEY, "true"); - oauthRetrofit.create(RedditAPI.class).favoriteThing(RedditUtils.getOAuthHeader(accessToken), params).enqueue(new Callback<String>() { + params.put(APIUtils.SR_NAME_KEY, "u_" + subscribedUserData.getName()); + params.put(APIUtils.MAKE_FAVORITE_KEY, "true"); + oauthRetrofit.create(RedditAPI.class).favoriteThing(APIUtils.getOAuthHeader(accessToken), params).enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { if (response.isSuccessful()) { @@ -91,9 +92,9 @@ public class FavoriteThing { String accessToken, SubscribedUserData subscribedUserData, FavoriteThingListener favoriteThingListener) { Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.SR_NAME_KEY, "u_" + subscribedUserData.getName()); - params.put(RedditUtils.MAKE_FAVORITE_KEY, "false"); - oauthRetrofit.create(RedditAPI.class).favoriteThing(RedditUtils.getOAuthHeader(accessToken), params).enqueue(new Callback<String>() { + params.put(APIUtils.SR_NAME_KEY, "u_" + subscribedUserData.getName()); + params.put(APIUtils.MAKE_FAVORITE_KEY, "false"); + oauthRetrofit.create(RedditAPI.class).favoriteThing(APIUtils.getOAuthHeader(accessToken), params).enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { if (response.isSuccessful()) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java index 2af9275e..b6b16c1b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java @@ -6,7 +6,8 @@ import androidx.annotation.Nullable; import java.util.ArrayList; import java.util.Locale; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.API.RedditAPI; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -26,10 +27,10 @@ public class FetchComment { } } else { if (commentId == null) { - comments = api.getPostAndCommentsByIdOauth(article, sortType, RedditUtils.getOAuthHeader(accessToken)); + comments = api.getPostAndCommentsByIdOauth(article, sortType, APIUtils.getOAuthHeader(accessToken)); } else { comments = api.getPostAndCommentsSingleThreadByIdOauth(article, commentId, sortType, - RedditUtils.getOAuthHeader(accessToken)); + APIUtils.getOAuthHeader(accessToken)); } } @@ -90,7 +91,7 @@ public class FetchComment { if (accessToken == null) { moreComments = api.getInfo(stringBuilder.toString()); } else { - moreComments = api.getInfoOauth(stringBuilder.toString(), RedditUtils.getOAuthHeader(accessToken)); + moreComments = api.getInfoOauth(stringBuilder.toString(), APIUtils.getOAuthHeader(accessToken)); } moreComments.enqueue(new Callback<String>() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchFlairs.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchFlairs.java index 28acfeb7..b00d52d3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchFlairs.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchFlairs.java @@ -9,8 +9,9 @@ import org.json.JSONException; import java.util.ArrayList; +import ml.docilealligator.infinityforreddit.API.RedditAPI; import ml.docilealligator.infinityforreddit.Utils.JSONUtils; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -20,7 +21,7 @@ public class FetchFlairs { public static void fetchFlairsInSubreddit(Retrofit oauthRetrofit, String accessToken, String subredditName, FetchFlairsInSubredditListener fetchFlairsInSubredditListener) { RedditAPI api = oauthRetrofit.create(RedditAPI.class); - Call<String> flairsCall = api.getFlairs(RedditUtils.getOAuthHeader(accessToken), subredditName); + Call<String> flairsCall = api.getFlairs(APIUtils.getOAuthHeader(accessToken), subredditName); flairsCall.enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatVideoLinks.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatVideoLinks.java index 49f823f9..730c050b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatVideoLinks.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatVideoLinks.java @@ -7,6 +7,7 @@ import androidx.annotation.NonNull; import org.json.JSONException; import org.json.JSONObject; +import ml.docilealligator.infinityforreddit.API.GfycatAPI; import ml.docilealligator.infinityforreddit.Utils.JSONUtils; import retrofit2.Call; import retrofit2.Callback; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchMessages.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchMessages.java index b2d33e69..e49e21ec 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchMessages.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchMessages.java @@ -14,8 +14,9 @@ import java.util.ArrayList; import java.util.Calendar; import java.util.Locale; +import ml.docilealligator.infinityforreddit.API.RedditAPI; import ml.docilealligator.infinityforreddit.Utils.JSONUtils; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import ml.docilealligator.infinityforreddit.Utils.Utils; import retrofit2.Call; import retrofit2.Callback; @@ -31,7 +32,7 @@ public class FetchMessages { static void fetchMessagesAsync(Retrofit oauthRetrofit, Locale locale, String accessToken, String where, String after, FetchMessagesListener fetchMessagesListener) { - oauthRetrofit.create(RedditAPI.class).getMessages(RedditUtils.getOAuthHeader(accessToken), where, after) + oauthRetrofit.create(RedditAPI.class).getMessages(APIUtils.getOAuthHeader(accessToken), where, after) .enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchMyInfo.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchMyInfo.java index a8430b01..3467599e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchMyInfo.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchMyInfo.java @@ -2,7 +2,8 @@ package ml.docilealligator.infinityforreddit; import androidx.annotation.NonNull; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.API.RedditAPI; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Retrofit; @@ -13,7 +14,7 @@ public class FetchMyInfo { final FetchUserMyListener fetchUserMyListener) { RedditAPI api = retrofit.create(RedditAPI.class); - Call<String> userInfo = api.getMyInfo(RedditUtils.getOAuthHeader(accessToken)); + Call<String> userInfo = api.getMyInfo(APIUtils.getOAuthHeader(accessToken)); userInfo.enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRules.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRules.java index 604fe82f..877c614c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRules.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRules.java @@ -10,6 +10,7 @@ import org.json.JSONObject; import java.util.ArrayList; +import ml.docilealligator.infinityforreddit.API.RedditAPI; import ml.docilealligator.infinityforreddit.Utils.JSONUtils; import ml.docilealligator.infinityforreddit.Utils.Utils; import retrofit2.Call; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java index 0172939f..b9623b91 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubredditData.java @@ -4,6 +4,7 @@ import androidx.annotation.NonNull; import java.util.ArrayList; +import ml.docilealligator.infinityforreddit.API.RedditAPI; import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; import retrofit2.Call; import retrofit2.Callback; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedThing.java index de1e1cfe..0fbfd856 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedThing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedThing.java @@ -4,10 +4,11 @@ import androidx.annotation.NonNull; import java.util.ArrayList; +import ml.docilealligator.infinityforreddit.API.RedditAPI; import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData; import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserData; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -21,7 +22,7 @@ public class FetchSubscribedThing { final FetchSubscribedThingListener fetchSubscribedThingListener) { RedditAPI api = retrofit.create(RedditAPI.class); - Call<String> subredditDataCall = api.getSubscribedThing(lastItem, RedditUtils.getOAuthHeader(accessToken)); + Call<String> subredditDataCall = api.getSubscribedThing(lastItem, APIUtils.getOAuthHeader(accessToken)); subredditDataCall.enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserData.java index f3197820..b698d8d1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchUserData.java @@ -4,6 +4,7 @@ import androidx.annotation.NonNull; import java.util.ArrayList; +import ml.docilealligator.infinityforreddit.API.RedditAPI; import ml.docilealligator.infinityforreddit.User.UserData; import retrofit2.Call; import retrofit2.Callback; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurGifFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurGifFragment.java new file mode 100644 index 00000000..41bd9c1d --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurGifFragment.java @@ -0,0 +1,112 @@ +package ml.docilealligator.infinityforreddit.Fragment; + +import android.app.Activity; +import android.content.Context; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.LinearLayout; +import android.widget.ProgressBar; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; + +import com.alexvasilkov.gestures.views.GestureImageView; +import com.bumptech.glide.Glide; +import com.bumptech.glide.RequestManager; +import com.bumptech.glide.load.DataSource; +import com.bumptech.glide.load.engine.GlideException; +import com.bumptech.glide.request.RequestListener; +import com.bumptech.glide.request.target.Target; + +import butterknife.BindView; +import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.ImgurMedia; +import ml.docilealligator.infinityforreddit.R; + +/** + * A simple {@link Fragment} subclass. + */ +public class ViewImgurGifFragment extends Fragment { + + public static final String EXTRA_IMGUR_GIF = "EIG"; + + @BindView(R.id.progress_bar_view_imgur_gif_fragment) + ProgressBar progressBar; + @BindView(R.id.image_view_view_imgur_gif_fragment) + GestureImageView imageView; + @BindView(R.id.load_image_error_linear_layout_view_imgur_gif_fragment) + LinearLayout errorLinearLayout; + + private Activity activity; + private RequestManager glide; + private ImgurMedia imgurMedia; + + public ViewImgurGifFragment() { + // Required empty public constructor + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + View rootView = inflater.inflate(R.layout.fragment_view_imgur_gif, container, false); + + ButterKnife.bind(this, rootView); + + setHasOptionsMenu(true); + + imgurMedia = getArguments().getParcelable(EXTRA_IMGUR_GIF); + glide = Glide.with(activity); + loadGif(); + + return rootView; + } + + private void loadGif() { + glide.load(imgurMedia.getLink()).listener(new RequestListener<Drawable>() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { + progressBar.setVisibility(View.GONE); + errorLinearLayout.setVisibility(View.VISIBLE); + return false; + } + + @Override + public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { + progressBar.setVisibility(View.GONE); + return false; + } + }).into(imageView); + } + + @Override + public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) { + inflater.inflate(R.menu.view_imgur_image_fragments, menu); + super.onCreateOptionsMenu(menu, inflater); + } + + @Override + public boolean onOptionsItemSelected(@NonNull MenuItem item) { + switch (item.getItemId()) { + case R.id.action_download_view_imgur_image_fragments: + return true; + case R.id.action_share_view_imgur_image_fragments: + return true; + } + + return false; + } + + @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + activity = (Activity) context; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurImageFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurImageFragment.java new file mode 100644 index 00000000..bc30a3f8 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurImageFragment.java @@ -0,0 +1,108 @@ +package ml.docilealligator.infinityforreddit.Fragment; + +import android.app.Activity; +import android.content.Context; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.LinearLayout; +import android.widget.ProgressBar; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; + +import com.alexvasilkov.gestures.views.GestureImageView; +import com.bumptech.glide.Glide; +import com.bumptech.glide.RequestManager; +import com.bumptech.glide.load.DataSource; +import com.bumptech.glide.load.engine.GlideException; +import com.bumptech.glide.request.RequestListener; +import com.bumptech.glide.request.target.Target; + +import butterknife.BindView; +import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.ImgurMedia; +import ml.docilealligator.infinityforreddit.R; + +public class ViewImgurImageFragment extends Fragment { + + public static final String EXTRA_IMGUR_IMAGES = "EII"; + + @BindView(R.id.progress_bar_view_imgur_image_fragment) + ProgressBar progressBar; + @BindView(R.id.image_view_view_imgur_image_fragment) + GestureImageView imageView; + @BindView(R.id.load_image_error_linear_layout_view_imgur_image_fragment) + LinearLayout errorLinearLayout; + + private Activity activity; + private RequestManager glide; + private ImgurMedia imgurMedia; + + public ViewImgurImageFragment() { + // Required empty public constructor + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View rootView = inflater.inflate(R.layout.fragment_view_imgur_images, container, false); + + ButterKnife.bind(this, rootView); + + setHasOptionsMenu(true); + + imgurMedia = getArguments().getParcelable(EXTRA_IMGUR_IMAGES); + glide = Glide.with(activity); + loadImage(); + + return rootView; + } + + private void loadImage() { + glide.load(imgurMedia.getLink()).listener(new RequestListener<Drawable>() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { + progressBar.setVisibility(View.GONE); + errorLinearLayout.setVisibility(View.VISIBLE); + return false; + } + + @Override + public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { + progressBar.setVisibility(View.GONE); + return false; + } + }).into(imageView); + } + + @Override + public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) { + inflater.inflate(R.menu.view_imgur_image_fragments, menu); + super.onCreateOptionsMenu(menu, inflater); + } + + @Override + public boolean onOptionsItemSelected(@NonNull MenuItem item) { + switch (item.getItemId()) { + case R.id.action_download_view_imgur_image_fragments: + return true; + case R.id.action_share_view_imgur_image_fragments: + return true; + } + + return false; + } + + @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + activity = (Activity) context; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurVideoFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurVideoFragment.java new file mode 100644 index 00000000..fdda3c51 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurVideoFragment.java @@ -0,0 +1,238 @@ +package ml.docilealligator.infinityforreddit.Fragment; + +import android.app.Activity; +import android.content.Context; +import android.content.SharedPreferences; +import android.content.res.Configuration; +import android.media.AudioManager; +import android.net.Uri; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageButton; +import android.widget.LinearLayout; + +import androidx.annotation.NonNull; +import androidx.fragment.app.Fragment; + +import com.google.android.exoplayer2.ExoPlayerFactory; +import com.google.android.exoplayer2.Player; +import com.google.android.exoplayer2.SimpleExoPlayer; +import com.google.android.exoplayer2.source.ProgressiveMediaSource; +import com.google.android.exoplayer2.source.TrackGroupArray; +import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection; +import com.google.android.exoplayer2.trackselection.DefaultTrackSelector; +import com.google.android.exoplayer2.trackselection.TrackSelection; +import com.google.android.exoplayer2.trackselection.TrackSelectionArray; +import com.google.android.exoplayer2.trackselection.TrackSelector; +import com.google.android.exoplayer2.ui.PlayerView; +import com.google.android.exoplayer2.upstream.DataSource; +import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; +import com.google.android.exoplayer2.util.Util; + +import javax.inject.Inject; +import javax.inject.Named; + +import butterknife.BindView; +import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.ImgurMedia; +import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; + +/** + * A simple {@link Fragment} subclass. + */ +public class ViewImgurVideoFragment extends Fragment { + + public static final String EXTRA_IMGUR_VIDEO = "EIV"; + private static final String IS_MUTE_STATE = "IMS"; + private static final String POSITION_STATE = "PS"; + @BindView(R.id.player_view_view_imgur_video_fragment) + PlayerView videoPlayerView; + @BindView(R.id.mute_exo_playback_control_view) + ImageButton muteButton; + private Activity activity; + private ImgurMedia imgurMedia; + private SimpleExoPlayer player; + private DataSource.Factory dataSourceFactory; + private boolean wasPlaying = false; + private boolean isMute = false; + @Inject + @Named("default") + SharedPreferences mSharedPreferences; + + public ViewImgurVideoFragment() { + // Required empty public constructor + } + + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View rootView = inflater.inflate(R.layout.fragment_view_imgur_video, container, false); + + ((Infinity) activity.getApplication()).getAppComponent().inject(this); + + ButterKnife.bind(this, rootView); + + setHasOptionsMenu(true); + + activity.setVolumeControlStream(AudioManager.STREAM_MUSIC); + + imgurMedia = getArguments().getParcelable(EXTRA_IMGUR_VIDEO); + + if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT || getResources().getBoolean(R.bool.isTablet)) { + //Set player controller bottom margin in order to display it above the navbar + int resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android"); + LinearLayout controllerLinearLayout = rootView.findViewById(R.id.linear_layout_exo_playback_control_view); + ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) controllerLinearLayout.getLayoutParams(); + params.bottomMargin = getResources().getDimensionPixelSize(resourceId); + } else { + //Set player controller right margin in order to display it above the navbar + int resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android"); + LinearLayout controllerLinearLayout = rootView.findViewById(R.id.linear_layout_exo_playback_control_view); + ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) controllerLinearLayout.getLayoutParams(); + params.rightMargin = getResources().getDimensionPixelSize(resourceId); + } + + videoPlayerView.setControllerVisibilityListener(visibility -> { + switch (visibility) { + case View.GONE: + activity.getWindow().getDecorView().setSystemUiVisibility( + View.SYSTEM_UI_FLAG_LAYOUT_STABLE + | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION + | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN + | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION + | View.SYSTEM_UI_FLAG_FULLSCREEN + | View.SYSTEM_UI_FLAG_IMMERSIVE); + break; + case View.VISIBLE: + activity.getWindow().getDecorView().setSystemUiVisibility( + View.SYSTEM_UI_FLAG_LAYOUT_STABLE + | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION + | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); + } + }); + + TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(); + TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory); + player = ExoPlayerFactory.newSimpleInstance(activity, trackSelector); + videoPlayerView.setPlayer(player); + dataSourceFactory = new DefaultDataSourceFactory(activity, + Util.getUserAgent(activity, "Infinity")); + player.prepare(new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(imgurMedia.getLink()))); + preparePlayer(savedInstanceState); + + return rootView; + } + + @Override + public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) { + inflater.inflate(R.menu.view_imgur_video_fragment, menu); + super.onCreateOptionsMenu(menu, inflater); + } + + @Override + public boolean onOptionsItemSelected(@NonNull MenuItem item) { + switch (item.getItemId()) { + case R.id.action_download_view_imgur_image_fragments: + return true; + } + return false; + } + + private void preparePlayer(Bundle savedInstanceState) { + player.setRepeatMode(Player.REPEAT_MODE_ALL); + wasPlaying = true; + + boolean muteVideo = mSharedPreferences.getBoolean(SharedPreferencesUtils.MUTE_VIDEO, false); + + if (savedInstanceState != null) { + long position = savedInstanceState.getLong(POSITION_STATE); + if (position > 0) { + player.seekTo(position); + } + isMute = savedInstanceState.getBoolean(IS_MUTE_STATE); + if (isMute) { + player.setVolume(0f); + muteButton.setImageResource(R.drawable.ic_mute_24dp); + } else { + player.setVolume(1f); + muteButton.setImageResource(R.drawable.ic_unmute_24dp); + } + } else if (muteVideo) { + isMute = true; + player.setVolume(0f); + muteButton.setImageResource(R.drawable.ic_mute_24dp); + } else { + muteButton.setImageResource(R.drawable.ic_unmute_24dp); + } + + player.addListener(new Player.EventListener() { + @Override + public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) { + if (!trackGroups.isEmpty()) { + for (int i = 0; i < trackGroups.length; i++) { + String mimeType = trackGroups.get(i).getFormat(0).sampleMimeType; + if (mimeType != null && mimeType.contains("audio")) { + muteButton.setVisibility(View.VISIBLE); + muteButton.setOnClickListener(view -> { + if (isMute) { + isMute = false; + player.setVolume(1f); + muteButton.setImageResource(R.drawable.ic_unmute_24dp); + } else { + isMute = true; + player.setVolume(0f); + muteButton.setImageResource(R.drawable.ic_mute_24dp); + } + }); + break; + } + } + } else { + muteButton.setVisibility(View.GONE); + } + } + }); + } + + @Override + public void onResume() { + super.onResume(); + if (wasPlaying) { + player.setPlayWhenReady(true); + } + } + + @Override + public void onPause() { + super.onPause(); + wasPlaying = player.getPlayWhenReady(); + player.setPlayWhenReady(false); + } + + @Override + public void onSaveInstanceState(@NonNull Bundle outState) { + super.onSaveInstanceState(outState); + outState.putBoolean(IS_MUTE_STATE, isMute); + outState.putLong(POSITION_STATE, player.getCurrentPosition()); + } + + @Override + public void onDestroy() { + super.onDestroy(); + player.release(); + } + + @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + activity = (Activity) context; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ImgurMedia.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ImgurMedia.java new file mode 100644 index 00000000..577b6fde --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ImgurMedia.java @@ -0,0 +1,79 @@ +package ml.docilealligator.infinityforreddit; + +import android.os.Parcel; +import android.os.Parcelable; + +public class ImgurMedia implements Parcelable { + public static final int TYPE_IMAGE = 0; + public static final int TYPE_GIF = 1; + public static final int TYPE_VIDEO = 2; + private String id; + private String title; + private String description; + private String link; + private int type; + + public ImgurMedia(String id, String title, String description, String type, String link) { + this.id = id; + this.title = title; + this.description = description; + if (type.contains("mp4")) { + this.type = TYPE_VIDEO; + } else if (type.contains("gif")) { + this.type = TYPE_GIF; + } else { + this.type = TYPE_IMAGE; + } + this.link = link; + } + + protected ImgurMedia(Parcel in) { + title = in.readString(); + description = in.readString(); + link = in.readString(); + } + + public static final Creator<ImgurMedia> CREATOR = new Creator<ImgurMedia>() { + @Override + public ImgurMedia createFromParcel(Parcel in) { + return new ImgurMedia(in); + } + + @Override + public ImgurMedia[] newArray(int size) { + return new ImgurMedia[size]; + } + }; + + public String getId() { + return id; + } + + public String getTitle() { + return title; + } + + public String getDescription() { + return description; + } + + public int getType() { + return type; + } + + public String getLink() { + return link; + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel parcel, int i) { + parcel.writeString(title); + parcel.writeString(description); + parcel.writeString(link); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/CreateMultiReddit.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/CreateMultiReddit.java index aa6bf468..67d11d57 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/CreateMultiReddit.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/CreateMultiReddit.java @@ -5,9 +5,9 @@ import androidx.annotation.NonNull; import java.util.HashMap; import java.util.Map; -import ml.docilealligator.infinityforreddit.RedditAPI; +import ml.docilealligator.infinityforreddit.API.RedditAPI; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -23,9 +23,9 @@ public class CreateMultiReddit { String accessToken, String multipath, String model, CreateMultiRedditListener createMultiRedditListener) { Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.MULTIPATH_KEY, multipath); - params.put(RedditUtils.MODEL_KEY, model); - oauthRetrofit.create(RedditAPI.class).createMultiReddit(RedditUtils.getOAuthHeader(accessToken), + params.put(APIUtils.MULTIPATH_KEY, multipath); + params.put(APIUtils.MODEL_KEY, model); + oauthRetrofit.create(RedditAPI.class).createMultiReddit(APIUtils.getOAuthHeader(accessToken), params).enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/DeleteMultiReddit.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/DeleteMultiReddit.java index 070f6944..39f9f253 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/DeleteMultiReddit.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/DeleteMultiReddit.java @@ -3,9 +3,9 @@ package ml.docilealligator.infinityforreddit.MultiReddit; import androidx.annotation.NonNull; import ml.docilealligator.infinityforreddit.AsyncTask.DeleteMultiredditInDatabaseAsyncTask; -import ml.docilealligator.infinityforreddit.RedditAPI; +import ml.docilealligator.infinityforreddit.API.RedditAPI; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -20,7 +20,7 @@ public class DeleteMultiReddit { public static void deleteMultiReddit(Retrofit oauthRetrofit, RedditDataRoomDatabase redditDataRoomDatabase, String accessToken, String accountName, String multipath, DeleteMultiRedditListener deleteMultiRedditListener) { - oauthRetrofit.create(RedditAPI.class).deleteMultiReddit(RedditUtils.getOAuthHeader(accessToken), + oauthRetrofit.create(RedditAPI.class).deleteMultiReddit(APIUtils.getOAuthHeader(accessToken), multipath).enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/EditMultiReddit.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/EditMultiReddit.java index 53ffd5f4..8f16398b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/EditMultiReddit.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/EditMultiReddit.java @@ -5,8 +5,8 @@ import androidx.annotation.NonNull; import java.util.HashMap; import java.util.Map; -import ml.docilealligator.infinityforreddit.RedditAPI; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.API.RedditAPI; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -21,9 +21,9 @@ public class EditMultiReddit { public static void editMultiReddit(Retrofit oauthRetrofit, String accessToken, String multipath, String model, EditMultiRedditListener editMultiRedditListener) { Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.MULTIPATH_KEY, multipath); - params.put(RedditUtils.MODEL_KEY, model); - oauthRetrofit.create(RedditAPI.class).updateMultiReddit(RedditUtils.getOAuthHeader(accessToken), + params.put(APIUtils.MULTIPATH_KEY, multipath); + params.put(APIUtils.MODEL_KEY, model); + oauthRetrofit.create(RedditAPI.class).updateMultiReddit(APIUtils.getOAuthHeader(accessToken), params).enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/FavoriteMultiReddit.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/FavoriteMultiReddit.java index 8c7a8947..54142d08 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/FavoriteMultiReddit.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/FavoriteMultiReddit.java @@ -7,9 +7,9 @@ import java.util.HashMap; import java.util.Map; import ml.docilealligator.infinityforreddit.AsyncTask.InsertMultiRedditAsyncTask; -import ml.docilealligator.infinityforreddit.RedditAPI; +import ml.docilealligator.infinityforreddit.API.RedditAPI; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -25,10 +25,10 @@ public class FavoriteMultiReddit { String accessToken, String accountName, boolean makeFavorite, MultiReddit multiReddit, FavoriteMultiRedditListener favoriteMultiRedditListener) { Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.MULTIPATH_KEY, multiReddit.getPath()); - params.put(RedditUtils.MAKE_FAVORITE_KEY, String.valueOf(makeFavorite)); - params.put(RedditUtils.API_TYPE_KEY, RedditUtils.API_TYPE_JSON); - oauthRetrofit.create(RedditAPI.class).favoriteMultiReddit(RedditUtils.getOAuthHeader(accessToken), + params.put(APIUtils.MULTIPATH_KEY, multiReddit.getPath()); + params.put(APIUtils.MAKE_FAVORITE_KEY, String.valueOf(makeFavorite)); + params.put(APIUtils.API_TYPE_KEY, APIUtils.API_TYPE_JSON); + oauthRetrofit.create(RedditAPI.class).favoriteMultiReddit(APIUtils.getOAuthHeader(accessToken), params).enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/FetchMultiRedditInfo.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/FetchMultiRedditInfo.java index 4c91585d..839f69f8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/FetchMultiRedditInfo.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/FetchMultiRedditInfo.java @@ -10,9 +10,9 @@ import org.json.JSONObject; import java.util.ArrayList; -import ml.docilealligator.infinityforreddit.RedditAPI; +import ml.docilealligator.infinityforreddit.API.RedditAPI; import ml.docilealligator.infinityforreddit.Utils.JSONUtils; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -27,7 +27,7 @@ public class FetchMultiRedditInfo { public static void fetchMultiRedditInfo(Retrofit retrofit, String accessToken, String multipath, FetchMultiRedditInfoListener fetchMultiRedditInfoListener) { - retrofit.create(RedditAPI.class).getMultiRedditInfo(RedditUtils.getOAuthHeader(accessToken), multipath).enqueue(new Callback<String>() { + retrofit.create(RedditAPI.class).getMultiRedditInfo(APIUtils.getOAuthHeader(accessToken), multipath).enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { if (response.isSuccessful()) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/FetchMyMultiReddits.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/FetchMyMultiReddits.java index 85d1aa6f..383c8f23 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/FetchMyMultiReddits.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MultiReddit/FetchMyMultiReddits.java @@ -4,8 +4,8 @@ import androidx.annotation.NonNull; import java.util.ArrayList; -import ml.docilealligator.infinityforreddit.RedditAPI; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.API.RedditAPI; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -19,7 +19,7 @@ public class FetchMyMultiReddits { public static void fetchMyMultiReddits(Retrofit oauthRetrofit, String accessToken, FetchMyMultiRedditsListener fetchMyMultiRedditsListener) { oauthRetrofit.create(RedditAPI.class) - .getMyMultiReddits(RedditUtils.getOAuthHeader(accessToken)).enqueue(new Callback<String>() { + .getMyMultiReddits(APIUtils.getOAuthHeader(accessToken)).enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { if (response.isSuccessful()) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/FetchPost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/FetchPost.java index ccbec648..9695419e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/FetchPost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/FetchPost.java @@ -4,8 +4,8 @@ import androidx.annotation.NonNull; import java.util.Locale; -import ml.docilealligator.infinityforreddit.RedditAPI; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.API.RedditAPI; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -17,7 +17,7 @@ public class FetchPost { if (accessToken == null) { postCall = retrofit.create(RedditAPI.class).getPost(id); } else { - postCall = retrofit.create(RedditAPI.class).getPostOauth(id, RedditUtils.getOAuthHeader(accessToken)); + postCall = retrofit.create(RedditAPI.class).getPostOauth(id, APIUtils.getOAuthHeader(accessToken)); } postCall.enqueue(new Callback<String>() { @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/HidePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/HidePost.java index ab031fa5..c71fd5a1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/HidePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/HidePost.java @@ -5,8 +5,8 @@ import androidx.annotation.NonNull; import java.util.HashMap; import java.util.Map; -import ml.docilealligator.infinityforreddit.RedditAPI; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.API.RedditAPI; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -16,8 +16,8 @@ public class HidePost { public static void hidePost(Retrofit oauthRetrofit, String accessToken, String fullname, HidePostListener hidePostListener) { Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.ID_KEY, fullname); - oauthRetrofit.create(RedditAPI.class).hide(RedditUtils.getOAuthHeader(accessToken), params).enqueue(new Callback<String>() { + params.put(APIUtils.ID_KEY, fullname); + oauthRetrofit.create(RedditAPI.class).hide(APIUtils.getOAuthHeader(accessToken), params).enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { if (response.isSuccessful()) { @@ -37,8 +37,8 @@ public class HidePost { public static void unhidePost(Retrofit oauthRetrofit, String accessToken, String fullname, HidePostListener hidePostListener) { Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.ID_KEY, fullname); - oauthRetrofit.create(RedditAPI.class).unhide(RedditUtils.getOAuthHeader(accessToken), params).enqueue(new Callback<String>() { + params.put(APIUtils.ID_KEY, fullname); + oauthRetrofit.create(RedditAPI.class).unhide(APIUtils.getOAuthHeader(accessToken), params).enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { if (response.isSuccessful()) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java index b5aa1f1c..9b5417b9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java @@ -5,7 +5,7 @@ import android.os.Parcelable; import androidx.annotation.Nullable; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; /** * Created by alex on 3/1/18. @@ -90,7 +90,7 @@ public class Post implements Parcelable { this.title = title; this.previewUrl = previewUrl; this.thumbnailPreviewUrl = thumbnailPreviewUrl; - this.permalink = RedditUtils.API_BASE_URI + permalink; + this.permalink = APIUtils.API_BASE_URI + permalink; this.score = score; this.postType = postType; this.voteType = voteType; @@ -128,7 +128,7 @@ public class Post implements Parcelable { this.previewUrl = previewUrl; this.thumbnailPreviewUrl = thumbnailPreviewUrl; this.url = url; - this.permalink = RedditUtils.API_BASE_URI + permalink; + this.permalink = APIUtils.API_BASE_URI + permalink; this.score = score; this.postType = postType; this.voteType = voteType; @@ -163,7 +163,7 @@ public class Post implements Parcelable { this.postTime = postTime; this.postTimeMillis = postTimeMillis; this.title = title; - this.permalink = RedditUtils.API_BASE_URI + permalink; + this.permalink = APIUtils.API_BASE_URI + permalink; this.score = score; this.postType = postType; this.voteType = voteType; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSource.java index b26eef79..56aff32d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSource.java @@ -10,9 +10,9 @@ import java.util.List; import java.util.Locale; import ml.docilealligator.infinityforreddit.NetworkState; -import ml.docilealligator.infinityforreddit.RedditAPI; +import ml.docilealligator.infinityforreddit.API.RedditAPI; import ml.docilealligator.infinityforreddit.SortType; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Retrofit; @@ -212,9 +212,9 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { Call<String> bestPost; if(sortType.getTime() != null) { bestPost = api.getBestPosts(sortType.getType().value, sortType.getTime().value, lastItem, - RedditUtils.getOAuthHeader(accessToken)); + APIUtils.getOAuthHeader(accessToken)); } else { - bestPost = api.getBestPosts(sortType.getType().value, lastItem, RedditUtils.getOAuthHeader(accessToken)); + bestPost = api.getBestPosts(sortType.getType().value, lastItem, APIUtils.getOAuthHeader(accessToken)); } bestPost.enqueue(new Callback<String>() { @Override @@ -292,9 +292,9 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { Call<String> bestPost; if(sortType.getTime() != null) { bestPost = api.getBestPosts(sortType.getType().value, sortType.getTime().value, after, - RedditUtils.getOAuthHeader(accessToken)); + APIUtils.getOAuthHeader(accessToken)); } else { - bestPost = api.getBestPosts(sortType.getType().value, after, RedditUtils.getOAuthHeader(accessToken)); + bestPost = api.getBestPosts(sortType.getType().value, after, APIUtils.getOAuthHeader(accessToken)); } bestPost.enqueue(new Callback<String>() { @@ -351,10 +351,10 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { } else { if (sortType.getTime() != null) { getPost = api.getSubredditBestPostsOauth(subredditOrUserName, sortType.getType().value, - sortType.getTime().value, lastItem, RedditUtils.getOAuthHeader(accessToken)); + sortType.getTime().value, lastItem, APIUtils.getOAuthHeader(accessToken)); } else { getPost = api.getSubredditBestPostsOauth(subredditOrUserName, sortType.getType().value, - lastItem, RedditUtils.getOAuthHeader(accessToken)); + lastItem, APIUtils.getOAuthHeader(accessToken)); } } getPost.enqueue(new Callback<String>() { @@ -442,10 +442,10 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { } else { if (sortType.getTime() != null) { getPost = api.getSubredditBestPostsOauth(subredditOrUserName, sortType.getType().value, - sortType.getTime().value, after, RedditUtils.getOAuthHeader(accessToken)); + sortType.getTime().value, after, APIUtils.getOAuthHeader(accessToken)); } else { getPost = api.getSubredditBestPostsOauth(subredditOrUserName, sortType.getType().value, - after, RedditUtils.getOAuthHeader(accessToken)); + after, APIUtils.getOAuthHeader(accessToken)); } } @@ -504,10 +504,10 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { } else { if (sortType.getTime() != null) { getPost = api.getUserPostsOauth(subredditOrUserName, userWhere, lastItem, sortType.getType().value, - sortType.getTime().value, RedditUtils.getOAuthHeader(accessToken)); + sortType.getTime().value, APIUtils.getOAuthHeader(accessToken)); } else { getPost = api.getUserPostsOauth(subredditOrUserName, userWhere, lastItem, sortType.getType().value, - RedditUtils.getOAuthHeader(accessToken)); + APIUtils.getOAuthHeader(accessToken)); } } getPost.enqueue(new Callback<String>() { @@ -575,10 +575,10 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { } else { if (sortType.getTime() != null) { getPost = api.getUserPostsOauth(subredditOrUserName, userWhere, after, sortType.getType().value, - sortType.getTime().value, RedditUtils.getOAuthHeader(accessToken)); + sortType.getTime().value, APIUtils.getOAuthHeader(accessToken)); } else { getPost = api.getUserPostsOauth(subredditOrUserName, userWhere, after, sortType.getType().value, - RedditUtils.getOAuthHeader(accessToken)); + APIUtils.getOAuthHeader(accessToken)); } } getPost.enqueue(new Callback<String>() { @@ -636,10 +636,10 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { } else { if(sortType.getTime() != null) { getPost = api.searchPostsOauth(query, lastItem, sortType.getType().value, - sortType.getTime().value, RedditUtils.getOAuthHeader(accessToken)); + sortType.getTime().value, APIUtils.getOAuthHeader(accessToken)); } else { getPost = api.searchPostsOauth(query, lastItem, sortType.getType().value, - RedditUtils.getOAuthHeader(accessToken)); + APIUtils.getOAuthHeader(accessToken)); } } } else { @@ -655,11 +655,11 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { if (sortType.getTime() != null) { getPost = api.searchPostsInSpecificSubredditOauth(subredditOrUserName, query, sortType.getType().value, sortType.getTime().value, lastItem, - RedditUtils.getOAuthHeader(accessToken)); + APIUtils.getOAuthHeader(accessToken)); } else { getPost = api.searchPostsInSpecificSubredditOauth(subredditOrUserName, query, sortType.getType().value, lastItem, - RedditUtils.getOAuthHeader(accessToken)); + APIUtils.getOAuthHeader(accessToken)); } } } @@ -729,9 +729,9 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { } else { if (sortType.getTime() != null) { getPost = api.searchPostsOauth(query, after, sortType.getType().value, - sortType.getTime().value, RedditUtils.getOAuthHeader(accessToken)); + sortType.getTime().value, APIUtils.getOAuthHeader(accessToken)); } else { - getPost = api.searchPostsOauth(query, after, sortType.getType().value, RedditUtils.getOAuthHeader(accessToken)); + getPost = api.searchPostsOauth(query, after, sortType.getType().value, APIUtils.getOAuthHeader(accessToken)); } } } else { @@ -747,10 +747,10 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { if (sortType.getTime() != null) { getPost = api.searchPostsInSpecificSubredditOauth(subredditOrUserName, query, sortType.getType().value, sortType.getTime().value, after, - RedditUtils.getOAuthHeader(accessToken)); + APIUtils.getOAuthHeader(accessToken)); } else { getPost = api.searchPostsInSpecificSubredditOauth(subredditOrUserName, query, - sortType.getType().value, after, RedditUtils.getOAuthHeader(accessToken)); + sortType.getType().value, after, APIUtils.getOAuthHeader(accessToken)); } } } @@ -808,10 +808,10 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { } else { if (sortType.getTime() != null) { getPost = api.getMultiRedditPostsOauth(multiRedditPath, lastItem, - sortType.getTime().value, RedditUtils.getOAuthHeader(accessToken)); + sortType.getTime().value, APIUtils.getOAuthHeader(accessToken)); } else { getPost = api.getMultiRedditPostsOauth(multiRedditPath, lastItem, - RedditUtils.getOAuthHeader(accessToken)); + APIUtils.getOAuthHeader(accessToken)); } } @@ -880,10 +880,10 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { } else { if (sortType.getTime() != null) { getPost = api.getMultiRedditPostsOauth(multiRedditPath, after, - sortType.getTime().value, RedditUtils.getOAuthHeader(accessToken)); + sortType.getTime().value, APIUtils.getOAuthHeader(accessToken)); } else { getPost = api.getMultiRedditPostsOauth(multiRedditPath, after, - RedditUtils.getOAuthHeader(accessToken)); + APIUtils.getOAuthHeader(accessToken)); } } getPost.enqueue(new Callback<String>() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/SubmitPost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/SubmitPost.java index e7479e36..9e27bce1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/SubmitPost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/SubmitPost.java @@ -20,9 +20,9 @@ import java.util.HashMap; import java.util.Locale; import java.util.Map; -import ml.docilealligator.infinityforreddit.RedditAPI; +import ml.docilealligator.infinityforreddit.API.RedditAPI; import ml.docilealligator.infinityforreddit.Utils.JSONUtils; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import okhttp3.MediaType; import okhttp3.MultipartBody; import okhttp3.RequestBody; @@ -50,7 +50,7 @@ public class SubmitPost { public void uploaded(String imageUrl) { submitPost(oauthRetrofit, accessToken, locale, subredditName, title, imageUrl, flair, isSpoiler, isNSFW, - RedditUtils.KIND_IMAGE, null, submitPostListener); + APIUtils.KIND_IMAGE, null, submitPostListener); } @Override @@ -70,10 +70,10 @@ public class SubmitPost { String fileType = mimeType.substring(mimeType.indexOf("/") + 1); Map<String, String> uploadImageParams = new HashMap<>(); - uploadImageParams.put(RedditUtils.FILEPATH_KEY, "post_video." + fileType); - uploadImageParams.put(RedditUtils.MIMETYPE_KEY, mimeType); + uploadImageParams.put(APIUtils.FILEPATH_KEY, "post_video." + fileType); + uploadImageParams.put(APIUtils.MIMETYPE_KEY, mimeType); - Call<String> uploadImageCall = api.uploadImage(RedditUtils.getOAuthHeader(accessToken), uploadImageParams); + Call<String> uploadImageCall = api.uploadImage(APIUtils.getOAuthHeader(accessToken), uploadImageParams); uploadImageCall.enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { @@ -106,11 +106,11 @@ public class SubmitPost { if (fileType.equals("gif")) { submitPost(oauthRetrofit, accessToken, locale, subredditName, title, url, flair, isSpoiler, isNSFW, - RedditUtils.KIND_VIDEOGIF, imageUrl, submitPostListener); + APIUtils.KIND_VIDEOGIF, imageUrl, submitPostListener); } else { submitPost(oauthRetrofit, accessToken, locale, subredditName, title, url, flair, isSpoiler, isNSFW, - RedditUtils.KIND_VIDEO, imageUrl, submitPostListener); + APIUtils.KIND_VIDEO, imageUrl, submitPostListener); } } @@ -162,36 +162,36 @@ public class SubmitPost { RedditAPI api = oauthRetrofit.create(RedditAPI.class); 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, kind); + params.put(APIUtils.API_TYPE_KEY, APIUtils.API_TYPE_JSON); + params.put(APIUtils.SR_KEY, subredditName); + params.put(APIUtils.TITLE_KEY, title); + params.put(APIUtils.KIND_KEY, kind); switch (kind) { - case RedditUtils.KIND_SELF: - params.put(RedditUtils.TEXT_KEY, content); + case APIUtils.KIND_SELF: + params.put(APIUtils.TEXT_KEY, content); break; - case RedditUtils.KIND_LINK: - case RedditUtils.KIND_IMAGE: - params.put(RedditUtils.URL_KEY, content); + case APIUtils.KIND_LINK: + case APIUtils.KIND_IMAGE: + params.put(APIUtils.URL_KEY, content); break; - case RedditUtils.KIND_VIDEOGIF: - params.put(RedditUtils.KIND_KEY, RedditUtils.KIND_IMAGE); - params.put(RedditUtils.URL_KEY, content); - params.put(RedditUtils.VIDEO_POSTER_URL_KEY, posterUrl); + case APIUtils.KIND_VIDEOGIF: + params.put(APIUtils.KIND_KEY, APIUtils.KIND_IMAGE); + params.put(APIUtils.URL_KEY, content); + params.put(APIUtils.VIDEO_POSTER_URL_KEY, posterUrl); break; - case RedditUtils.KIND_VIDEO: - params.put(RedditUtils.URL_KEY, content); - params.put(RedditUtils.VIDEO_POSTER_URL_KEY, posterUrl); + case APIUtils.KIND_VIDEO: + params.put(APIUtils.URL_KEY, content); + params.put(APIUtils.VIDEO_POSTER_URL_KEY, posterUrl); break; } if (flair != null) { - params.put(RedditUtils.FLAIR_TEXT_KEY, flair); + params.put(APIUtils.FLAIR_TEXT_KEY, flair); } - params.put(RedditUtils.SPOILER_KEY, Boolean.toString(isSpoiler)); - params.put(RedditUtils.NSFW_KEY, Boolean.toString(isNSFW)); + params.put(APIUtils.SPOILER_KEY, Boolean.toString(isSpoiler)); + params.put(APIUtils.NSFW_KEY, Boolean.toString(isNSFW)); - Call<String> submitPostCall = api.submit(RedditUtils.getOAuthHeader(accessToken), params); + Call<String> submitPostCall = api.submit(APIUtils.getOAuthHeader(accessToken), params); submitPostCall.enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { @@ -221,10 +221,10 @@ public class SubmitPost { RedditAPI api = oauthRetrofit.create(RedditAPI.class); Map<String, String> uploadImageParams = new HashMap<>(); - uploadImageParams.put(RedditUtils.FILEPATH_KEY, "post_image.jpg"); - uploadImageParams.put(RedditUtils.MIMETYPE_KEY, "image/jpeg"); + uploadImageParams.put(APIUtils.FILEPATH_KEY, "post_image.jpg"); + uploadImageParams.put(APIUtils.MIMETYPE_KEY, "image/jpeg"); - Call<String> uploadImageCall = api.uploadImage(RedditUtils.getOAuthHeader(accessToken), uploadImageParams); + Call<String> uploadImageCall = api.uploadImage(APIUtils.getOAuthHeader(accessToken), uploadImageParams); uploadImageCall.enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { @@ -308,12 +308,12 @@ public class SubmitPost { return; } - if (!kind.equals(RedditUtils.KIND_IMAGE) && !kind.equals(RedditUtils.KIND_VIDEO) && !kind.equals(RedditUtils.KIND_VIDEOGIF)) { + if (!kind.equals(APIUtils.KIND_IMAGE) && !kind.equals(APIUtils.KIND_VIDEO) && !kind.equals(APIUtils.KIND_VIDEOGIF)) { String postId = responseObject.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.ID_KEY); RedditAPI api = oauthRetrofit.create(RedditAPI.class); - Call<String> getPostCall = api.getPostOauth(postId, RedditUtils.getOAuthHeader(accessToken)); + Call<String> getPostCall = api.getPostOauth(postId, APIUtils.getOAuthHeader(accessToken)); getPostCall.enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { @@ -377,7 +377,7 @@ public class SubmitPost { 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))); + APIUtils.getRequestBody(nameValuePairs.getJSONObject(i).getString(JSONUtils.VALUE_KEY))); } successful = true; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java index 8c0b118e..214a9736 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java @@ -25,11 +25,12 @@ import java.util.Map; import javax.inject.Inject; import javax.inject.Named; +import ml.docilealligator.infinityforreddit.API.RedditAPI; import ml.docilealligator.infinityforreddit.Account.Account; import ml.docilealligator.infinityforreddit.Activity.LinkResolverActivity; import ml.docilealligator.infinityforreddit.Activity.ViewMessageActivity; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import retrofit2.Call; import retrofit2.Response; @@ -204,7 +205,7 @@ public class PullNotificationWorker extends Worker { } Call<String> call = mOauthWithoutAuthenticatorRetrofit.create(RedditAPI.class) - .getMessages(RedditUtils.getOAuthHeader(account.getAccessToken()), + .getMessages(APIUtils.getOAuthHeader(account.getAccessToken()), FetchMessages.WHERE_UNREAD, null); Response<String> response = call.execute(); @@ -230,15 +231,15 @@ public class PullNotificationWorker extends Worker { RedditAPI api = mRetrofit.create(RedditAPI.class); Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.GRANT_TYPE_KEY, RedditUtils.GRANT_TYPE_REFRESH_TOKEN); - params.put(RedditUtils.REFRESH_TOKEN_KEY, refreshToken); + params.put(APIUtils.GRANT_TYPE_KEY, APIUtils.GRANT_TYPE_REFRESH_TOKEN); + params.put(APIUtils.REFRESH_TOKEN_KEY, refreshToken); - Call<String> accessTokenCall = api.getAccessToken(RedditUtils.getHttpBasicAuthHeader(), params); + Call<String> accessTokenCall = api.getAccessToken(APIUtils.getHttpBasicAuthHeader(), params); try { Response response = accessTokenCall.execute(); if (response.isSuccessful() && response.body() != null) { JSONObject jsonObject = new JSONObject(response.body().toString()); - String newAccessToken = jsonObject.getString(RedditUtils.ACCESS_TOKEN_KEY); + String newAccessToken = jsonObject.getString(APIUtils.ACCESS_TOKEN_KEY); mRedditDataRoomDatabase.accountDao().changeAccessToken(account.getUsername(), newAccessToken); return newAccessToken; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ReadMessage.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ReadMessage.java index 03479021..1b039178 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ReadMessage.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ReadMessage.java @@ -5,7 +5,8 @@ import androidx.annotation.NonNull; import java.util.HashMap; import java.util.Map; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.API.RedditAPI; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -15,8 +16,8 @@ public class ReadMessage { public static void readMessage(Retrofit oauthRetrofit, String accessToken, String commaSeparatedFullnames, ReadMessageListener readMessageListener) { Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.ID_KEY, commaSeparatedFullnames); - oauthRetrofit.create(RedditAPI.class).readMessage(RedditUtils.getOAuthHeader(accessToken), params) + params.put(APIUtils.ID_KEY, commaSeparatedFullnames); + oauthRetrofit.create(RedditAPI.class).readMessage(APIUtils.getOAuthHeader(accessToken), params) .enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ReportThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ReportThing.java index 8a617179..305da882 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ReportThing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ReportThing.java @@ -5,7 +5,8 @@ import androidx.annotation.NonNull; import java.util.HashMap; import java.util.Map; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.API.RedditAPI; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -21,19 +22,19 @@ public class ReportThing { public static void reportThing(Retrofit oauthRetrofit, String accessToken, String thingFullname, String subredditName, String reasonType, String reason, ReportThingListener reportThingListener) { - Map<String, String> header = RedditUtils.getOAuthHeader(accessToken); + Map<String, String> header = APIUtils.getOAuthHeader(accessToken); Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.THING_ID_KEY, thingFullname); - params.put(RedditUtils.SR_NAME_KEY, subredditName); + params.put(APIUtils.THING_ID_KEY, thingFullname); + params.put(APIUtils.SR_NAME_KEY, subredditName); params.put(reasonType, reason); if (reasonType.equals(ReportReason.REASON_TYPE_SITE_REASON)) { - params.put(RedditUtils.REASON_KEY, ReportReason.REASON_SITE_REASON_SELECTED); + params.put(APIUtils.REASON_KEY, ReportReason.REASON_SITE_REASON_SELECTED); } else if (reasonType.equals(ReportReason.REASON_TYPE_RULE_REASON)) { - params.put(RedditUtils.REASON_KEY, ReportReason.REASON_RULE_REASON_SELECTED); + params.put(APIUtils.REASON_KEY, ReportReason.REASON_RULE_REASON_SELECTED); } else { - params.put(RedditUtils.REASON_KEY, ReportReason.REASON_OTHER); + params.put(APIUtils.REASON_KEY, ReportReason.REASON_OTHER); } - params.put(RedditUtils.API_TYPE_KEY, RedditUtils.API_TYPE_JSON); + params.put(APIUtils.API_TYPE_KEY, APIUtils.API_TYPE_JSON); oauthRetrofit.create(RedditAPI.class).report(header, params).enqueue(new Callback<String>() { @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SaveThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SaveThing.java index 55babf4e..4b510d8c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SaveThing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SaveThing.java @@ -5,7 +5,8 @@ import androidx.annotation.NonNull; import java.util.HashMap; import java.util.Map; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.API.RedditAPI; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -15,8 +16,8 @@ public class SaveThing { public static void saveThing(Retrofit oauthRetrofit, String accessToken, String fullname, SaveThingListener saveThingListener) { Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.ID_KEY, fullname); - oauthRetrofit.create(RedditAPI.class).save(RedditUtils.getOAuthHeader(accessToken), params).enqueue(new Callback<String>() { + params.put(APIUtils.ID_KEY, fullname); + oauthRetrofit.create(RedditAPI.class).save(APIUtils.getOAuthHeader(accessToken), params).enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { if (response.isSuccessful()) { @@ -36,8 +37,8 @@ public class SaveThing { public static void unsaveThing(Retrofit oauthRetrofit, String accessToken, String fullname, SaveThingListener saveThingListener) { Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.ID_KEY, fullname); - oauthRetrofit.create(RedditAPI.class).unsave(RedditUtils.getOAuthHeader(accessToken), params).enqueue(new Callback<String>() { + params.put(APIUtils.ID_KEY, fullname); + oauthRetrofit.create(RedditAPI.class).unsave(APIUtils.getOAuthHeader(accessToken), params).enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { if (response.isSuccessful()) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SendComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SendComment.java index d2d34471..cb27368e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SendComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SendComment.java @@ -7,7 +7,8 @@ import java.util.HashMap; import java.util.Locale; import java.util.Map; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.API.RedditAPI; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -19,12 +20,12 @@ public class SendComment { Locale locale, Retrofit oauthRetrofit, String accessToken, SendCommentListener sendCommentListener) { RedditAPI api = oauthRetrofit.create(RedditAPI.class); - Map<String, String> headers = RedditUtils.getOAuthHeader(accessToken); + Map<String, String> headers = APIUtils.getOAuthHeader(accessToken); Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.API_TYPE_KEY, "json"); - params.put(RedditUtils.RETURN_RTJSON_KEY, "true"); - params.put(RedditUtils.TEXT_KEY, commentMarkdown); - params.put(RedditUtils.THING_ID_KEY, thingFullname); + params.put(APIUtils.API_TYPE_KEY, "json"); + params.put(APIUtils.RETURN_RTJSON_KEY, "true"); + params.put(APIUtils.TEXT_KEY, commentMarkdown); + params.put(APIUtils.THING_ID_KEY, thingFullname); api.sendComment(headers, params); Call<String> sendCommentCall = api.sendComment(headers, params); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSubscription.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSubscription.java index f79ae19b..3f2225e8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSubscription.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSubscription.java @@ -7,9 +7,10 @@ import androidx.annotation.NonNull; import java.util.HashMap; import java.util.Map; +import ml.docilealligator.infinityforreddit.API.RedditAPI; import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Retrofit; @@ -38,10 +39,10 @@ public class SubredditSubscription { RedditAPI api = oauthRetrofit.create(RedditAPI.class); Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.ACTION_KEY, action); - params.put(RedditUtils.SR_NAME_KEY, subredditName); + params.put(APIUtils.ACTION_KEY, action); + params.put(APIUtils.SR_NAME_KEY, subredditName); - Call<String> subredditSubscriptionCall = api.subredditSubscription(RedditUtils.getOAuthHeader(accessToken), params); + Call<String> subredditSubscriptionCall = api.subredditSubscription(APIUtils.getOAuthHeader(accessToken), params); subredditSubscriptionCall.enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/UserFollowing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/UserFollowing.java index bb85667a..93ec3bc3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/UserFollowing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/UserFollowing.java @@ -7,10 +7,11 @@ import androidx.annotation.NonNull; import java.util.HashMap; import java.util.Map; +import ml.docilealligator.infinityforreddit.API.RedditAPI; import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserDao; import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserData; import ml.docilealligator.infinityforreddit.User.UserData; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Retrofit; @@ -38,10 +39,10 @@ public class UserFollowing { RedditAPI api = oauthRetrofit.create(RedditAPI.class); Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.ACTION_KEY, action); - params.put(RedditUtils.SR_NAME_KEY, "u_" + username); + params.put(APIUtils.ACTION_KEY, action); + params.put(APIUtils.SR_NAME_KEY, "u_" + username); - Call<String> subredditSubscriptionCall = api.subredditSubscription(RedditUtils.getOAuthHeader(accessToken), params); + Call<String> subredditSubscriptionCall = api.subredditSubscription(APIUtils.getOAuthHeader(accessToken), params); subredditSubscriptionCall.enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/RedditUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/APIUtils.java index 5d901277..a87ce406 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/RedditUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/APIUtils.java @@ -12,16 +12,18 @@ import okhttp3.RequestBody; * Created by alex on 2/23/18. */ -public class RedditUtils { +public class APIUtils { public static final String OAUTH_URL = "https://www.reddit.com/api/v1/authorize.compact"; public static final String OAUTH_API_BASE_URI = "https://oauth.reddit.com"; public static final String API_BASE_URI = "https://www.reddit.com"; public static final String API_UPLOAD_MEDIA_URI = "https://reddit-uploaded-media.s3-accelerate.amazonaws.com"; public static final String API_UPLOAD_VIDEO_URI = "https://reddit-uploaded-video.s3-accelerate.amazonaws.com"; public static final String GFYCAT_API_BASE_URI = "https://api.gfycat.com/v1/gfycats/"; + public static final String IMGUR_API_BASE_URI = "https://api.imgur.com/3/"; public static final String CLIENT_ID_KEY = "client_id"; public static final String CLIENT_ID = ""; + public static final String IMGUR_CLIENT_ID = ""; public static final String RESPONSE_TYPE_KEY = "response_type"; public static final String RESPONSE_TYPE = "code"; public static final String STATE_KEY = "state"; @@ -89,16 +91,16 @@ public class RedditUtils { public static Map<String, String> getHttpBasicAuthHeader() { Map<String, String> params = new HashMap<>(); - String credentials = String.format("%s:%s", RedditUtils.CLIENT_ID, ""); + String credentials = String.format("%s:%s", APIUtils.CLIENT_ID, ""); String auth = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP); - params.put(RedditUtils.AUTHORIZATION_KEY, auth); + params.put(APIUtils.AUTHORIZATION_KEY, auth); return params; } public static Map<String, String> getOAuthHeader(String accessToken) { Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.AUTHORIZATION_KEY, RedditUtils.AUTHORIZATION_BASE + accessToken); - params.put(RedditUtils.USER_AGENT_KEY, RedditUtils.USER_AGENT); + params.put(APIUtils.AUTHORIZATION_KEY, APIUtils.AUTHORIZATION_BASE + accessToken); + params.put(APIUtils.USER_AGENT_KEY, APIUtils.USER_AGENT); return params; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/JSONUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/JSONUtils.java index 57064f39..10ae166a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/JSONUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/JSONUtils.java @@ -21,12 +21,12 @@ public class JSONUtils { public static final String E_KEY = "e"; public static final String T_KEY = "t"; public static final String U_KEY = "u"; + public static final String LINK_KEY = "link"; public static final String LINK_AUTHOR_KEY = "link_author"; public static final String LINK_FLAIR_RICHTEXT_KEY = "link_flair_richtext"; public static final String SCORE_KEY = "score"; public static final String LIKES_KEY = "likes"; public static final String NSFW_KEY = "over_18"; - public static final String GILDED_KEY = "gilded"; public static final String PERMALINK_KEY = "permalink"; public static final String CREATED_UTC_KEY = "created_utc"; public static final String PREVIEW_KEY = "preview"; @@ -109,4 +109,5 @@ public class JSONUtils { public static final String GFY_ITEM_KEY = "gfyItem"; public static final String MP4_URL_KEY = "mp4Url"; public static final String WEBM_URL_KEY = "webmUrl"; + public static final String TYPE_KEY = "type"; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/VoteThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/VoteThing.java index f1e9a3f1..bb0264bf 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/VoteThing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/VoteThing.java @@ -8,7 +8,8 @@ import androidx.annotation.NonNull; import java.util.HashMap; import java.util.Map; -import ml.docilealligator.infinityforreddit.Utils.RedditUtils; +import ml.docilealligator.infinityforreddit.API.RedditAPI; +import ml.docilealligator.infinityforreddit.Utils.APIUtils; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Retrofit; @@ -25,11 +26,11 @@ public class VoteThing { RedditAPI api = retrofit.create(RedditAPI.class); Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.DIR_KEY, point); - params.put(RedditUtils.ID_KEY, fullName); - params.put(RedditUtils.RANK_KEY, RedditUtils.RANK); + params.put(APIUtils.DIR_KEY, point); + params.put(APIUtils.ID_KEY, fullName); + params.put(APIUtils.RANK_KEY, APIUtils.RANK); - Call<String> voteThingCall = api.voteThing(RedditUtils.getOAuthHeader(accessToken), params); + Call<String> voteThingCall = api.voteThing(APIUtils.getOAuthHeader(accessToken), params); voteThingCall.enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { @@ -55,11 +56,11 @@ public class VoteThing { RedditAPI api = retrofit.create(RedditAPI.class); Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.DIR_KEY, point); - params.put(RedditUtils.ID_KEY, fullName); - params.put(RedditUtils.RANK_KEY, RedditUtils.RANK); + params.put(APIUtils.DIR_KEY, point); + params.put(APIUtils.ID_KEY, fullName); + params.put(APIUtils.RANK_KEY, APIUtils.RANK); - Call<String> voteThingCall = api.voteThing(RedditUtils.getOAuthHeader(accessToken), params); + Call<String> voteThingCall = api.voteThing(APIUtils.getOAuthHeader(accessToken), params); voteThingCall.enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { |