From 843bf2641b9c87a02747ada3b0d5a7e0e55d9361 Mon Sep 17 00:00:00 2001 From: stom79 Date: Wed, 15 Nov 2017 09:44:12 +0100 Subject: [PATCH] Issue #41 - Fixes potential duplicate toots --- .../activities/ShowAccountActivity.java | 15 +- .../fragments/DisplayAccountsFragment.java | 117 +++++------ .../DisplayNotificationsFragment.java | 33 +-- .../fragments/DisplayStatusFragment.java | 191 ++++++++---------- app/src/main/res/values/colors.xml | 10 +- 5 files changed, 155 insertions(+), 211 deletions(-) diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/ShowAccountActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/ShowAccountActivity.java index 6441f4dc8..9a958a964 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/ShowAccountActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/ShowAccountActivity.java @@ -30,6 +30,7 @@ import android.graphics.drawable.Drawable; import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; +import android.support.annotation.NonNull; import android.support.annotation.RequiresApi; import android.support.design.widget.AppBarLayout; import android.support.design.widget.FloatingActionButton; @@ -236,7 +237,9 @@ public class ShowAccountActivity extends AppCompatActivity implements OnPostActi @Override public void onTabReselected(TabLayout.Tab tab) { - Fragment fragment = (Fragment) mPager.getAdapter().instantiateItem(mPager, tab.getPosition()); + Fragment fragment = null; + if( mPager.getAdapter() != null) + fragment = (Fragment) mPager.getAdapter().instantiateItem(mPager, tab.getPosition()); switch (tab.getPosition()){ case 0: if( displayStatusFragment != null ) @@ -244,9 +247,10 @@ public class ShowAccountActivity extends AppCompatActivity implements OnPostActi break; case 1: case 2: - DisplayAccountsFragment displayAccountsFragment = ((DisplayAccountsFragment) fragment); - if (displayAccountsFragment != null) + if( fragment != null) { + DisplayAccountsFragment displayAccountsFragment = ((DisplayAccountsFragment) fragment); displayAccountsFragment.scrollToTop(); + } break; } } @@ -458,6 +462,7 @@ public class ShowAccountActivity extends AppCompatActivity implements OnPostActi final float scale = getResources().getDisplayMetrics().density; if(account.isLocked()){ Drawable img = ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_lock_outline); + assert img != null; img.setBounds(0,0,(int) (20 * scale + 0.5f),(int) (20 * scale + 0.5f)); account_dn.setCompoundDrawables( img, null, null, null); }else{ @@ -738,7 +743,6 @@ public class ShowAccountActivity extends AppCompatActivity implements OnPostActi displayStatusFragment = new DisplayStatusFragment(); bundle.putSerializable("type", RetrieveFeedsAsyncTask.Type.USER); bundle.putString("targetedId", accountId); - bundle.putBoolean("hideHeader",true); bundle.putBoolean("showMediaOnly",showMediaOnly); bundle.putBoolean("showPinned",showPinned); displayStatusFragment.setArguments(bundle); @@ -747,20 +751,19 @@ public class ShowAccountActivity extends AppCompatActivity implements OnPostActi DisplayAccountsFragment displayAccountsFragment = new DisplayAccountsFragment(); bundle.putSerializable("type", RetrieveAccountsAsyncTask.Type.FOLLOWING); bundle.putString("targetedId", accountId); - bundle.putBoolean("hideHeader",true); displayAccountsFragment.setArguments(bundle); return displayAccountsFragment; case 2: displayAccountsFragment = new DisplayAccountsFragment(); bundle.putSerializable("type", RetrieveAccountsAsyncTask.Type.FOLLOWERS); bundle.putString("targetedId", accountId); - bundle.putBoolean("hideHeader",true); displayAccountsFragment.setArguments(bundle); return displayAccountsFragment; } return null; } + @NonNull @Override public Object instantiateItem(ViewGroup container, int position) { Fragment createdFragment = (Fragment) super.instantiateItem(container, position); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayAccountsFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayAccountsFragment.java index 8ea0952e9..2a5bf3948 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayAccountsFragment.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayAccountsFragment.java @@ -16,11 +16,9 @@ package fr.gouv.etalab.mastodon.fragments; import android.content.Context; import android.content.SharedPreferences; import android.os.AsyncTask; -import android.os.Build; import android.os.Bundle; -import android.os.Parcelable; +import android.support.annotation.NonNull; import android.support.v4.app.Fragment; -import android.support.v4.view.ViewCompat; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.LinearLayoutManager; @@ -65,30 +63,19 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou private String targetedId; private boolean swiped; private RecyclerView lv_accounts; - boolean hideHeader; + @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_accounts, container, false); context = getContext(); - boolean comesFromSearch = false; - hideHeader = false; Bundle bundle = this.getArguments(); accounts = new ArrayList<>(); if (bundle != null) { type = (RetrieveAccountsAsyncTask.Type) bundle.get("type"); targetedId = bundle.getString("targetedId", null); - hideHeader = bundle.getBoolean("hideHeader", false); - if( bundle.containsKey("accounts")){ - ArrayList accountsReceived = bundle.getParcelableArrayList("accounts"); - assert accountsReceived != null; - for(Parcelable account: accountsReceived){ - accounts.add((Account)account); - } - comesFromSearch = true; - } } max_id = null; firstLoad = true; @@ -106,64 +93,54 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou accountsListAdapter = new AccountsListAdapter(context, type, targetedId, this.accounts); lv_accounts.setAdapter(accountsListAdapter); - if( !comesFromSearch) { - //Hide account header when scrolling for ShowAccountActivity - if (hideHeader && Build.VERSION.SDK_INT >= 21) - ViewCompat.setNestedScrollingEnabled(lv_accounts, true); - final LinearLayoutManager mLayoutManager; - mLayoutManager = new LinearLayoutManager(context); - lv_accounts.setLayoutManager(mLayoutManager); - lv_accounts.addOnScrollListener(new RecyclerView.OnScrollListener() { - public void onScrolled(RecyclerView recyclerView, int dx, int dy) - { - if(dy > 0) { - int visibleItemCount = mLayoutManager.getChildCount(); - int totalItemCount = mLayoutManager.getItemCount(); - int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition(); - if (firstVisibleItem + visibleItemCount == totalItemCount) { - if (!flag_loading) { - flag_loading = true; - if (type != RetrieveAccountsAsyncTask.Type.FOLLOWERS && type != RetrieveAccountsAsyncTask.Type.FOLLOWING) - asyncTask = new RetrieveAccountsAsyncTask(context, type, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - else - asyncTask = new RetrieveAccountsAsyncTask(context, type, targetedId, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - nextElementLoader.setVisibility(View.VISIBLE); - } - } else { - nextElementLoader.setVisibility(View.GONE); + final LinearLayoutManager mLayoutManager; + mLayoutManager = new LinearLayoutManager(context); + lv_accounts.setLayoutManager(mLayoutManager); + lv_accounts.addOnScrollListener(new RecyclerView.OnScrollListener() { + public void onScrolled(RecyclerView recyclerView, int dx, int dy) + { + if(dy > 0) { + int visibleItemCount = mLayoutManager.getChildCount(); + int totalItemCount = mLayoutManager.getItemCount(); + int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition(); + if (firstVisibleItem + visibleItemCount == totalItemCount) { + if (!flag_loading) { + flag_loading = true; + if (type != RetrieveAccountsAsyncTask.Type.FOLLOWERS && type != RetrieveAccountsAsyncTask.Type.FOLLOWING) + asyncTask = new RetrieveAccountsAsyncTask(context, type, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + else + asyncTask = new RetrieveAccountsAsyncTask(context, type, targetedId, max_id, DisplayAccountsFragment.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; - accounts = new ArrayList<>(); - firstLoad = true; - flag_loading = true; - swiped = true; - if (type != RetrieveAccountsAsyncTask.Type.FOLLOWERS && type != RetrieveAccountsAsyncTask.Type.FOLLOWING) - asyncTask = new RetrieveAccountsAsyncTask(context, type, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - else - asyncTask = new RetrieveAccountsAsyncTask(context, type, targetedId, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - } - }); - swipeRefreshLayout.setColorSchemeResources(R.color.mastodonC4, - R.color.mastodonC2, - R.color.mastodonC3); + } + }); + swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { + @Override + public void onRefresh() { + max_id = null; + accounts = new ArrayList<>(); + firstLoad = true; + flag_loading = true; + swiped = true; + if (type != RetrieveAccountsAsyncTask.Type.FOLLOWERS && type != RetrieveAccountsAsyncTask.Type.FOLLOWING) + asyncTask = new RetrieveAccountsAsyncTask(context, type, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + else + asyncTask = new RetrieveAccountsAsyncTask(context, type, targetedId, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } + }); + swipeRefreshLayout.setColorSchemeResources(R.color.mastodonC4, + R.color.mastodonC2, + R.color.mastodonC3); + + if (type != RetrieveAccountsAsyncTask.Type.FOLLOWERS && type != RetrieveAccountsAsyncTask.Type.FOLLOWING) + asyncTask = new RetrieveAccountsAsyncTask(context, type, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + else + asyncTask = new RetrieveAccountsAsyncTask(context, type, targetedId, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - if (type != RetrieveAccountsAsyncTask.Type.FOLLOWERS && type != RetrieveAccountsAsyncTask.Type.FOLLOWING) - asyncTask = new RetrieveAccountsAsyncTask(context, type, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - else - asyncTask = new RetrieveAccountsAsyncTask(context, type, targetedId, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - }else { - accountsListAdapter.notifyDataSetChanged(); - mainLoader.setVisibility(View.GONE); - nextElementLoader.setVisibility(View.GONE); - if( accounts == null || accounts.size() == 0 ) - textviewNoAction.setVisibility(View.VISIBLE); - } return rootView; } 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 83dec2708..8486c2b72 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 @@ -280,17 +280,21 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve if( context == null) return; if( notification != null){ - //Update the id of the last notification retrieved - MainActivity.lastNotificationId = notification.getId(); - notifications.add(0, notification); - MainActivity.countNewNotifications++; - int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition(); - if( firstVisibleItem > 0) - notificationsListAdapter.notifyItemInserted(0); - else - notificationsListAdapter.notifyDataSetChanged(); - if( textviewNoAction.getVisibility() == View.VISIBLE) - textviewNoAction.setVisibility(View.GONE); + //Makes sure the notifications is not already displayed + if( notification.getId() != null && notifications.get(0)!= null + && Long.parseLong(notification.getId()) > Long.parseLong(notifications.get(0).getId())) { + //Update the id of the last notification retrieved + MainActivity.lastNotificationId = notification.getId(); + notifications.add(0, notification); + MainActivity.countNewNotifications++; + int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition(); + if (firstVisibleItem > 0) + notificationsListAdapter.notifyItemInserted(0); + else + notificationsListAdapter.notifyDataSetChanged(); + if (textviewNoAction.getVisibility() == View.VISIBLE) + textviewNoAction.setVisibility(View.GONE); + } } } @@ -299,12 +303,9 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve @Override public void onRetrieveMissingNotifications(List notifications) { if( notifications != null && notifications.size() > 0) { - ArrayList knownId = new ArrayList<>(); - for (Notification nt : this.notifications) { - knownId.add(nt.getId()); - } for (int i = notifications.size()-1 ; i >= 0 ; i--) { - if (!knownId.contains(notifications.get(i).getId())) { + if (this.notifications.size() == 0 || + Long.parseLong(notifications.get(i).getId()) > Long.parseLong(this.notifications.get(0).getId())) { MainActivity.countNewNotifications++; this.notifications.add(0, notifications.get(i)); } 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 78c9ccc1b..66e53a9f3 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 @@ -20,10 +20,8 @@ import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.Looper; -import android.os.Parcelable; import android.support.annotation.NonNull; import android.support.v4.app.Fragment; -import android.support.v4.view.ViewCompat; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; @@ -62,7 +60,6 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn private StatusListAdapter statusListAdapter; private String max_id; private List statuses; - private ArrayList knownId; private RetrieveFeedsAsyncTask.Type type; private RelativeLayout mainLoader, nextElementLoader, textviewNoAction; private boolean firstLoad; @@ -86,35 +83,23 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_status, container, false); statuses = new ArrayList<>(); - knownId = new ArrayList<>(); context = getContext(); Bundle bundle = this.getArguments(); - boolean comesFromSearch = false; - boolean hideHeader = false; showMediaOnly = false; showPinned = false; if (bundle != null) { type = (RetrieveFeedsAsyncTask.Type) bundle.get("type"); targetedId = bundle.getString("targetedId", null); tag = bundle.getString("tag", null); - hideHeader = bundle.getBoolean("hideHeader", false); showMediaOnly = bundle.getBoolean("showMediaOnly",false); showPinned = bundle.getBoolean("showPinned",false); - if( bundle.containsKey("statuses")){ - ArrayList statusesReceived = bundle.getParcelableArrayList("statuses"); - assert statusesReceived != null; - for(Parcelable status: statusesReceived){ - statuses.add((Status) status); - knownId.add(((Status) status).getId()); - } - comesFromSearch = true; - } } max_id = null; flag_loading = true; firstLoad = true; swiped = false; + assert context != null; final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); isOnWifi = Helper.isOnWIFI(context); positionSpinnerTrans = sharedpreferences.getInt(Helper.SET_TRANSLATOR, Helper.TRANS_YANDEX); @@ -133,90 +118,78 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn lv_status.setAdapter(statusListAdapter); mLayoutManager = new LinearLayoutManager(context); lv_status.setLayoutManager(mLayoutManager); - if( !comesFromSearch){ - - //Hide account header when scrolling for ShowAccountActivity - if (hideHeader) - ViewCompat.setNestedScrollingEnabled(lv_status, true); - - lv_status.addOnScrollListener(new RecyclerView.OnScrollListener() { - public void onScrolled(RecyclerView recyclerView, int dx, int dy) - { - if(dy > 0){ - int visibleItemCount = mLayoutManager.getChildCount(); - int totalItemCount = mLayoutManager.getItemCount(); - int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition(); - if(firstVisibleItem + visibleItemCount == totalItemCount && context != null) { - if(!flag_loading ) { - flag_loading = true; - 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); - - nextElementLoader.setVisibility(View.VISIBLE); - } - } else { - nextElementLoader.setVisibility(View.GONE); - } - } - } - }); - swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { - @Override - public void onRefresh() { - max_id = null; - statuses = new ArrayList<>(); - knownId = new ArrayList<>(); - firstLoad = true; - flag_loading = true; - swiped = true; - MainActivity.countNewStatus = 0; - 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); - } - }); - swipeRefreshLayout.setColorSchemeResources(R.color.mastodonC4, - R.color.mastodonC2, - R.color.mastodonC3); - 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) + lv_status.addOnScrollListener(new RecyclerView.OnScrollListener() { + public void onScrolled(RecyclerView recyclerView, int dx, int dy) + { + if(dy > 0){ + int visibleItemCount = mLayoutManager.getChildCount(); + int totalItemCount = mLayoutManager.getItemCount(); + int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition(); + if(firstVisibleItem + visibleItemCount == totalItemCount && context != null) { + if(!flag_loading ) { + flag_loading = true; + 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) + else if( type == RetrieveFeedsAsyncTask.Type.TAG) asyncTask = new RetrieveFeedsAsyncTask(context, type, tag, targetedId, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - else { + else asyncTask = new RetrieveFeedsAsyncTask(context, type, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - } + + nextElementLoader.setVisibility(View.VISIBLE); } + } else { + nextElementLoader.setVisibility(View.GONE); } - }, 500); + } + } + }); + + + swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { + @Override + public void onRefresh() { + max_id = null; + statuses = new ArrayList<>(); + firstLoad = true; + flag_loading = true; + swiped = true; + MainActivity.countNewStatus = 0; + 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); + } + }); + swipeRefreshLayout.setColorSchemeResources(R.color.mastodonC4, + R.color.mastodonC2, + R.color.mastodonC3); + 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 { - statusListAdapter.notifyDataSetChanged(); - mainLoader.setVisibility(View.GONE); - nextElementLoader.setVisibility(View.GONE); - if( statuses == null || statuses.size() == 0 ) - textviewNoAction.setVisibility(View.VISIBLE); + 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); } return rootView; @@ -277,7 +250,7 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn if( statuses != null && statuses.size() > 0) { for(Status tmpStatus: statuses){ - if( !knownId.contains(tmpStatus.getId())) { + if( this.statuses.size() == 0 || Long.parseLong(tmpStatus.getId()) < Long.parseLong(this.statuses.get(this.statuses.size()-1).getId())) { if( type == RetrieveFeedsAsyncTask.Type.HOME && firstLoad && lastReadStatus != null && Long.parseLong(tmpStatus.getId()) > Long.parseLong(lastReadStatus)){ tmpStatus.setNew(true); MainActivity.countNewStatus++; @@ -285,7 +258,6 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn tmpStatus.setNew(false); } this.statuses.add(tmpStatus); - knownId.add(tmpStatus.getId()); } } @@ -317,42 +289,41 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn */ public void refresh(Status status){ //New data are available - if( type == RetrieveFeedsAsyncTask.Type.HOME) { - if (context == null) - return; - if (status != null && !knownId.contains(status.getId())) { + if (context == null) + return; + if( status.getId() != null && statuses.get(0)!= null + && Long.parseLong(status.getId()) > Long.parseLong(statuses.get(0).getId())) { + if (type == RetrieveFeedsAsyncTask.Type.HOME) { + //Update the id of the last toot retrieved MainActivity.lastHomeId = status.getId(); status.setReplies(new ArrayList()); - statuses.add(0,status); - knownId.add(0,status.getId()); + statuses.add(0, status); SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); - if( !status.getAccount().getId().equals(userId)) + if (!status.getAccount().getId().equals(userId)) MainActivity.countNewStatus++; int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition(); - if( firstVisibleItem > 0) + if (firstVisibleItem > 0) statusListAdapter.notifyItemInserted(0); else statusListAdapter.notifyDataSetChanged(); if (textviewNoAction.getVisibility() == View.VISIBLE) textviewNoAction.setVisibility(View.GONE); - } - }else if(type == RetrieveFeedsAsyncTask.Type.PUBLIC || type == RetrieveFeedsAsyncTask.Type.LOCAL){ - if (context == null) - return; - if (status != null && !knownId.contains(status.getId())) { + + } else if (type == RetrieveFeedsAsyncTask.Type.PUBLIC || type == RetrieveFeedsAsyncTask.Type.LOCAL) { + status.setReplies(new ArrayList()); status.setNew(false); statuses.add(0, status); int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition(); - if( firstVisibleItem > 0) + if (firstVisibleItem > 0) statusListAdapter.notifyItemInserted(0); else statusListAdapter.notifyDataSetChanged(); - knownId.add(0, status.getId()); if (textviewNoAction.getVisibility() == View.VISIBLE) textviewNoAction.setVisibility(View.GONE); + } } } @@ -506,10 +477,10 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn if( statuses != null && statuses.size() > 0) { int inserted = 0; for (int i = statuses.size() - 1; i >= 0; i--) { - if (!knownId.contains(statuses.get(i).getId())) { + if (this.statuses.size() == 0 || + Long.parseLong(statuses.get(i).getId()) > Long.parseLong(this.statuses.get(0).getId())) { if (type == RetrieveFeedsAsyncTask.Type.HOME) statuses.get(i).setNew(true); - knownId.add(0,statuses.get(i).getId()); inserted++; statuses.get(i).setReplies(new ArrayList()); this.statuses.add(0, statuses.get(i)); diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 8a42b19af..f2a582305 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -1,14 +1,6 @@ - - - #3F51B5 - #303F9F - #2196F3 - - #616161 - #212121 - #4db6ac + #0288D1