Improves unread toots/notifications -

This commit is contained in:
tom79 2017-09-03 16:35:45 +02:00
parent 03d9be6cbf
commit 418c79e64a
6 changed files with 61 additions and 82 deletions

View File

@ -61,6 +61,7 @@ import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.utils.L;
import java.io.File;
import java.util.ArrayList;
@ -142,13 +143,9 @@ public class MainActivity extends AppCompatActivity
public void onReceive(Context context, Intent intent) {
Bundle b = intent.getExtras();
StreamingService.EventStreaming eventStreaming = (StreamingService.EventStreaming) intent.getSerializableExtra("eventStreaming");
Log.v(Helper.TAG,"eventStreaming= " + eventStreaming);
if( eventStreaming == StreamingService.EventStreaming.NOTIFICATION){
Notification notification = b.getParcelable("data");
Log.v(Helper.TAG,"notificationsFragment= " + notificationsFragment);
if(notificationsFragment != null){
Log.v(Helper.TAG,"getUserVisibleHint= " + notificationsFragment.getUserVisibleHint());
Log.v(Helper.TAG,"isActivityVisible= " + isActivityVisible());
if(notificationsFragment.getUserVisibleHint() && isActivityVisible()){
notificationsFragment.updateData(notification);
@ -158,10 +155,7 @@ public class MainActivity extends AppCompatActivity
}
}else if(eventStreaming == StreamingService.EventStreaming.UPDATE){
Status status = b.getParcelable("data");
Log.v(Helper.TAG,"homeFragment= " + homeFragment);
if( homeFragment != null){
Log.v(Helper.TAG,"getUserVisibleHint= " + homeFragment.getUserVisibleHint());
Log.v(Helper.TAG,"isActivityVisible= " + isActivityVisible());
if(homeFragment.getUserVisibleHint() && isActivityVisible()){
homeFragment.updateData(status);
}else{
@ -289,14 +283,15 @@ public class MainActivity extends AppCompatActivity
if (homeFragment != null && Helper.getUnreadToots(getApplicationContext(), null) > 0) {
homeFragment.refresh(null);
}
Helper.clearUnreadToots(getApplicationContext(), null);
updateHomeCounter();
}else if( tab.getPosition() == 1) {
fragmentTag = "NOTIFICATIONS";
item = navigationView.getMenu().findItem(R.id.nav_notification);
if (notificationsFragment != null && Helper.getUnreadNotifications(getApplicationContext(), null) > 0) {
notificationsFragment.refresh(null);
}
Helper.clearUnreadNotifications(getApplicationContext(), null);
updateNotifCounter();
}else if( tab.getPosition() == 2 && display_local) {
@ -918,20 +913,25 @@ public class MainActivity extends AppCompatActivity
}
toolbarTitle.setText(item.getTitle());
if (id == R.id.nav_home) {
//noinspection ConstantConditions
tabLayout.getTabAt(0).select();
if( tabLayout.getSelectedTabPosition() != 0)
//noinspection ConstantConditions
tabLayout.getTabAt(0).select();
return true;
} else if( id == R.id.nav_notification){
//noinspection ConstantConditions
tabLayout.getTabAt(1).select();
if( tabLayout.getSelectedTabPosition() != 1)
//noinspection ConstantConditions
tabLayout.getTabAt(1).select();
return true;
}else if (id == R.id.nav_local) {
//noinspection ConstantConditions
tabLayout.getTabAt(2).select();
if( tabLayout.getSelectedTabPosition() != 2)
//noinspection ConstantConditions
tabLayout.getTabAt(2).select();
return true;
} else if (id == R.id.nav_global) {
//noinspection ConstantConditions
tabLayout.getTabAt(3).select();
if( tabLayout.getSelectedTabPosition() != 3)
//noinspection ConstantConditions
tabLayout.getTabAt(3).select();
return true;
}
DisplayStatusFragment statusFragment;
@ -1108,10 +1108,7 @@ public class MainActivity extends AppCompatActivity
if( Helper.getUnreadToots(getApplicationContext(), null) > 0){
//New data are available
//The fragment is not displayed, so the counter is displayed
if( tabLayout.getSelectedTabPosition() != 0)
tabCounterHome.setVisibility(View.VISIBLE);
else
tabCounterHome.setVisibility(View.GONE);
tabCounterHome.setVisibility(View.VISIBLE);
}else {
tabCounterHome.setVisibility(View.GONE);
}
@ -1127,10 +1124,7 @@ public class MainActivity extends AppCompatActivity
TextView tabCounterNotif = (TextView) tabNotif.findViewById(R.id.tab_counter);
tabCounterNotif.setText(String.valueOf(Helper.getUnreadNotifications(getApplicationContext(), null)));
if( Helper.getUnreadNotifications(getApplicationContext(), null) > 0){
if( tabLayout.getSelectedTabPosition() != 1)
tabCounterNotif.setVisibility(View.VISIBLE);
else
tabCounterNotif.setVisibility(View.GONE);
tabCounterNotif.setVisibility(View.VISIBLE);
}else {
tabCounterNotif.setVisibility(View.GONE);
}

View File

@ -40,12 +40,10 @@ public class RetrieveRelationshipAsyncTask extends AsyncTask<Void, Void, Void> {
this.context = context;
this.listener = onRetrieveRelationshipInterface;
this.accountId = accountId;
Log.v(Helper.TAG,"RetrieveRelationshipAsyncTask");
}
@Override
protected Void doInBackground(Void... params) {
Log.v(Helper.TAG,"doInBackground");
api = new API(context);
relationship = api.getRelationship(accountId);
return null;

View File

@ -33,6 +33,7 @@ import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import fr.gouv.etalab.mastodon.activities.MainActivity;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.drawers.NotificationsListAdapter;
@ -208,6 +209,8 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
else
textviewNoAction.setVisibility(View.GONE);
if( swiped ){
Helper.clearUnreadNotifications(context,null);
((MainActivity) context).updateNotifCounter();
boolean isOnWifi = Helper.isOnWIFI(context);
int behaviorWithAttachments = sharedpreferences.getInt(Helper.SET_ATTACHMENT_ACTION, Helper.ATTACHMENT_ALWAYS);
notificationsListAdapter = new NotificationsListAdapter(context,isOnWifi, behaviorWithAttachments, this.notifications);
@ -256,11 +259,8 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
}
public void refresh(Notification notification){
Log.v(Helper.TAG,"notification: " + notification);
if( notification != null){
Log.v(Helper.TAG,"notificationsTmp: " + notificationsTmp);
if( notificationsTmp != null && notificationsTmp.size() > 0){
Log.v(Helper.TAG,"size: " + notificationsTmp.size());
notificationsTmp.add(0,notification);
}else {
notificationsTmp = new ArrayList<>();

View File

@ -36,6 +36,8 @@ import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import fr.gouv.etalab.mastodon.activities.MainActivity;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveRepliesAsyncTask;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.client.Entities.Account;
@ -311,6 +313,10 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
else
textviewNoAction.setVisibility(View.GONE);
if( swiped ){
if( type == RetrieveFeedsAsyncTask.Type.HOME ) {
Helper.clearUnreadToots(context,null);
((MainActivity) context).updateHomeCounter();
}
statusListAdapter = new StatusListAdapter(context, type, targetedId, isOnWifi, behaviorWithAttachments, positionSpinnerTrans, this.statuses);
lv_status.setAdapter(statusListAdapter);
swiped = false;
@ -361,11 +367,8 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
}
public void refresh(Status status){
Log.v(Helper.TAG,"status: " + status);
if( status != null){
Log.v(Helper.TAG,"statusesTmp: " + statusesTmp);
if( statusesTmp != null && statusesTmp.size() > 0){
Log.v(Helper.TAG,"size: " + statusesTmp.size());
statusesTmp.add(0,status);
}else {
statusesTmp = new ArrayList<>();

View File

@ -321,7 +321,6 @@ public class StreamingService extends Service {
//No previous notifications in cache, so no notification will be sent
boolean notify = false;
String notificationUrl = null;
String title = null;
Status status = null;
@ -329,7 +328,6 @@ public class StreamingService extends Service {
String dataId = null;
if( event == EventStreaming.NOTIFICATION){
notification = API.parseNotificationResponse(getApplicationContext(), response);
max_id_notif = notification.getId();
switch (notification.getType()){
case "mention":
if(notif_mention){
@ -376,9 +374,6 @@ public class StreamingService extends Service {
break;
}
Helper.increaseUnreadNotifications(getApplicationContext(), userId);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + userId, notification.getId());
editor.apply();
if( notification.getStatus().getContent()!= null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
message = Html.fromHtml(notification.getStatus().getContent(), Html.FROM_HTML_MODE_LEGACY).toString();
@ -394,7 +389,7 @@ public class StreamingService extends Service {
}else if ( event == EventStreaming.UPDATE){
status = API.parseStatuses(getApplicationContext(), response);
status.setReplies(new ArrayList<Status>()); //Force to don't display replies.
max_id_home = status.getId();
Helper.increaseUnreadToots(getApplicationContext(), userId);
if( status.getContent() != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
@ -412,21 +407,9 @@ public class StreamingService extends Service {
}else if( event == EventStreaming.DELETE){
try {
dataId = response.getString("id");
} catch (JSONException e) {
e.printStackTrace();
}
}
if( max_id_notif != null){
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + userId, max_id_notif);
editor.apply();
}
if( max_id_home != null){
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_HOMETIMELINE_MAX_ID + userId, max_id_home);
editor.apply();
}
//Check which user is connected and if activity is to front
@ -434,13 +417,10 @@ public class StreamingService extends Service {
try{
activityVisible = MainActivity.isActivityVisible();
}catch (Exception ignored){}
String userconnected = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
String connectedUser = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
Account account = new AccountDAO(getApplicationContext(), db).getAccountByID(userconnected);
//User receiving the notification is connected and application is to front, notification won't be pushed
//Instead, the interaction is done in the activity
Account account = new AccountDAO(getApplicationContext(), db).getAccountByID(connectedUser);
//User receiving the notification is connected
if( isCurrentAccountLoggedIn(acct, userId)){
notify = false;
Intent intentBC = new Intent(Helper.RECEIVE_DATA);
@ -455,12 +435,12 @@ public class StreamingService extends Service {
intentBC.putExtras(b);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intentBC);
}
//User receiving the notification is connected and application is to front, notification won't be pushed
//Instead, the interaction is done in the activity
if( activityVisible && isCurrentAccountLoggedIn(acct, userId)){
notify = false;
}else if(event == EventStreaming.NOTIFICATION ){
notify = true;
}else if(event == EventStreaming.UPDATE ){
//lastPreviousContent contains the content of the last notification, if it was a mention it will avoid to push two notifications
if( account == null || (lastPreviousContent != null && lastPreviousContent.equals(status.getContent()))) {

View File

@ -148,11 +148,8 @@ public class MainActivity extends AppCompatActivity
if(notificationsFragment.getUserVisibleHint() && isActivityVisible()){
notificationsFragment.updateData(notification);
}else{
updateNotifCounter();
notificationsFragment.refresh(notification);
}
}else {
updateNotifCounter();
}
}else if(eventStreaming == StreamingService.EventStreaming.UPDATE){
Status status = b.getParcelable("data");
@ -160,11 +157,8 @@ public class MainActivity extends AppCompatActivity
if(homeFragment.getUserVisibleHint() && isActivityVisible()){
homeFragment.updateData(status);
}else{
updateHomeCounter();
homeFragment.refresh(status);
}
}else{
updateHomeCounter();
}
}else if(eventStreaming == StreamingService.EventStreaming.DELETE){
String id = b.getString("id");
@ -176,6 +170,8 @@ public class MainActivity extends AppCompatActivity
}
}
}
updateNotifCounter();
updateHomeCounter();
}
};
LocalBroadcastManager.getInstance(this).registerReceiver(receive_data, new IntentFilter(Helper.RECEIVE_DATA));
@ -291,7 +287,6 @@ public class MainActivity extends AppCompatActivity
Helper.clearUnreadToots(getApplicationContext(), null);
updateHomeCounter();
}else if( tab.getPosition() == 1) {
fragmentTag = "NOTIFICATIONS";
item = navigationView.getMenu().findItem(R.id.nav_notification);
if (notificationsFragment != null && Helper.getUnreadNotifications(getApplicationContext(), null) > 0) {
@ -345,9 +340,15 @@ public class MainActivity extends AppCompatActivity
Fragment fragment = (Fragment) viewPager.getAdapter().instantiateItem(viewPager, tab.getPosition());
switch (tab.getPosition()){
case 0:
DisplayStatusFragment displayStatusFragment = ((DisplayStatusFragment) fragment);
if( displayStatusFragment != null )
displayStatusFragment.scrollToTop();
Helper.clearUnreadToots(getApplicationContext(), null);
updateHomeCounter();
break;
case 2:
case 3:
DisplayStatusFragment displayStatusFragment = ((DisplayStatusFragment) fragment);
displayStatusFragment = ((DisplayStatusFragment) fragment);
if( displayStatusFragment != null )
displayStatusFragment.scrollToTop();
break;
@ -355,6 +356,8 @@ public class MainActivity extends AppCompatActivity
DisplayNotificationsFragment displayNotificationsFragment = ((DisplayNotificationsFragment) fragment);
if( displayNotificationsFragment != null )
displayNotificationsFragment.scrollToTop();
Helper.clearUnreadNotifications(getApplicationContext(), null);
updateNotifCounter();
break;
}
}
@ -873,6 +876,8 @@ public class MainActivity extends AppCompatActivity
public void onResume(){
super.onResume();
MainActivity.activityResumed();
updateNotifCounter();
updateHomeCounter();
//Proceeds to update of the authenticated account
if(Helper.isLoggedIn(getApplicationContext()))
new UpdateAccountInfoByIDAsyncTask(getApplicationContext(), MainActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
@ -912,20 +917,25 @@ public class MainActivity extends AppCompatActivity
}
toolbarTitle.setText(item.getTitle());
if (id == R.id.nav_home) {
//noinspection ConstantConditions
tabLayout.getTabAt(0).select();
if( tabLayout.getSelectedTabPosition() != 0)
//noinspection ConstantConditions
tabLayout.getTabAt(0).select();
return true;
} else if( id == R.id.nav_notification){
//noinspection ConstantConditions
tabLayout.getTabAt(1).select();
if( tabLayout.getSelectedTabPosition() != 1)
//noinspection ConstantConditions
tabLayout.getTabAt(1).select();
return true;
}else if (id == R.id.nav_local) {
//noinspection ConstantConditions
tabLayout.getTabAt(2).select();
if( tabLayout.getSelectedTabPosition() != 2)
//noinspection ConstantConditions
tabLayout.getTabAt(2).select();
return true;
} else if (id == R.id.nav_global) {
//noinspection ConstantConditions
tabLayout.getTabAt(3).select();
if( tabLayout.getSelectedTabPosition() != 3)
//noinspection ConstantConditions
tabLayout.getTabAt(3).select();
return true;
}
DisplayStatusFragment statusFragment;
@ -1149,10 +1159,7 @@ public class MainActivity extends AppCompatActivity
if( Helper.getUnreadToots(getApplicationContext(), null) > 0){
//New data are available
//The fragment is not displayed, so the counter is displayed
if( tabLayout.getSelectedTabPosition() != 0)
tabCounterHome.setVisibility(View.VISIBLE);
else
tabCounterHome.setVisibility(View.GONE);
tabCounterHome.setVisibility(View.VISIBLE);
}else {
tabCounterHome.setVisibility(View.GONE);
}
@ -1168,10 +1175,7 @@ public class MainActivity extends AppCompatActivity
TextView tabCounterNotif = (TextView) tabNotif.findViewById(R.id.tab_counter);
tabCounterNotif.setText(String.valueOf(Helper.getUnreadNotifications(getApplicationContext(), null)));
if( Helper.getUnreadNotifications(getApplicationContext(), null) > 0){
if( tabLayout.getSelectedTabPosition() != 1)
tabCounterNotif.setVisibility(View.VISIBLE);
else
tabCounterNotif.setVisibility(View.GONE);
tabCounterNotif.setVisibility(View.VISIBLE);
}else {
tabCounterNotif.setVisibility(View.GONE);
}