diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/managers/NotificationManager.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/managers/NotificationManager.kt new file mode 100644 index 0000000..636e527 --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/managers/NotificationManager.kt @@ -0,0 +1,296 @@ +package com.keyvalue.siren.androidsdk.data.managers + +import com.keyvalue.siren.androidsdk.data.model.AllNotificationResponseData +import com.keyvalue.siren.androidsdk.data.model.DataStatus +import com.keyvalue.siren.androidsdk.data.model.MarkAsReadByIdResponseData +import com.keyvalue.siren.androidsdk.data.model.MarkAsViewedResponseData +import com.keyvalue.siren.androidsdk.data.model.UnViewedNotificationResponseData +import com.keyvalue.siren.androidsdk.data.networkcallbacks.NetworkCallback +import com.keyvalue.siren.androidsdk.data.repository.NotificationRepository +import com.keyvalue.siren.androidsdk.data.repository.NotificationRepositoryImplementation +import com.keyvalue.siren.androidsdk.data.state.AllNotificationState +import com.keyvalue.siren.androidsdk.data.state.ClearAllNotificationsState +import com.keyvalue.siren.androidsdk.data.state.DeleteNotificationByIdState +import com.keyvalue.siren.androidsdk.data.state.MarkAllAsReadState +import com.keyvalue.siren.androidsdk.data.state.MarkAsReadByIdState +import com.keyvalue.siren.androidsdk.data.state.MarkAsViewedViewedState +import com.keyvalue.siren.androidsdk.data.state.NotificationUnViewedState +import kotlinx.coroutines.flow.MutableStateFlow +import org.json.JSONObject + +class NotificationManager(baseURL: String) { + private var service: NotificationRepository = + NotificationRepositoryImplementation(baseURL) + var notificationUnViewedState: MutableStateFlow = + MutableStateFlow(null) + var allNotificationsState: MutableStateFlow = + MutableStateFlow(null) + var markAsReadByIdState: MutableStateFlow = + MutableStateFlow(null) + var markAllAsReadState: MutableStateFlow = + MutableStateFlow(null) + var markAsViewedState: MutableStateFlow = + MutableStateFlow(null) + var deleteNotificationByIdState: MutableStateFlow = + MutableStateFlow(null) + var clearAllNotificationsState: MutableStateFlow = + MutableStateFlow(null) + + suspend fun fetchUnViewedNotificationsCount( + userToken: String, + recipientId: String, + ) { + service.fetchUnViewedNotificationsCount( + userToken, + recipientId, + object : NetworkCallback { + override suspend fun onResult(classObject: Any) { + val notificationUnViewedState = + NotificationUnViewedState( + notificationUnViewedResponse = classObject as UnViewedNotificationResponseData, + errorResponse = null, + ) + this@NotificationManager.notificationUnViewedState.emit( + notificationUnViewedState, + ) + } + + override suspend fun onError(errorObject: JSONObject) { + val notificationUnViewedState = + NotificationUnViewedState( + notificationUnViewedResponse = null, + errorResponse = errorObject, + ) + this@NotificationManager.notificationUnViewedState.emit( + notificationUnViewedState, + ) + } + }, + ) + } + + suspend fun fetchAllNotifications( + userToken: String, + recipientId: String, + page: Int? = null, + size: Int? = null, + start: String? = null, + end: String? = null, + isRead: Boolean? = null, + ) { + service.fetchAllNotifications( + userToken = userToken, + recipientId = recipientId, + page = page, + size = size, + start = start, + end = end, + isRead = isRead, + networkCallback = + object : NetworkCallback { + override suspend fun onResult(classObject: Any) { + val allNotificationsState = + AllNotificationState( + allNotificationResponse = classObject as List, + errorResponse = null, + ) + this@NotificationManager.allNotificationsState.emit( + allNotificationsState, + ) + } + + override suspend fun onError(errorObject: JSONObject) { + val allNotificationsState = + AllNotificationState( + allNotificationResponse = null, + errorResponse = errorObject, + ) + this@NotificationManager.allNotificationsState.emit( + allNotificationsState, + ) + } + }, + ) + } + + suspend fun markAsReadById( + userToken: String, + recipientId: String, + notificationId: String, + ) { + service.markAsReadById( + userToken = userToken, + recipientId = recipientId, + notificationId = notificationId, + networkCallback = + object : NetworkCallback { + override suspend fun onResult(classObject: Any) { + val markAsReadByIdState = + MarkAsReadByIdState( + markAsReadByIdResponse = classObject as MarkAsReadByIdResponseData, + errorResponse = null, + ) + this@NotificationManager.markAsReadByIdState.emit( + markAsReadByIdState, + ) + } + + override suspend fun onError(errorObject: JSONObject) { + val markAsReadByIdState = + MarkAsReadByIdState( + markAsReadByIdResponse = null, + errorResponse = errorObject, + ) + this@NotificationManager.markAsReadByIdState.emit( + markAsReadByIdState, + ) + } + }, + ) + } + + suspend fun markAllAsRead( + userToken: String, + recipientId: String, + startDate: String, + ) { + service.markAllAsRead( + userToken = userToken, + recipientId = recipientId, + startDate = startDate, + networkCallback = + object : NetworkCallback { + override suspend fun onResult(classObject: Any) { + val markAllAsReadState = + MarkAllAsReadState( + markAllAsReadResponse = classObject as DataStatus, + errorResponse = null, + ) + this@NotificationManager.markAllAsReadState.emit( + markAllAsReadState, + ) + } + + override suspend fun onError(errorObject: JSONObject) { + val markAllAsReadState = + MarkAllAsReadState( + markAllAsReadResponse = null, + errorResponse = errorObject, + ) + this@NotificationManager.markAllAsReadState.emit( + markAllAsReadState, + ) + } + }, + ) + } + + suspend fun markAsViewed( + userToken: String, + recipientId: String, + startDate: String, + ) { + service.markAsViewed( + userToken = userToken, + recipientId = recipientId, + startDate = startDate, + networkCallback = + object : NetworkCallback { + override suspend fun onResult(classObject: Any) { + val markAsViewedViewedState = + MarkAsViewedViewedState( + markAsViewedResponse = classObject as MarkAsViewedResponseData, + errorResponse = null, + ) + this@NotificationManager.markAsViewedState.emit( + markAsViewedViewedState, + ) + } + + override suspend fun onError(errorObject: JSONObject) { + val markAsViewedViewedState = + MarkAsViewedViewedState( + markAsViewedResponse = null, + errorResponse = errorObject, + ) + this@NotificationManager.markAsViewedState.emit( + markAsViewedViewedState, + ) + } + }, + ) + } + + suspend fun deleteNotificationById( + userToken: String, + recipientId: String, + notificationId: String, + ) { + service.deleteNotificationById( + userToken = userToken, + recipientId = recipientId, + notificationId = notificationId, + networkCallback = + object : NetworkCallback { + override suspend fun onResult(classObject: Any) { + val deleteNotificationByIdState = + DeleteNotificationByIdState( + deleteStatus = classObject as DataStatus, + deleteId = notificationId, + errorResponse = null, + ) + this@NotificationManager.deleteNotificationByIdState.emit( + deleteNotificationByIdState, + ) + } + + override suspend fun onError(errorObject: JSONObject) { + val deleteNotificationByIdState = + DeleteNotificationByIdState( + deleteStatus = null, + errorResponse = errorObject, + ) + this@NotificationManager.deleteNotificationByIdState.emit( + deleteNotificationByIdState, + ) + } + }, + ) + } + + suspend fun clearAllNotifications( + userToken: String, + recipientId: String, + startDate: String, + ) { + service.clearAllNotifications( + userToken = userToken, + recipientId = recipientId, + startDate = startDate, + networkCallback = + object : NetworkCallback { + override suspend fun onResult(classObject: Any) { + val clearAllNotificationsState = + ClearAllNotificationsState( + clearAllNotificationsResponse = classObject as DataStatus, + errorResponse = null, + ) + this@NotificationManager.clearAllNotificationsState.emit( + clearAllNotificationsState, + ) + } + + override suspend fun onError(errorObject: JSONObject) { + val clearAllNotificationsState = + ClearAllNotificationsState( + clearAllNotificationsResponse = null, + errorResponse = errorObject, + ) + this@NotificationManager.clearAllNotificationsState.emit( + clearAllNotificationsState, + ) + } + }, + ) + } +} diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/AllNotificationResponse.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/AllNotificationResponse.kt new file mode 100644 index 0000000..4e83e94 --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/AllNotificationResponse.kt @@ -0,0 +1,12 @@ +package com.keyvalue.siren.androidsdk.data.model + +import androidx.annotation.Keep +import com.google.gson.annotations.SerializedName + +@Keep +data class AllNotificationResponse( + @SerializedName("data") + val allNotificationResponse: List?, + @SerializedName("error") + val error: ErrorResponse?, +) diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/AllNotificationResponseData.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/AllNotificationResponseData.kt new file mode 100644 index 0000000..79b2cab --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/AllNotificationResponseData.kt @@ -0,0 +1,18 @@ +package com.keyvalue.siren.androidsdk.data.model + +import androidx.annotation.Keep +import com.google.gson.annotations.SerializedName + +@Keep +data class AllNotificationResponseData( + @SerializedName("createdAt") + val createdAt: String, + @SerializedName("id") + val id: String, + @SerializedName("isRead") + val isRead: Boolean, + @SerializedName("message") + val message: Message, + @SerializedName("requestId") + val requestId: String, +) diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/Avatar.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/Avatar.kt new file mode 100644 index 0000000..d01a445 --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/Avatar.kt @@ -0,0 +1,12 @@ +package com.keyvalue.siren.androidsdk.data.model + +import androidx.annotation.Keep +import com.google.gson.annotations.SerializedName + +@Keep +data class Avatar( + @SerializedName("actionUrl") + val actionUrl: String?, + @SerializedName("imageUrl") + val imageUrl: String?, +) diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/BulkUpdateBody.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/BulkUpdateBody.kt new file mode 100644 index 0000000..965462a --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/BulkUpdateBody.kt @@ -0,0 +1,8 @@ +package com.keyvalue.siren.androidsdk.data.model + +import com.keyvalue.siren.androidsdk.utils.constants.BulkUpdateType + +data class BulkUpdateBody( + val until: String?, + val operation: BulkUpdateType?, +) diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/ClearAllNotificationsResponse.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/ClearAllNotificationsResponse.kt new file mode 100644 index 0000000..ad19373 --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/ClearAllNotificationsResponse.kt @@ -0,0 +1,12 @@ +package com.keyvalue.siren.androidsdk.data.model + +import androidx.annotation.Keep +import com.google.gson.annotations.SerializedName + +@Keep +data class ClearAllNotificationsResponse( + @SerializedName("data") + val clearAllNotificationsResponse: DataStatus, + @SerializedName("error") + val error: ErrorResponse?, +) diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/DataStatus.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/DataStatus.kt new file mode 100644 index 0000000..afd696d --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/DataStatus.kt @@ -0,0 +1,5 @@ +package com.keyvalue.siren.androidsdk.data.model + +data class DataStatus( + val status: String, +) diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/DeleteNotificationByIdResponse.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/DeleteNotificationByIdResponse.kt new file mode 100644 index 0000000..07216da --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/DeleteNotificationByIdResponse.kt @@ -0,0 +1,12 @@ +package com.keyvalue.siren.androidsdk.data.model + +import androidx.annotation.Keep +import com.google.gson.annotations.SerializedName + +@Keep +data class DeleteNotificationByIdResponse( + @SerializedName("data") + val deleteNotificationByIdResponse: DataStatus?, + @SerializedName("error") + val error: ErrorResponse?, +) diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/InAppRecipient.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/InAppRecipient.kt new file mode 100644 index 0000000..288dc84 --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/InAppRecipient.kt @@ -0,0 +1,28 @@ +package com.keyvalue.siren.androidsdk.data.model + +import com.google.gson.annotations.SerializedName + +data class InAppRecipient( + @SerializedName("id") + val id: String, + @SerializedName("createdAt") + val createdAt: String, + @SerializedName("updatedAt") + val updatedAt: String, + @SerializedName("deletedAt") + val deletedAt: String?, + @SerializedName("createdBy") + val createdBy: String, + @SerializedName("updatedBy") + val updatedBy: String, + @SerializedName("deletedBy") + val deletedBy: String?, + @SerializedName("projectEnvironmentId") + val projectEnvironmentId: String, + @SerializedName("referenceId") + val referenceId: String, + @SerializedName("providerIntegrationId") + val providerIntegrationId: String, + @SerializedName("lastOpenedAt") + val lastOpenedAt: String, +) diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/MarkAllAsReadResponse.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/MarkAllAsReadResponse.kt new file mode 100644 index 0000000..733218d --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/MarkAllAsReadResponse.kt @@ -0,0 +1,10 @@ +package com.keyvalue.siren.androidsdk.data.model + +import com.google.gson.annotations.SerializedName + +data class MarkAllAsReadResponse( + @SerializedName("data") + val markAllAsReadResponse: DataStatus?, + @SerializedName("error") + val error: ErrorResponse?, +) diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/MarkAsReadBody.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/MarkAsReadBody.kt new file mode 100644 index 0000000..cdd6e8f --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/MarkAsReadBody.kt @@ -0,0 +1,6 @@ +package com.keyvalue.siren.androidsdk.data.model + +data class MarkAsReadBody( + val isRead: Boolean?, + val isDelivered: Boolean?, +) diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/MarkAsReadByIdResponse.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/MarkAsReadByIdResponse.kt new file mode 100644 index 0000000..346d0b6 --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/MarkAsReadByIdResponse.kt @@ -0,0 +1,10 @@ +package com.keyvalue.siren.androidsdk.data.model + +import com.google.gson.annotations.SerializedName + +data class MarkAsReadByIdResponse( + @SerializedName("data") + val markAsReadByIdResponse: MarkAsReadByIdResponseData, + @SerializedName("error") + val error: ErrorResponse?, +) diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/MarkAsReadByIdResponseData.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/MarkAsReadByIdResponseData.kt new file mode 100644 index 0000000..e173dba --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/MarkAsReadByIdResponseData.kt @@ -0,0 +1,34 @@ +package com.keyvalue.siren.androidsdk.data.model + +import androidx.annotation.Keep +import com.google.gson.annotations.SerializedName + +@Keep +data class MarkAsReadByIdResponseData( + @SerializedName("createdAt") + val createdAt: String, + @SerializedName("createdBy") + val createdBy: String, + @SerializedName("updatedAt") + val updatedAt: String, + @SerializedName("updatedBy") + val updatedBy: String, + @SerializedName("deletedAt") + val deletedAt: String?, + @SerializedName("deletedBy") + val deletedBy: String?, + @SerializedName("id") + val id: String, + @SerializedName("projectEnvironmentId") + val projectEnvironmentId: String, + @SerializedName("isRead") + val isRead: Boolean, + @SerializedName("isDelivered") + val isDelivered: Boolean, + @SerializedName("inAppRecipient") + val inAppRecipient: InAppRecipient, + @SerializedName("message") + val message: Message, + @SerializedName("requestId") + val requestId: String, +) diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/MarkAsViewedBody.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/MarkAsViewedBody.kt new file mode 100644 index 0000000..a56efb2 --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/MarkAsViewedBody.kt @@ -0,0 +1,5 @@ +package com.keyvalue.siren.androidsdk.data.model + +data class MarkAsViewedBody( + val lastOpenedAt: String, +) diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/MarkAsViewedResponse.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/MarkAsViewedResponse.kt new file mode 100644 index 0000000..8e815da --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/MarkAsViewedResponse.kt @@ -0,0 +1,10 @@ +package com.keyvalue.siren.androidsdk.data.model + +import com.google.gson.annotations.SerializedName + +data class MarkAsViewedResponse( + @SerializedName("data") + val markAsViewedResponse: MarkAsViewedResponseData?, + @SerializedName("error") + val error: ErrorResponse?, +) diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/MarkAsViewedResponseData.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/MarkAsViewedResponseData.kt new file mode 100644 index 0000000..4932414 --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/MarkAsViewedResponseData.kt @@ -0,0 +1,15 @@ +package com.keyvalue.siren.androidsdk.data.model + +data class MarkAsViewedResponseData( + val createdAt: String, + val createdBy: String, + val deletedAt: String?, + val deletedBy: String?, + val id: String, + val lastOpenedAt: String, + val projectEnvironmentId: String, + val providerIntegrationId: String, + val referenceId: String, + val updatedAt: String, + val updatedBy: String, +) diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/Message.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/Message.kt new file mode 100644 index 0000000..53a0691 --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/Message.kt @@ -0,0 +1,22 @@ +package com.keyvalue.siren.androidsdk.data.model + +import androidx.annotation.Keep +import com.google.gson.annotations.SerializedName + +@Keep +data class Message( + @SerializedName("actionUrl") + val actionUrl: String?, + @SerializedName("additionalData") + val additionalData: String?, + @SerializedName("avatar") + val avatar: Avatar, + @SerializedName("body") + val body: String, + @SerializedName("channel") + val channel: String, + @SerializedName("header") + val header: String?, + @SerializedName("subHeader") + val subHeader: String?, +) diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/UnViewedNotificationResponse.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/UnViewedNotificationResponse.kt new file mode 100644 index 0000000..8d4f545 --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/UnViewedNotificationResponse.kt @@ -0,0 +1,10 @@ +package com.keyvalue.siren.androidsdk.data.model + +import com.google.gson.annotations.SerializedName + +data class UnViewedNotificationResponse( + @SerializedName("data") + val unViewedNotificationResponse: UnViewedNotificationResponseData?, + @SerializedName("error") + val error: ErrorResponse?, +) diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/UnViewedNotificationResponseData.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/UnViewedNotificationResponseData.kt new file mode 100644 index 0000000..71e049c --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/model/UnViewedNotificationResponseData.kt @@ -0,0 +1,16 @@ +package com.keyvalue.siren.androidsdk.data.model + +data class UnViewedNotificationResponseData( + val id: String, + val createdAt: String, + val updatedAt: String, + val deletedAt: String?, + val createdBy: String, + val updatedBy: String, + val deletedBy: String?, + val projectEnvironmentId: String, + val referenceId: String, + val providerIntegrationId: String, + val lastOpenedAt: String?, + val totalUnviewed: Long, +) diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/repository/NotificationRepository.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/repository/NotificationRepository.kt new file mode 100644 index 0000000..05fc8d6 --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/repository/NotificationRepository.kt @@ -0,0 +1,57 @@ +package com.keyvalue.siren.androidsdk.data.repository + +import com.keyvalue.siren.androidsdk.data.networkcallbacks.NetworkCallback + +interface NotificationRepository { + suspend fun fetchUnViewedNotificationsCount( + userToken: String, + recipientId: String, + networkCallback: NetworkCallback, + ) + + suspend fun fetchAllNotifications( + userToken: String, + recipientId: String, + page: Int? = null, + size: Int? = null, + start: String? = null, + end: String? = null, + isRead: Boolean? = null, + networkCallback: NetworkCallback, + ) + + suspend fun markAsReadById( + userToken: String, + recipientId: String, + notificationId: String, + networkCallback: NetworkCallback, + ) + + suspend fun markAllAsRead( + userToken: String, + recipientId: String, + startDate: String, + networkCallback: NetworkCallback, + ) + + suspend fun markAsViewed( + userToken: String, + recipientId: String, + startDate: String, + networkCallback: NetworkCallback, + ) + + suspend fun deleteNotificationById( + userToken: String, + recipientId: String, + notificationId: String, + networkCallback: NetworkCallback, + ) + + suspend fun clearAllNotifications( + userToken: String, + recipientId: String, + startDate: String, + networkCallback: NetworkCallback, + ) +} diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/repository/NotificationRepositoryImplementation.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/repository/NotificationRepositoryImplementation.kt new file mode 100644 index 0000000..9f08623 --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/repository/NotificationRepositoryImplementation.kt @@ -0,0 +1,368 @@ +package com.keyvalue.siren.androidsdk.data.repository + +import com.google.gson.Gson +import com.keyvalue.siren.androidsdk.data.model.AllNotificationResponse +import com.keyvalue.siren.androidsdk.data.model.BulkUpdateBody +import com.keyvalue.siren.androidsdk.data.model.ClearAllNotificationsResponse +import com.keyvalue.siren.androidsdk.data.model.DeleteNotificationByIdResponse +import com.keyvalue.siren.androidsdk.data.model.MarkAllAsReadResponse +import com.keyvalue.siren.androidsdk.data.model.MarkAsReadBody +import com.keyvalue.siren.androidsdk.data.model.MarkAsReadByIdResponse +import com.keyvalue.siren.androidsdk.data.model.MarkAsViewedBody +import com.keyvalue.siren.androidsdk.data.model.MarkAsViewedResponse +import com.keyvalue.siren.androidsdk.data.model.UnViewedNotificationResponse +import com.keyvalue.siren.androidsdk.data.networkcallbacks.NetworkCallback +import com.keyvalue.siren.androidsdk.data.retrofit.RetrofitClient +import com.keyvalue.siren.androidsdk.data.service.NotificationApiService +import com.keyvalue.siren.androidsdk.utils.constants.BulkUpdateType +import com.keyvalue.siren.androidsdk.utils.constants.CODE_GENERIC_API_ERROR +import com.keyvalue.siren.androidsdk.utils.constants.CODE_TIMED_OUT +import com.keyvalue.siren.androidsdk.utils.constants.ERROR_MESSAGE_SERVICE_NOT_AVAILABLE +import com.keyvalue.siren.androidsdk.utils.constants.ERROR_MESSAGE_TIMED_OUT +import com.keyvalue.siren.androidsdk.utils.constants.SirenErrorTypes +import org.json.JSONObject +import java.net.SocketTimeoutException + +class NotificationRepositoryImplementation(baseURL: String) : NotificationRepository { + private var notificationsApiService: NotificationApiService? = null + + init { + notificationsApiService = + RetrofitClient.getRetrofitInstance(baseURL)?.create( + NotificationApiService::class.java, + ) + } + + override suspend fun fetchUnViewedNotificationsCount( + userToken: String, + recipientId: String, + networkCallback: NetworkCallback, + ) { + try { + val parentResponse = + notificationsApiService?.fetchUnViewedNotificationsCount(recipientId, userToken) + val response = parentResponse?.body() + if (response?.unViewedNotificationResponse != null) { + response.unViewedNotificationResponse.let { + networkCallback.onResult(it) + } + } else { + val errorBody = parentResponse?.errorBody()?.string() + if (errorBody != null) { + val errors = + Gson().fromJson( + errorBody, + UnViewedNotificationResponse::class.java, + ) + networkCallback.onError( + JSONObject().put("type", SirenErrorTypes.ERROR) + .put("code", errors.error?.errorCode ?: CODE_GENERIC_API_ERROR).put( + "message", + errors.error?.message + ?: "HTTP error! status: ${parentResponse.raw().code} ${parentResponse.raw().message}", + ), + ) + } + } + } catch (e: SocketTimeoutException) { + networkCallback.onError( + JSONObject().put("code", CODE_TIMED_OUT).put("message", ERROR_MESSAGE_TIMED_OUT), + ) + } catch (e: Exception) { + networkCallback.onError( + JSONObject().put("code", CODE_GENERIC_API_ERROR) + .put("message", ERROR_MESSAGE_SERVICE_NOT_AVAILABLE), + ) + } + } + + override suspend fun fetchAllNotifications( + userToken: String, + recipientId: String, + page: Int?, + size: Int?, + start: String?, + end: String?, + isRead: Boolean?, + networkCallback: NetworkCallback, + ) { + try { + val parentResponse = + notificationsApiService?.fetchAllNotifications( + recipientId, + userToken, + page, + size, + start, + end, + isRead, + ) + val response = parentResponse?.body() + if (parentResponse?.isSuccessful == true && response?.allNotificationResponse != null) { + response.allNotificationResponse.let { networkCallback.onResult(it) } + } else { + val errorBody = parentResponse?.errorBody()?.string() + if (errorBody != null) { + val errors = + Gson().fromJson( + errorBody, + AllNotificationResponse::class.java, + ) + networkCallback.onError( + JSONObject().put("type", SirenErrorTypes.ERROR) + .put("code", errors.error?.errorCode ?: CODE_GENERIC_API_ERROR).put( + "message", + errors.error?.message + ?: "HTTP error! status: ${parentResponse.raw().code} ${parentResponse.raw().message}", + ), + ) + } + } + } catch (e: SocketTimeoutException) { + networkCallback.onError( + JSONObject().put("code", CODE_TIMED_OUT).put("message", ERROR_MESSAGE_TIMED_OUT), + ) + } catch (e: Exception) { + networkCallback.onError( + JSONObject().put("code", CODE_GENERIC_API_ERROR) + .put("message", ERROR_MESSAGE_SERVICE_NOT_AVAILABLE), + ) + } + } + + override suspend fun markAsReadById( + userToken: String, + recipientId: String, + notificationId: String, + networkCallback: NetworkCallback, + ) { + try { + val parentResponse = + notificationsApiService?.markAsReadById( + recipientId, + userToken, + notificationId, + MarkAsReadBody(isRead = true, isDelivered = false), + ) + val response = parentResponse?.body() + if (parentResponse?.isSuccessful == true && response?.markAsReadByIdResponse != null) { + response.markAsReadByIdResponse.let { networkCallback.onResult(it) } + } else { + val errorBody = parentResponse?.errorBody()?.string() + if (errorBody != null) { + val errors = + Gson().fromJson( + errorBody, + MarkAsReadByIdResponse::class.java, + ) + networkCallback.onError( + JSONObject().put("type", SirenErrorTypes.ERROR) + .put("code", errors.error?.errorCode ?: CODE_GENERIC_API_ERROR).put( + "message", + errors.error?.message + ?: "HTTP error! status: ${parentResponse.raw().code} ${parentResponse.raw().message}", + ), + ) + } + } + } catch (e: SocketTimeoutException) { + networkCallback.onError( + JSONObject().put("code", CODE_TIMED_OUT).put("message", ERROR_MESSAGE_TIMED_OUT), + ) + } catch (e: Exception) { + networkCallback.onError( + JSONObject().put("code", CODE_GENERIC_API_ERROR) + .put("message", ERROR_MESSAGE_SERVICE_NOT_AVAILABLE), + ) + } + } + + override suspend fun markAllAsRead( + userToken: String, + recipientId: String, + startDate: String, + networkCallback: NetworkCallback, + ) { + try { + val parentResponse = + notificationsApiService?.markAllAsRead( + recipientId, + userToken, + BulkUpdateBody( + startDate, + operation = BulkUpdateType.MARK_AS_READ, + ), + ) + val response = parentResponse?.body() + if (parentResponse?.isSuccessful == true && response?.markAllAsReadResponse != null) { + response.markAllAsReadResponse.let { networkCallback.onResult(it) } + } else { + val errorBody = parentResponse?.errorBody()?.string() + if (errorBody != null) { + val errors = + Gson().fromJson( + errorBody, + MarkAllAsReadResponse::class.java, + ) + networkCallback.onError( + JSONObject().put("type", SirenErrorTypes.ERROR) + .put("code", errors.error?.errorCode ?: CODE_GENERIC_API_ERROR).put( + "message", + errors.error?.message + ?: "HTTP error! status: ${parentResponse.raw().code} ${parentResponse.raw().message}", + ), + ) + } + } + } catch (e: SocketTimeoutException) { + networkCallback.onError( + JSONObject().put("code", CODE_TIMED_OUT).put("message", ERROR_MESSAGE_TIMED_OUT), + ) + } catch (e: Exception) { + networkCallback.onError( + JSONObject().put("code", CODE_GENERIC_API_ERROR) + .put("message", ERROR_MESSAGE_SERVICE_NOT_AVAILABLE), + ) + } + } + + override suspend fun markAsViewed( + userToken: String, + recipientId: String, + startDate: String, + networkCallback: NetworkCallback, + ) { + try { + val parentResponse = + notificationsApiService?.markAsViewed( + recipientId, + userToken, + MarkAsViewedBody( + startDate, + ), + ) + val response = parentResponse?.body() + if (parentResponse?.isSuccessful == true && response?.markAsViewedResponse != null) { + response.markAsViewedResponse.let { networkCallback.onResult(it) } + } else { + val errorBody = parentResponse?.errorBody()?.string() + if (errorBody != null) { + val errors = + Gson().fromJson( + errorBody, + MarkAsViewedResponse::class.java, + ) + networkCallback.onError( + JSONObject().put("type", SirenErrorTypes.ERROR) + .put("code", errors.error?.errorCode ?: CODE_GENERIC_API_ERROR).put( + "message", + errors.error?.message + ?: "HTTP error! status: ${parentResponse.raw().code} ${parentResponse.raw().message}", + ), + ) + } + } + } catch (e: SocketTimeoutException) { + networkCallback.onError( + JSONObject().put("code", CODE_TIMED_OUT).put("message", ERROR_MESSAGE_TIMED_OUT), + ) + } catch (e: Exception) { + networkCallback.onError( + JSONObject().put("code", CODE_GENERIC_API_ERROR) + .put("message", ERROR_MESSAGE_SERVICE_NOT_AVAILABLE), + ) + } + } + + override suspend fun deleteNotificationById( + userToken: String, + recipientId: String, + notificationId: String, + networkCallback: NetworkCallback, + ) { + try { + val parentResponse = + notificationsApiService?.deleteNotificationById( + recipientId, + userToken, + notificationId, + ) + val response = parentResponse?.body() + if (parentResponse?.isSuccessful == true && response?.deleteNotificationByIdResponse != null) { + response.deleteNotificationByIdResponse.let { networkCallback.onResult(it) } + } else { + val errorBody = parentResponse?.errorBody()?.string() + if (errorBody != null) { + val errors = + Gson().fromJson(errorBody, DeleteNotificationByIdResponse::class.java) + networkCallback.onError( + JSONObject().put("type", SirenErrorTypes.ERROR) + .put("code", errors.error?.errorCode ?: CODE_GENERIC_API_ERROR).put( + "message", + errors.error?.message + ?: "HTTP error! status: ${parentResponse.raw().code} ${parentResponse.raw().message}", + ), + ) + } + } + } catch (e: SocketTimeoutException) { + networkCallback.onError( + JSONObject().put("code", CODE_TIMED_OUT).put("message", ERROR_MESSAGE_TIMED_OUT), + ) + } catch (e: Exception) { + networkCallback.onError( + JSONObject().put("code", CODE_GENERIC_API_ERROR) + .put("message", ERROR_MESSAGE_SERVICE_NOT_AVAILABLE), + ) + } + } + + override suspend fun clearAllNotifications( + userToken: String, + recipientId: String, + startDate: String, + networkCallback: NetworkCallback, + ) { + try { + val parentResponse = + notificationsApiService?.clearAllNotifications( + recipientId, + userToken, + BulkUpdateBody( + startDate, + operation = BulkUpdateType.MARK_AS_DELETED, + ), + ) + val response = parentResponse?.body() + if (parentResponse?.isSuccessful == true && response?.clearAllNotificationsResponse != null) { + response.clearAllNotificationsResponse.let { networkCallback.onResult(it) } + } else { + val errorBody = parentResponse?.errorBody()?.string() + if (errorBody != null) { + val errors = + Gson().fromJson( + errorBody, + ClearAllNotificationsResponse::class.java, + ) + networkCallback.onError( + JSONObject().put("type", SirenErrorTypes.ERROR) + .put("code", errors.error?.errorCode ?: CODE_GENERIC_API_ERROR).put( + "message", + errors.error?.message + ?: "HTTP error! status: ${parentResponse.raw().code} ${parentResponse.raw().message}", + ), + ) + } + } + } catch (e: SocketTimeoutException) { + networkCallback.onError( + JSONObject().put("code", CODE_TIMED_OUT).put("message", ERROR_MESSAGE_TIMED_OUT), + ) + } catch (e: Exception) { + networkCallback.onError( + JSONObject().put("code", CODE_GENERIC_API_ERROR) + .put("message", ERROR_MESSAGE_SERVICE_NOT_AVAILABLE), + ) + } + } +} diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/service/NotificationApiService.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/service/NotificationApiService.kt new file mode 100644 index 0000000..c1c84a8 --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/service/NotificationApiService.kt @@ -0,0 +1,77 @@ +package com.keyvalue.siren.androidsdk.data.service + +import com.keyvalue.siren.androidsdk.data.model.AllNotificationResponse +import com.keyvalue.siren.androidsdk.data.model.BulkUpdateBody +import com.keyvalue.siren.androidsdk.data.model.ClearAllNotificationsResponse +import com.keyvalue.siren.androidsdk.data.model.DeleteNotificationByIdResponse +import com.keyvalue.siren.androidsdk.data.model.MarkAllAsReadResponse +import com.keyvalue.siren.androidsdk.data.model.MarkAsReadBody +import com.keyvalue.siren.androidsdk.data.model.MarkAsReadByIdResponse +import com.keyvalue.siren.androidsdk.data.model.MarkAsViewedBody +import com.keyvalue.siren.androidsdk.data.model.MarkAsViewedResponse +import com.keyvalue.siren.androidsdk.data.model.UnViewedNotificationResponse +import retrofit2.Response +import retrofit2.http.Body +import retrofit2.http.DELETE +import retrofit2.http.GET +import retrofit2.http.Header +import retrofit2.http.PATCH +import retrofit2.http.POST +import retrofit2.http.Path +import retrofit2.http.Query + +interface NotificationApiService { + @GET("api/v2/in-app/recipients/{inAppRecipientId}") + suspend fun fetchUnViewedNotificationsCount( + @Path("inAppRecipientId") id: String, + @Header("Authorization") token: String, + ): Response + + @GET("api/v2/in-app/recipients/{inAppRecipientId}/notifications") + suspend fun fetchAllNotifications( + @Path("inAppRecipientId") id: String, + @Header("Authorization") token: String, + @Query("page") page: Int? = null, + @Query("size") size: Int? = null, + @Query("start") start: String? = null, + @Query("end") end: String? = null, + @Query("isRead") isRead: Boolean? = null, + @Query("sort") sort: String? = "createdAt", + ): Response + + @PATCH("api/v2/in-app/recipients/{inAppRecipientId}/notifications/{inAppNotificationId}") + suspend fun markAsReadById( + @Path("inAppRecipientId") id: String, + @Header("Authorization") token: String, + @Path("inAppNotificationId") notificationId: String, + @Body data: MarkAsReadBody, + ): Response + + @POST("api/v2/in-app/recipients/{inAppRecipientId}/notifications/bulk-update") + suspend fun markAllAsRead( + @Path("inAppRecipientId") id: String, + @Header("Authorization") token: String, + @Body data: BulkUpdateBody, + ): Response + + @PATCH("api/v2/in-app/recipients/{inAppRecipientId}") + suspend fun markAsViewed( + @Path("inAppRecipientId") id: String, + @Header("Authorization") token: String, + @Body data: MarkAsViewedBody, + ): Response + + @DELETE("api/v2/in-app/recipients/{inAppRecipientId}/notifications/{inAppNotificationId}") + suspend fun deleteNotificationById( + @Path("inAppRecipientId") id: String, + @Header("Authorization") token: String, + @Path("inAppNotificationId") notificationId: String, + ): Response + + @POST("api/v2/in-app/recipients/{inAppRecipientId}/notifications/bulk-update") + suspend fun clearAllNotifications( + @Path("inAppRecipientId") id: String, + @Header("Authorization") token: String, + @Body data: BulkUpdateBody, + ): Response +} diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/state/AllNotificationState.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/state/AllNotificationState.kt new file mode 100644 index 0000000..2df905e --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/state/AllNotificationState.kt @@ -0,0 +1,9 @@ +package com.keyvalue.siren.androidsdk.data.state + +import com.keyvalue.siren.androidsdk.data.model.AllNotificationResponseData +import org.json.JSONObject + +class AllNotificationState( + var errorResponse: JSONObject? = null, + var allNotificationResponse: List? = null, +) diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/state/ClearAllNotificationsState.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/state/ClearAllNotificationsState.kt new file mode 100644 index 0000000..0a5f521 --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/state/ClearAllNotificationsState.kt @@ -0,0 +1,9 @@ +package com.keyvalue.siren.androidsdk.data.state + +import com.keyvalue.siren.androidsdk.data.model.DataStatus +import org.json.JSONObject + +class ClearAllNotificationsState( + var errorResponse: JSONObject? = null, + var clearAllNotificationsResponse: DataStatus? = null, +) diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/state/DeleteNotificationByIdState.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/state/DeleteNotificationByIdState.kt new file mode 100644 index 0000000..f1cc356 --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/state/DeleteNotificationByIdState.kt @@ -0,0 +1,10 @@ +package com.keyvalue.siren.androidsdk.data.state + +import com.keyvalue.siren.androidsdk.data.model.DataStatus +import org.json.JSONObject + +data class DeleteNotificationByIdState( + var errorResponse: JSONObject? = null, + var deleteStatus: DataStatus? = null, + var deleteId: String? = null, +) diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/state/MarkAllAsReadState.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/state/MarkAllAsReadState.kt new file mode 100644 index 0000000..778d24b --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/state/MarkAllAsReadState.kt @@ -0,0 +1,9 @@ +package com.keyvalue.siren.androidsdk.data.state + +import com.keyvalue.siren.androidsdk.data.model.DataStatus +import org.json.JSONObject + +class MarkAllAsReadState( + var errorResponse: JSONObject? = null, + var markAllAsReadResponse: DataStatus? = null, +) diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/state/MarkAsReadByIdState.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/state/MarkAsReadByIdState.kt new file mode 100644 index 0000000..62d6ab8 --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/state/MarkAsReadByIdState.kt @@ -0,0 +1,9 @@ +package com.keyvalue.siren.androidsdk.data.state + +import com.keyvalue.siren.androidsdk.data.model.MarkAsReadByIdResponseData +import org.json.JSONObject + +class MarkAsReadByIdState( + var errorResponse: JSONObject? = null, + var markAsReadByIdResponse: MarkAsReadByIdResponseData? = null, +) diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/state/MarkAsViewedViewedState.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/state/MarkAsViewedViewedState.kt new file mode 100644 index 0000000..6237b40 --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/state/MarkAsViewedViewedState.kt @@ -0,0 +1,9 @@ +package com.keyvalue.siren.androidsdk.data.state + +import com.keyvalue.siren.androidsdk.data.model.MarkAsViewedResponseData +import org.json.JSONObject + +class MarkAsViewedViewedState( + var errorResponse: JSONObject? = null, + var markAsViewedResponse: MarkAsViewedResponseData? = null, +) diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/state/NotificationUnViewedState.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/state/NotificationUnViewedState.kt new file mode 100644 index 0000000..fbceabf --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/data/state/NotificationUnViewedState.kt @@ -0,0 +1,9 @@ +package com.keyvalue.siren.androidsdk.data.state + +import com.keyvalue.siren.androidsdk.data.model.UnViewedNotificationResponseData +import org.json.JSONObject + +class NotificationUnViewedState( + var errorResponse: JSONObject? = null, + var notificationUnViewedResponse: UnViewedNotificationResponseData? = null, +) diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/helper/client/callbacks/MarkAllAsReadCallback.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/helper/client/callbacks/MarkAllAsReadCallback.kt new file mode 100644 index 0000000..2dada76 --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/helper/client/callbacks/MarkAllAsReadCallback.kt @@ -0,0 +1,10 @@ +package com.keyvalue.siren.androidsdk.helper.client.callbacks + +import com.keyvalue.siren.androidsdk.data.model.AllNotificationResponseData +import org.json.JSONObject + +interface MarkAllAsReadCallback { + fun onSuccess(responseData: AllNotificationResponseData) + + fun onError(jsonObject: JSONObject) +} diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/helper/client/callbacks/MarkAsReadByIdCallback.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/helper/client/callbacks/MarkAsReadByIdCallback.kt new file mode 100644 index 0000000..17c9dde --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/helper/client/callbacks/MarkAsReadByIdCallback.kt @@ -0,0 +1,10 @@ +package com.keyvalue.siren.androidsdk.helper.client.callbacks + +import com.keyvalue.siren.androidsdk.data.model.MarkAsReadByIdResponseData +import org.json.JSONObject + +interface MarkAsReadByIdCallback { + fun onSuccess(responseData: MarkAsReadByIdResponseData?) + + fun onError(jsonObject: JSONObject) +} diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/presenter/NotificationPresenter.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/presenter/NotificationPresenter.kt new file mode 100644 index 0000000..6cc4607 --- /dev/null +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/presenter/NotificationPresenter.kt @@ -0,0 +1,236 @@ +package com.keyvalue.siren.androidsdk.presenter + +import com.keyvalue.siren.androidsdk.data.managers.NotificationManager +import com.keyvalue.siren.androidsdk.data.model.AllNotificationResponseData +import com.keyvalue.siren.androidsdk.data.model.DataStatus +import com.keyvalue.siren.androidsdk.data.model.MarkAsReadByIdResponseData +import com.keyvalue.siren.androidsdk.data.model.MarkAsViewedResponseData +import com.keyvalue.siren.androidsdk.data.model.UnViewedNotificationResponseData +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext +import org.json.JSONObject + +class NotificationPresenter( + private var userToken: String, + private var recipientId: String, +) : BasePresenter() { + private var notificationManager: NotificationManager? = null + + init { + notificationManager = NotificationManager(baseURL) + } + + fun fetchUnViewedNotificationsCount(callback: (UnViewedNotificationResponseData?, JSONObject?, Boolean) -> Unit) { + CoroutineScope(Dispatchers.IO).launch { + notificationManager?.fetchUnViewedNotificationsCount(userToken, recipientId) + notificationManager?.notificationUnViewedState?.collect { notificationUnViewedState -> + if (notificationUnViewedState?.errorResponse == null) { + notificationUnViewedState?.notificationUnViewedResponse?.let { response -> + callback( + response, + null, + false, + ) + } + } else { + callback( + null, + notificationUnViewedState.errorResponse, + true, + ) + } + } + } + } + + fun fetchAllNotifications( + page: Int? = null, + size: Int? = null, + start: String? = null, + end: String? = null, + isRead: Boolean? = null, + callback: (List, JSONObject?, Boolean) -> Unit, + ) { + CoroutineScope(Dispatchers.IO).launch { + notificationManager?.fetchAllNotifications( + userToken = userToken, + recipientId = recipientId, + page = page, + size = size, + start = start, + end = end, + isRead = isRead, + ) + notificationManager?.allNotificationsState?.collect { allNotificationState -> + if (allNotificationState?.errorResponse == null) { + allNotificationState?.allNotificationResponse?.let { response -> + withContext(Dispatchers.Main) { + callback( + response, + null, + false, + ) + } + } + } else { + withContext(Dispatchers.Main) { + callback( + emptyList(), + allNotificationState.errorResponse, + true, + ) + } + } + } + } + } + + fun markAsReadById( + callback: (data: MarkAsReadByIdResponseData?, error: JSONObject?, isError: Boolean) -> Unit, + notificationId: String, + ) { + CoroutineScope(Dispatchers.IO).launch { + notificationManager?.markAsReadById(userToken, recipientId, notificationId) + + notificationManager?.markAsReadByIdState?.collect { markAsReadByIdState -> + if (markAsReadByIdState?.errorResponse == null) { + markAsReadByIdState?.markAsReadByIdResponse?.let { response -> + withContext(Dispatchers.Main) { + callback( + response, + null, + false, + ) + } + } + } else { + callback( + null, + markAsReadByIdState.errorResponse, + true, + ) + } + } + } + } + + fun markAllAsRead( + callback: (data: DataStatus?, error: JSONObject?, isError: Boolean) -> Unit, + startDate: String, + ) { + CoroutineScope(Dispatchers.IO).launch { + notificationManager?.markAllAsRead(userToken, recipientId, startDate) + + notificationManager?.markAllAsReadState?.collect { markAllAsReadState -> + if (markAllAsReadState?.errorResponse == null) { + markAllAsReadState?.markAllAsReadResponse?.let { response -> + withContext(Dispatchers.Main) { + callback( + response, + null, + false, + ) + } + } + } else { + callback( + null, + markAllAsReadState.errorResponse, + true, + ) + } + } + } + } + + fun markAsViewed( + callback: (data: MarkAsViewedResponseData?, error: JSONObject?, isError: Boolean) -> Unit, + startDate: String, + ) { + CoroutineScope(Dispatchers.IO).launch { + notificationManager?.markAsViewed(userToken, recipientId, startDate) + + notificationManager?.markAsViewedState?.collect { markAsViewedState -> + if (markAsViewedState?.errorResponse == null) { + markAsViewedState?.markAsViewedResponse?.let { response -> + withContext(Dispatchers.Main) { + callback( + response, + null, + false, + ) + } + } + } else { + callback( + null, + markAsViewedState.errorResponse, + true, + ) + } + } + } + } + + fun deleteNotificationById( + notificationId: String, + callback: (data: DataStatus?, deleteId: String?, error: JSONObject?, isError: Boolean) -> Unit, + ) { + CoroutineScope(Dispatchers.IO).launch { + notificationManager?.deleteNotificationById(userToken, recipientId, notificationId) + + notificationManager?.deleteNotificationByIdState?.collect { clearAllNotificationsState -> + if (clearAllNotificationsState?.errorResponse == null) { + clearAllNotificationsState?.deleteStatus?.let { response -> + withContext(Dispatchers.Main) { + callback( + response, + notificationId, + null, + false, + ) + } + } + } else { + callback( + null, + null, + clearAllNotificationsState.errorResponse, + true, + ) + } + } + } + } + + fun clearAllNotifications( + callback: (data: DataStatus?, error: JSONObject?, isError: Boolean) -> Unit, + startDate: String, + ) { + CoroutineScope(Dispatchers.IO).launch { + notificationManager?.clearAllNotifications(userToken, recipientId, startDate) + + notificationManager?.clearAllNotificationsState?.collect { clearAllNotificationsState -> + if (clearAllNotificationsState?.errorResponse == null) { + clearAllNotificationsState?.clearAllNotificationsResponse?.let { response -> + withContext(Dispatchers.Main) { + callback( + response, + null, + false, + ) + } + } + } else { + callback( + null, + clearAllNotificationsState.errorResponse, + true, + ) + } + } + } + } +} diff --git a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/utils/constants/Constants.kt b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/utils/constants/Constants.kt index ce4ca9a..5977181 100644 --- a/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/utils/constants/Constants.kt +++ b/siren-sdk/src/main/java/com/keyvalue/siren/androidsdk/utils/constants/Constants.kt @@ -10,3 +10,8 @@ enum class SirenErrorTypes { /** Network error. */ NETWORK_ERROR, } + +enum class BulkUpdateType { + MARK_AS_READ, + MARK_AS_DELETED, +}