Skip to content

Commit

Permalink
Merge pull request #6 from KeyValueSoftwareSystems/feat-mark-all-as-r…
Browse files Browse the repository at this point in the history
…ead-api

feat: Add mark all as read api
  • Loading branch information
varghese-babu authored Mar 15, 2024
2 parents 7898945 + cdfca11 commit 83db90e
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
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.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.MarkAllAsReadState
import com.keyvalue.siren.androidsdk.data.state.MarkAsReadByIdState
import com.keyvalue.siren.androidsdk.data.state.NotificationUnViewedState
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -21,6 +23,8 @@ class NotificationManager(baseURL: String) {
MutableStateFlow(null)
var markAsReadByIdState: MutableStateFlow<MarkAsReadByIdState?> =
MutableStateFlow(null)
var markAllAsReadState: MutableStateFlow<MarkAllAsReadState?> =
MutableStateFlow(null)

suspend fun fetchUnViewedNotificationsCount(
userToken: String,
Expand Down Expand Up @@ -134,4 +138,40 @@ class NotificationManager(baseURL: String) {
},
)
}

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,
)
}
},
)
}
}
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,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,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?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ interface NotificationRepository {
notificationId: String,
networkCallback: NetworkCallback,
)

suspend fun markAllAsRead(
userToken: String,
recipientId: String,
startDate: String,
networkCallback: NetworkCallback,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ 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.MarkAllAsReadResponse
import com.keyvalue.siren.androidsdk.data.model.MarkAsReadBody
import com.keyvalue.siren.androidsdk.data.model.MarkAsReadByIdResponse
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
Expand Down Expand Up @@ -169,4 +172,53 @@ class NotificationRepositoryImplementation(baseURL: String) : NotificationReposi
)
}
}

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<MarkAllAsReadResponse>(
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),
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
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.MarkAllAsReadResponse
import com.keyvalue.siren.androidsdk.data.model.MarkAsReadBody
import com.keyvalue.siren.androidsdk.data.model.MarkAsReadByIdResponse
import com.keyvalue.siren.androidsdk.data.model.UnViewedNotificationResponse
Expand All @@ -9,6 +11,7 @@ import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.Header
import retrofit2.http.PATCH
import retrofit2.http.POST
import retrofit2.http.Path
import retrofit2.http.Query

Expand Down Expand Up @@ -38,4 +41,11 @@ interface NotificationApiService {
@Path("inAppNotificationId") notificationId: String,
@Body data: MarkAsReadBody,
): Response<MarkAsReadByIdResponse>

@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<MarkAllAsReadResponse>
}
Original file line number Diff line number Diff line change
@@ -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,
)
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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.UnViewedNotificationResponseData
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -113,4 +114,33 @@ class NotificationPresenter(
}
}
}

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,
)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ enum class SirenErrorTypes {
/** Network error. */
NETWORK_ERROR,
}

enum class BulkUpdateType {
MARK_AS_READ,
MARK_AS_DELETED,
}

0 comments on commit 83db90e

Please sign in to comment.