Skip to content
24 changes: 14 additions & 10 deletions app/src/org/commcare/activities/PushNotificationActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.commcare.adapters.PushNotificationAdapter
import org.commcare.android.database.connect.models.PushNotificationRecord
import org.commcare.dalvik.R
import org.commcare.dalvik.databinding.ActivityPushNotificationBinding
import org.commcare.preferences.NotificationPrefs
import org.commcare.utils.FirebaseMessagingUtil.getIntentForPNClick

class PushNotificationActivity : AppCompatActivity() {
Expand All @@ -32,7 +33,7 @@ class PushNotificationActivity : AppCompatActivity() {
private fun observeRetrieveNotificationApi() {
pushNotificationViewModel.allNotifications.observe(this) { notifications ->
val isLoading = pushNotificationViewModel.isLoading.value == true

NotificationPrefs.setNotificationAsRead(this)
when {
notifications.isNotEmpty() -> {
pushNotificationAdapter.submitList(notifications)
Expand Down Expand Up @@ -62,17 +63,20 @@ class PushNotificationActivity : AppCompatActivity() {
setDisplayHomeAsUpEnabled(true)
}
pushNotificationViewModel = ViewModelProvider(
this, ViewModelProvider.AndroidViewModelFactory.getInstance(application)
this,
ViewModelProvider.AndroidViewModelFactory.getInstance(application)
)[PushNotificationViewModel::class.java]
pushNotificationAdapter = PushNotificationAdapter(listener = object :
PushNotificationAdapter.OnNotificationClickListener {
override fun onNotificationClick(notificationRecord: PushNotificationRecord) {
val activityIntent = getIntentForPNClick(application,notificationRecord)
if(activityIntent!=null) {
startActivity(activityIntent)
pushNotificationAdapter = PushNotificationAdapter(
listener = object :
PushNotificationAdapter.OnNotificationClickListener {
override fun onNotificationClick(notificationRecord: PushNotificationRecord) {
val activityIntent = getIntentForPNClick(application, notificationRecord)
if (activityIntent != null) {
startActivity(activityIntent)
}
}
}
})
)
binding.rvNotifications.adapter = pushNotificationAdapter
}

Expand All @@ -96,4 +100,4 @@ class PushNotificationActivity : AppCompatActivity() {
else -> super.onOptionsItemSelected(item)
}
}
}
}
26 changes: 24 additions & 2 deletions app/src/org/commcare/activities/connect/ConnectActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.commcare.connect.ConnectConstants.REDIRECT_ACTION;
import static org.commcare.connect.ConnectConstants.SHOW_LAUNCH_BUTTON;
import static org.commcare.personalId.PersonalIdFeatureFlagChecker.FeatureFlag.NOTIFICATIONS;
import static org.commcare.utils.NotificationUtil.getNotificationIcon;

import android.content.BroadcastReceiver;
import android.content.Context;
Expand Down Expand Up @@ -39,19 +40,24 @@
import org.commcare.dalvik.R;
import org.commcare.fragments.RefreshableFragment;
import org.commcare.personalId.PersonalIdFeatureFlagChecker;
import org.commcare.pn.helper.NotificationBroadcastHelper;
import org.commcare.utils.FirebaseMessagingUtil;
import org.commcare.views.dialogs.CustomProgressDialog;

import java.util.Objects;

import javax.annotation.Nullable;

import kotlin.Unit;

public class ConnectActivity extends NavigationHostCommCareActivity<ConnectActivity> {
private boolean backButtonAndActionBarEnabled = true;
private boolean waitDialogEnabled = true;
private String redirectionAction = "";
private ConnectJobRecord job;
private MenuItem messagingMenuItem = null;
private MenuItem notificationsMenuItem = null;

private static final int REQUEST_CODE_PERSONAL_ID_ACTIVITY = 1000;

@Override
Expand All @@ -63,6 +69,14 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
personalIdManager.init(this);

if(personalIdManager.isloggedIn()){
NotificationBroadcastHelper.INSTANCE.registerForNotifications(
this,
this,
() -> {
updateNotificationIcon();
return Unit.INSTANCE;
}
);
initStateFromExtras();
updateBackButton();

Expand Down Expand Up @@ -142,11 +156,18 @@ public boolean onCreateOptionsMenu(Menu menu) {
MenuItem notification = menu.findItem(R.id.action_sync);
notification.getIcon().setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_ATOP);

MenuItem notiificationsMenuItem = menu.findItem(R.id.action_bell);
notiificationsMenuItem.setVisible(PersonalIdFeatureFlagChecker.isFeatureEnabled(NOTIFICATIONS));
notificationsMenuItem = menu.findItem(R.id.action_bell);
notificationsMenuItem.setVisible(PersonalIdFeatureFlagChecker.isFeatureEnabled(NOTIFICATIONS));
updateNotificationIcon();

return super.onCreateOptionsMenu(menu);
}
private void updateNotificationIcon() {
if (notificationsMenuItem == null) return;

int iconRes = getNotificationIcon(this);
notificationsMenuItem.setIcon(iconRes);
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
Expand All @@ -169,6 +190,7 @@ private void retrieveMessages(){
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_bell) {
updateNotificationIcon();
ConnectNavHelper.goToNotification(this);
return true;
}
Expand Down
54 changes: 34 additions & 20 deletions app/src/org/commcare/navdrawer/BaseDrawerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import androidx.localbroadcastmanager.content.LocalBroadcastManager
Expand All @@ -13,8 +14,8 @@ import org.commcare.connect.ConnectNavHelper.unlockAndGoToConnectJobsList
import org.commcare.connect.ConnectNavHelper.unlockAndGoToCredentials
import org.commcare.connect.ConnectNavHelper.unlockAndGoToMessaging
import org.commcare.navdrawer.BaseDrawerController.NavItemType
import org.commcare.pn.helper.NotificationBroadcastHelper
import org.commcare.utils.FirebaseMessagingUtil
import android.os.Bundle

abstract class BaseDrawerActivity<T> : CommCareActivity<T>() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jaypanchal-13 Need to listen in ConnectActivity also to update the bell icon


Expand All @@ -23,6 +24,11 @@ abstract class BaseDrawerActivity<T> : CommCareActivity<T>() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
checkForDrawerSetUp()
if (drawerController != null) {
NotificationBroadcastHelper.registerForNotifications(this, this) {
drawerController?.refreshDrawerContent()
}
}
}
override fun onResume() {
super.onResume()
Expand Down Expand Up @@ -55,7 +61,7 @@ abstract class BaseDrawerActivity<T> : CommCareActivity<T>() {
return false
}

fun checkForDrawerSetUp(){
fun checkForDrawerSetUp() {
if (shouldShowDrawer()) {
setupDrawerController()
}
Expand All @@ -77,7 +83,7 @@ abstract class BaseDrawerActivity<T> : CommCareActivity<T>() {
protected open fun handleDrawerItemClick(itemType: NavItemType, recordId: String?) {
when (itemType) {
NavItemType.OPPORTUNITIES -> { navigateToConnectMenu() }
NavItemType.COMMCARE_APPS -> { /* No nav, expands/collapses menu */}
NavItemType.COMMCARE_APPS -> { /* No nav, expands/collapses menu */ }
NavItemType.PAYMENTS -> {}
NavItemType.MESSAGING -> { navigateToMessaging() }
NavItemType.WORK_HISTORY -> {}
Expand All @@ -94,37 +100,45 @@ abstract class BaseDrawerActivity<T> : CommCareActivity<T>() {
}

protected fun navigateToConnectMenu() {
unlockAndGoToConnectJobsList(this, object : ConnectActivityCompleteListener {
override fun connectActivityComplete(success: Boolean) {
if (success) {
closeDrawer()
unlockAndGoToConnectJobsList(
this,
object : ConnectActivityCompleteListener {
override fun connectActivityComplete(success: Boolean) {
if (success) {
closeDrawer()
}
}
}
})
)
}

protected fun navigateToMessaging() {
unlockAndGoToMessaging(this, object : ConnectActivityCompleteListener {
override fun connectActivityComplete(success: Boolean) {
if (success) {
closeDrawer()
unlockAndGoToMessaging(
this,
object : ConnectActivityCompleteListener {
override fun connectActivityComplete(success: Boolean) {
if (success) {
closeDrawer()
}
}
}
})
)
}

protected fun navigateToCredential() {
unlockAndGoToCredentials(this, object : ConnectActivityCompleteListener {
override fun connectActivityComplete(success: Boolean) {
if (success) {
closeDrawer()
protected fun navigateToCredential() {
unlockAndGoToCredentials(
this,
object : ConnectActivityCompleteListener {
override fun connectActivityComplete(success: Boolean) {
if (success) {
closeDrawer()
}
}
}
})
)
}

protected fun closeDrawer() {
drawerController?.closeDrawer()
}
}

36 changes: 21 additions & 15 deletions app/src/org/commcare/navdrawer/BaseDrawerController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import org.commcare.connect.ConnectNavHelper
import org.commcare.connect.PersonalIdManager
import org.commcare.connect.database.ConnectMessagingDatabaseHelper
import org.commcare.connect.database.ConnectUserDatabaseUtil
import org.commcare.connect.database.NotificationRecordDatabaseHelper
import org.commcare.dalvik.BuildConfig
import org.commcare.dalvik.R
import org.commcare.google.services.analytics.FirebaseAnalyticsUtil
import org.commcare.personalId.PersonalIdFeatureFlagChecker.Companion.isFeatureEnabled
import org.commcare.personalId.PersonalIdFeatureFlagChecker.FeatureFlag.Companion.NOTIFICATIONS
import org.commcare.personalId.PersonalIdFeatureFlagChecker.FeatureFlag.Companion.WORK_HISTORY
import org.commcare.utils.MultipleAppsUtil
import org.commcare.utils.NotificationUtil.getNotificationIcon
import org.commcare.views.ViewUtil
import org.commcare.views.dialogs.DialogCreationHelpers

Expand All @@ -44,7 +44,7 @@ class BaseDrawerController(
WORK_HISTORY,
MESSAGING,
PAYMENTS,
CREDENTIAL,
CREDENTIAL
}

fun setupDrawer() {
Expand Down Expand Up @@ -131,8 +131,8 @@ class BaseDrawerController(
fun refreshDrawerContent() {
if (PersonalIdManager.getInstance().isloggedIn()) {
setSignedInState(true)
binding.ivNotification.setImageResource(R.drawable.ic_bell)
binding.ivNotification.setImageResource(getNotificationIcon(activity))

val user = ConnectUserDatabaseUtil.getUser(activity)
binding.userName.text = user.name
Glide.with(binding.imageUserProfile)
Expand All @@ -145,12 +145,17 @@ class BaseDrawerController(

val appRecords = MultipleAppsUtil.getUsableAppRecords()

val seatedApp = if (highlightSeatedApp && appRecords.count() > 1)
CommCareApplication.instance().currentApp.uniqueId else null
val seatedApp = if (highlightSeatedApp && appRecords.count() > 1) {
CommCareApplication.instance().currentApp.uniqueId
} else {
null
}

val commcareApps = appRecords.map {
NavDrawerItem.ChildItem(
it.displayName, it.uniqueId, NavItemType.COMMCARE_APPS,
it.displayName,
it.uniqueId,
NavItemType.COMMCARE_APPS,
it.uniqueId == seatedApp
)
}
Expand All @@ -163,7 +168,7 @@ class BaseDrawerController(
NavDrawerItem.ParentItem(
activity.getString(R.string.nav_drawer_opportunities),
R.drawable.nav_drawer_opportunity_icon,
NavItemType.OPPORTUNITIES,
NavItemType.OPPORTUNITIES
)
)
}
Expand All @@ -181,15 +186,17 @@ class BaseDrawerController(

if (ConnectMessagingDatabaseHelper.getMessagingChannels(activity).isNotEmpty()) {
val iconId =
if (ConnectMessagingDatabaseHelper.getUnviewedMessages(activity).isNotEmpty())
if (ConnectMessagingDatabaseHelper.getUnviewedMessages(activity).isNotEmpty()) {
R.drawable.nav_drawer_message_unread_icon
else R.drawable.nav_drawer_message_icon
} else {
R.drawable.nav_drawer_message_icon
}

items.add(
NavDrawerItem.ParentItem(
activity.getString(R.string.connect_messaging_title),
iconId,
NavItemType.MESSAGING,
NavItemType.MESSAGING
)
)
}
Expand All @@ -199,7 +206,7 @@ class BaseDrawerController(
NavDrawerItem.ParentItem(
activity.getString(R.string.personalid_work_history),
R.drawable.ic_credential,
NavItemType.CREDENTIAL,
NavItemType.CREDENTIAL
)
)
}
Expand Down Expand Up @@ -229,11 +236,11 @@ class BaseDrawerController(

private fun shouldShowCredential(): Boolean {
// we are keeping this off for now until we have go ahead to release this feature
return PersonalIdManager.getInstance().isloggedIn() && isFeatureEnabled(WORK_HISTORY);
return PersonalIdManager.getInstance().isloggedIn() && isFeatureEnabled(WORK_HISTORY)
}

private fun shouldShowNotiifcations(): Boolean {
return PersonalIdManager.getInstance().isloggedIn() && isFeatureEnabled(NOTIFICATIONS);
return PersonalIdManager.getInstance().isloggedIn() && isFeatureEnabled(NOTIFICATIONS)
}

fun closeDrawer() {
Expand All @@ -243,5 +250,4 @@ class BaseDrawerController(
fun handleOptionsItem(item: MenuItem): Boolean {
return drawerToggle.onOptionsItemSelected(item)
}

}
42 changes: 42 additions & 0 deletions app/src/org/commcare/pn/helper/NotificationBroadcastHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.commcare.pn.helper

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.localbroadcastmanager.content.LocalBroadcastManager
object NotificationBroadcastHelper {
const val ACTION_NEW_NOTIFICATIONS = "org.commcare.dalvik.action.NEW_NOTIFICATION"

fun registerForNotifications(
context: Context,
owner: LifecycleOwner,
onNewNotification: () -> Unit
) {
val receiver = object : BroadcastReceiver() {
override fun onReceive(c: Context?, i: Intent?) {
onNewNotification()
}
}

val manager = LocalBroadcastManager.getInstance(context)

owner.lifecycle.addObserver(object : DefaultLifecycleObserver {
override fun onResume(owner: LifecycleOwner) {
manager.registerReceiver(receiver, IntentFilter(ACTION_NEW_NOTIFICATIONS))
onNewNotification()
}

override fun onPause(owner: LifecycleOwner) {
manager.unregisterReceiver(receiver)
}
})
}

fun sendNewNotificationBroadcast(context: Context) {
val intent = Intent(ACTION_NEW_NOTIFICATIONS)
LocalBroadcastManager.getInstance(context).sendBroadcast(intent)
}
}
Loading
Loading