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
public void onResume(){
super.onResume();
refreshData();
//Proceeds to update of the authenticated account
if(Helper.isLoggedIn(getApplicationContext()))
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);
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)) {
homeFragment.update();
notificationsFragment.update();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_BUBBLE_REFRESH+ account.getId(),Helper.dateToString(getApplicationContext(), new Date()));
editor.apply();
if( homeFragment != null && notificationsFragment != null){
homeFragment.update();
notificationsFragment.update();
SharedPreferences.Editor editor = sharedpreferences.edit();
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;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import java.util.Date;
import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.helper.Helper;
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.listener = onRetrieveFeedsInterface;
this.refreshData = true;
updateTimeRefresh();
}
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.showMediaOnly = showMediaOnly;
this.refreshData = true;
updateTimeRefresh();
}
public RetrieveFeedsAsyncTask(Context context, Type action, String tag, String targetedID, String max_id, OnRetrieveFeedsInterface onRetrieveFeedsInterface){
this.context = context;
@ -75,6 +83,7 @@ public class RetrieveFeedsAsyncTask extends AsyncTask<Void, Void, Void> {
this.targetedID = targetedID;
this.tag = tag;
this.refreshData = true;
updateTimeRefresh();
}
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.listener = onRetrieveFeedsInterface;
this.refreshData = refreshData;
updateTimeRefresh();
}
@Override
@ -125,4 +135,13 @@ public class RetrieveFeedsAsyncTask extends AsyncTask<Void, Void, Void> {
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;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import java.util.Date;
import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveNotificationsInterface;
@ -46,6 +51,7 @@ public class RetrieveNotificationsAsyncTask extends AsyncTask<Void, Void, Void>
this.userId = userId;
this.token = token;
this.refreshData = true;
updateTimeRefresh();
}
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.token = token;
this.refreshData = refreshData;
updateTimeRefresh();
}
@Override
@ -75,4 +82,10 @@ public class RetrieveNotificationsAsyncTask extends AsyncTask<Void, Void, Void>
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.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import fr.gouv.etalab.mastodon.activities.MainActivity;
@ -69,6 +71,12 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
private DisplayNotificationsFragment displayNotificationsFragment;
private List<Notification> notificationsTemp;
private String new_max_id;
private TextView new_data;
public DisplayNotificationsFragment(){
displayNotificationsFragment = this;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
@ -81,12 +89,12 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
notifications = new ArrayList<>();
swiped = false;
newElements = 0;
displayNotificationsFragment = this;
swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipeContainer);
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
notificationPerPage = sharedpreferences.getInt(Helper.SET_NOTIFICATIONS_PER_PAGE, 15);
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);
nextElementLoader = (RelativeLayout) rootView.findViewById(R.id.loading_next_notifications);
textviewNoAction = (RelativeLayout) rootView.findViewById(R.id.no_action);
@ -132,6 +140,17 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
R.color.mastodonC3);
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;
}
@ -173,11 +192,15 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
swiped = false;
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;
new_max_id = apiResponse.getMax_id();
List<Notification> notifications = apiResponse.getNotifications();
if( refreshData || !displayNotificationsFragment.isVisible()) {
manageNotifications(notifications, new_max_id);
max_id = apiResponse.getMax_id();
manageNotifications(notifications, max_id);
if( !displayNotificationsFragment.isVisible()){
int countData = 0;
for(Notification nt : notifications){
@ -188,6 +211,7 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
((MainActivity)getActivity()).updateNotifCounter(countData);
}
}else {
new_max_id = apiResponse.getMax_id();
notificationsTemp = notifications;
}

View File

@ -29,8 +29,10 @@ import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import fr.gouv.etalab.mastodon.activities.MainActivity;
@ -78,6 +80,12 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
private DisplayStatusFragment displayStatusFragment;
private List<Status> statusesTemp;
private String new_max_id;
private TextView new_data;
public DisplayStatusFragment(){
displayStatusFragment = this;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
@ -110,7 +118,7 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
firstLoad = true;
swiped = false;
newElements = 0;
displayStatusFragment = this;
isOnWifi = Helper.isOnWIFI(context);
swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipeContainer);
@ -126,7 +134,7 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
nextElementLoader.setVisibility(View.GONE);
statusListAdapter = new StatusListAdapter(context, type, targetedId, isOnWifi, behaviorWithAttachments, this.statuses);
lv_status.setAdapter(statusListAdapter);
new_data = (TextView) rootView.findViewById(R.id.new_data);
if( !comesFromSearch){
//Hide account header when scrolling for ShowAccountActivity
@ -194,6 +202,16 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
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;
}
@ -226,8 +244,8 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
mainLoader.setVisibility(View.GONE);
nextElementLoader.setVisibility(View.GONE);
//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 -")){
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
boolean show_error_messages = sharedpreferences.getBoolean(Helper.SET_SHOW_ERROR_MESSAGES, true);
if( show_error_messages)
Toast.makeText(context, apiResponse.getError().getError(),Toast.LENGTH_LONG).show();
@ -236,25 +254,36 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
flag_loading = false;
return;
}
List<Status> statuses = apiResponse.getStatuses();
String old_max_id = max_id;
new_max_id = apiResponse.getMax_id();
if( refreshData || !displayStatusFragment.isVisible()) {
manageStatus(statuses, max_id);
if( !displayStatusFragment.isVisible()){
int countData = 0;
for(Status st : statuses){
if( st.getId().equals(old_max_id))
break;
countData++;
if( type == RetrieveFeedsAsyncTask.Type.HOME){
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();
List<Status> statuses = apiResponse.getStatuses();
String old_max_id = max_id;
if( refreshData || !displayStatusFragment.isVisible()) {
max_id = apiResponse.getMax_id();
manageStatus(statuses, max_id);
if( !displayStatusFragment.isVisible()){
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 {
statusesTemp = statuses;
max_id = apiResponse.getMax_id();
manageStatus(statuses, max_id);
}
}
private void manageStatus(List<Status> statuses, String max_id){

View File

@ -79,5 +79,16 @@
android:layout_height="match_parent"
android:indeterminate="true" />
</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>

View File

@ -77,5 +77,17 @@
android:layout_height="match_parent"
android:indeterminate="true" />
</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>

View File

@ -79,5 +79,16 @@
android:layout_height="match_parent"
android:indeterminate="true" />
</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>

View File

@ -77,5 +77,17 @@
android:layout_height="match_parent"
android:indeterminate="true" />
</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>

View File

@ -57,6 +57,8 @@
<string name="shared_via">Partagé via Mastalab</string>
<string name="replies">Réponses</string>
<string name="username">Nom d\'utilisateur</string>
<string name="new_data">De nouvelles données sont disponibles ! Souhaitez-vous les afficher ?</string>
<!--- Menu -->
<string name="home_menu">Accueil</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="replies">Replies</string>
<string name="username">User name</string>
<string name="new_data">New data are available! Do you want to display them?</string>
<!--- Menu -->
<string name="home_menu">Home</string>
<string name="local_menu">Local timeline</string>