mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2024-11-07 03:07:26 +01:00
Show trending searches in TrendingActivity.
This commit is contained in:
parent
08cf438bb0
commit
e2fa6e5ecb
@ -34,7 +34,12 @@
|
|||||||
android:theme="@style/AppTheme"
|
android:theme="@style/AppTheme"
|
||||||
android:usesCleartextTraffic="true"
|
android:usesCleartextTraffic="true"
|
||||||
tools:replace="android:label">
|
tools:replace="android:label">
|
||||||
<activity android:name=".activities.PostGalleryActivity"
|
<activity android:name=".activities.TrendingActivity"
|
||||||
|
android:label="@string/trending_activity_label"
|
||||||
|
android:parentActivityName=".activities.MainActivity"
|
||||||
|
android:theme="@style/AppTheme.NoActionBar" />
|
||||||
|
<activity
|
||||||
|
android:name=".activities.PostGalleryActivity"
|
||||||
android:label="@string/post_gallery_activity_label"
|
android:label="@string/post_gallery_activity_label"
|
||||||
android:parentActivityName=".activities.MainActivity"
|
android:parentActivityName=".activities.MainActivity"
|
||||||
android:theme="@style/AppTheme.NoActionBar"
|
android:theme="@style/AppTheme.NoActionBar"
|
||||||
|
@ -48,6 +48,7 @@ import ml.docilealligator.infinityforreddit.activities.SubredditMultiselectionAc
|
|||||||
import ml.docilealligator.infinityforreddit.activities.SubredditSelectionActivity;
|
import ml.docilealligator.infinityforreddit.activities.SubredditSelectionActivity;
|
||||||
import ml.docilealligator.infinityforreddit.activities.SubscribedThingListingActivity;
|
import ml.docilealligator.infinityforreddit.activities.SubscribedThingListingActivity;
|
||||||
import ml.docilealligator.infinityforreddit.activities.SuicidePreventionActivity;
|
import ml.docilealligator.infinityforreddit.activities.SuicidePreventionActivity;
|
||||||
|
import ml.docilealligator.infinityforreddit.activities.TrendingActivity;
|
||||||
import ml.docilealligator.infinityforreddit.activities.ViewImageOrGifActivity;
|
import ml.docilealligator.infinityforreddit.activities.ViewImageOrGifActivity;
|
||||||
import ml.docilealligator.infinityforreddit.activities.ViewImgurMediaActivity;
|
import ml.docilealligator.infinityforreddit.activities.ViewImgurMediaActivity;
|
||||||
import ml.docilealligator.infinityforreddit.activities.ViewMultiRedditDetailActivity;
|
import ml.docilealligator.infinityforreddit.activities.ViewMultiRedditDetailActivity;
|
||||||
@ -278,4 +279,6 @@ public interface AppComponent {
|
|||||||
void inject(ViewRPANBroadcastFragment viewRPANBroadcastFragment);
|
void inject(ViewRPANBroadcastFragment viewRPANBroadcastFragment);
|
||||||
|
|
||||||
void inject(PostGalleryActivity postGalleryActivity);
|
void inject(PostGalleryActivity postGalleryActivity);
|
||||||
|
|
||||||
|
void inject(TrendingActivity trendingActivity);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,54 @@
|
|||||||
|
package ml.docilealligator.infinityforreddit;
|
||||||
|
|
||||||
|
import android.os.Parcel;
|
||||||
|
import android.os.Parcelable;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import ml.docilealligator.infinityforreddit.post.Post;
|
||||||
|
|
||||||
|
public class TrendingSearch implements Parcelable {
|
||||||
|
public String queryString;
|
||||||
|
public String displayString;
|
||||||
|
public String title;
|
||||||
|
public ArrayList<Post.Preview> previews;
|
||||||
|
|
||||||
|
public TrendingSearch(String queryString, String displayString, String title, ArrayList<Post.Preview> previews) {
|
||||||
|
this.queryString = queryString;
|
||||||
|
this.displayString = displayString;
|
||||||
|
this.title = title;
|
||||||
|
this.previews = previews;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected TrendingSearch(Parcel in) {
|
||||||
|
queryString = in.readString();
|
||||||
|
displayString = in.readString();
|
||||||
|
title = in.readString();
|
||||||
|
previews = in.createTypedArrayList(Post.Preview.CREATOR);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final Creator<TrendingSearch> CREATOR = new Creator<TrendingSearch>() {
|
||||||
|
@Override
|
||||||
|
public TrendingSearch createFromParcel(Parcel in) {
|
||||||
|
return new TrendingSearch(in);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TrendingSearch[] newArray(int size) {
|
||||||
|
return new TrendingSearch[size];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int describeContents() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeToParcel(Parcel parcel, int i) {
|
||||||
|
parcel.writeString(queryString);
|
||||||
|
parcel.writeString(displayString);
|
||||||
|
parcel.writeString(title);
|
||||||
|
parcel.writeTypedList(previews);
|
||||||
|
}
|
||||||
|
}
|
@ -765,7 +765,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
|
|||||||
} else if (stringId == R.string.rpan) {
|
} else if (stringId == R.string.rpan) {
|
||||||
intent = new Intent(MainActivity.this, RPANActivity.class);
|
intent = new Intent(MainActivity.this, RPANActivity.class);
|
||||||
} else if (stringId == R.string.trending) {
|
} else if (stringId == R.string.trending) {
|
||||||
|
intent = new Intent(MainActivity.this, TrendingActivity.class);
|
||||||
} else if (stringId == R.string.upvoted) {
|
} else if (stringId == R.string.upvoted) {
|
||||||
intent = new Intent(MainActivity.this, AccountPostsActivity.class);
|
intent = new Intent(MainActivity.this, AccountPostsActivity.class);
|
||||||
intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_UPVOTED);
|
intent.putExtra(AccountPostsActivity.EXTRA_USER_WHERE, PostDataSource.USER_WHERE_UPVOTED);
|
||||||
|
@ -0,0 +1,227 @@
|
|||||||
|
package ml.docilealligator.infinityforreddit.activities;
|
||||||
|
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
import android.os.Build;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.util.DisplayMetrics;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.Window;
|
||||||
|
import android.view.WindowManager;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.appcompat.widget.Toolbar;
|
||||||
|
import androidx.coordinatorlayout.widget.CoordinatorLayout;
|
||||||
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||||
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
|
|
||||||
|
import com.google.android.material.appbar.AppBarLayout;
|
||||||
|
import com.google.android.material.appbar.CollapsingToolbarLayout;
|
||||||
|
import com.r0adkll.slidr.Slidr;
|
||||||
|
|
||||||
|
import org.greenrobot.eventbus.EventBus;
|
||||||
|
import org.greenrobot.eventbus.Subscribe;
|
||||||
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import javax.inject.Named;
|
||||||
|
|
||||||
|
import butterknife.BindView;
|
||||||
|
import butterknife.ButterKnife;
|
||||||
|
import ml.docilealligator.infinityforreddit.Infinity;
|
||||||
|
import ml.docilealligator.infinityforreddit.R;
|
||||||
|
import ml.docilealligator.infinityforreddit.TrendingSearch;
|
||||||
|
import ml.docilealligator.infinityforreddit.adapters.TrendingSearchRecyclerViewAdapter;
|
||||||
|
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
|
||||||
|
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
|
||||||
|
import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent;
|
||||||
|
import ml.docilealligator.infinityforreddit.post.ParsePost;
|
||||||
|
import ml.docilealligator.infinityforreddit.post.Post;
|
||||||
|
import ml.docilealligator.infinityforreddit.utils.APIUtils;
|
||||||
|
import ml.docilealligator.infinityforreddit.utils.JSONUtils;
|
||||||
|
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
|
||||||
|
import ml.docilealligator.infinityforreddit.utils.Utils;
|
||||||
|
import retrofit2.Call;
|
||||||
|
import retrofit2.Callback;
|
||||||
|
import retrofit2.Response;
|
||||||
|
import retrofit2.Retrofit;
|
||||||
|
|
||||||
|
public class TrendingActivity extends BaseActivity {
|
||||||
|
|
||||||
|
@BindView(R.id.coordinator_layout_trending_activity)
|
||||||
|
CoordinatorLayout coordinatorLayout;
|
||||||
|
@BindView(R.id.collapsing_toolbar_layout_trending_activity)
|
||||||
|
CollapsingToolbarLayout collapsingToolbarLayout;
|
||||||
|
@BindView(R.id.appbar_layout_trending_activity)
|
||||||
|
AppBarLayout appBarLayout;
|
||||||
|
@BindView(R.id.toolbar_trending_activity)
|
||||||
|
Toolbar toolbar;
|
||||||
|
@BindView(R.id.recycler_view_trending_activity)
|
||||||
|
RecyclerView recyclerView;
|
||||||
|
@Inject
|
||||||
|
@Named("oauth")
|
||||||
|
Retrofit mOauthRetrofit;
|
||||||
|
@Inject
|
||||||
|
@Named("default")
|
||||||
|
SharedPreferences mSharedPreferences;
|
||||||
|
@Inject
|
||||||
|
@Named("post_layout")
|
||||||
|
SharedPreferences mPostLayoutSharedPreferences;
|
||||||
|
@Inject
|
||||||
|
@Named("current_account")
|
||||||
|
SharedPreferences mCurrentAccountSharedPreferences;
|
||||||
|
@Inject
|
||||||
|
CustomThemeWrapper mCustomThemeWrapper;
|
||||||
|
@Inject
|
||||||
|
Executor mExecutor;
|
||||||
|
private String mAccessToken;
|
||||||
|
private String mAccountName;
|
||||||
|
private ArrayList<TrendingSearch> trendingSearches;
|
||||||
|
private TrendingSearchRecyclerViewAdapter adapter;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
((Infinity) getApplication()).getAppComponent().inject(this);
|
||||||
|
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
setContentView(R.layout.activity_trending);
|
||||||
|
|
||||||
|
ButterKnife.bind(this);
|
||||||
|
|
||||||
|
EventBus.getDefault().register(this);
|
||||||
|
|
||||||
|
applyCustomTheme();
|
||||||
|
|
||||||
|
if (mSharedPreferences.getBoolean(SharedPreferencesUtils.SWIPE_RIGHT_TO_GO_BACK, true)) {
|
||||||
|
Slidr.attach(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||||
|
Window window = getWindow();
|
||||||
|
|
||||||
|
if (isChangeStatusBarIconColor()) {
|
||||||
|
addOnOffsetChangedListener(appBarLayout);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isImmersiveInterface()) {
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||||
|
coordinatorLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
|
||||||
|
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
|
||||||
|
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
|
||||||
|
} else {
|
||||||
|
window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
|
||||||
|
}
|
||||||
|
adjustToolbar(toolbar);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setSupportActionBar(toolbar);
|
||||||
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||||
|
setToolbarGoToTop(toolbar);
|
||||||
|
|
||||||
|
mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null);
|
||||||
|
mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null);
|
||||||
|
|
||||||
|
DisplayMetrics displayMetrics = new DisplayMetrics();
|
||||||
|
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
|
||||||
|
int windowWidth = displayMetrics.widthPixels;
|
||||||
|
|
||||||
|
String dataSavingModeString = mSharedPreferences.getString(SharedPreferencesUtils.DATA_SAVING_MODE, SharedPreferencesUtils.DATA_SAVING_MODE_OFF);
|
||||||
|
boolean dataSavingMode = false;
|
||||||
|
if (dataSavingModeString.equals(SharedPreferencesUtils.DATA_SAVING_MODE_ALWAYS)) {
|
||||||
|
dataSavingMode = true;
|
||||||
|
} else if (dataSavingModeString.equals(SharedPreferencesUtils.DATA_SAVING_MODE_ONLY_ON_CELLULAR_DATA)) {
|
||||||
|
int networkType = Utils.getConnectedNetwork(this);
|
||||||
|
dataSavingMode = networkType == Utils.NETWORK_TYPE_CELLULAR;
|
||||||
|
}
|
||||||
|
boolean disableImagePreview = mSharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_IMAGE_PREVIEW, false);
|
||||||
|
adapter = new TrendingSearchRecyclerViewAdapter(this, mCustomThemeWrapper, windowWidth,
|
||||||
|
dataSavingMode, disableImagePreview, new TrendingSearchRecyclerViewAdapter.ItemClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(TrendingSearch trendingSearch) {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
recyclerView.setLayoutManager(new LinearLayoutManager(this));
|
||||||
|
recyclerView.setAdapter(adapter);
|
||||||
|
|
||||||
|
loadTrendingSearches();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadTrendingSearches() {
|
||||||
|
Handler handler = new Handler();
|
||||||
|
mOauthRetrofit.create(RedditAPI.class).getTrendingSearches(APIUtils.getOAuthHeader(mAccessToken)).enqueue(new Callback<String>() {
|
||||||
|
@Override
|
||||||
|
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
|
||||||
|
if (response.isSuccessful()) {
|
||||||
|
mExecutor.execute(() -> {
|
||||||
|
try {
|
||||||
|
JSONArray trendingSearchesArray = new JSONObject(response.body()).getJSONArray(JSONUtils.TRENDING_SEARCHES_KEY);
|
||||||
|
ArrayList<TrendingSearch> trendingSearchList = new ArrayList<>();
|
||||||
|
for (int i = 0; i < trendingSearchesArray.length(); i++) {
|
||||||
|
try {
|
||||||
|
JSONObject trendingSearchObject = trendingSearchesArray.getJSONObject(i);
|
||||||
|
String queryString = trendingSearchObject.getString(JSONUtils.QUERY_STRING_KEY);
|
||||||
|
String displayString = trendingSearchObject.getString(JSONUtils.DISPLAY_STRING_KEY);
|
||||||
|
JSONArray childrenWithOnlyOneChild = trendingSearchObject
|
||||||
|
.getJSONObject(JSONUtils.RESULTS_KEY)
|
||||||
|
.getJSONObject(JSONUtils.DATA_KEY)
|
||||||
|
.getJSONArray(JSONUtils.CHILDREN_KEY);
|
||||||
|
if (childrenWithOnlyOneChild.length() > 0) {
|
||||||
|
Post post = ParsePost.parseBasicData(childrenWithOnlyOneChild.getJSONObject(0)
|
||||||
|
.getJSONObject(JSONUtils.DATA_KEY));
|
||||||
|
|
||||||
|
trendingSearchList.add(new TrendingSearch(queryString, displayString,
|
||||||
|
post.getTitle(), post.getPreviews()));
|
||||||
|
}
|
||||||
|
} catch (JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
handler.post(() -> {
|
||||||
|
trendingSearches = trendingSearchList;
|
||||||
|
adapter.setTrendingSearches(trendingSearches);
|
||||||
|
});
|
||||||
|
} catch (JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected SharedPreferences getDefaultSharedPreferences() {
|
||||||
|
return mSharedPreferences;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected CustomThemeWrapper getCustomThemeWrapper() {
|
||||||
|
return mCustomThemeWrapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void applyCustomTheme() {
|
||||||
|
coordinatorLayout.setBackgroundColor(mCustomThemeWrapper.getBackgroundColor());
|
||||||
|
applyAppBarLayoutAndToolbarTheme(appBarLayout, toolbar);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Subscribe
|
||||||
|
public void onAccountSwitchEvent(SwitchAccountEvent event) {
|
||||||
|
finish();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,221 @@
|
|||||||
|
package ml.docilealligator.infinityforreddit.adapters;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.res.ColorStateList;
|
||||||
|
import android.graphics.drawable.Drawable;
|
||||||
|
import android.view.LayoutInflater;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
import android.widget.ImageView;
|
||||||
|
import android.widget.ProgressBar;
|
||||||
|
import android.widget.RelativeLayout;
|
||||||
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
|
|
||||||
|
import com.bumptech.glide.Glide;
|
||||||
|
import com.bumptech.glide.RequestBuilder;
|
||||||
|
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 java.util.ArrayList;
|
||||||
|
|
||||||
|
import butterknife.BindView;
|
||||||
|
import butterknife.ButterKnife;
|
||||||
|
import ml.docilealligator.infinityforreddit.R;
|
||||||
|
import ml.docilealligator.infinityforreddit.TrendingSearch;
|
||||||
|
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
|
||||||
|
import ml.docilealligator.infinityforreddit.customviews.AspectRatioGifImageView;
|
||||||
|
import ml.docilealligator.infinityforreddit.post.Post;
|
||||||
|
|
||||||
|
public class TrendingSearchRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
||||||
|
private ArrayList<TrendingSearch> trendingSearches;
|
||||||
|
private CustomThemeWrapper customThemeWrapper;
|
||||||
|
private RequestManager glide;
|
||||||
|
private int imageViewWidth;
|
||||||
|
private boolean dataSavingMode;
|
||||||
|
private boolean disableImagePreview;
|
||||||
|
private float mScale;
|
||||||
|
private ItemClickListener itemClickListener;
|
||||||
|
|
||||||
|
public TrendingSearchRecyclerViewAdapter(Context context, CustomThemeWrapper customThemeWrapper,
|
||||||
|
int imageViewWidth, boolean dataSavingMode, boolean disableImagePreview,
|
||||||
|
ItemClickListener itemClickListener) {
|
||||||
|
this.customThemeWrapper = customThemeWrapper;
|
||||||
|
this.glide = Glide.with(context);
|
||||||
|
this.imageViewWidth = imageViewWidth;
|
||||||
|
this.dataSavingMode = dataSavingMode;
|
||||||
|
this.disableImagePreview = disableImagePreview;
|
||||||
|
mScale = context.getResources().getDisplayMetrics().density;
|
||||||
|
this.itemClickListener = itemClickListener;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
@Override
|
||||||
|
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||||
|
return new TrendingSearchViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_trending_search, parent, false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
|
||||||
|
if (holder instanceof TrendingSearchViewHolder) {
|
||||||
|
TrendingSearch trendingSearch = trendingSearches.get(position);
|
||||||
|
if (dataSavingMode && disableImagePreview) {
|
||||||
|
((TrendingSearchViewHolder) holder).noPreviewLinkImageView.setVisibility(View.VISIBLE);
|
||||||
|
} else {
|
||||||
|
Post.Preview preview = getSuitablePreview(trendingSearch.previews);
|
||||||
|
if (preview != null) {
|
||||||
|
((TrendingSearchViewHolder) holder).imageWrapperRelativeLayout.setVisibility(View.VISIBLE);
|
||||||
|
if (preview.getPreviewWidth() <= 0 || preview.getPreviewHeight() <= 0) {
|
||||||
|
int height = (int) (400 * mScale);
|
||||||
|
((TrendingSearchViewHolder) holder).imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
|
||||||
|
((TrendingSearchViewHolder) holder).imageView.getLayoutParams().height = height;
|
||||||
|
preview.setPreviewWidth(imageViewWidth);
|
||||||
|
preview.setPreviewHeight(height);
|
||||||
|
} else {
|
||||||
|
((TrendingSearchViewHolder) holder).imageView
|
||||||
|
.setRatio((float) preview.getPreviewHeight() / preview.getPreviewWidth());
|
||||||
|
}
|
||||||
|
loadImage((TrendingSearchViewHolder) holder, preview);
|
||||||
|
} else {
|
||||||
|
((TrendingSearchViewHolder) holder).noPreviewLinkImageView.setVisibility(View.VISIBLE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getItemCount() {
|
||||||
|
return trendingSearches == null ? 0 : trendingSearches.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
|
||||||
|
super.onViewRecycled(holder);
|
||||||
|
if (holder instanceof TrendingSearchViewHolder) {
|
||||||
|
glide.clear(((TrendingSearchViewHolder) holder).imageView);
|
||||||
|
((TrendingSearchViewHolder) holder).imageWrapperRelativeLayout.setVisibility(View.GONE);
|
||||||
|
((TrendingSearchViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE);
|
||||||
|
((TrendingSearchViewHolder) holder).noPreviewLinkImageView.setVisibility(View.GONE);
|
||||||
|
((TrendingSearchViewHolder) holder).progressBar.setVisibility(View.GONE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private Post.Preview getSuitablePreview(ArrayList<Post.Preview> previews) {
|
||||||
|
Post.Preview preview;
|
||||||
|
if (!previews.isEmpty()) {
|
||||||
|
int previewIndex;
|
||||||
|
if (dataSavingMode && previews.size() > 2) {
|
||||||
|
previewIndex = previews.size() / 2;
|
||||||
|
} else {
|
||||||
|
previewIndex = 0;
|
||||||
|
}
|
||||||
|
preview = previews.get(previewIndex);
|
||||||
|
if (preview.getPreviewWidth() * preview.getPreviewHeight() > 10_000_000) {
|
||||||
|
for (int i = previews.size() - 1; i >= 1; i--) {
|
||||||
|
preview = previews.get(i);
|
||||||
|
if (imageViewWidth >= preview.getPreviewWidth()) {
|
||||||
|
if (preview.getPreviewWidth() * preview.getPreviewHeight() <= 10_000_000) {
|
||||||
|
return preview;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
int height = imageViewWidth / preview.getPreviewWidth() * preview.getPreviewHeight();
|
||||||
|
if (imageViewWidth * height <= 10_000_000) {
|
||||||
|
return preview;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (preview.getPreviewWidth() * preview.getPreviewHeight() > 10_000_000) {
|
||||||
|
int divisor = 2;
|
||||||
|
do {
|
||||||
|
preview.setPreviewWidth(preview.getPreviewWidth() / divisor);
|
||||||
|
preview.setPreviewHeight(preview.getPreviewHeight() / divisor);
|
||||||
|
divisor *= 2;
|
||||||
|
} while (preview.getPreviewWidth() * preview.getPreviewHeight() > 10_000_000);
|
||||||
|
}
|
||||||
|
return preview;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadImage(final TrendingSearchViewHolder holder, @NonNull Post.Preview preview) {
|
||||||
|
String url = preview.getPreviewUrl();
|
||||||
|
RequestBuilder<Drawable> imageRequestBuilder = glide.load(url).listener(new RequestListener<Drawable>() {
|
||||||
|
@Override
|
||||||
|
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
|
||||||
|
holder.progressBar.setVisibility(View.GONE);
|
||||||
|
holder.errorRelativeLayout.setVisibility(View.VISIBLE);
|
||||||
|
holder.errorRelativeLayout.setOnClickListener(view -> {
|
||||||
|
holder.progressBar.setVisibility(View.VISIBLE);
|
||||||
|
holder.errorRelativeLayout.setVisibility(View.GONE);
|
||||||
|
loadImage(holder, preview);
|
||||||
|
});
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
|
||||||
|
holder.errorRelativeLayout.setVisibility(View.GONE);
|
||||||
|
holder.progressBar.setVisibility(View.GONE);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (imageViewWidth > preview.getPreviewWidth()) {
|
||||||
|
imageRequestBuilder.override(preview.getPreviewWidth(), preview.getPreviewHeight()).into(holder.imageView);
|
||||||
|
} else {
|
||||||
|
imageRequestBuilder.into(holder.imageView);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTrendingSearches(ArrayList<TrendingSearch> trendingSearches) {
|
||||||
|
this.trendingSearches = trendingSearches;
|
||||||
|
notifyDataSetChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
class TrendingSearchViewHolder extends RecyclerView.ViewHolder {
|
||||||
|
|
||||||
|
@BindView(R.id.image_wrapper_relative_layout_item_trending_search)
|
||||||
|
RelativeLayout imageWrapperRelativeLayout;
|
||||||
|
@BindView(R.id.progress_bar_item_trending_search)
|
||||||
|
ProgressBar progressBar;
|
||||||
|
@BindView(R.id.image_view_item_trending_search)
|
||||||
|
AspectRatioGifImageView imageView;
|
||||||
|
@BindView(R.id.load_image_error_relative_layout_item_trending_search)
|
||||||
|
RelativeLayout errorRelativeLayout;
|
||||||
|
@BindView(R.id.load_image_error_text_view_item_trending_search)
|
||||||
|
TextView errorTextView;
|
||||||
|
@BindView(R.id.image_view_no_preview_gallery_item_trending_search)
|
||||||
|
ImageView noPreviewLinkImageView;
|
||||||
|
@BindView(R.id.title_text_view_item_trending_search)
|
||||||
|
TextView titleTextView;
|
||||||
|
|
||||||
|
public TrendingSearchViewHolder(@NonNull View itemView) {
|
||||||
|
super(itemView);
|
||||||
|
ButterKnife.bind(this, itemView);
|
||||||
|
|
||||||
|
noPreviewLinkImageView.setBackgroundColor(customThemeWrapper.getNoPreviewPostTypeBackgroundColor());
|
||||||
|
noPreviewLinkImageView.setColorFilter(customThemeWrapper.getNoPreviewPostTypeIconTint(), android.graphics.PorterDuff.Mode.SRC_IN);
|
||||||
|
progressBar.setIndeterminateTintList(ColorStateList.valueOf(customThemeWrapper.getColorAccent()));
|
||||||
|
errorTextView.setTextColor(customThemeWrapper.getPrimaryTextColor());
|
||||||
|
titleTextView.setTextColor(customThemeWrapper.getPostTitleColor());
|
||||||
|
|
||||||
|
itemView.setOnClickListener(view -> {
|
||||||
|
itemClickListener.onClick(trendingSearches.get(getBindingAdapterPosition()));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface ItemClickListener {
|
||||||
|
void onClick(TrendingSearch trendingSearch);
|
||||||
|
}
|
||||||
|
}
|
File diff suppressed because one or more lines are too long
@ -119,7 +119,7 @@ public class ParsePost {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Post parseBasicData(JSONObject data) throws JSONException {
|
public static Post parseBasicData(JSONObject data) throws JSONException {
|
||||||
String id = data.getString(JSONUtils.ID_KEY);
|
String id = data.getString(JSONUtils.ID_KEY);
|
||||||
String fullName = data.getString(JSONUtils.NAME_KEY);
|
String fullName = data.getString(JSONUtils.NAME_KEY);
|
||||||
String subredditName = data.getString(JSONUtils.SUBREDDIT_KEY);
|
String subredditName = data.getString(JSONUtils.SUBREDDIT_KEY);
|
||||||
|
@ -166,4 +166,8 @@ public class JSONUtils {
|
|||||||
public static final String AUTHOR_ICON_IMAGE = "author_icon_img";
|
public static final String AUTHOR_ICON_IMAGE = "author_icon_img";
|
||||||
public static final String ASSET_KEY = "asset";
|
public static final String ASSET_KEY = "asset";
|
||||||
public static final String ASSET_ID_KEY = "asset_id";
|
public static final String ASSET_ID_KEY = "asset_id";
|
||||||
|
public static final String TRENDING_SEARCHES_KEY = "trending_searches";
|
||||||
|
public static final String QUERY_STRING_KEY = "query_string";
|
||||||
|
public static final String DISPLAY_STRING_KEY = "display_string";
|
||||||
|
public static final String RESULTS_KEY = "results";
|
||||||
}
|
}
|
||||||
|
42
app/src/main/res/layout/activity_trending.xml
Normal file
42
app/src/main/res/layout/activity_trending.xml
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:id="@+id/coordinator_layout_trending_activity"
|
||||||
|
tools:context=".activities.TrendingActivity">
|
||||||
|
|
||||||
|
<com.google.android.material.appbar.AppBarLayout
|
||||||
|
android:id="@+id/appbar_layout_trending_activity"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:theme="@style/AppTheme.AppBarOverlay">
|
||||||
|
|
||||||
|
<com.google.android.material.appbar.CollapsingToolbarLayout
|
||||||
|
android:id="@+id/collapsing_toolbar_layout_trending_activity"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
app:layout_scrollFlags="scroll|enterAlways"
|
||||||
|
app:titleEnabled="false"
|
||||||
|
app:toolbarId="@+id/toolbar_trending_activity">
|
||||||
|
|
||||||
|
<androidx.appcompat.widget.Toolbar
|
||||||
|
android:id="@+id/toolbar_trending_activity"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:minHeight="?attr/actionBarSize"
|
||||||
|
app:popupTheme="@style/AppTheme.PopupOverlay"
|
||||||
|
app:navigationIcon="?attr/homeAsUpIndicator" />
|
||||||
|
|
||||||
|
</com.google.android.material.appbar.CollapsingToolbarLayout>
|
||||||
|
|
||||||
|
</com.google.android.material.appbar.AppBarLayout>
|
||||||
|
|
||||||
|
<androidx.recyclerview.widget.RecyclerView
|
||||||
|
android:id="@+id/recycler_view_trending_activity"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
|
||||||
|
|
||||||
|
</androidx.coordinatorlayout.widget.CoordinatorLayout>
|
66
app/src/main/res/layout/item_trending_search.xml
Normal file
66
app/src/main/res/layout/item_trending_search.xml
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="8dp"
|
||||||
|
android:layout_marginBottom="8dp">
|
||||||
|
|
||||||
|
<RelativeLayout
|
||||||
|
android:id="@+id/image_wrapper_relative_layout_item_trending_search"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:visibility="gone">
|
||||||
|
|
||||||
|
<ml.docilealligator.infinityforreddit.customviews.AspectRatioGifImageView
|
||||||
|
android:id="@+id/image_view_item_trending_search"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:adjustViewBounds="true"
|
||||||
|
android:scaleType="fitStart" />
|
||||||
|
|
||||||
|
<ProgressBar
|
||||||
|
android:id="@+id/progress_bar_item_trending_search"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_centerInParent="true" />
|
||||||
|
|
||||||
|
<RelativeLayout
|
||||||
|
android:id="@+id/load_image_error_relative_layout_item_trending_search"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:layout_centerInParent="true"
|
||||||
|
android:visibility="gone">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/load_image_error_text_view_item_trending_search"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:drawableTop="@drawable/ic_error_outline_black_24dp"
|
||||||
|
android:layout_gravity="center"
|
||||||
|
android:gravity="center"
|
||||||
|
android:text="@string/error_loading_image_tap_to_retry"
|
||||||
|
android:textSize="?attr/font_default"
|
||||||
|
android:fontFamily="?attr/font_family" />
|
||||||
|
|
||||||
|
</RelativeLayout>
|
||||||
|
|
||||||
|
</RelativeLayout>
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/image_view_no_preview_gallery_item_trending_search"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="150dp"
|
||||||
|
android:scaleType="center"
|
||||||
|
android:src="@drawable/ic_image_24dp"
|
||||||
|
android:visibility="gone" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/title_text_view_item_trending_search"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="bottom"
|
||||||
|
android:maxLines="2"
|
||||||
|
android:textSize="?attr/title_font_18"
|
||||||
|
android:fontFamily="?attr/title_font_family" />
|
||||||
|
|
||||||
|
</com.google.android.material.card.MaterialCardView>
|
@ -41,6 +41,7 @@
|
|||||||
<string name="multireddit_selection_activity_label">Select a Multireddit</string>
|
<string name="multireddit_selection_activity_label">Select a Multireddit</string>
|
||||||
<string name="rpan_activity_label">RPAN</string>
|
<string name="rpan_activity_label">RPAN</string>
|
||||||
<string name="post_gallery_activity_label">Gallery Post</string>
|
<string name="post_gallery_activity_label">Gallery Post</string>
|
||||||
|
<string name="trending_activity_label">Trending</string>
|
||||||
|
|
||||||
<string name="navigation_drawer_open">Open navigation drawer</string>
|
<string name="navigation_drawer_open">Open navigation drawer</string>
|
||||||
<string name="navigation_drawer_close">Close navigation drawer</string>
|
<string name="navigation_drawer_close">Close navigation drawer</string>
|
||||||
|
Loading…
Reference in New Issue
Block a user