Some other implementations...

This commit is contained in:
tom79 2017-08-24 15:57:35 +02:00
parent f21881f697
commit 34701d2dbb
11 changed files with 164 additions and 26 deletions

View File

@ -779,6 +779,7 @@ public class MainActivity extends AppCompatActivity
@Override @Override
public void onResume(){ public void onResume(){
super.onResume(); super.onResume();
refreshData();
//Proceeds to update of the authenticated account //Proceeds to update of the authenticated account
if(Helper.isLoggedIn(getApplicationContext())) if(Helper.isLoggedIn(getApplicationContext()))
new UpdateAccountInfoByIDAsyncTask(getApplicationContext(), MainActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); new UpdateAccountInfoByIDAsyncTask(getApplicationContext(), MainActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
@ -980,11 +981,13 @@ public class MainActivity extends AppCompatActivity
String last_refresh = sharedpreferences.getString(Helper.LAST_BUBBLE_REFRESH + account.getId(), null); String last_refresh = sharedpreferences.getString(Helper.LAST_BUBBLE_REFRESH + account.getId(), null);
Date last_refresh_date = Helper.stringToDate(getApplicationContext(), last_refresh); Date last_refresh_date = Helper.stringToDate(getApplicationContext(), last_refresh);
if (last_refresh_date == null || new Date().getTime() - last_refresh_date.getTime() >= TimeUnit.MINUTES.toMillis(5)) { if (last_refresh_date == null || new Date().getTime() - last_refresh_date.getTime() >= TimeUnit.MINUTES.toMillis(5)) {
homeFragment.update(); if( homeFragment != null && notificationsFragment != null){
notificationsFragment.update(); homeFragment.update();
SharedPreferences.Editor editor = sharedpreferences.edit(); notificationsFragment.update();
editor.putString(Helper.LAST_BUBBLE_REFRESH+ account.getId(),Helper.dateToString(getApplicationContext(), new Date())); SharedPreferences.Editor editor = sharedpreferences.edit();
editor.apply(); editor.putString(Helper.LAST_BUBBLE_REFRESH+ account.getId(),Helper.dateToString(getApplicationContext(), new Date()));
editor.apply();
}
} }
} }

View File

@ -15,9 +15,14 @@
package fr.gouv.etalab.mastodon.asynctasks; package fr.gouv.etalab.mastodon.asynctasks;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask; import android.os.AsyncTask;
import java.util.Date;
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.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveFeedsInterface; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveFeedsInterface;
@ -56,6 +61,8 @@ public class RetrieveFeedsAsyncTask extends AsyncTask<Void, Void, Void> {
this.max_id = max_id; this.max_id = max_id;
this.listener = onRetrieveFeedsInterface; this.listener = onRetrieveFeedsInterface;
this.refreshData = true; this.refreshData = true;
updateTimeRefresh();
} }
public RetrieveFeedsAsyncTask(Context context, Type action, String targetedID, String max_id, boolean showMediaOnly, OnRetrieveFeedsInterface onRetrieveFeedsInterface){ public RetrieveFeedsAsyncTask(Context context, Type action, String targetedID, String max_id, boolean showMediaOnly, OnRetrieveFeedsInterface onRetrieveFeedsInterface){
@ -66,6 +73,7 @@ public class RetrieveFeedsAsyncTask extends AsyncTask<Void, Void, Void> {
this.targetedID = targetedID; this.targetedID = targetedID;
this.showMediaOnly = showMediaOnly; this.showMediaOnly = showMediaOnly;
this.refreshData = true; this.refreshData = true;
updateTimeRefresh();
} }
public RetrieveFeedsAsyncTask(Context context, Type action, String tag, String targetedID, String max_id, OnRetrieveFeedsInterface onRetrieveFeedsInterface){ public RetrieveFeedsAsyncTask(Context context, Type action, String tag, String targetedID, String max_id, OnRetrieveFeedsInterface onRetrieveFeedsInterface){
this.context = context; this.context = context;
@ -75,6 +83,7 @@ public class RetrieveFeedsAsyncTask extends AsyncTask<Void, Void, Void> {
this.targetedID = targetedID; this.targetedID = targetedID;
this.tag = tag; this.tag = tag;
this.refreshData = true; this.refreshData = true;
updateTimeRefresh();
} }
public RetrieveFeedsAsyncTask(Context context, Type action, String max_id, boolean refreshData, OnRetrieveFeedsInterface onRetrieveFeedsInterface){ public RetrieveFeedsAsyncTask(Context context, Type action, String max_id, boolean refreshData, OnRetrieveFeedsInterface onRetrieveFeedsInterface){
@ -83,6 +92,7 @@ public class RetrieveFeedsAsyncTask extends AsyncTask<Void, Void, Void> {
this.max_id = max_id; this.max_id = max_id;
this.listener = onRetrieveFeedsInterface; this.listener = onRetrieveFeedsInterface;
this.refreshData = refreshData; this.refreshData = refreshData;
updateTimeRefresh();
} }
@Override @Override
@ -125,4 +135,13 @@ public class RetrieveFeedsAsyncTask extends AsyncTask<Void, Void, Void> {
listener.onRetrieveFeeds(apiResponse, refreshData); listener.onRetrieveFeeds(apiResponse, refreshData);
} }
private void updateTimeRefresh(){
if( action == Type.HOME) {
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
editor.putString(Helper.LAST_BUBBLE_REFRESH + userId, Helper.dateToString(context, new Date()));
editor.apply();
}
}
} }

View File

@ -15,9 +15,14 @@
package fr.gouv.etalab.mastodon.asynctasks; package fr.gouv.etalab.mastodon.asynctasks;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask; import android.os.AsyncTask;
import java.util.Date;
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.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveNotificationsInterface; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveNotificationsInterface;
@ -46,6 +51,7 @@ public class RetrieveNotificationsAsyncTask extends AsyncTask<Void, Void, Void>
this.userId = userId; this.userId = userId;
this.token = token; this.token = token;
this.refreshData = true; this.refreshData = true;
updateTimeRefresh();
} }
public RetrieveNotificationsAsyncTask(Context context, String instance, String token, String max_id, String acct, String userId, boolean refreshData, OnRetrieveNotificationsInterface onRetrieveNotificationsInterface){ public RetrieveNotificationsAsyncTask(Context context, String instance, String token, String max_id, String acct, String userId, boolean refreshData, OnRetrieveNotificationsInterface onRetrieveNotificationsInterface){
@ -57,6 +63,7 @@ public class RetrieveNotificationsAsyncTask extends AsyncTask<Void, Void, Void>
this.userId = userId; this.userId = userId;
this.token = token; this.token = token;
this.refreshData = refreshData; this.refreshData = refreshData;
updateTimeRefresh();
} }
@Override @Override
@ -75,4 +82,10 @@ public class RetrieveNotificationsAsyncTask extends AsyncTask<Void, Void, Void>
listener.onRetrieveNotifications(apiResponse, acct, userId, refreshData); listener.onRetrieveNotifications(apiResponse, acct, userId, refreshData);
} }
private void updateTimeRefresh(){
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_BUBBLE_REFRESH+ userId,Helper.dateToString(context, new Date()));
editor.apply();
}
} }

View File

@ -26,9 +26,11 @@ import android.view.ViewGroup;
import android.widget.AbsListView; import android.widget.AbsListView;
import android.widget.ListView; import android.widget.ListView;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
import fr.gouv.etalab.mastodon.activities.MainActivity; import fr.gouv.etalab.mastodon.activities.MainActivity;
@ -69,6 +71,12 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
private DisplayNotificationsFragment displayNotificationsFragment; private DisplayNotificationsFragment displayNotificationsFragment;
private List<Notification> notificationsTemp; private List<Notification> notificationsTemp;
private String new_max_id; private String new_max_id;
private TextView new_data;
public DisplayNotificationsFragment(){
displayNotificationsFragment = this;
}
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
@ -81,12 +89,12 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
notifications = new ArrayList<>(); notifications = new ArrayList<>();
swiped = false; swiped = false;
newElements = 0; newElements = 0;
displayNotificationsFragment = this;
swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipeContainer); swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipeContainer);
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
notificationPerPage = sharedpreferences.getInt(Helper.SET_NOTIFICATIONS_PER_PAGE, 15); notificationPerPage = sharedpreferences.getInt(Helper.SET_NOTIFICATIONS_PER_PAGE, 15);
lv_notifications = (ListView) rootView.findViewById(R.id.lv_notifications); lv_notifications = (ListView) rootView.findViewById(R.id.lv_notifications);
new_data = (TextView) rootView.findViewById(R.id.new_data);
mainLoader = (RelativeLayout) rootView.findViewById(R.id.loader); mainLoader = (RelativeLayout) rootView.findViewById(R.id.loader);
nextElementLoader = (RelativeLayout) rootView.findViewById(R.id.loading_next_notifications); nextElementLoader = (RelativeLayout) rootView.findViewById(R.id.loading_next_notifications);
textviewNoAction = (RelativeLayout) rootView.findViewById(R.id.no_action); textviewNoAction = (RelativeLayout) rootView.findViewById(R.id.no_action);
@ -132,6 +140,17 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
R.color.mastodonC3); R.color.mastodonC3);
asyncTask = new RetrieveNotificationsAsyncTask(context, null, null, max_id, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); asyncTask = new RetrieveNotificationsAsyncTask(context, null, null, max_id, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new_data.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if( notificationsTemp != null && notificationsTemp.size() > 0 && new_max_id != null){
new_data.setVisibility(View.GONE);
manageNotifications(notificationsTemp, new_max_id);
}
}
});
return rootView; return rootView;
} }
@ -173,11 +192,15 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
swiped = false; swiped = false;
return; return;
} }
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_BUBBLE_REFRESH+ userId,Helper.dateToString(context, new Date()));
editor.apply();
String old_max_id = max_id; String old_max_id = max_id;
new_max_id = apiResponse.getMax_id();
List<Notification> notifications = apiResponse.getNotifications(); List<Notification> notifications = apiResponse.getNotifications();
if( refreshData || !displayNotificationsFragment.isVisible()) { if( refreshData || !displayNotificationsFragment.isVisible()) {
manageNotifications(notifications, new_max_id); max_id = apiResponse.getMax_id();
manageNotifications(notifications, max_id);
if( !displayNotificationsFragment.isVisible()){ if( !displayNotificationsFragment.isVisible()){
int countData = 0; int countData = 0;
for(Notification nt : notifications){ for(Notification nt : notifications){
@ -188,6 +211,7 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
((MainActivity)getActivity()).updateNotifCounter(countData); ((MainActivity)getActivity()).updateNotifCounter(countData);
} }
}else { }else {
new_max_id = apiResponse.getMax_id();
notificationsTemp = notifications; notificationsTemp = notifications;
} }

View File

@ -29,8 +29,10 @@ import android.view.ViewGroup;
import android.widget.AbsListView; import android.widget.AbsListView;
import android.widget.ListView; import android.widget.ListView;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
import fr.gouv.etalab.mastodon.activities.MainActivity; import fr.gouv.etalab.mastodon.activities.MainActivity;
@ -78,6 +80,12 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
private DisplayStatusFragment displayStatusFragment; private DisplayStatusFragment displayStatusFragment;
private List<Status> statusesTemp; private List<Status> statusesTemp;
private String new_max_id; private String new_max_id;
private TextView new_data;
public DisplayStatusFragment(){
displayStatusFragment = this;
}
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
@ -110,7 +118,7 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
firstLoad = true; firstLoad = true;
swiped = false; swiped = false;
newElements = 0; newElements = 0;
displayStatusFragment = this;
isOnWifi = Helper.isOnWIFI(context); isOnWifi = Helper.isOnWIFI(context);
swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipeContainer); swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipeContainer);
@ -126,7 +134,7 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
nextElementLoader.setVisibility(View.GONE); nextElementLoader.setVisibility(View.GONE);
statusListAdapter = new StatusListAdapter(context, type, targetedId, isOnWifi, behaviorWithAttachments, this.statuses); statusListAdapter = new StatusListAdapter(context, type, targetedId, isOnWifi, behaviorWithAttachments, this.statuses);
lv_status.setAdapter(statusListAdapter); lv_status.setAdapter(statusListAdapter);
new_data = (TextView) rootView.findViewById(R.id.new_data);
if( !comesFromSearch){ if( !comesFromSearch){
//Hide account header when scrolling for ShowAccountActivity //Hide account header when scrolling for ShowAccountActivity
@ -194,6 +202,16 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
textviewNoAction.setVisibility(View.VISIBLE); textviewNoAction.setVisibility(View.VISIBLE);
} }
new_data.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if( statusesTemp != null && statusesTemp.size() > 0 && new_max_id != null){
new_data.setVisibility(View.GONE);
manageStatus(statusesTemp, new_max_id);
}
}
});
return rootView; return rootView;
} }
@ -226,8 +244,8 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
mainLoader.setVisibility(View.GONE); mainLoader.setVisibility(View.GONE);
nextElementLoader.setVisibility(View.GONE); nextElementLoader.setVisibility(View.GONE);
//Discards 404 - error which can often happen due to toots which have been deleted //Discards 404 - error which can often happen due to toots which have been deleted
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
if( apiResponse.getError() != null && !apiResponse.getError().getError().startsWith("404 -")){ if( apiResponse.getError() != null && !apiResponse.getError().getError().startsWith("404 -")){
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
boolean show_error_messages = sharedpreferences.getBoolean(Helper.SET_SHOW_ERROR_MESSAGES, true); boolean show_error_messages = sharedpreferences.getBoolean(Helper.SET_SHOW_ERROR_MESSAGES, true);
if( show_error_messages) if( show_error_messages)
Toast.makeText(context, apiResponse.getError().getError(),Toast.LENGTH_LONG).show(); Toast.makeText(context, apiResponse.getError().getError(),Toast.LENGTH_LONG).show();
@ -236,25 +254,36 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
flag_loading = false; flag_loading = false;
return; return;
} }
List<Status> statuses = apiResponse.getStatuses(); if( type == RetrieveFeedsAsyncTask.Type.HOME){
String old_max_id = max_id; SharedPreferences.Editor editor = sharedpreferences.edit();
new_max_id = apiResponse.getMax_id(); String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
editor.putString(Helper.LAST_BUBBLE_REFRESH+ userId,Helper.dateToString(context, new Date()));
if( refreshData || !displayStatusFragment.isVisible()) { editor.apply();
manageStatus(statuses, max_id); List<Status> statuses = apiResponse.getStatuses();
if( !displayStatusFragment.isVisible()){ String old_max_id = max_id;
int countData = 0; if( refreshData || !displayStatusFragment.isVisible()) {
for(Status st : statuses){ max_id = apiResponse.getMax_id();
if( st.getId().equals(old_max_id)) manageStatus(statuses, max_id);
break; if( !displayStatusFragment.isVisible()){
countData++; int countData = 0;
for(Status st : statuses){
if( st.getId().equals(old_max_id))
break;
countData++;
}
((MainActivity)getActivity()).updateHomeCounter(countData);
} }
((MainActivity)getActivity()).updateNotifCounter(countData); }else {
new_max_id = apiResponse.getMax_id();
statusesTemp = statuses;
} }
}else { }else {
statusesTemp = statuses; max_id = apiResponse.getMax_id();
manageStatus(statuses, max_id);
} }
} }
private void manageStatus(List<Status> statuses, String max_id){ private void manageStatus(List<Status> statuses, String max_id){

View File

@ -79,5 +79,16 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:indeterminate="true" /> android:indeterminate="true" />
</RelativeLayout> </RelativeLayout>
<TextView
android:visibility="gone"
android:padding="10dp"
android:gravity="center"
android:text="@string/new_data"
android:layout_alignParentBottom="true"
android:id="@+id/new_data"
android:layout_width="match_parent"
android:textSize="18sp"
android:layout_height="wrap_content"/>
</RelativeLayout> </RelativeLayout>

View File

@ -77,5 +77,17 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:indeterminate="true" /> android:indeterminate="true" />
</RelativeLayout> </RelativeLayout>
<TextView
android:visibility="gone"
android:padding="10dp"
android:gravity="center"
android:text="@string/new_data"
android:layout_alignParentBottom="true"
android:id="@+id/new_data"
android:layout_width="match_parent"
android:textSize="18sp"
android:layout_height="wrap_content"/>
</RelativeLayout> </RelativeLayout>

View File

@ -79,5 +79,16 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:indeterminate="true" /> android:indeterminate="true" />
</RelativeLayout> </RelativeLayout>
<TextView
android:visibility="gone"
android:padding="10dp"
android:gravity="center"
android:text="@string/new_data"
android:layout_alignParentBottom="true"
android:id="@+id/new_data"
android:layout_width="match_parent"
android:textSize="16sp"
android:layout_height="wrap_content"/>
</RelativeLayout> </RelativeLayout>

View File

@ -77,5 +77,17 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:indeterminate="true" /> android:indeterminate="true" />
</RelativeLayout> </RelativeLayout>
<TextView
android:visibility="gone"
android:padding="10dp"
android:gravity="center"
android:text="@string/new_data"
android:layout_alignParentBottom="true"
android:id="@+id/new_data"
android:layout_width="match_parent"
android:textSize="16sp"
android:layout_height="wrap_content"/>
</RelativeLayout> </RelativeLayout>

View File

@ -57,6 +57,8 @@
<string name="shared_via">Partagé via Mastalab</string> <string name="shared_via">Partagé via Mastalab</string>
<string name="replies">Réponses</string> <string name="replies">Réponses</string>
<string name="username">Nom d\'utilisateur</string> <string name="username">Nom d\'utilisateur</string>
<string name="new_data">De nouvelles données sont disponibles ! Souhaitez-vous les afficher ?</string>
<!--- Menu --> <!--- Menu -->
<string name="home_menu">Accueil</string> <string name="home_menu">Accueil</string>
<string name="local_menu">Fil public local</string> <string name="local_menu">Fil public local</string>

View File

@ -59,6 +59,8 @@
<string name="shared_via">Shared via Mastalab</string> <string name="shared_via">Shared via Mastalab</string>
<string name="replies">Replies</string> <string name="replies">Replies</string>
<string name="username">User name</string> <string name="username">User name</string>
<string name="new_data">New data are available! Do you want to display them?</string>
<!--- Menu --> <!--- Menu -->
<string name="home_menu">Home</string> <string name="home_menu">Home</string>
<string name="local_menu">Local timeline</string> <string name="local_menu">Local timeline</string>