mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2024-12-29 04:17:12 +01:00
Testing Paging 3 library.
This commit is contained in:
parent
f13012d21c
commit
6dc0521e8e
@ -51,8 +51,10 @@ dependencies {
|
|||||||
def lifecycleVersion = "2.2.0"
|
def lifecycleVersion = "2.2.0"
|
||||||
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycleVersion"
|
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycleVersion"
|
||||||
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycleVersion"
|
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycleVersion"
|
||||||
|
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycleVersion"
|
||||||
annotationProcessor "androidx.lifecycle:lifecycle-common-java8:$lifecycleVersion"
|
annotationProcessor "androidx.lifecycle:lifecycle-common-java8:$lifecycleVersion"
|
||||||
implementation 'androidx.paging:paging-runtime:3.0.0'
|
implementation 'androidx.paging:paging-runtime:3.0.1'
|
||||||
|
implementation "androidx.paging:paging-guava:3.0.1"
|
||||||
implementation 'androidx.preference:preference:1.1.1'
|
implementation 'androidx.preference:preference:1.1.1'
|
||||||
def roomVersion = "2.3.0"
|
def roomVersion = "2.3.0"
|
||||||
implementation "androidx.room:room-runtime:$roomVersion"
|
implementation "androidx.room:room-runtime:$roomVersion"
|
||||||
@ -85,6 +87,7 @@ dependencies {
|
|||||||
def retrofitVersion = "2.9.0"
|
def retrofitVersion = "2.9.0"
|
||||||
implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
|
implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
|
||||||
implementation "com.squareup.retrofit2:converter-scalars:$retrofitVersion"
|
implementation "com.squareup.retrofit2:converter-scalars:$retrofitVersion"
|
||||||
|
implementation "com.squareup.retrofit2:adapter-guava:$retrofitVersion"
|
||||||
implementation 'com.squareup.okhttp3:okhttp:4.9.1'
|
implementation 'com.squareup.okhttp3:okhttp:4.9.1'
|
||||||
|
|
||||||
// Dependency injection
|
// Dependency injection
|
||||||
|
@ -34,7 +34,11 @@
|
|||||||
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.WikiActivity"
|
<activity
|
||||||
|
android:name=".activities.Paging3TestActivity"
|
||||||
|
android:exported="true" />
|
||||||
|
<activity
|
||||||
|
android:name=".activities.WikiActivity"
|
||||||
android:label="@string/wiki_activity_label"
|
android:label="@string/wiki_activity_label"
|
||||||
android:parentActivityName=".activities.MainActivity"
|
android:parentActivityName=".activities.MainActivity"
|
||||||
android:theme="@style/AppTheme.Slidable" />
|
android:theme="@style/AppTheme.Slidable" />
|
||||||
|
@ -25,6 +25,7 @@ import ml.docilealligator.infinityforreddit.activities.LockScreenActivity;
|
|||||||
import ml.docilealligator.infinityforreddit.activities.LoginActivity;
|
import ml.docilealligator.infinityforreddit.activities.LoginActivity;
|
||||||
import ml.docilealligator.infinityforreddit.activities.MainActivity;
|
import ml.docilealligator.infinityforreddit.activities.MainActivity;
|
||||||
import ml.docilealligator.infinityforreddit.activities.MultiredditSelectionActivity;
|
import ml.docilealligator.infinityforreddit.activities.MultiredditSelectionActivity;
|
||||||
|
import ml.docilealligator.infinityforreddit.activities.Paging3TestActivity;
|
||||||
import ml.docilealligator.infinityforreddit.activities.PostFilterPreferenceActivity;
|
import ml.docilealligator.infinityforreddit.activities.PostFilterPreferenceActivity;
|
||||||
import ml.docilealligator.infinityforreddit.activities.PostFilterUsageListingActivity;
|
import ml.docilealligator.infinityforreddit.activities.PostFilterUsageListingActivity;
|
||||||
import ml.docilealligator.infinityforreddit.activities.PostGalleryActivity;
|
import ml.docilealligator.infinityforreddit.activities.PostGalleryActivity;
|
||||||
@ -286,4 +287,6 @@ public interface AppComponent {
|
|||||||
void inject(WikiActivity wikiActivity);
|
void inject(WikiActivity wikiActivity);
|
||||||
|
|
||||||
void inject(Infinity infinity);
|
void inject(Infinity infinity);
|
||||||
|
|
||||||
|
void inject(Paging3TestActivity paging3TestActivity);
|
||||||
}
|
}
|
||||||
|
@ -31,6 +31,7 @@ import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
|
|||||||
import okhttp3.ConnectionPool;
|
import okhttp3.ConnectionPool;
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
import retrofit2.Retrofit;
|
import retrofit2.Retrofit;
|
||||||
|
import retrofit2.adapter.guava.GuavaCallAdapterFactory;
|
||||||
import retrofit2.converter.scalars.ScalarsConverterFactory;
|
import retrofit2.converter.scalars.ScalarsConverterFactory;
|
||||||
|
|
||||||
@Module
|
@Module
|
||||||
@ -52,6 +53,18 @@ class AppModule {
|
|||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
@Named("paging_3_test")
|
||||||
|
@Singleton
|
||||||
|
Retrofit providePaging3TestRetrofit(@Named("default") OkHttpClient okHttpClient) {
|
||||||
|
return new Retrofit.Builder()
|
||||||
|
.baseUrl(APIUtils.OAUTH_API_BASE_URI)
|
||||||
|
.client(okHttpClient)
|
||||||
|
.addConverterFactory(ScalarsConverterFactory.create())
|
||||||
|
.addCallAdapterFactory(GuavaCallAdapterFactory.create())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Named("oauth_without_authenticator")
|
@Named("oauth_without_authenticator")
|
||||||
@Singleton
|
@Singleton
|
||||||
|
@ -729,8 +729,10 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
PostTypeBottomSheetFragment postTypeBottomSheetFragment = new PostTypeBottomSheetFragment();
|
Intent intent = new Intent(this, Paging3TestActivity.class);
|
||||||
postTypeBottomSheetFragment.show(getSupportFragmentManager(), postTypeBottomSheetFragment.getTag());
|
startActivity(intent);
|
||||||
|
/*PostTypeBottomSheetFragment postTypeBottomSheetFragment = new PostTypeBottomSheetFragment();
|
||||||
|
postTypeBottomSheetFragment.show(getSupportFragmentManager(), postTypeBottomSheetFragment.getTag());*/
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -0,0 +1,166 @@
|
|||||||
|
package ml.docilealligator.infinityforreddit.activities;
|
||||||
|
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.util.DisplayMetrics;
|
||||||
|
|
||||||
|
import androidx.lifecycle.ViewModelProvider;
|
||||||
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
|
import androidx.transition.AutoTransition;
|
||||||
|
import androidx.transition.TransitionManager;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import javax.inject.Named;
|
||||||
|
|
||||||
|
import im.ene.toro.exoplayer.ExoCreator;
|
||||||
|
import ml.docilealligator.infinityforreddit.Infinity;
|
||||||
|
import ml.docilealligator.infinityforreddit.R;
|
||||||
|
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
|
||||||
|
import ml.docilealligator.infinityforreddit.SortType;
|
||||||
|
import ml.docilealligator.infinityforreddit.adapters.Paging3TestAdapter;
|
||||||
|
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
|
||||||
|
import ml.docilealligator.infinityforreddit.fragments.PostFragment;
|
||||||
|
import ml.docilealligator.infinityforreddit.post.PostDataSource;
|
||||||
|
import ml.docilealligator.infinityforreddit.post.PostPaging3ViewModel;
|
||||||
|
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
|
||||||
|
import retrofit2.Retrofit;
|
||||||
|
|
||||||
|
public class Paging3TestActivity extends BaseActivity {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
@Named("no_oauth")
|
||||||
|
Retrofit mRetrofit;
|
||||||
|
@Inject
|
||||||
|
@Named("oauth")
|
||||||
|
Retrofit mOauthRetrofit;
|
||||||
|
@Inject
|
||||||
|
@Named("paging_3_test")
|
||||||
|
Retrofit mPaging3Retrofit;
|
||||||
|
@Inject
|
||||||
|
@Named("gfycat")
|
||||||
|
Retrofit mGfycatRetrofit;
|
||||||
|
@Inject
|
||||||
|
@Named("redgifs")
|
||||||
|
Retrofit mRedgifsRetrofit;
|
||||||
|
@Inject
|
||||||
|
RedditDataRoomDatabase mRedditDataRoomDatabase;
|
||||||
|
@Inject
|
||||||
|
@Named("default")
|
||||||
|
SharedPreferences mSharedPreferences;
|
||||||
|
@Inject
|
||||||
|
@Named("post_layout")
|
||||||
|
SharedPreferences mPostLayoutSharedPreferences;
|
||||||
|
@Inject
|
||||||
|
@Named("nsfw_and_spoiler")
|
||||||
|
SharedPreferences mNsfwAndSpoilerSharedPreferences;
|
||||||
|
@Inject
|
||||||
|
@Named("post_history")
|
||||||
|
SharedPreferences mPostHistorySharedPreferences;
|
||||||
|
@Inject
|
||||||
|
@Named("post_feed_scrolled_position_cache")
|
||||||
|
SharedPreferences mPostFeedScrolledPositionSharedPreferences;
|
||||||
|
@Inject
|
||||||
|
@Named("current_account")
|
||||||
|
SharedPreferences mCurrentAccountSharedPreferences;
|
||||||
|
@Inject
|
||||||
|
@Named("sort_type")
|
||||||
|
SharedPreferences mSortTypeSharedPreferences;
|
||||||
|
@Inject
|
||||||
|
CustomThemeWrapper mCustomThemeWrapper;
|
||||||
|
@Inject
|
||||||
|
ExoCreator mExoCreator;
|
||||||
|
@Inject
|
||||||
|
Executor mExecutor;
|
||||||
|
public PostPaging3ViewModel viewModel;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
((Infinity) getApplication()).getAppComponent().inject(this);
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
setContentView(R.layout.activity_paging3_test);
|
||||||
|
|
||||||
|
String accessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null);
|
||||||
|
String accountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null);
|
||||||
|
int defaultPostLayout = Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.DEFAULT_POST_LAYOUT_KEY, "0"));
|
||||||
|
boolean savePostFeedScrolledPosition = mSharedPreferences.getBoolean(SharedPreferencesUtils.SAVE_FRONT_PAGE_SCROLLED_POSITION, false);
|
||||||
|
boolean rememberMutingOptionInPostFeed = mSharedPreferences.getBoolean(SharedPreferencesUtils.REMEMBER_MUTING_OPTION_IN_POST_FEED, false);
|
||||||
|
Locale locale = getResources().getConfiguration().locale;
|
||||||
|
DisplayMetrics displayMetrics = new DisplayMetrics();
|
||||||
|
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
|
||||||
|
int windowWidth = displayMetrics.widthPixels;
|
||||||
|
int postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_FRONT_PAGE_POST, defaultPostLayout);
|
||||||
|
int postType = PostDataSource.TYPE_FRONT_PAGE;
|
||||||
|
String sort = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_BEST_POST, SortType.Type.BEST.name());
|
||||||
|
SortType sortType;
|
||||||
|
if (sort.equals(SortType.Type.CONTROVERSIAL.name()) || sort.equals(SortType.Type.TOP.name())) {
|
||||||
|
String sortTime = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TIME_BEST_POST, SortType.Time.ALL.name());
|
||||||
|
sortType = new SortType(SortType.Type.valueOf(sort), SortType.Time.valueOf(sortTime));
|
||||||
|
} else {
|
||||||
|
sortType = new SortType(SortType.Type.valueOf(sort));
|
||||||
|
}
|
||||||
|
|
||||||
|
RecyclerView recyclerView = findViewById(R.id.recycler_view);
|
||||||
|
Paging3TestAdapter adapter = new Paging3TestAdapter(this, new PostFragment(), mExecutor,
|
||||||
|
mOauthRetrofit, mGfycatRetrofit,
|
||||||
|
mRedgifsRetrofit, mCustomThemeWrapper, locale,
|
||||||
|
windowWidth, accessToken, accountName, postType, postLayout, true,
|
||||||
|
mSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostHistorySharedPreferences,
|
||||||
|
mExoCreator, new Paging3TestAdapter.Callback() {
|
||||||
|
@Override
|
||||||
|
public void retryLoadingMore() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void typeChipClicked(int filter) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void flairChipClicked(String flair) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void nsfwChipClicked() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void currentlyBindItem(int position) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void delayTransition() {
|
||||||
|
TransitionManager.beginDelayedTransition(recyclerView, new AutoTransition());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
recyclerView.setAdapter(adapter);
|
||||||
|
|
||||||
|
viewModel = new ViewModelProvider(this, new PostPaging3ViewModel.Factory(mExecutor, mPaging3Retrofit,
|
||||||
|
accessToken, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences,
|
||||||
|
null, null, null, postType, sortType, null, null,
|
||||||
|
null, null, null)).get(PostPaging3ViewModel.class);
|
||||||
|
|
||||||
|
viewModel.getPosts().observe(this, postPagingData -> adapter.submitData(getLifecycle(), postPagingData));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected SharedPreferences getDefaultSharedPreferences() {
|
||||||
|
return mSharedPreferences;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected CustomThemeWrapper getCustomThemeWrapper() {
|
||||||
|
return mCustomThemeWrapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void applyCustomTheme() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
@ -1,10 +1,13 @@
|
|||||||
package ml.docilealligator.infinityforreddit.apis;
|
package ml.docilealligator.infinityforreddit.apis;
|
||||||
|
|
||||||
|
import com.google.common.util.concurrent.ListenableFuture;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import okhttp3.MultipartBody;
|
import okhttp3.MultipartBody;
|
||||||
import okhttp3.RequestBody;
|
import okhttp3.RequestBody;
|
||||||
import retrofit2.Call;
|
import retrofit2.Call;
|
||||||
|
import retrofit2.Response;
|
||||||
import retrofit2.http.Body;
|
import retrofit2.http.Body;
|
||||||
import retrofit2.http.DELETE;
|
import retrofit2.http.DELETE;
|
||||||
import retrofit2.http.FieldMap;
|
import retrofit2.http.FieldMap;
|
||||||
@ -368,4 +371,11 @@ public interface RedditAPI {
|
|||||||
|
|
||||||
@GET("/r/{subredditName}/wiki/index.json?raw_json=1")
|
@GET("/r/{subredditName}/wiki/index.json?raw_json=1")
|
||||||
Call<String> getWiki(@Path("subredditName") String subredditName);
|
Call<String> getWiki(@Path("subredditName") String subredditName);
|
||||||
|
|
||||||
|
@GET("{sortType}?raw_json=1")
|
||||||
|
ListenableFuture<Response<String>> getBestPostsListenableFuture(@Path("sortType") String sortType, @Query("after") String lastItem, @HeaderMap Map<String, String> headers);
|
||||||
|
|
||||||
|
@GET("{sortType}?raw_json=1")
|
||||||
|
ListenableFuture<Response<String>> getBestPostsListenableFuture(@Path("sortType") String sortType, @Query("t") String sortTime,
|
||||||
|
@Query("after") String lastItem, @HeaderMap Map<String, String> headers);
|
||||||
}
|
}
|
||||||
|
@ -73,6 +73,58 @@ public class ParsePost {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static LinkedHashSet<Post> parsePostsSync(String response, int nPosts, PostFilter postFilter, List<ReadPost> readPostList) {
|
||||||
|
LinkedHashSet<Post> newPosts = new LinkedHashSet<>();
|
||||||
|
try {
|
||||||
|
JSONObject jsonResponse = new JSONObject(response);
|
||||||
|
JSONArray allData = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
|
||||||
|
|
||||||
|
//Posts listing
|
||||||
|
int size;
|
||||||
|
if (nPosts < 0 || nPosts > allData.length()) {
|
||||||
|
size = allData.length();
|
||||||
|
} else {
|
||||||
|
size = nPosts;
|
||||||
|
}
|
||||||
|
|
||||||
|
HashSet<ReadPost> readPostHashSet = null;
|
||||||
|
if (readPostList != null) {
|
||||||
|
readPostHashSet = new HashSet<>(readPostList);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
try {
|
||||||
|
if (allData.getJSONObject(i).getString(JSONUtils.KIND_KEY).equals("t3")) {
|
||||||
|
JSONObject data = allData.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY);
|
||||||
|
Post post = parseBasicData(data);
|
||||||
|
if (readPostHashSet != null && readPostHashSet.contains(ReadPost.convertPost(post))) {
|
||||||
|
post.markAsRead(false);
|
||||||
|
}
|
||||||
|
if (PostFilter.isPostAllowed(post, postFilter)) {
|
||||||
|
newPosts.add(post);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return newPosts;
|
||||||
|
} catch (JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getLastItem(String response) {
|
||||||
|
try {
|
||||||
|
return new JSONObject(response).getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY);
|
||||||
|
} catch (JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void parsePost(Executor executor, Handler handler, String response, ParsePostListener parsePostListener) {
|
public static void parsePost(Executor executor, Handler handler, String response, ParsePostListener parsePostListener) {
|
||||||
PostFilter postFilter = new PostFilter();
|
PostFilter postFilter = new PostFilter();
|
||||||
postFilter.allowNSFW = true;
|
postFilter.allowNSFW = true;
|
||||||
|
@ -0,0 +1,159 @@
|
|||||||
|
package ml.docilealligator.infinityforreddit.post;
|
||||||
|
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.lifecycle.MutableLiveData;
|
||||||
|
import androidx.paging.ListenableFuturePagingSource;
|
||||||
|
import androidx.paging.PagingState;
|
||||||
|
|
||||||
|
import com.google.common.util.concurrent.Futures;
|
||||||
|
import com.google.common.util.concurrent.ListenableFuture;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
|
import ml.docilealligator.infinityforreddit.NetworkState;
|
||||||
|
import ml.docilealligator.infinityforreddit.SortType;
|
||||||
|
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
|
||||||
|
import ml.docilealligator.infinityforreddit.postfilter.PostFilter;
|
||||||
|
import ml.docilealligator.infinityforreddit.readpost.ReadPost;
|
||||||
|
import ml.docilealligator.infinityforreddit.utils.APIUtils;
|
||||||
|
import retrofit2.HttpException;
|
||||||
|
import retrofit2.Response;
|
||||||
|
import retrofit2.Retrofit;
|
||||||
|
|
||||||
|
public class PostPaging3PagingSource extends ListenableFuturePagingSource<String, Post> {
|
||||||
|
private Executor executor;
|
||||||
|
private Retrofit retrofit;
|
||||||
|
private String accessToken;
|
||||||
|
private String accountName;
|
||||||
|
private SharedPreferences sharedPreferences;
|
||||||
|
private SharedPreferences postFeedScrolledPositionSharedPreferences;
|
||||||
|
private String subredditOrUserName;
|
||||||
|
private String query;
|
||||||
|
private String trendingSource;
|
||||||
|
private int postType;
|
||||||
|
private SortType sortType;
|
||||||
|
private PostFilter postFilter;
|
||||||
|
private List<ReadPost> readPostList;
|
||||||
|
private String userWhere;
|
||||||
|
private String multiRedditPath;
|
||||||
|
private LinkedHashSet<Post> postLinkedHashSet;
|
||||||
|
|
||||||
|
private MutableLiveData<NetworkState> paginationNetworkStateLiveData;
|
||||||
|
private MutableLiveData<NetworkState> initialLoadStateLiveData;
|
||||||
|
private MutableLiveData<Boolean> hasPostLiveData;
|
||||||
|
|
||||||
|
PostPaging3PagingSource(Executor executor, Retrofit retrofit, String accessToken, String accountName,
|
||||||
|
SharedPreferences sharedPreferences,
|
||||||
|
SharedPreferences postFeedScrolledPositionSharedPreferences, int postType,
|
||||||
|
SortType sortType, PostFilter postFilter, List<ReadPost> readPostList) {
|
||||||
|
this.executor = executor;
|
||||||
|
this.retrofit = retrofit;
|
||||||
|
this.accessToken = accessToken;
|
||||||
|
this.accountName = accountName;
|
||||||
|
this.sharedPreferences = sharedPreferences;
|
||||||
|
this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences;
|
||||||
|
paginationNetworkStateLiveData = new MutableLiveData<>();
|
||||||
|
initialLoadStateLiveData = new MutableLiveData<>();
|
||||||
|
hasPostLiveData = new MutableLiveData<>();
|
||||||
|
this.postType = postType;
|
||||||
|
this.sortType = sortType == null ? new SortType(SortType.Type.BEST) : sortType;
|
||||||
|
this.postFilter = postFilter;
|
||||||
|
this.readPostList = readPostList;
|
||||||
|
postLinkedHashSet = new LinkedHashSet<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public String getRefreshKey(@NonNull PagingState<String, Post> pagingState) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*@Nullable
|
||||||
|
@Override
|
||||||
|
public Object load(@NonNull LoadParams<String> loadParams, @NonNull Continuation<? super LoadResult<String, Post>> continuation) {
|
||||||
|
RedditAPI api = retrofit.create(RedditAPI.class);
|
||||||
|
Call<String> bestPost;
|
||||||
|
if(sortType.getTime() != null) {
|
||||||
|
bestPost = api.getBestPosts(sortType.getType().value, sortType.getTime().value, loadParams.getKey(),
|
||||||
|
APIUtils.getOAuthHeader(accessToken));
|
||||||
|
} else {
|
||||||
|
bestPost = api.getBestPosts(sortType.getType().value, loadParams.getKey(), APIUtils.getOAuthHeader(accessToken));
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Response<String> response = bestPost.execute();
|
||||||
|
if (response.isSuccessful()) {
|
||||||
|
String responseString = response.body();
|
||||||
|
LinkedHashSet<Post> newPosts = ParsePost.parsePostsSync(responseString, -1, postFilter, readPostList);
|
||||||
|
String lastItem = ParsePost.getLastItem(responseString);
|
||||||
|
if (newPosts == null) {
|
||||||
|
return new LoadResult.Error<>(new Exception("Error parsing more posts"));
|
||||||
|
} else {
|
||||||
|
int currentPostsSize = postLinkedHashSet.size();
|
||||||
|
postLinkedHashSet.addAll(newPosts);
|
||||||
|
if (currentPostsSize == postLinkedHashSet.size()) {
|
||||||
|
return new LoadResult.Page<>(new ArrayList<>(), null, lastItem);
|
||||||
|
} else {
|
||||||
|
return new LoadResult.Page<>(new ArrayList<>(postLinkedHashSet).subList(currentPostsSize, postLinkedHashSet.size()), null, lastItem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return new LoadResult.Error<>(new Exception("Response failed"));
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return new LoadResult.Error<>(e);
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
@Override
|
||||||
|
public ListenableFuture<LoadResult<String, Post>> loadFuture(@NonNull LoadParams<String> loadParams) {
|
||||||
|
RedditAPI api = retrofit.create(RedditAPI.class);
|
||||||
|
ListenableFuture<Response<String>> bestPost;
|
||||||
|
if(sortType.getTime() != null) {
|
||||||
|
bestPost = api.getBestPostsListenableFuture(sortType.getType().value, sortType.getTime().value, loadParams.getKey(),
|
||||||
|
APIUtils.getOAuthHeader(accessToken));
|
||||||
|
} else {
|
||||||
|
bestPost = api.getBestPostsListenableFuture(sortType.getType().value, loadParams.getKey(), APIUtils.getOAuthHeader(accessToken));
|
||||||
|
}
|
||||||
|
|
||||||
|
ListenableFuture<LoadResult<String, Post>> pageFuture = Futures.transform(bestPost, this::transformData, executor);
|
||||||
|
|
||||||
|
ListenableFuture<LoadResult<String, Post>> partialLoadResultFuture =
|
||||||
|
Futures.catching(pageFuture, HttpException.class,
|
||||||
|
LoadResult.Error::new, executor);
|
||||||
|
|
||||||
|
return Futures.catching(partialLoadResultFuture,
|
||||||
|
IOException.class, LoadResult.Error::new, executor);
|
||||||
|
}
|
||||||
|
|
||||||
|
public LoadResult<String, Post> transformData(Response<String> response) {
|
||||||
|
if (response.isSuccessful()) {
|
||||||
|
String responseString = response.body();
|
||||||
|
LinkedHashSet<Post> newPosts = ParsePost.parsePostsSync(responseString, -1, postFilter, readPostList);
|
||||||
|
String lastItem = ParsePost.getLastItem(responseString);
|
||||||
|
if (newPosts == null) {
|
||||||
|
return new LoadResult.Error<>(new Exception("Error parsing more posts"));
|
||||||
|
} else {
|
||||||
|
int currentPostsSize = postLinkedHashSet.size();
|
||||||
|
postLinkedHashSet.addAll(newPosts);
|
||||||
|
if (currentPostsSize == postLinkedHashSet.size()) {
|
||||||
|
return new LoadResult.Page<>(new ArrayList<>(), null, lastItem);
|
||||||
|
} else {
|
||||||
|
return new LoadResult.Page<>(new ArrayList<>(postLinkedHashSet).subList(currentPostsSize, postLinkedHashSet.size()), null, lastItem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return new LoadResult.Error<>(new Exception("Response failed"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,71 @@
|
|||||||
|
package ml.docilealligator.infinityforreddit.post;
|
||||||
|
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
|
||||||
|
import androidx.lifecycle.LiveData;
|
||||||
|
import androidx.paging.Pager;
|
||||||
|
import androidx.paging.PagingConfig;
|
||||||
|
import androidx.paging.PagingData;
|
||||||
|
import androidx.paging.PagingLiveData;
|
||||||
|
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
|
import ml.docilealligator.infinityforreddit.SortType;
|
||||||
|
import ml.docilealligator.infinityforreddit.postfilter.PostFilter;
|
||||||
|
import ml.docilealligator.infinityforreddit.readpost.ReadPost;
|
||||||
|
import retrofit2.Retrofit;
|
||||||
|
|
||||||
|
public class PostPaging3Repository {
|
||||||
|
private Executor executor;
|
||||||
|
private Retrofit retrofit;
|
||||||
|
private String accessToken;
|
||||||
|
private String accountName;
|
||||||
|
private SharedPreferences sharedPreferences;
|
||||||
|
private SharedPreferences postFeedScrolledPositionSharedPreferences;
|
||||||
|
private String subredditOrUserName;
|
||||||
|
private String query;
|
||||||
|
private String trendingSource;
|
||||||
|
private int postType;
|
||||||
|
private SortType sortType;
|
||||||
|
private PostFilter postFilter;
|
||||||
|
private List<ReadPost> readPostList;
|
||||||
|
private String userWhere;
|
||||||
|
private String multiRedditPath;
|
||||||
|
private LinkedHashSet<Post> postLinkedHashSet;
|
||||||
|
|
||||||
|
public PostPaging3Repository(Executor executor, Retrofit retrofit, String accessToken, String accountName,
|
||||||
|
SharedPreferences sharedPreferences,
|
||||||
|
SharedPreferences postFeedScrolledPositionSharedPreferences,
|
||||||
|
String subredditOrUserName, String query, String trendingSource, int postType,
|
||||||
|
SortType sortType, PostFilter postFilter, List<ReadPost> readPostList,
|
||||||
|
String userWhere, String multiRedditPath, LinkedHashSet<Post> postLinkedHashSet) {
|
||||||
|
this.executor = executor;
|
||||||
|
this.retrofit = retrofit;
|
||||||
|
this.accessToken = accessToken;
|
||||||
|
this.accountName = accountName;
|
||||||
|
this.sharedPreferences = sharedPreferences;
|
||||||
|
this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences;
|
||||||
|
this.subredditOrUserName = subredditOrUserName;
|
||||||
|
this.query = query;
|
||||||
|
this.trendingSource = trendingSource;
|
||||||
|
this.postType = postType;
|
||||||
|
this.sortType = sortType;
|
||||||
|
this.postFilter = postFilter;
|
||||||
|
this.readPostList = readPostList;
|
||||||
|
this.userWhere = userWhere;
|
||||||
|
this.multiRedditPath = multiRedditPath;
|
||||||
|
this.postLinkedHashSet = postLinkedHashSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LiveData<PagingData<Post>> getPostsLiveData() {
|
||||||
|
return PagingLiveData.getLiveData(new Pager<>(new PagingConfig(25, 50, false),
|
||||||
|
this::returnPagingSoruce));
|
||||||
|
}
|
||||||
|
|
||||||
|
private PostPaging3PagingSource returnPagingSoruce() {
|
||||||
|
return new PostPaging3PagingSource(executor, retrofit, accessToken, accountName, sharedPreferences,
|
||||||
|
postFeedScrolledPositionSharedPreferences, postType, sortType, postFilter, readPostList);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,91 @@
|
|||||||
|
package ml.docilealligator.infinityforreddit.post;
|
||||||
|
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.lifecycle.LiveData;
|
||||||
|
import androidx.lifecycle.ViewModel;
|
||||||
|
import androidx.lifecycle.ViewModelKt;
|
||||||
|
import androidx.lifecycle.ViewModelProvider;
|
||||||
|
import androidx.paging.PagingData;
|
||||||
|
import androidx.paging.PagingLiveData;
|
||||||
|
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
|
import ml.docilealligator.infinityforreddit.SortType;
|
||||||
|
import ml.docilealligator.infinityforreddit.postfilter.PostFilter;
|
||||||
|
import ml.docilealligator.infinityforreddit.readpost.ReadPost;
|
||||||
|
import retrofit2.Retrofit;
|
||||||
|
|
||||||
|
public class PostPaging3ViewModel extends ViewModel {
|
||||||
|
|
||||||
|
private PostPaging3Repository repository;
|
||||||
|
private LiveData<PagingData<Post>> posts;
|
||||||
|
|
||||||
|
public PostPaging3ViewModel(Executor executor, Retrofit retrofit, String accessToken, String accountName,
|
||||||
|
SharedPreferences sharedPreferences,
|
||||||
|
SharedPreferences postFeedScrolledPositionSharedPreferences,
|
||||||
|
String subredditOrUserName, String query, String trendingSource, int postType,
|
||||||
|
SortType sortType, PostFilter postFilter, List<ReadPost> readPostList,
|
||||||
|
String userWhere, String multiRedditPath, LinkedHashSet<Post> postLinkedHashSet) {
|
||||||
|
repository = new PostPaging3Repository(executor, retrofit, accessToken, accountName, sharedPreferences, postFeedScrolledPositionSharedPreferences,
|
||||||
|
subredditOrUserName, query, trendingSource, postType, sortType, postFilter, readPostList, userWhere, multiRedditPath, postLinkedHashSet);
|
||||||
|
}
|
||||||
|
|
||||||
|
public LiveData<PagingData<Post>> getPosts() {
|
||||||
|
posts = PagingLiveData.cachedIn(repository.getPostsLiveData(), ViewModelKt.getViewModelScope(this));
|
||||||
|
return posts;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Factory extends ViewModelProvider.NewInstanceFactory {
|
||||||
|
private Executor executor;
|
||||||
|
private Retrofit retrofit;
|
||||||
|
private String accessToken;
|
||||||
|
private String accountName;
|
||||||
|
private SharedPreferences sharedPreferences;
|
||||||
|
private SharedPreferences postFeedScrolledPositionSharedPreferences;
|
||||||
|
private String subredditOrUserName;
|
||||||
|
private String query;
|
||||||
|
private String trendingSource;
|
||||||
|
private int postType;
|
||||||
|
private SortType sortType;
|
||||||
|
private PostFilter postFilter;
|
||||||
|
private List<ReadPost> readPostList;
|
||||||
|
private String userWhere;
|
||||||
|
private String multiRedditPath;
|
||||||
|
private LinkedHashSet<Post> postLinkedHashSet;
|
||||||
|
|
||||||
|
public Factory(Executor executor, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences,
|
||||||
|
SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditOrUserName,
|
||||||
|
String query, String trendingSource, int postType, SortType sortType, PostFilter postFilter,
|
||||||
|
List<ReadPost> readPostList, String userWhere, String multiRedditPath,
|
||||||
|
LinkedHashSet<Post> postLinkedHashSet) {
|
||||||
|
this.executor = executor;
|
||||||
|
this.retrofit = retrofit;
|
||||||
|
this.accessToken = accessToken;
|
||||||
|
this.accountName = accountName;
|
||||||
|
this.sharedPreferences = sharedPreferences;
|
||||||
|
this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences;
|
||||||
|
this.subredditOrUserName = subredditOrUserName;
|
||||||
|
this.query = query;
|
||||||
|
this.trendingSource = trendingSource;
|
||||||
|
this.postType = postType;
|
||||||
|
this.sortType = sortType;
|
||||||
|
this.postFilter = postFilter;
|
||||||
|
this.readPostList = readPostList;
|
||||||
|
this.userWhere = userWhere;
|
||||||
|
this.multiRedditPath = multiRedditPath;
|
||||||
|
this.postLinkedHashSet = postLinkedHashSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
@Override
|
||||||
|
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
|
||||||
|
return (T) new PostPaging3ViewModel(executor, retrofit, accessToken, accountName, sharedPreferences,
|
||||||
|
postFeedScrolledPositionSharedPreferences, subredditOrUserName, query, trendingSource,
|
||||||
|
postType, sortType, postFilter, readPostList, userWhere, multiRedditPath, postLinkedHashSet);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
9
app/src/main/res/layout/activity_paging3_test.xml
Normal file
9
app/src/main/res/layout/activity_paging3_test.xml
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<androidx.recyclerview.widget.RecyclerView 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"
|
||||||
|
tools:context=".activities.Paging3TestActivity"
|
||||||
|
android:id="@+id/recycler_view"
|
||||||
|
app:layoutManager=".customviews.LinearLayoutManagerBugFixed" />
|
Loading…
Reference in New Issue
Block a user