mirror of
https://github.com/sudoxnym/habitica-android.git
synced 2026-07-14 18:21:57 +00:00
Rename PopupNotificationsManager to NotificationsManager
This commit is contained in:
parent
a78897cda7
commit
c5b5aac774
6 changed files with 129 additions and 136 deletions
|
|
@ -14,7 +14,7 @@ import com.habitrpg.android.habitica.api.HostConfig
|
|||
import com.habitrpg.android.habitica.api.Server
|
||||
import com.habitrpg.android.habitica.data.ApiClient
|
||||
import com.habitrpg.android.habitica.events.ShowConnectionProblemEvent
|
||||
import com.habitrpg.android.habitica.helpers.PopupNotificationsManager
|
||||
import com.habitrpg.android.habitica.helpers.NotificationsManager
|
||||
import com.habitrpg.android.habitica.models.*
|
||||
import com.habitrpg.android.habitica.models.auth.UserAuth
|
||||
import com.habitrpg.android.habitica.models.auth.UserAuthResponse
|
||||
|
|
@ -61,7 +61,7 @@ import javax.net.ssl.SSLException
|
|||
|
||||
class ApiClientImpl//private OnHabitsAPIResult mResultListener;
|
||||
//private HostConfig mConfig;
|
||||
(private val gsonConverter: GsonConverterFactory, override val hostConfig: HostConfig, private val crashlyticsProxy: CrashlyticsProxy, private val popupNotificationsManager: PopupNotificationsManager, private val context: Context) : Consumer<Throwable>, ApiClient {
|
||||
(private val gsonConverter: GsonConverterFactory, override val hostConfig: HostConfig, private val crashlyticsProxy: CrashlyticsProxy, private val notificationsManager: NotificationsManager, private val context: Context) : Consumer<Throwable>, ApiClient {
|
||||
|
||||
|
||||
private lateinit var retrofitAdapter: Retrofit
|
||||
|
|
@ -74,7 +74,7 @@ class ApiClientImpl//private OnHabitsAPIResult mResultListener;
|
|||
.filter { it.data != null }
|
||||
.map { habitResponse ->
|
||||
if (habitResponse.notifications != null) {
|
||||
popupNotificationsManager.setNotifications(habitResponse.notifications)
|
||||
notificationsManager.setNotifications(habitResponse.notifications)
|
||||
}
|
||||
habitResponse.data
|
||||
}
|
||||
|
|
@ -87,7 +87,7 @@ class ApiClientImpl//private OnHabitsAPIResult mResultListener;
|
|||
private var lastAPICallURL: String? = null
|
||||
|
||||
init {
|
||||
this.popupNotificationsManager.setApiClient(this)
|
||||
this.notificationsManager.setApiClient(this)
|
||||
|
||||
HabiticaBaseApplication.component?.inject(this)
|
||||
crashlyticsProxy.setUserIdentifier(this.hostConfig.user)
|
||||
|
|
@ -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) {
|
||||
popupNotificationsManager.showNotificationDialog(habitResponse.notifications)
|
||||
notificationsManager.showNotificationDialog(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) {
|
||||
popupNotificationsManager.showNotificationDialog(habitResponse.notifications)
|
||||
notificationsManager.showNotificationDialog(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) {
|
||||
popupNotificationsManager.showNotificationDialog(habitResponse.notifications)
|
||||
notificationsManager.showNotificationDialog(habitResponse.notifications)
|
||||
}
|
||||
habitResponse.getData()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,104 +1,104 @@
|
|||
package com.habitrpg.android.habitica.helpers;
|
||||
|
||||
import android.content.Context;
|
||||
import androidx.annotation.Nullable;
|
||||
import io.reactivex.BackpressureStrategy;
|
||||
import io.reactivex.Flowable;
|
||||
import io.reactivex.subjects.BehaviorSubject;
|
||||
|
||||
import com.habitrpg.android.habitica.R;
|
||||
import com.habitrpg.android.habitica.data.ApiClient;
|
||||
import com.habitrpg.android.habitica.events.ShowCheckinDialog;
|
||||
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 org.greenrobot.eventbus.EventBus;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by krh12 on 12/9/2016.
|
||||
*/
|
||||
|
||||
public class PopupNotificationsManager {
|
||||
private Map<String, Boolean> seenNotifications;
|
||||
@Nullable
|
||||
private ApiClient apiClient;
|
||||
private Context context;
|
||||
|
||||
private BehaviorSubject<List<Notification>> notifications;
|
||||
|
||||
// @TODO: A queue for displaying alert dialogues
|
||||
|
||||
public PopupNotificationsManager(Context context) {
|
||||
this.seenNotifications = new HashMap<>();
|
||||
this.context = context;
|
||||
this.notifications = BehaviorSubject.create();
|
||||
}
|
||||
|
||||
public void setNotifications(List<Notification> current) {
|
||||
this.notifications.onNext(current);
|
||||
|
||||
this.showNotificationDialog(current);
|
||||
}
|
||||
|
||||
public Flowable<List<Notification>> getNotifications() {
|
||||
return this.notifications.toFlowable(BackpressureStrategy.LATEST);
|
||||
}
|
||||
|
||||
public void setApiClient(@Nullable ApiClient apiClient) {
|
||||
this.apiClient = apiClient;
|
||||
}
|
||||
|
||||
Boolean displayNotification(Notification notification) {
|
||||
LoginIncentiveData notificationData = (LoginIncentiveData)notification.getData();
|
||||
String nextUnlockText = context.getString(R.string.nextPrizeUnlocks, notificationData.getNextRewardAt());
|
||||
if (notificationData.getRewardKey() != null) {
|
||||
ShowCheckinDialog event = new ShowCheckinDialog();
|
||||
event.notification = notification;
|
||||
event.nextUnlockText = nextUnlockText;
|
||||
EventBus.getDefault().post(event);
|
||||
} else {
|
||||
ShowSnackbarEvent event = new ShowSnackbarEvent();
|
||||
event.title = notificationData.getMessage();
|
||||
event.text = nextUnlockText;
|
||||
event.type = HabiticaSnackbar.SnackbarDisplayType.BLUE;
|
||||
EventBus.getDefault().post(event);
|
||||
if (apiClient != null) {
|
||||
// @TODO: This should be handled somewhere else? MAybe we notifiy via event
|
||||
apiClient.readNotification(notification.getId())
|
||||
.subscribe(next -> {}, RxErrorHandler.handleEmptyError());
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public Boolean showNotificationDialog(final List<Notification> notifications) {
|
||||
if (notifications == null || notifications.size() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.seenNotifications == null) {
|
||||
this.seenNotifications = new HashMap<>();
|
||||
}
|
||||
|
||||
for (Notification notification : notifications) {
|
||||
if (this.seenNotifications.get(notification.getId()) != null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!notification.getType().equals("LOGIN_INCENTIVE")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
this.displayNotification(notification);
|
||||
this.seenNotifications.put(notification.getId(), true);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
package com.habitrpg.android.habitica.helpers;
|
||||
|
||||
import android.content.Context;
|
||||
import androidx.annotation.Nullable;
|
||||
import io.reactivex.BackpressureStrategy;
|
||||
import io.reactivex.Flowable;
|
||||
import io.reactivex.subjects.BehaviorSubject;
|
||||
|
||||
import com.habitrpg.android.habitica.R;
|
||||
import com.habitrpg.android.habitica.data.ApiClient;
|
||||
import com.habitrpg.android.habitica.events.ShowCheckinDialog;
|
||||
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 org.greenrobot.eventbus.EventBus;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by krh12 on 12/9/2016.
|
||||
*/
|
||||
|
||||
public class NotificationsManager {
|
||||
private Map<String, Boolean> seenNotifications;
|
||||
@Nullable
|
||||
private ApiClient apiClient;
|
||||
private Context context;
|
||||
|
||||
private BehaviorSubject<List<Notification>> notifications;
|
||||
|
||||
// @TODO: A queue for displaying alert dialogues
|
||||
|
||||
public NotificationsManager(Context context) {
|
||||
this.seenNotifications = new HashMap<>();
|
||||
this.context = context;
|
||||
this.notifications = BehaviorSubject.create();
|
||||
}
|
||||
|
||||
public void setNotifications(List<Notification> current) {
|
||||
this.notifications.onNext(current);
|
||||
|
||||
this.showNotificationDialog(current);
|
||||
}
|
||||
|
||||
public Flowable<List<Notification>> getNotifications() {
|
||||
return this.notifications.toFlowable(BackpressureStrategy.LATEST);
|
||||
}
|
||||
|
||||
public void setApiClient(@Nullable ApiClient apiClient) {
|
||||
this.apiClient = apiClient;
|
||||
}
|
||||
|
||||
Boolean displayNotification(Notification notification) {
|
||||
LoginIncentiveData notificationData = (LoginIncentiveData)notification.getData();
|
||||
String nextUnlockText = context.getString(R.string.nextPrizeUnlocks, notificationData.getNextRewardAt());
|
||||
if (notificationData.getRewardKey() != null) {
|
||||
ShowCheckinDialog event = new ShowCheckinDialog();
|
||||
event.notification = notification;
|
||||
event.nextUnlockText = nextUnlockText;
|
||||
EventBus.getDefault().post(event);
|
||||
} else {
|
||||
ShowSnackbarEvent event = new ShowSnackbarEvent();
|
||||
event.title = notificationData.getMessage();
|
||||
event.text = nextUnlockText;
|
||||
event.type = HabiticaSnackbar.SnackbarDisplayType.BLUE;
|
||||
EventBus.getDefault().post(event);
|
||||
if (apiClient != null) {
|
||||
// @TODO: This should be handled somewhere else? MAybe we notifiy via event
|
||||
apiClient.readNotification(notification.getId())
|
||||
.subscribe(next -> {}, RxErrorHandler.handleEmptyError());
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public Boolean showNotificationDialog(final List<Notification> notifications) {
|
||||
if (notifications == null || notifications.size() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.seenNotifications == null) {
|
||||
this.seenNotifications = new HashMap<>();
|
||||
}
|
||||
|
||||
for (Notification notification : notifications) {
|
||||
if (this.seenNotifications.get(notification.getId()) != null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!notification.getType().equals("LOGIN_INCENTIVE")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
this.displayNotification(notification);
|
||||
this.seenNotifications.put(notification.getId(), true);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -7,7 +7,7 @@ import com.habitrpg.android.habitica.api.HostConfig;
|
|||
import com.habitrpg.android.habitica.api.MaintenanceApiService;
|
||||
import com.habitrpg.android.habitica.data.ApiClient;
|
||||
import com.habitrpg.android.habitica.data.implementation.ApiClientImpl;
|
||||
import com.habitrpg.android.habitica.helpers.PopupNotificationsManager;
|
||||
import com.habitrpg.android.habitica.helpers.NotificationsManager;
|
||||
import com.habitrpg.android.habitica.proxy.CrashlyticsProxy;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
|
@ -34,14 +34,14 @@ public class ApiModule {
|
|||
|
||||
@Provides
|
||||
@Singleton
|
||||
public PopupNotificationsManager providesPopupNotificationsManager(Context context) {
|
||||
return new PopupNotificationsManager(context);
|
||||
public NotificationsManager providesPopupNotificationsManager(Context context) {
|
||||
return new NotificationsManager(context);
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
public ApiClient providesApiHelper(GsonConverterFactory gsonConverter, HostConfig hostConfig, CrashlyticsProxy crashlyticsProxy, PopupNotificationsManager popupNotificationsManager, Context context) {
|
||||
return new ApiClientImpl(gsonConverter, hostConfig, crashlyticsProxy, popupNotificationsManager, context);
|
||||
public ApiClient providesApiHelper(GsonConverterFactory gsonConverter, HostConfig hostConfig, CrashlyticsProxy crashlyticsProxy, NotificationsManager notificationsManager, Context context) {
|
||||
return new ApiClientImpl(gsonConverter, hostConfig, crashlyticsProxy, notificationsManager, context);
|
||||
}
|
||||
|
||||
@Provides
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
package com.habitrpg.android.habitica.ui.viewmodels
|
||||
|
||||
import com.habitrpg.android.habitica.components.AppComponent
|
||||
import com.habitrpg.android.habitica.helpers.PopupNotificationsManager
|
||||
import com.habitrpg.android.habitica.helpers.NotificationsManager
|
||||
import com.habitrpg.android.habitica.helpers.RxErrorHandler
|
||||
import com.habitrpg.android.habitica.models.Notification
|
||||
import com.habitrpg.android.habitica.models.notifications.NewChatMessageData
|
||||
|
|
@ -14,7 +14,7 @@ import javax.inject.Inject
|
|||
|
||||
open class NotificationsViewModel : BaseViewModel() {
|
||||
@Inject
|
||||
lateinit var notificationsManager: PopupNotificationsManager
|
||||
lateinit var notificationsManager: NotificationsManager
|
||||
|
||||
/**
|
||||
* A list of notification types handled by this component.
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
//
|
||||
//
|
||||
//import com.habitrpg.android.habitica.data.implementation.ApiClientImpl;
|
||||
//import com.habitrpg.android.habitica.helpers.PopupNotificationsManager;
|
||||
//import com.habitrpg.android.habitica.helpers.NotificationsManager;
|
||||
//import com.habitrpg.android.habitica.proxy.implementation.EmptyCrashlyticsProxy;
|
||||
//import com.habitrpg.android.habitica.data.ApiClient;
|
||||
//import com.habitrpg.android.habitica.BuildConfig;
|
||||
|
|
@ -62,7 +62,7 @@
|
|||
// BuildConfig.PORT,
|
||||
// "",
|
||||
// "");
|
||||
// //apiClient = new ApiClientImpl(ApiClientImpl.createGsonFactory(), hostConfig, new EmptyCrashlyticsProxy(), new PopupNotificationsManager(context), context);
|
||||
// //apiClient = new ApiClientImpl(ApiClientImpl.createGsonFactory(), hostConfig, new EmptyCrashlyticsProxy(), new NotificationsManager(context), context);
|
||||
// //generateUser();
|
||||
// }
|
||||
//
|
||||
|
|
|
|||
|
|
@ -1,26 +1,19 @@
|
|||
package com.habitrpg.android.habitica.helpers;
|
||||
|
||||
import com.habitrpg.android.habitica.data.implementation.ApiClientImpl;
|
||||
import com.habitrpg.android.habitica.BuildConfig;
|
||||
import com.habitrpg.android.habitica.HabiticaApplication;
|
||||
import com.habitrpg.android.habitica.api.HostConfig;
|
||||
import com.habitrpg.android.habitica.proxy.implementation.EmptyCrashlyticsProxy;
|
||||
import com.habitrpg.android.habitica.data.ApiClient;
|
||||
import com.habitrpg.android.habitica.models.Notification;
|
||||
import com.habitrpg.android.habitica.models.notifications.NotificationData;
|
||||
import com.habitrpg.android.habitica.models.notifications.LoginIncentiveData;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.robolectric.Robolectric;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.shadows.ShadowAlertDialog;
|
||||
import org.robolectric.shadows.ShadowApplication;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
|
|
@ -42,7 +35,7 @@ public class PopupNotificationsManagerTest {
|
|||
|
||||
public HostConfig hostConfig;
|
||||
private Context context;
|
||||
private PopupNotificationsManager popupNotificationsManager;
|
||||
private NotificationsManager notificationsManager;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
|
@ -51,13 +44,13 @@ public class PopupNotificationsManagerTest {
|
|||
BuildConfig.PORT,
|
||||
"",
|
||||
"");
|
||||
popupNotificationsManager =new PopupNotificationsManager(context);
|
||||
notificationsManager =new NotificationsManager(context);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void itDoesNothingWhenNotificationsListIsEmpty() {
|
||||
List<Notification> notifications = new ArrayList<>();
|
||||
popupNotificationsManager.showNotificationDialog(notifications);
|
||||
notificationsManager.showNotificationDialog(notifications);
|
||||
|
||||
AlertDialog alert =
|
||||
ShadowAlertDialog.getLatestAlertDialog();
|
||||
|
|
@ -74,7 +67,7 @@ public class PopupNotificationsManagerTest {
|
|||
|
||||
notifications.add(notification);
|
||||
|
||||
final PopupNotificationsManager testClass = Mockito.mock(PopupNotificationsManager.class);
|
||||
final NotificationsManager testClass = Mockito.mock(NotificationsManager.class);
|
||||
Mockito.when(testClass.displayNotification(notification)).thenReturn(true);
|
||||
Mockito.when(testClass.showNotificationDialog(notifications)).thenCallRealMethod();
|
||||
|
||||
|
|
@ -89,7 +82,7 @@ public class PopupNotificationsManagerTest {
|
|||
|
||||
List<Notification> notifications = new ArrayList<>();
|
||||
|
||||
NotificationData notificationData = new NotificationData();
|
||||
LoginIncentiveData notificationData = new LoginIncentiveData();
|
||||
notificationData.message = testTitle;
|
||||
|
||||
Notification notification = new Notification();
|
||||
|
|
@ -98,7 +91,7 @@ public class PopupNotificationsManagerTest {
|
|||
|
||||
notifications.add(notification);
|
||||
|
||||
final PopupNotificationsManager testClass = Mockito.mock(PopupNotificationsManager.class);
|
||||
final NotificationsManager testClass = Mockito.mock(NotificationsManager.class);
|
||||
Mockito.when(testClass.displayNotification(notification)).thenReturn(true);
|
||||
Mockito.when(testClass.showNotificationDialog(notifications)).thenCallRealMethod();
|
||||
|
||||
|
|
@ -113,7 +106,7 @@ public class PopupNotificationsManagerTest {
|
|||
|
||||
List<Notification> notifications = new ArrayList<>();
|
||||
|
||||
NotificationData notificationData = new NotificationData();
|
||||
LoginIncentiveData notificationData = new LoginIncentiveData();
|
||||
notificationData.message = testTitle;
|
||||
|
||||
Notification notification = new Notification();
|
||||
|
|
@ -123,7 +116,7 @@ public class PopupNotificationsManagerTest {
|
|||
notifications.add(notification);
|
||||
notifications.add(notification);
|
||||
|
||||
final PopupNotificationsManager testClass = Mockito.mock(PopupNotificationsManager.class);
|
||||
final NotificationsManager testClass = Mockito.mock(NotificationsManager.class);
|
||||
Mockito.when(testClass.displayNotification(notification)).thenReturn(true);
|
||||
Mockito.when(testClass.showNotificationDialog(notifications)).thenCallRealMethod();
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue