From fe29b83e2e6055141c58459e165f5ee7bd512b65 Mon Sep 17 00:00:00 2001 From: stom79 Date: Fri, 3 Nov 2017 10:18:47 +0100 Subject: [PATCH] Redo with control on context --- .../DisplayNotificationsFragment.java | 15 +++++++-- .../fragments/DisplayStatusFragment.java | 33 +++++++++++++++---- .../etalab/mastodon/helper/CrossActions.java | 2 -- .../StreamingFederatedTimelineService.java | 1 - .../StreamingLocalTimelineService.java | 1 - .../mastodon/services/StreamingService.java | 1 - 6 files changed, 38 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayNotificationsFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayNotificationsFragment.java index 2baf9697c..f0b94bfa4 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayNotificationsFragment.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayNotificationsFragment.java @@ -17,6 +17,8 @@ import android.content.Context; import android.content.SharedPreferences; import android.os.AsyncTask; import android.os.Bundle; +import android.os.Handler; +import android.os.Looper; import android.support.v4.app.Fragment; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.widget.DividerItemDecoration; @@ -132,7 +134,16 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve swipeRefreshLayout.setColorSchemeResources(R.color.mastodonC4, R.color.mastodonC2, R.color.mastodonC3); - + if( context != null) + asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, null, max_id, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + else + new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { + @Override + public void run() { + if( context != null) + asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, null, max_id, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } + }, 500); return rootView; } @@ -150,8 +161,6 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve public void onAttach(Context context) { super.onAttach(context); this.context = context; - if( context != null) - asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, null, max_id, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } public void onDestroy() { diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java index 45771bf62..9183818c4 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java @@ -18,6 +18,8 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.AsyncTask; import android.os.Bundle; +import android.os.Handler; +import android.os.Looper; import android.os.Parcelable; import android.support.v4.app.Fragment; import android.support.v4.view.ViewCompat; @@ -145,7 +147,7 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn int visibleItemCount = mLayoutManager.getChildCount(); int totalItemCount = mLayoutManager.getItemCount(); int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition(); - if(firstVisibleItem + visibleItemCount == totalItemCount ) { + if(firstVisibleItem + visibleItemCount == totalItemCount && context != null) { if(!flag_loading ) { flag_loading = true; if( type == RetrieveFeedsAsyncTask.Type.USER) @@ -186,12 +188,29 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn swipeRefreshLayout.setColorSchemeResources(R.color.mastodonC4, R.color.mastodonC2, R.color.mastodonC3); - if( type == RetrieveFeedsAsyncTask.Type.USER) - asyncTask = new RetrieveFeedsAsyncTask(context, type, targetedId, max_id, showMediaOnly, showPinned, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - else if( type == RetrieveFeedsAsyncTask.Type.TAG) - asyncTask = new RetrieveFeedsAsyncTask(context, type, tag, targetedId, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - else { - asyncTask = new RetrieveFeedsAsyncTask(context, type, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + if( context != null) { + if (type == RetrieveFeedsAsyncTask.Type.USER) + asyncTask = new RetrieveFeedsAsyncTask(context, type, targetedId, max_id, showMediaOnly, showPinned, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + else if (type == RetrieveFeedsAsyncTask.Type.TAG) + asyncTask = new RetrieveFeedsAsyncTask(context, type, tag, targetedId, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + else { + asyncTask = new RetrieveFeedsAsyncTask(context, type, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } + }else { + new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { + @Override + public void run() { + if( context != null){ + if (type == RetrieveFeedsAsyncTask.Type.USER) + asyncTask = new RetrieveFeedsAsyncTask(context, type, targetedId, max_id, showMediaOnly, showPinned, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + else if (type == RetrieveFeedsAsyncTask.Type.TAG) + asyncTask = new RetrieveFeedsAsyncTask(context, type, tag, targetedId, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + else { + asyncTask = new RetrieveFeedsAsyncTask(context, type, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } + } + } + }, 500); } }else { statusListAdapter.notifyDataSetChanged(); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/helper/CrossActions.java b/app/src/main/java/fr/gouv/etalab/mastodon/helper/CrossActions.java index 5766aaec5..5d2487571 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/helper/CrossActions.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/helper/CrossActions.java @@ -26,8 +26,6 @@ import android.os.Bundle; import android.support.v7.app.AlertDialog; import android.support.v7.widget.RecyclerView; import android.text.Html; -import android.util.Log; -import android.widget.BaseAdapter; import java.util.ArrayList; import java.util.List; diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/services/StreamingFederatedTimelineService.java b/app/src/main/java/fr/gouv/etalab/mastodon/services/StreamingFederatedTimelineService.java index 467f73704..bf259dfb3 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/services/StreamingFederatedTimelineService.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/services/StreamingFederatedTimelineService.java @@ -23,7 +23,6 @@ import android.os.Bundle; import android.os.SystemClock; import android.support.annotation.Nullable; import android.support.v4.content.LocalBroadcastManager; -import android.util.Log; import org.json.JSONException; diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/services/StreamingLocalTimelineService.java b/app/src/main/java/fr/gouv/etalab/mastodon/services/StreamingLocalTimelineService.java index 537be634d..1fba974e0 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/services/StreamingLocalTimelineService.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/services/StreamingLocalTimelineService.java @@ -23,7 +23,6 @@ import android.os.Bundle; import android.os.SystemClock; import android.support.annotation.Nullable; import android.support.v4.content.LocalBroadcastManager; -import android.util.Log; import org.json.JSONException; import org.json.JSONObject; diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/services/StreamingService.java b/app/src/main/java/fr/gouv/etalab/mastodon/services/StreamingService.java index e2fede163..32373705e 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/services/StreamingService.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/services/StreamingService.java @@ -22,7 +22,6 @@ import android.os.Bundle; import android.os.SystemClock; import android.support.annotation.Nullable; import android.support.v4.content.LocalBroadcastManager; -import android.util.Log; import org.json.JSONException;