Makes bookmarks work async

This commit is contained in:
stom79 2018-02-15 16:59:52 +01:00
parent 68d049a4fa
commit 42c055f66e
2 changed files with 50 additions and 30 deletions

View File

@ -15,13 +15,18 @@
package fr.gouv.etalab.mastodon.asynctasks; package fr.gouv.etalab.mastodon.asynctasks;
import android.content.Context; import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask; import android.os.AsyncTask;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.List;
import fr.gouv.etalab.mastodon.client.API; import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.APIResponse; import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.client.Entities.Status;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveFeedsInterface; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveFeedsInterface;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import fr.gouv.etalab.mastodon.sqlite.StatusCacheDAO;
/** /**
@ -112,6 +117,12 @@ public class RetrieveFeedsAsyncTask extends AsyncTask<Void, Void, Void> {
case TAG: case TAG:
apiResponse = api.getPublicTimelineTag(tag, false, max_id); apiResponse = api.getPublicTimelineTag(tag, false, max_id);
break; break;
case CACHE_BOOKMARKS:
apiResponse = new APIResponse();
SQLiteDatabase db = Sqlite.getInstance(contextReference.get(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
List<fr.gouv.etalab.mastodon.client.Entities.Status> statuses = new StatusCacheDAO(contextReference.get(), db).getAllStatus(StatusCacheDAO.BOOKMARK_CACHE);
apiResponse.setStatuses(statuses);
break;
case HASHTAG: case HASHTAG:
break; break;
} }

View File

@ -18,6 +18,7 @@ import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
@ -36,9 +37,11 @@ import java.util.List;
import fr.gouv.etalab.mastodon.R; import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.activities.MainActivity; import fr.gouv.etalab.mastodon.activities.MainActivity;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveFeedsAsyncTask; import fr.gouv.etalab.mastodon.asynctasks.RetrieveFeedsAsyncTask;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.client.Entities.Status; import fr.gouv.etalab.mastodon.client.Entities.Status;
import fr.gouv.etalab.mastodon.drawers.StatusListAdapter; import fr.gouv.etalab.mastodon.drawers.StatusListAdapter;
import fr.gouv.etalab.mastodon.helper.Helper; import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveFeedsInterface;
import fr.gouv.etalab.mastodon.sqlite.Sqlite; import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import fr.gouv.etalab.mastodon.sqlite.StatusCacheDAO; import fr.gouv.etalab.mastodon.sqlite.StatusCacheDAO;
@ -47,14 +50,15 @@ import fr.gouv.etalab.mastodon.sqlite.StatusCacheDAO;
* Created by Thomas on 15/02/2018. * Created by Thomas on 15/02/2018.
* Fragment to display bookmarks * Fragment to display bookmarks
*/ */
public class DisplayBookmarksFragment extends Fragment { public class DisplayBookmarksFragment extends Fragment implements OnRetrieveFeedsInterface {
private Context context; private Context context;
private List<Status> statuses; private List<Status> statuses;
private StatusListAdapter statusListAdapter; private StatusListAdapter statusListAdapter;
private RelativeLayout textviewNoAction; private RelativeLayout textviewNoAction;
private LinearLayoutManager mLayoutManager; private RelativeLayout mainLoader;
private RecyclerView lv_status;
@Override @Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
@ -62,31 +66,53 @@ public class DisplayBookmarksFragment extends Fragment {
View rootView = inflater.inflate(R.layout.fragment_bookmarks, container, false); View rootView = inflater.inflate(R.layout.fragment_bookmarks, container, false);
context = getContext(); context = getContext();
final RecyclerView lv_status = rootView.findViewById(R.id.lv_status); lv_status = rootView.findViewById(R.id.lv_status);
RelativeLayout mainLoader = rootView.findViewById(R.id.loader); mainLoader = rootView.findViewById(R.id.loader);
textviewNoAction = rootView.findViewById(R.id.no_action); textviewNoAction = rootView.findViewById(R.id.no_action);
mainLoader.setVisibility(View.VISIBLE); mainLoader.setVisibility(View.VISIBLE);
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); new RetrieveFeedsAsyncTask(context, RetrieveFeedsAsyncTask.Type.CACHE_BOOKMARKS, null, DisplayBookmarksFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
final SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); return rootView;
}
statuses = new StatusCacheDAO(context, db).getAllStatus(StatusCacheDAO.BOOKMARK_CACHE);
@Override
public void onCreate(Bundle saveInstance)
{
super.onCreate(saveInstance);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.context = context;
}
@Override
public void onRetrieveFeeds(APIResponse apiResponse) {
final SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
mainLoader.setVisibility(View.GONE);
FloatingActionButton delete_all = null;
try {
delete_all = ((MainActivity) context).findViewById(R.id.delete_all);
}catch (Exception ignored){}
final boolean isOnWifi = Helper.isOnWIFI(context); final boolean isOnWifi = Helper.isOnWIFI(context);
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
final int behaviorWithAttachments = sharedpreferences.getInt(Helper.SET_ATTACHMENT_ACTION, Helper.ATTACHMENT_ALWAYS); final int behaviorWithAttachments = sharedpreferences.getInt(Helper.SET_ATTACHMENT_ACTION, Helper.ATTACHMENT_ALWAYS);
final int positionSpinnerTrans = sharedpreferences.getInt(Helper.SET_TRANSLATOR, Helper.TRANS_YANDEX); final int positionSpinnerTrans = sharedpreferences.getInt(Helper.SET_TRANSLATOR, Helper.TRANS_YANDEX);
mLayoutManager = new LinearLayoutManager(context); statuses = apiResponse.getStatuses();
if( statuses != null && statuses.size() > 0) { if( statuses != null && statuses.size() > 0) {
LinearLayoutManager mLayoutManager = new LinearLayoutManager(context);
statusListAdapter = new StatusListAdapter(context, RetrieveFeedsAsyncTask.Type.CACHE_BOOKMARKS, null, isOnWifi, behaviorWithAttachments, positionSpinnerTrans, this.statuses); statusListAdapter = new StatusListAdapter(context, RetrieveFeedsAsyncTask.Type.CACHE_BOOKMARKS, null, isOnWifi, behaviorWithAttachments, positionSpinnerTrans, this.statuses);
lv_status.setAdapter(statusListAdapter); lv_status.setAdapter(statusListAdapter);
lv_status.setLayoutManager(mLayoutManager); lv_status.setLayoutManager(mLayoutManager);
}else { }else {
textviewNoAction.setVisibility(View.VISIBLE); textviewNoAction.setVisibility(View.VISIBLE);
} }
mainLoader.setVisibility(View.GONE);
FloatingActionButton delete_all = null;
try {
delete_all = ((MainActivity) context).findViewById(R.id.delete_all);
}catch (Exception ignored){}
if( delete_all != null) if( delete_all != null)
delete_all.setOnClickListener(new View.OnClickListener() { delete_all.setOnClickListener(new View.OnClickListener() {
@Override @Override
@ -116,22 +142,5 @@ public class DisplayBookmarksFragment extends Fragment {
.show(); .show();
} }
}); });
return rootView;
} }
@Override
public void onCreate(Bundle saveInstance)
{
super.onCreate(saveInstance);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.context = context;
}
} }