1
1
package fr .free .nrw .commons .contributions ;
2
2
3
+ import android .annotation .SuppressLint ;
3
4
import android .app .AlertDialog ;
4
5
import android .content .Intent ;
5
6
import android .content .pm .PackageManager ;
8
9
import android .support .v4 .app .Fragment ;
9
10
import android .support .v4 .app .FragmentManager ;
10
11
import android .support .v4 .app .FragmentPagerAdapter ;
11
- import android .support .v4 .content .ContextCompat ;
12
12
import android .support .v4 .view .ViewPager ;
13
13
import android .view .LayoutInflater ;
14
14
import android .view .Menu ;
15
15
import android .view .MenuInflater ;
16
16
import android .view .MenuItem ;
17
17
import android .view .View ;
18
18
import android .widget .ImageView ;
19
+ import android .widget .TextView ;
19
20
20
21
import com .esafirm .imagepicker .features .ImagePicker ;
21
22
import com .esafirm .imagepicker .model .Image ;
35
36
import fr .free .nrw .commons .location .LocationServiceManager ;
36
37
import fr .free .nrw .commons .nearby .NearbyFragment ;
37
38
import fr .free .nrw .commons .nearby .NearbyNotificationCardView ;
39
+ import fr .free .nrw .commons .notification .Notification ;
38
40
import fr .free .nrw .commons .notification .NotificationActivity ;
41
+ import fr .free .nrw .commons .notification .NotificationController ;
39
42
import fr .free .nrw .commons .upload .UploadService ;
40
43
import fr .free .nrw .commons .utils .ImageUtils ;
41
44
import fr .free .nrw .commons .utils .IntentUtils ;
45
+ import io .reactivex .Observable ;
46
+ import io .reactivex .android .schedulers .AndroidSchedulers ;
47
+ import io .reactivex .schedulers .Schedulers ;
42
48
import timber .log .Timber ;
43
49
44
50
import static android .content .ContentResolver .requestSync ;
@@ -58,6 +64,8 @@ public class MainActivity extends AuthenticatedActivity implements FragmentManag
58
64
@ Inject
59
65
@ Named ("default_preferences" )
60
66
public BasicKvStore defaultKvStore ;
67
+ @ Inject
68
+ NotificationController notificationController ;
61
69
62
70
63
71
public Intent uploadServiceIntent ;
@@ -69,10 +77,12 @@ public class MainActivity extends AuthenticatedActivity implements FragmentManag
69
77
70
78
public boolean isContributionsFragmentVisible = true ; // False means nearby fragment is visible
71
79
private Menu menu ;
72
- private boolean isThereUnreadNotifications = false ;
73
80
74
81
private boolean onOrientationChanged = false ;
75
82
83
+ private MenuItem notificationsMenuItem ;
84
+ private TextView notificationCount ;
85
+
76
86
public void onCreate (Bundle savedInstanceState ) {
77
87
super .onCreate (savedInstanceState );
78
88
setContentView (R .layout .activity_contributions );
@@ -82,6 +92,7 @@ public void onCreate(Bundle savedInstanceState) {
82
92
initDrawer ();
83
93
setTitle (getString (R .string .navigation_item_home )); // Should I create a new string variable with another name instead?
84
94
95
+
85
96
if (savedInstanceState != null ) {
86
97
onOrientationChanged = true ; // Will be used in nearby fragment to determine significant update of map
87
98
@@ -126,13 +137,11 @@ private void addTabsAndFragments() {
126
137
tabLayout .getTabAt (1 ).setCustomView (nearbyTabLinearLayout );
127
138
128
139
nearbyInfo .setOnClickListener (view ->
129
- new AlertDialog .Builder (MainActivity .this )
130
- .setTitle (R .string .title_activity_nearby )
131
- .setMessage (R .string .showcase_view_whole_nearby_activity )
132
- .setCancelable (true )
133
- .setPositiveButton (android .R .string .ok , (dialog , id ) -> dialog .cancel ())
134
- .create ()
135
- .show ()
140
+ new AlertDialog .Builder (MainActivity .this ).setTitle (R .string .title_activity_nearby ).setMessage (R .string .showcase_view_whole_nearby_activity )
141
+ .setCancelable (true )
142
+ .setPositiveButton (android .R .string .ok , (dialog , id ) -> dialog .cancel ())
143
+ .create ()
144
+ .show ()
136
145
);
137
146
138
147
if (uploadServiceIntent != null ) {
@@ -278,20 +287,35 @@ public boolean onCreateOptionsMenu(Menu menu) {
278
287
MenuInflater inflater = getMenuInflater ();
279
288
inflater .inflate (R .menu .contribution_activity_notification_menu , menu );
280
289
281
- if (!isThereUnreadNotifications ) {
282
- // TODO: used vectors are not compatible with API 19 and below, change them
283
- menu .findItem (R .id .notifications ).setIcon (ContextCompat .getDrawable (this , R .drawable .ic_notification_white_clip_art ));
284
- } else {
285
- menu .findItem (R .id .notifications ).setIcon (ContextCompat .getDrawable (this , R .drawable .ic_notification_white_clip_art_dot ));
286
- }
287
-
290
+ notificationsMenuItem = menu .findItem (R .id .notifications );
291
+ final View notification = notificationsMenuItem .getActionView ();
292
+ notificationCount = notification .findViewById (R .id .notification_count_badge );
293
+ notification .setOnClickListener (view -> NotificationActivity .startYourself (MainActivity .this ));
288
294
this .menu = menu ;
289
-
290
295
updateMenuItem ();
291
-
296
+ setNotificationCount ();
292
297
return true ;
293
298
}
294
299
300
+ @ SuppressLint ("CheckResult" )
301
+ private void setNotificationCount () {
302
+ Observable .fromCallable (() -> notificationController .getNotifications ())
303
+ .subscribeOn (Schedulers .io ())
304
+ .observeOn (AndroidSchedulers .mainThread ())
305
+ .subscribe (this ::initNotificationViews ,
306
+ throwable -> Timber .e (throwable , "Error occurred while loading notifications" ));
307
+ }
308
+
309
+ private void initNotificationViews (List <Notification > notificationList ) {
310
+ Timber .d ("Number of notifications is %d" , notificationList .size ());
311
+ if (notificationList .isEmpty ()) {
312
+ notificationCount .setVisibility (View .GONE );
313
+ } else {
314
+ notificationCount .setVisibility (View .VISIBLE );
315
+ notificationCount .setText (String .valueOf (notificationList .size ()));
316
+ }
317
+ }
318
+
295
319
/**
296
320
* Responsible with displaying required menu items according to displayed fragment.
297
321
* Notifications icon when contributions list is visible, list sheet icon when nearby is visible
@@ -319,7 +343,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
319
343
// Starts notification activity on click to notification icon
320
344
NotificationActivity .startYourself (this );
321
345
return true ;
322
- case R .id .list_sheet :
346
+ case R .id .list_sheet :NotificationActivity . startYourself ( this );
323
347
if (contributionsActivityPagerAdapter .getItem (1 ) != null ) {
324
348
((NearbyFragment )contributionsActivityPagerAdapter .getItem (1 )).listOptionMenuIteClicked ();
325
349
}
@@ -335,21 +359,6 @@ private boolean deviceHasCamera() {
335
359
pm .hasSystemFeature (PackageManager .FEATURE_CAMERA_FRONT );
336
360
}
337
361
338
- /**
339
- * Update notification icon if there is an unread notification
340
- * @param isThereUnreadNotifications true if user didn't visit notifications activity since
341
- * latest notification came to account
342
- */
343
- public void updateNotificationIcon (boolean isThereUnreadNotifications ) {
344
- if (!isThereUnreadNotifications ) {
345
- this .isThereUnreadNotifications = false ;
346
- menu .findItem (R .id .notifications ).setIcon (ContextCompat .getDrawable (this , R .drawable .ic_notification_white_clip_art ));
347
- } else {
348
- this .isThereUnreadNotifications = true ;
349
- menu .findItem (R .id .notifications ).setIcon (ContextCompat .getDrawable (this , R .drawable .ic_notification_white_clip_art_dot ));
350
- }
351
- }
352
-
353
362
public class ContributionsActivityPagerAdapter extends FragmentPagerAdapter {
354
363
FragmentManager fragmentManager ;
355
364
private boolean isContributionsListFragment = true ; // to know what to put in first tab, Contributions of Media Details
@@ -471,7 +480,7 @@ public void onRequestPermissionsResult(int requestCode,
471
480
if (!isContributionsFragmentVisible ) {
472
481
viewPager .setCurrentItem (CONTRIBUTIONS_TAB_POSITION );
473
482
474
- // TODO: If contrib fragment is visible and location permission is not given, display permission request button
483
+ // TODO: If contrib fragment is visible and location permission is not given, display permission request button
475
484
} else {
476
485
477
486
}
0 commit comments