package com.h.pixeldroid.fragments.feeds.postFeeds import android.util.Log import android.view.View import android.widget.Toast import androidx.lifecycle.LiveData import androidx.paging.LivePagedListBuilder import androidx.paging.PagedList import com.h.pixeldroid.R import com.h.pixeldroid.objects.Status import com.h.pixeldroid.utils.DBUtils import retrofit2.Call import retrofit2.Callback import retrofit2.Response class HomeTimelineFragment: PostsFeedFragment() { override fun makeContent(): LiveData> { val config: PagedList.Config = PagedList.Config.Builder().setPageSize(10).build() val dataSource = PostFeedDataSource() factory = FeedDataSourceFactory(dataSource) return LivePagedListBuilder(factory, config).build() } inner class PostFeedDataSource: FeedDataSource() { override fun newSource(): PostFeedDataSource { return PostFeedDataSource() } override fun makeInitialCall(requestedLoadSize: Int): Call> { return pixelfedAPI .timelineHome("Bearer $accessToken", limit="$requestedLoadSize") } override fun makeAfterCall(requestedLoadSize: Int, key: String): Call> { return pixelfedAPI .timelineHome("Bearer $accessToken", max_id=key, limit="$requestedLoadSize") } //We use the id as the key override fun getKey(item: Status): String { return item.id!! } override fun enqueueCall(call: Call>, callback: LoadCallback){ call.enqueue(object : Callback> { override fun onResponse(call: Call>, response: Response>) { if (response.isSuccessful && response.body() != null) { val notifications = response.body()!! callback.onResult(notifications) DBUtils.storePosts(db, notifications, user!!) } else { showError() } swipeRefreshLayout.isRefreshing = false loadingIndicator.visibility = View.GONE } override fun onFailure(call: Call>, t: Throwable) { showError(errorText = R.string.feed_failed) Log.e("PostsFeedFragment", t.toString()) } }) } } }