Convert NotificationsManager to Kotlin

This commit is contained in:
Carl Vuorinen 2019-04-30 08:34:58 +03:00
parent b668cccd7f
commit f1939123be
3 changed files with 102 additions and 105 deletions

View file

@ -1,104 +0,0 @@
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;
}
}

View file

@ -0,0 +1,101 @@
package com.habitrpg.android.habitica.helpers
import android.content.Context
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 com.playseeds.android.sdk.inappmessaging.Log
import io.reactivex.functions.Consumer
import org.greenrobot.eventbus.EventBus
import java.util.HashMap
/**
* Created by krh12 on 12/9/2016.
*/
class NotificationsManager (private val context: Context) {
// @TODO: A queue for displaying alert dialogues
private var seenNotifications: MutableMap<String, Boolean>? = null
private var apiClient: ApiClient? = null
private val notifications: BehaviorSubject<List<Notification>>
init {
this.seenNotifications = HashMap()
this.notifications = BehaviorSubject.create()
}
fun setNotifications(current: List<Notification>) {
this.notifications.onNext(current)
current.map { Log.d("NotificationsManager.setNotifications." + it.type) }
this.showNotificationDialog(current)
}
fun getNotifications(): Flowable<List<Notification>> {
return this.notifications.toFlowable(BackpressureStrategy.LATEST)
}
fun setApiClient(apiClient: ApiClient?) {
this.apiClient = apiClient
}
fun displayNotification(notification: Notification): Boolean? {
val notificationData = notification.data as LoginIncentiveData?
val nextUnlockText = context.getString(R.string.nextPrizeUnlocks, notificationData!!.nextRewardAt)
if (notificationData.rewardKey != null) {
val event = ShowCheckinDialog()
event.notification = notification
event.nextUnlockText = nextUnlockText
EventBus.getDefault().post(event)
} else {
val event = ShowSnackbarEvent()
event.title = notificationData.message
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.id)
.subscribe(Consumer {}, RxErrorHandler.handleEmptyError())
}
}
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
}
}

View file

@ -46,7 +46,7 @@ open class NotificationsViewModel : BaseViewModel() {
fun getNotifications(): Flowable<List<Notification>> {
return notificationsManager.notifications
return notificationsManager.getNotifications()
.map { filterSupportedTypes(it) }
.observeOn(AndroidSchedulers.mainThread())
}