mirror of
https://github.com/sudoxnym/habitica-android.git
synced 2026-07-14 10:11:58 +00:00
Refactor NotificationsManager
This commit is contained in:
parent
f1939123be
commit
21d29b51e5
4 changed files with 37 additions and 47 deletions
|
|
@ -337,7 +337,7 @@ class ApiClientImpl//private OnHabitsAPIResult mResultListener;
|
|||
override fun validateSubscription(request: SubscriptionValidationRequest): Flowable<Any> {
|
||||
return apiService.validateSubscription(request).map { habitResponse ->
|
||||
if (habitResponse.notifications != null) {
|
||||
notificationsManager.showNotificationDialog(habitResponse.notifications)
|
||||
notificationsManager.setNotifications(habitResponse.notifications)
|
||||
}
|
||||
habitResponse.getData()
|
||||
}
|
||||
|
|
@ -346,7 +346,7 @@ class ApiClientImpl//private OnHabitsAPIResult mResultListener;
|
|||
override fun validateNoRenewSubscription(request: PurchaseValidationRequest): Flowable<Any> {
|
||||
return apiService.validateNoRenewSubscription(request).map { habitResponse ->
|
||||
if (habitResponse.notifications != null) {
|
||||
notificationsManager.showNotificationDialog(habitResponse.notifications)
|
||||
notificationsManager.setNotifications(habitResponse.notifications)
|
||||
}
|
||||
habitResponse.getData()
|
||||
}
|
||||
|
|
@ -571,7 +571,7 @@ class ApiClientImpl//private OnHabitsAPIResult mResultListener;
|
|||
override fun validatePurchase(request: PurchaseValidationRequest): Flowable<PurchaseValidationResult> {
|
||||
return apiService.validatePurchase(request).map { habitResponse ->
|
||||
if (habitResponse.notifications != null) {
|
||||
notificationsManager.showNotificationDialog(habitResponse.notifications)
|
||||
notificationsManager.setNotifications(habitResponse.notifications)
|
||||
}
|
||||
habitResponse.getData()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,7 +12,6 @@ import com.habitrpg.android.habitica.events.ShowSnackbarEvent
|
|||
import com.habitrpg.android.habitica.models.Notification
|
||||
import com.habitrpg.android.habitica.models.notifications.LoginIncentiveData
|
||||
import com.habitrpg.android.habitica.ui.views.HabiticaSnackbar
|
||||
import com.playseeds.android.sdk.inappmessaging.Log
|
||||
import io.reactivex.functions.Consumer
|
||||
|
||||
import org.greenrobot.eventbus.EventBus
|
||||
|
|
@ -26,7 +25,7 @@ import java.util.HashMap
|
|||
class NotificationsManager (private val context: Context) {
|
||||
// @TODO: A queue for displaying alert dialogues
|
||||
|
||||
private var seenNotifications: MutableMap<String, Boolean>? = null
|
||||
private val seenNotifications: MutableMap<String, Boolean>
|
||||
private var apiClient: ApiClient? = null
|
||||
|
||||
private val notifications: BehaviorSubject<List<Notification>>
|
||||
|
|
@ -38,9 +37,8 @@ class NotificationsManager (private val context: Context) {
|
|||
|
||||
fun setNotifications(current: List<Notification>) {
|
||||
this.notifications.onNext(current)
|
||||
current.map { Log.d("NotificationsManager.setNotifications." + it.type) }
|
||||
|
||||
this.showNotificationDialog(current)
|
||||
this.handlePopupNotifications(current)
|
||||
}
|
||||
|
||||
fun getNotifications(): Flowable<List<Notification>> {
|
||||
|
|
@ -51,7 +49,24 @@ current.map { Log.d("NotificationsManager.setNotifications." + it.type) }
|
|||
this.apiClient = apiClient
|
||||
}
|
||||
|
||||
fun displayNotification(notification: Notification): Boolean? {
|
||||
fun handlePopupNotifications(notifications: List<Notification>): Boolean? {
|
||||
notifications
|
||||
.filter { !this.seenNotifications.containsKey(it.id) }
|
||||
.map {
|
||||
val notificationDisplayed = when (it.type) {
|
||||
Notification.Type.LOGIN_INCENTIVE.type -> displayLoginIncentiveNotification(it)
|
||||
else -> false
|
||||
}
|
||||
|
||||
if (notificationDisplayed == true) {
|
||||
this.seenNotifications[it.id] = true
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
fun displayLoginIncentiveNotification(notification: Notification): Boolean? {
|
||||
val notificationData = notification.data as LoginIncentiveData?
|
||||
val nextUnlockText = context.getString(R.string.nextPrizeUnlocks, notificationData!!.nextRewardAt)
|
||||
if (notificationData.rewardKey != null) {
|
||||
|
|
@ -73,29 +88,4 @@ current.map { Log.d("NotificationsManager.setNotifications." + it.type) }
|
|||
}
|
||||
return true
|
||||
}
|
||||
|
||||
fun showNotificationDialog(notifications: List<Notification>?): Boolean? {
|
||||
if (notifications == null || notifications.size == 0) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (this.seenNotifications == null) {
|
||||
this.seenNotifications = HashMap()
|
||||
}
|
||||
|
||||
for (notification in notifications) {
|
||||
if (this.seenNotifications!![notification.id] != null) {
|
||||
continue
|
||||
}
|
||||
|
||||
if (notification.type != "LOGIN_INCENTIVE") {
|
||||
continue
|
||||
}
|
||||
|
||||
this.displayNotification(notification)
|
||||
this.seenNotifications!![notification.id] = true
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,4 +27,4 @@ class Notification {
|
|||
else -> null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ public class PopupNotificationsManagerTest {
|
|||
@Test
|
||||
public void itDoesNothingWhenNotificationsListIsEmpty() {
|
||||
List<Notification> notifications = new ArrayList<>();
|
||||
notificationsManager.showNotificationDialog(notifications);
|
||||
notificationsManager.handlePopupNotifications(notifications);
|
||||
|
||||
AlertDialog alert =
|
||||
ShadowAlertDialog.getLatestAlertDialog();
|
||||
|
|
@ -68,12 +68,12 @@ public class PopupNotificationsManagerTest {
|
|||
notifications.add(notification);
|
||||
|
||||
final NotificationsManager testClass = Mockito.mock(NotificationsManager.class);
|
||||
Mockito.when(testClass.displayNotification(notification)).thenReturn(true);
|
||||
Mockito.when(testClass.showNotificationDialog(notifications)).thenCallRealMethod();
|
||||
Mockito.when(testClass.displayLoginIncentiveNotification(notification)).thenReturn(true);
|
||||
Mockito.when(testClass.handlePopupNotifications(notifications)).thenCallRealMethod();
|
||||
|
||||
testClass.showNotificationDialog(notifications);
|
||||
testClass.handlePopupNotifications(notifications);
|
||||
|
||||
verify(testClass, times(0)).displayNotification(notification);
|
||||
verify(testClass, times(0)).displayLoginIncentiveNotification(notification);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -92,12 +92,12 @@ public class PopupNotificationsManagerTest {
|
|||
notifications.add(notification);
|
||||
|
||||
final NotificationsManager testClass = Mockito.mock(NotificationsManager.class);
|
||||
Mockito.when(testClass.displayNotification(notification)).thenReturn(true);
|
||||
Mockito.when(testClass.showNotificationDialog(notifications)).thenCallRealMethod();
|
||||
Mockito.when(testClass.displayLoginIncentiveNotification(notification)).thenReturn(true);
|
||||
Mockito.when(testClass.handlePopupNotifications(notifications)).thenCallRealMethod();
|
||||
|
||||
testClass.showNotificationDialog(notifications);
|
||||
testClass.handlePopupNotifications(notifications);
|
||||
|
||||
verify(testClass, times(1)).displayNotification(notification);
|
||||
verify(testClass, times(1)).displayLoginIncentiveNotification(notification);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -117,11 +117,11 @@ public class PopupNotificationsManagerTest {
|
|||
notifications.add(notification);
|
||||
|
||||
final NotificationsManager testClass = Mockito.mock(NotificationsManager.class);
|
||||
Mockito.when(testClass.displayNotification(notification)).thenReturn(true);
|
||||
Mockito.when(testClass.showNotificationDialog(notifications)).thenCallRealMethod();
|
||||
Mockito.when(testClass.displayLoginIncentiveNotification(notification)).thenReturn(true);
|
||||
Mockito.when(testClass.handlePopupNotifications(notifications)).thenCallRealMethod();
|
||||
|
||||
testClass.showNotificationDialog(notifications);
|
||||
testClass.handlePopupNotifications(notifications);
|
||||
|
||||
verify(testClass, times(1)).displayNotification(notification);
|
||||
verify(testClass, times(1)).displayLoginIncentiveNotification(notification);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue