From f1939123be9d0d7dbdabf64c6c41fa556ef697f3 Mon Sep 17 00:00:00 2001 From: Carl Vuorinen Date: Tue, 30 Apr 2019 08:34:58 +0300 Subject: [PATCH] Convert NotificationsManager to Kotlin --- .../helpers/NotificationsManager.java | 104 ------------------ .../habitica/helpers/NotificationsManager.kt | 101 +++++++++++++++++ .../ui/viewmodels/NotificationsViewModel.kt | 2 +- 3 files changed, 102 insertions(+), 105 deletions(-) delete mode 100644 Habitica/src/main/java/com/habitrpg/android/habitica/helpers/NotificationsManager.java create mode 100644 Habitica/src/main/java/com/habitrpg/android/habitica/helpers/NotificationsManager.kt diff --git a/Habitica/src/main/java/com/habitrpg/android/habitica/helpers/NotificationsManager.java b/Habitica/src/main/java/com/habitrpg/android/habitica/helpers/NotificationsManager.java deleted file mode 100644 index aa6071ccc..000000000 --- a/Habitica/src/main/java/com/habitrpg/android/habitica/helpers/NotificationsManager.java +++ /dev/null @@ -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 seenNotifications; - @Nullable - private ApiClient apiClient; - private Context context; - - private BehaviorSubject> 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 current) { - this.notifications.onNext(current); - - this.showNotificationDialog(current); - } - - public Flowable> 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 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; - } -} diff --git a/Habitica/src/main/java/com/habitrpg/android/habitica/helpers/NotificationsManager.kt b/Habitica/src/main/java/com/habitrpg/android/habitica/helpers/NotificationsManager.kt new file mode 100644 index 000000000..090d9661e --- /dev/null +++ b/Habitica/src/main/java/com/habitrpg/android/habitica/helpers/NotificationsManager.kt @@ -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? = null + private var apiClient: ApiClient? = null + + private val notifications: BehaviorSubject> + + init { + this.seenNotifications = HashMap() + this.notifications = BehaviorSubject.create() + } + + fun setNotifications(current: List) { + this.notifications.onNext(current) +current.map { Log.d("NotificationsManager.setNotifications." + it.type) } + + this.showNotificationDialog(current) + } + + fun getNotifications(): Flowable> { + 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?): 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 + } +} diff --git a/Habitica/src/main/java/com/habitrpg/android/habitica/ui/viewmodels/NotificationsViewModel.kt b/Habitica/src/main/java/com/habitrpg/android/habitica/ui/viewmodels/NotificationsViewModel.kt index 915362f22..a241204bf 100644 --- a/Habitica/src/main/java/com/habitrpg/android/habitica/ui/viewmodels/NotificationsViewModel.kt +++ b/Habitica/src/main/java/com/habitrpg/android/habitica/ui/viewmodels/NotificationsViewModel.kt @@ -46,7 +46,7 @@ open class NotificationsViewModel : BaseViewModel() { fun getNotifications(): Flowable> { - return notificationsManager.notifications + return notificationsManager.getNotifications() .map { filterSupportedTypes(it) } .observeOn(AndroidSchedulers.mainThread()) }