Issue #41 - Fixes potential duplicate toots

This commit is contained in:
stom79 2017-11-15 09:44:12 +01:00
parent e765c01e97
commit 843bf2641b
5 changed files with 155 additions and 211 deletions

View File

@ -30,6 +30,7 @@ import android.graphics.drawable.Drawable;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi; import android.support.annotation.RequiresApi;
import android.support.design.widget.AppBarLayout; import android.support.design.widget.AppBarLayout;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
@ -236,7 +237,9 @@ public class ShowAccountActivity extends AppCompatActivity implements OnPostActi
@Override @Override
public void onTabReselected(TabLayout.Tab tab) { 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()){ switch (tab.getPosition()){
case 0: case 0:
if( displayStatusFragment != null ) if( displayStatusFragment != null )
@ -244,9 +247,10 @@ public class ShowAccountActivity extends AppCompatActivity implements OnPostActi
break; break;
case 1: case 1:
case 2: case 2:
DisplayAccountsFragment displayAccountsFragment = ((DisplayAccountsFragment) fragment); if( fragment != null) {
if (displayAccountsFragment != null) DisplayAccountsFragment displayAccountsFragment = ((DisplayAccountsFragment) fragment);
displayAccountsFragment.scrollToTop(); displayAccountsFragment.scrollToTop();
}
break; break;
} }
} }
@ -458,6 +462,7 @@ public class ShowAccountActivity extends AppCompatActivity implements OnPostActi
final float scale = getResources().getDisplayMetrics().density; final float scale = getResources().getDisplayMetrics().density;
if(account.isLocked()){ if(account.isLocked()){
Drawable img = ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_lock_outline); 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)); img.setBounds(0,0,(int) (20 * scale + 0.5f),(int) (20 * scale + 0.5f));
account_dn.setCompoundDrawables( img, null, null, null); account_dn.setCompoundDrawables( img, null, null, null);
}else{ }else{
@ -738,7 +743,6 @@ public class ShowAccountActivity extends AppCompatActivity implements OnPostActi
displayStatusFragment = new DisplayStatusFragment(); displayStatusFragment = new DisplayStatusFragment();
bundle.putSerializable("type", RetrieveFeedsAsyncTask.Type.USER); bundle.putSerializable("type", RetrieveFeedsAsyncTask.Type.USER);
bundle.putString("targetedId", accountId); bundle.putString("targetedId", accountId);
bundle.putBoolean("hideHeader",true);
bundle.putBoolean("showMediaOnly",showMediaOnly); bundle.putBoolean("showMediaOnly",showMediaOnly);
bundle.putBoolean("showPinned",showPinned); bundle.putBoolean("showPinned",showPinned);
displayStatusFragment.setArguments(bundle); displayStatusFragment.setArguments(bundle);
@ -747,20 +751,19 @@ public class ShowAccountActivity extends AppCompatActivity implements OnPostActi
DisplayAccountsFragment displayAccountsFragment = new DisplayAccountsFragment(); DisplayAccountsFragment displayAccountsFragment = new DisplayAccountsFragment();
bundle.putSerializable("type", RetrieveAccountsAsyncTask.Type.FOLLOWING); bundle.putSerializable("type", RetrieveAccountsAsyncTask.Type.FOLLOWING);
bundle.putString("targetedId", accountId); bundle.putString("targetedId", accountId);
bundle.putBoolean("hideHeader",true);
displayAccountsFragment.setArguments(bundle); displayAccountsFragment.setArguments(bundle);
return displayAccountsFragment; return displayAccountsFragment;
case 2: case 2:
displayAccountsFragment = new DisplayAccountsFragment(); displayAccountsFragment = new DisplayAccountsFragment();
bundle.putSerializable("type", RetrieveAccountsAsyncTask.Type.FOLLOWERS); bundle.putSerializable("type", RetrieveAccountsAsyncTask.Type.FOLLOWERS);
bundle.putString("targetedId", accountId); bundle.putString("targetedId", accountId);
bundle.putBoolean("hideHeader",true);
displayAccountsFragment.setArguments(bundle); displayAccountsFragment.setArguments(bundle);
return displayAccountsFragment; return displayAccountsFragment;
} }
return null; return null;
} }
@NonNull
@Override @Override
public Object instantiateItem(ViewGroup container, int position) { public Object instantiateItem(ViewGroup container, int position) {
Fragment createdFragment = (Fragment) super.instantiateItem(container, position); Fragment createdFragment = (Fragment) super.instantiateItem(container, position);

View File

@ -16,11 +16,9 @@ package fr.gouv.etalab.mastodon.fragments;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Parcelable; import android.support.annotation.NonNull;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.SwipeRefreshLayout; import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
@ -65,30 +63,19 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou
private String targetedId; private String targetedId;
private boolean swiped; private boolean swiped;
private RecyclerView lv_accounts; private RecyclerView lv_accounts;
boolean hideHeader;
@Override @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); View rootView = inflater.inflate(R.layout.fragment_accounts, container, false);
context = getContext(); context = getContext();
boolean comesFromSearch = false;
hideHeader = false;
Bundle bundle = this.getArguments(); Bundle bundle = this.getArguments();
accounts = new ArrayList<>(); accounts = new ArrayList<>();
if (bundle != null) { if (bundle != null) {
type = (RetrieveAccountsAsyncTask.Type) bundle.get("type"); type = (RetrieveAccountsAsyncTask.Type) bundle.get("type");
targetedId = bundle.getString("targetedId", null); targetedId = bundle.getString("targetedId", null);
hideHeader = bundle.getBoolean("hideHeader", false);
if( bundle.containsKey("accounts")){
ArrayList<Parcelable> accountsReceived = bundle.getParcelableArrayList("accounts");
assert accountsReceived != null;
for(Parcelable account: accountsReceived){
accounts.add((Account)account);
}
comesFromSearch = true;
}
} }
max_id = null; max_id = null;
firstLoad = true; firstLoad = true;
@ -106,64 +93,54 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou
accountsListAdapter = new AccountsListAdapter(context, type, targetedId, this.accounts); accountsListAdapter = new AccountsListAdapter(context, type, targetedId, this.accounts);
lv_accounts.setAdapter(accountsListAdapter); lv_accounts.setAdapter(accountsListAdapter);
if( !comesFromSearch) { final LinearLayoutManager mLayoutManager;
//Hide account header when scrolling for ShowAccountActivity mLayoutManager = new LinearLayoutManager(context);
if (hideHeader && Build.VERSION.SDK_INT >= 21) lv_accounts.setLayoutManager(mLayoutManager);
ViewCompat.setNestedScrollingEnabled(lv_accounts, true); lv_accounts.addOnScrollListener(new RecyclerView.OnScrollListener() {
final LinearLayoutManager mLayoutManager; public void onScrolled(RecyclerView recyclerView, int dx, int dy)
mLayoutManager = new LinearLayoutManager(context); {
lv_accounts.setLayoutManager(mLayoutManager); if(dy > 0) {
lv_accounts.addOnScrollListener(new RecyclerView.OnScrollListener() { int visibleItemCount = mLayoutManager.getChildCount();
public void onScrolled(RecyclerView recyclerView, int dx, int dy) int totalItemCount = mLayoutManager.getItemCount();
{ int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
if(dy > 0) { if (firstVisibleItem + visibleItemCount == totalItemCount) {
int visibleItemCount = mLayoutManager.getChildCount(); if (!flag_loading) {
int totalItemCount = mLayoutManager.getItemCount(); flag_loading = true;
int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition(); if (type != RetrieveAccountsAsyncTask.Type.FOLLOWERS && type != RetrieveAccountsAsyncTask.Type.FOLLOWING)
if (firstVisibleItem + visibleItemCount == totalItemCount) { asyncTask = new RetrieveAccountsAsyncTask(context, type, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
if (!flag_loading) { else
flag_loading = true; asyncTask = new RetrieveAccountsAsyncTask(context, type, targetedId, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
if (type != RetrieveAccountsAsyncTask.Type.FOLLOWERS && type != RetrieveAccountsAsyncTask.Type.FOLLOWING) nextElementLoader.setVisibility(View.VISIBLE);
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);
} }
} else {
nextElementLoader.setVisibility(View.GONE);
} }
} }
}); }
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { });
@Override swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
public void onRefresh() { @Override
max_id = null; public void onRefresh() {
accounts = new ArrayList<>(); max_id = null;
firstLoad = true; accounts = new ArrayList<>();
flag_loading = true; firstLoad = true;
swiped = true; flag_loading = true;
if (type != RetrieveAccountsAsyncTask.Type.FOLLOWERS && type != RetrieveAccountsAsyncTask.Type.FOLLOWING) swiped = true;
asyncTask = new RetrieveAccountsAsyncTask(context, type, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); if (type != RetrieveAccountsAsyncTask.Type.FOLLOWERS && type != RetrieveAccountsAsyncTask.Type.FOLLOWING)
else asyncTask = new RetrieveAccountsAsyncTask(context, type, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
asyncTask = new RetrieveAccountsAsyncTask(context, type, targetedId, 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, swipeRefreshLayout.setColorSchemeResources(R.color.mastodonC4,
R.color.mastodonC3); 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; return rootView;
} }

View File

@ -280,17 +280,21 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
if( context == null) if( context == null)
return; return;
if( notification != null){ if( notification != null){
//Update the id of the last notification retrieved //Makes sure the notifications is not already displayed
MainActivity.lastNotificationId = notification.getId(); if( notification.getId() != null && notifications.get(0)!= null
notifications.add(0, notification); && Long.parseLong(notification.getId()) > Long.parseLong(notifications.get(0).getId())) {
MainActivity.countNewNotifications++; //Update the id of the last notification retrieved
int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition(); MainActivity.lastNotificationId = notification.getId();
if( firstVisibleItem > 0) notifications.add(0, notification);
notificationsListAdapter.notifyItemInserted(0); MainActivity.countNewNotifications++;
else int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
notificationsListAdapter.notifyDataSetChanged(); if (firstVisibleItem > 0)
if( textviewNoAction.getVisibility() == View.VISIBLE) notificationsListAdapter.notifyItemInserted(0);
textviewNoAction.setVisibility(View.GONE); else
notificationsListAdapter.notifyDataSetChanged();
if (textviewNoAction.getVisibility() == View.VISIBLE)
textviewNoAction.setVisibility(View.GONE);
}
} }
} }
@ -299,12 +303,9 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
@Override @Override
public void onRetrieveMissingNotifications(List<Notification> notifications) { public void onRetrieveMissingNotifications(List<Notification> notifications) {
if( notifications != null && notifications.size() > 0) { if( notifications != null && notifications.size() > 0) {
ArrayList<String> knownId = new ArrayList<>();
for (Notification nt : this.notifications) {
knownId.add(nt.getId());
}
for (int i = notifications.size()-1 ; i >= 0 ; i--) { 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++; MainActivity.countNewNotifications++;
this.notifications.add(0, notifications.get(i)); this.notifications.add(0, notifications.get(i));
} }

View File

@ -20,10 +20,8 @@ import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.os.Parcelable;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.SwipeRefreshLayout; import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
@ -62,7 +60,6 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
private StatusListAdapter statusListAdapter; private StatusListAdapter statusListAdapter;
private String max_id; private String max_id;
private List<Status> statuses; private List<Status> statuses;
private ArrayList<String> knownId;
private RetrieveFeedsAsyncTask.Type type; private RetrieveFeedsAsyncTask.Type type;
private RelativeLayout mainLoader, nextElementLoader, textviewNoAction; private RelativeLayout mainLoader, nextElementLoader, textviewNoAction;
private boolean firstLoad; private boolean firstLoad;
@ -86,35 +83,23 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_status, container, false); View rootView = inflater.inflate(R.layout.fragment_status, container, false);
statuses = new ArrayList<>(); statuses = new ArrayList<>();
knownId = new ArrayList<>();
context = getContext(); context = getContext();
Bundle bundle = this.getArguments(); Bundle bundle = this.getArguments();
boolean comesFromSearch = false;
boolean hideHeader = false;
showMediaOnly = false; showMediaOnly = false;
showPinned = false; showPinned = false;
if (bundle != null) { if (bundle != null) {
type = (RetrieveFeedsAsyncTask.Type) bundle.get("type"); type = (RetrieveFeedsAsyncTask.Type) bundle.get("type");
targetedId = bundle.getString("targetedId", null); targetedId = bundle.getString("targetedId", null);
tag = bundle.getString("tag", null); tag = bundle.getString("tag", null);
hideHeader = bundle.getBoolean("hideHeader", false);
showMediaOnly = bundle.getBoolean("showMediaOnly",false); showMediaOnly = bundle.getBoolean("showMediaOnly",false);
showPinned = bundle.getBoolean("showPinned",false); showPinned = bundle.getBoolean("showPinned",false);
if( bundle.containsKey("statuses")){
ArrayList<Parcelable> 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; max_id = null;
flag_loading = true; flag_loading = true;
firstLoad = true; firstLoad = true;
swiped = false; swiped = false;
assert context != null;
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
isOnWifi = Helper.isOnWIFI(context); isOnWifi = Helper.isOnWIFI(context);
positionSpinnerTrans = sharedpreferences.getInt(Helper.SET_TRANSLATOR, Helper.TRANS_YANDEX); positionSpinnerTrans = sharedpreferences.getInt(Helper.SET_TRANSLATOR, Helper.TRANS_YANDEX);
@ -133,90 +118,78 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
lv_status.setAdapter(statusListAdapter); lv_status.setAdapter(statusListAdapter);
mLayoutManager = new LinearLayoutManager(context); mLayoutManager = new LinearLayoutManager(context);
lv_status.setLayoutManager(mLayoutManager); 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() { lv_status.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy)
public void onRefresh() { {
max_id = null; if(dy > 0){
statuses = new ArrayList<>(); int visibleItemCount = mLayoutManager.getChildCount();
knownId = new ArrayList<>(); int totalItemCount = mLayoutManager.getItemCount();
firstLoad = true; int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
flag_loading = true; if(firstVisibleItem + visibleItemCount == totalItemCount && context != null) {
swiped = true; if(!flag_loading ) {
MainActivity.countNewStatus = 0; flag_loading = true;
if( type == RetrieveFeedsAsyncTask.Type.USER) 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)
asyncTask = new RetrieveFeedsAsyncTask(context, type, targetedId, max_id, showMediaOnly, showPinned, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 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); 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); 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 { }else {
statusListAdapter.notifyDataSetChanged(); new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
mainLoader.setVisibility(View.GONE); @Override
nextElementLoader.setVisibility(View.GONE); public void run() {
if( statuses == null || statuses.size() == 0 ) if( context != null){
textviewNoAction.setVisibility(View.VISIBLE); 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; return rootView;
@ -277,7 +250,7 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
if( statuses != null && statuses.size() > 0) { if( statuses != null && statuses.size() > 0) {
for(Status tmpStatus: statuses){ 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)){ if( type == RetrieveFeedsAsyncTask.Type.HOME && firstLoad && lastReadStatus != null && Long.parseLong(tmpStatus.getId()) > Long.parseLong(lastReadStatus)){
tmpStatus.setNew(true); tmpStatus.setNew(true);
MainActivity.countNewStatus++; MainActivity.countNewStatus++;
@ -285,7 +258,6 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
tmpStatus.setNew(false); tmpStatus.setNew(false);
} }
this.statuses.add(tmpStatus); this.statuses.add(tmpStatus);
knownId.add(tmpStatus.getId());
} }
} }
@ -317,42 +289,41 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
*/ */
public void refresh(Status status){ public void refresh(Status status){
//New data are available //New data are available
if( type == RetrieveFeedsAsyncTask.Type.HOME) { if (context == null)
if (context == null) return;
return; if( status.getId() != null && statuses.get(0)!= null
if (status != null && !knownId.contains(status.getId())) { && Long.parseLong(status.getId()) > Long.parseLong(statuses.get(0).getId())) {
if (type == RetrieveFeedsAsyncTask.Type.HOME) {
//Update the id of the last toot retrieved //Update the id of the last toot retrieved
MainActivity.lastHomeId = status.getId(); MainActivity.lastHomeId = status.getId();
status.setReplies(new ArrayList<Status>()); status.setReplies(new ArrayList<Status>());
statuses.add(0,status); statuses.add(0, status);
knownId.add(0,status.getId());
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
if( !status.getAccount().getId().equals(userId)) if (!status.getAccount().getId().equals(userId))
MainActivity.countNewStatus++; MainActivity.countNewStatus++;
int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition(); int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
if( firstVisibleItem > 0) if (firstVisibleItem > 0)
statusListAdapter.notifyItemInserted(0); statusListAdapter.notifyItemInserted(0);
else else
statusListAdapter.notifyDataSetChanged(); statusListAdapter.notifyDataSetChanged();
if (textviewNoAction.getVisibility() == View.VISIBLE) if (textviewNoAction.getVisibility() == View.VISIBLE)
textviewNoAction.setVisibility(View.GONE); textviewNoAction.setVisibility(View.GONE);
}
}else if(type == RetrieveFeedsAsyncTask.Type.PUBLIC || type == RetrieveFeedsAsyncTask.Type.LOCAL){ } else if (type == RetrieveFeedsAsyncTask.Type.PUBLIC || type == RetrieveFeedsAsyncTask.Type.LOCAL) {
if (context == null)
return;
if (status != null && !knownId.contains(status.getId())) {
status.setReplies(new ArrayList<Status>()); status.setReplies(new ArrayList<Status>());
status.setNew(false); status.setNew(false);
statuses.add(0, status); statuses.add(0, status);
int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition(); int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
if( firstVisibleItem > 0) if (firstVisibleItem > 0)
statusListAdapter.notifyItemInserted(0); statusListAdapter.notifyItemInserted(0);
else else
statusListAdapter.notifyDataSetChanged(); statusListAdapter.notifyDataSetChanged();
knownId.add(0, status.getId());
if (textviewNoAction.getVisibility() == View.VISIBLE) if (textviewNoAction.getVisibility() == View.VISIBLE)
textviewNoAction.setVisibility(View.GONE); textviewNoAction.setVisibility(View.GONE);
} }
} }
} }
@ -506,10 +477,10 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
if( statuses != null && statuses.size() > 0) { if( statuses != null && statuses.size() > 0) {
int inserted = 0; int inserted = 0;
for (int i = statuses.size() - 1; i >= 0; i--) { 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) if (type == RetrieveFeedsAsyncTask.Type.HOME)
statuses.get(i).setNew(true); statuses.get(i).setNew(true);
knownId.add(0,statuses.get(i).getId());
inserted++; inserted++;
statuses.get(i).setReplies(new ArrayList<Status>()); statuses.get(i).setReplies(new ArrayList<Status>());
this.statuses.add(0, statuses.get(i)); this.statuses.add(0, statuses.get(i));

View File

@ -1,14 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources tools:ignore="PrivateResource" xmlns:tools="http://schemas.android.com/tools"> <resources tools:ignore="PrivateResource" xmlns:tools="http://schemas.android.com/tools">
<!-- LIGHT -->
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#2196F3</color>
<!-- DARK -->
<color name="colorPrimaryD">#616161</color>
<color name="colorPrimaryDarkD">#212121</color>
<color name="colorAccentD">#4db6ac</color>
<!-- Header Profile--> <!-- Header Profile-->
<color name="header1">#0288D1</color> <color name="header1">#0288D1</color>