Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add notifications apis #10

Merged
merged 14 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<NotificationUnViewedState?> =
MutableStateFlow(null)
var allNotificationsState: MutableStateFlow<AllNotificationState?> =
MutableStateFlow(null)
var markAsReadByIdState: MutableStateFlow<MarkAsReadByIdState?> =
MutableStateFlow(null)
var markAllAsReadState: MutableStateFlow<MarkAllAsReadState?> =
MutableStateFlow(null)
var markAsViewedState: MutableStateFlow<MarkAsViewedViewedState?> =
MutableStateFlow(null)
var deleteNotificationByIdState: MutableStateFlow<DeleteNotificationByIdState?> =
MutableStateFlow(null)
var clearAllNotificationsState: MutableStateFlow<ClearAllNotificationsState?> =
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,
)
[email protected](
notificationUnViewedState,
)
}

override suspend fun onError(errorObject: JSONObject) {
val notificationUnViewedState =
NotificationUnViewedState(
notificationUnViewedResponse = null,
errorResponse = errorObject,
)
[email protected](
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<AllNotificationResponseData>,
errorResponse = null,
)
[email protected](
allNotificationsState,
)
}

override suspend fun onError(errorObject: JSONObject) {
val allNotificationsState =
AllNotificationState(
allNotificationResponse = null,
errorResponse = errorObject,
)
[email protected](
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,
)
[email protected](
markAsReadByIdState,
)
}

override suspend fun onError(errorObject: JSONObject) {
val markAsReadByIdState =
MarkAsReadByIdState(
markAsReadByIdResponse = null,
errorResponse = errorObject,
)
[email protected](
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,
)
[email protected](
markAllAsReadState,
)
}

override suspend fun onError(errorObject: JSONObject) {
val markAllAsReadState =
MarkAllAsReadState(
markAllAsReadResponse = null,
errorResponse = errorObject,
)
[email protected](
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,
)
[email protected](
markAsViewedViewedState,
)
}

override suspend fun onError(errorObject: JSONObject) {
val markAsViewedViewedState =
MarkAsViewedViewedState(
markAsViewedResponse = null,
errorResponse = errorObject,
)
[email protected](
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,
)
[email protected](
deleteNotificationByIdState,
)
}

override suspend fun onError(errorObject: JSONObject) {
val deleteNotificationByIdState =
DeleteNotificationByIdState(
deleteStatus = null,
errorResponse = errorObject,
)
[email protected](
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,
)
[email protected](
clearAllNotificationsState,
)
}

override suspend fun onError(errorObject: JSONObject) {
val clearAllNotificationsState =
ClearAllNotificationsState(
clearAllNotificationsResponse = null,
errorResponse = errorObject,
)
[email protected](
clearAllNotificationsState,
)
}
},
)
}
}
Original file line number Diff line number Diff line change
@@ -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<AllNotificationResponseData?>?,
@SerializedName("error")
val error: ErrorResponse?,
)
Original file line number Diff line number Diff line change
@@ -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,
)
Original file line number Diff line number Diff line change
@@ -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?,
)
Original file line number Diff line number Diff line change
@@ -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?,
)
Original file line number Diff line number Diff line change
@@ -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?,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.keyvalue.siren.androidsdk.data.model

data class DataStatus(
val status: String,
)
Original file line number Diff line number Diff line change
@@ -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?,
)
Loading