Display lists content

This commit is contained in:
stom79 2017-12-14 18:28:06 +01:00
parent 88572430ae
commit 07847f0b28
5 changed files with 123 additions and 12 deletions

View File

@ -18,6 +18,8 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;
import android.view.View;
@ -25,10 +27,15 @@ import android.widget.RelativeLayout;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.asynctasks.ManageListsAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveFeedsAsyncTask;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.drawers.ListAdapter;
import fr.gouv.etalab.mastodon.client.Entities.Status;
import fr.gouv.etalab.mastodon.drawers.StatusListAdapter;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnListActionInterface;
@ -42,8 +49,17 @@ public class ListActivity extends BaseActivity implements OnListActionInterface
private RecyclerView lv_status;
private RelativeLayout loader;
private String title, listId;
private RelativeLayout mainLoader, nextElementLoader, textviewNoAction;
private SwipeRefreshLayout swipeRefreshLayout;
private boolean swiped;
private List<Status> statuses;
private String max_id;
private boolean firstLoad;
private boolean flag_loading;
private StatusListAdapter statusListAdapter;
LinearLayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -57,8 +73,31 @@ public class ListActivity extends BaseActivity implements OnListActionInterface
}
setContentView(R.layout.activity_list);
loader = findViewById(R.id.loader);
statuses = new ArrayList<>();
lv_status = findViewById(R.id.lv_status);
mainLoader = findViewById(R.id.loader);
nextElementLoader = findViewById(R.id.loading_next_status);
textviewNoAction = findViewById(R.id.no_action);
mainLoader.setVisibility(View.VISIBLE);
swipeRefreshLayout = findViewById(R.id.swipeContainer);
max_id = null;
flag_loading = true;
firstLoad = true;
swiped = false;
mainLoader.setVisibility(View.VISIBLE);
nextElementLoader.setVisibility(View.GONE);
boolean isOnWifi = Helper.isOnWIFI(ListActivity.this);
int behaviorWithAttachments = sharedpreferences.getInt(Helper.SET_ATTACHMENT_ACTION, Helper.ATTACHMENT_ALWAYS);
int positionSpinnerTrans = sharedpreferences.getInt(Helper.SET_TRANSLATOR, Helper.TRANS_YANDEX);
statusListAdapter = new StatusListAdapter(ListActivity.this, RetrieveFeedsAsyncTask.Type.LIST, null, isOnWifi, behaviorWithAttachments, positionSpinnerTrans, this.statuses);
lv_status.setAdapter(statusListAdapter);
mLayoutManager = new LinearLayoutManager(ListActivity.this);
lv_status.setLayoutManager(mLayoutManager);
Bundle b = getIntent().getExtras();
if(b != null){
@ -71,9 +110,45 @@ public class ListActivity extends BaseActivity implements OnListActionInterface
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setTitle(title);
loader.setVisibility(View.VISIBLE);
lv_status.setVisibility(View.GONE);
new ManageListsAsyncTask(getApplicationContext(),listId, null ,null, ListActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
lv_status.addOnScrollListener(new RecyclerView.OnScrollListener() {
public void onScrolled(RecyclerView recyclerView, int dx, int dy)
{
int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
if(dy > 0){
int visibleItemCount = mLayoutManager.getChildCount();
int totalItemCount = mLayoutManager.getItemCount();
if(firstVisibleItem + visibleItemCount == totalItemCount ) {
if(!flag_loading ) {
flag_loading = true;
new ManageListsAsyncTask(ListActivity.this,listId, max_id ,null, ListActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
nextElementLoader.setVisibility(View.VISIBLE);
}
} else {
nextElementLoader.setVisibility(View.GONE);
}
}
}
});
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
max_id = null;
firstLoad = true;
flag_loading = true;
swiped = true;
MainActivity.countNewStatus = 0;
new ManageListsAsyncTask(ListActivity.this,listId, null ,null, ListActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
});
swipeRefreshLayout.setColorSchemeResources(R.color.mastodonC4,
R.color.mastodonC2,
R.color.mastodonC3);
new ManageListsAsyncTask(ListActivity.this,listId, null ,null, ListActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
@ -91,6 +166,42 @@ public class ListActivity extends BaseActivity implements OnListActionInterface
@Override
public void onActionDone(ManageListsAsyncTask.action actionType, APIResponse apiResponse, int statusCode) {
mainLoader.setVisibility(View.GONE);
nextElementLoader.setVisibility(View.GONE);
//Discards 404 - error which can often happen due to toots which have been deleted
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
if( apiResponse.getError() != null ){
boolean show_error_messages = sharedpreferences.getBoolean(Helper.SET_SHOW_ERROR_MESSAGES, true);
if( show_error_messages && !apiResponse.getError().getError().startsWith("404 -"))
Toast.makeText(ListActivity.this, apiResponse.getError().getError(),Toast.LENGTH_LONG).show();
swipeRefreshLayout.setRefreshing(false);
swiped = false;
flag_loading = false;
return;
}
int previousPosition = this.statuses.size();
List<Status> statuses = apiResponse.getStatuses();
max_id = apiResponse.getMax_id();
flag_loading = (max_id == null );
if( !swiped && firstLoad && (statuses == null || statuses.size() == 0))
textviewNoAction.setVisibility(View.VISIBLE);
else
textviewNoAction.setVisibility(View.GONE);
if( swiped ){
if (previousPosition > 0) {
for (int i = 0; i < previousPosition; i++) {
this.statuses.remove(0);
}
statusListAdapter.notifyItemRangeRemoved(0, previousPosition);
}
swiped = false;
}
if( statuses != null && statuses.size() > 0) {
this.statuses.addAll(statuses);
statusListAdapter.notifyItemRangeInserted(previousPosition, statuses.size());
}
swipeRefreshLayout.setRefreshing(false);
firstLoad = false;
}
}

View File

@ -16,13 +16,11 @@ package fr.gouv.etalab.mastodon.asynctasks;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import java.lang.ref.WeakReference;
import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnListActionInterface;
@ -73,15 +71,15 @@ public class ManageListsAsyncTask extends AsyncTask<Void, Void, Void> {
this.max_id = max_id;
this.since_id = since_id;
this.limit = 40;
this.apiAction = action.GET_LIST_TIMELINE;
}
@Override
protected Void doInBackground(Void... params) {
Log.v(Helper.TAG, "apiAction: " + apiAction);
if (apiAction == action.GET_LIST) {
apiResponse = new API(contextReference.get()).getLists();
}else if(apiAction == action.GET_LIST_TIMELINE){
apiResponse = new API(contextReference.get()).getListTimeline(this.targetedId, this.max_id, this.since_id, this.limit);
apiResponse = new API(contextReference.get()).getListTimeline(this.listId, this.max_id, this.since_id, this.limit);
}else if(apiAction == action.GET_LIST_ACCOUNT){
apiResponse = new API(contextReference.get()).getLists(this.targetedId);
}else if( apiAction == action.CREATE_LIST){
@ -100,7 +98,6 @@ public class ManageListsAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPostExecute(Void result) {
Log.v(Helper.TAG,"onPostExecute: " + apiResponse);
listener.onActionDone(this.apiAction, apiResponse, statusCode);
}

View File

@ -47,6 +47,7 @@ public class RetrieveFeedsAsyncTask extends AsyncTask<Void, Void, Void> {
LOCAL,
PUBLIC,
HASHTAG,
LIST,
USER,
FAVOURITES,
ONESTATUS,

View File

@ -1213,8 +1213,10 @@ public class API {
statuses = parseStatuses(new JSONArray(response));
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);
e.printStackTrace();
}catch (Exception e) {
setDefaultError();
e.printStackTrace();
}
apiResponse.setStatuses(statuses);
return apiResponse;

View File

@ -46,7 +46,7 @@
android:layout_gravity="center"
android:textStyle="italic|bold"
android:typeface="serif"
android:text="@string/no_status"
android:text="@string/action_lists_empty_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>