diff --git a/pushsdk/build.gradle b/pushsdk/build.gradle index 234ec9a..c4d8872 100644 --- a/pushsdk/build.gradle +++ b/pushsdk/build.gradle @@ -9,7 +9,7 @@ android { minSdkVersion 16 targetSdkVersion 29 versionCode 1 - versionName "0.1.1" + versionName "0.1.2" consumerProguardFiles 'consumer-rules.pro' } diff --git a/pushsdk/src/main/java/ir/ayantech/pushsdk/core/AyanNotification.kt b/pushsdk/src/main/java/ir/ayantech/pushsdk/core/AyanNotification.kt index 087ca03..366746f 100644 --- a/pushsdk/src/main/java/ir/ayantech/pushsdk/core/AyanNotification.kt +++ b/pushsdk/src/main/java/ir/ayantech/pushsdk/core/AyanNotification.kt @@ -7,6 +7,7 @@ import ir.ayantech.pushsdk.helper.NotificationUtils import ir.ayantech.pushsdk.model.Message import ir.ayantech.pushsdk.model.MessageDeserializer import ir.ayantech.pushsdk.model.api.NotificationObject +import ir.ayantech.pushsdk.networking.BooleanCallBack import ir.ayantech.pushsdk.networking.NotificationObjectsCallBack import ir.ayantech.pushsdk.networking.PushNotificationNetworking import ir.ayantech.pushsdk.networking.SimpleCallBack @@ -92,7 +93,7 @@ object AyanNotification { getNotificationList(itemCount, 0, notificationObjectsCallBack) } - fun removeAllNotifications(success: SimpleCallBack) { + fun removeAllNotifications(success: BooleanCallBack) { PushNotificationNetworking.removeAllNotifications(success) } @@ -101,29 +102,36 @@ object AyanNotification { offset: Long, notificationObjectsCallBack: NotificationObjectsCallBack ) { - PushNotificationNetworking.getNotificationsList(itemCount, offset) { + PushNotificationNetworking.getNotificationsList(itemCount, offset) { success, output -> var nextPageClosure: SimpleCallBack? = null - if (it.HasMore) { - nextPageClosure = { - getNotificationList(itemCount, it.NextOffset, notificationObjectsCallBack) + output?.let { + if (!success) { + notificationObjectsCallBack(success, 0L, 0L, listOf(), null) + return@let } + if (it.HasMore) { + nextPageClosure = { + getNotificationList(itemCount, it.NextOffset, notificationObjectsCallBack) + } + } + notificationObjectsCallBack( + success, + it.TotalCount, + it.UnSeenCount, + it.Notifications.map { + NotificationObject( + it.NotificationID, + MessageDeserializer.stringToMessage( + it.Notification.Data.message, + it.NotificationID.toString() + ), + it.Seen, + it.SendDateTime + ) + }, + nextPageClosure + ) } - notificationObjectsCallBack( - it.TotalCount, - it.UnSeenCount, - it.Notifications.map { - NotificationObject( - it.NotificationID, - MessageDeserializer.stringToMessage( - it.Notification.Data.message, - it.NotificationID.toString() - ), - it.Seen, - it.SendDateTime - ) - }, - nextPageClosure - ) } } } \ No newline at end of file diff --git a/pushsdk/src/main/java/ir/ayantech/pushsdk/model/api/NotificationObject.kt b/pushsdk/src/main/java/ir/ayantech/pushsdk/model/api/NotificationObject.kt index 9b37cc8..4bdf8da 100644 --- a/pushsdk/src/main/java/ir/ayantech/pushsdk/model/api/NotificationObject.kt +++ b/pushsdk/src/main/java/ir/ayantech/pushsdk/model/api/NotificationObject.kt @@ -6,8 +6,8 @@ import ir.ayantech.pushsdk.helper.ImageHelper import ir.ayantech.pushsdk.model.Message import ir.ayantech.pushsdk.model.MessageDeserializer import ir.ayantech.pushsdk.model.action.PushNotificationAction +import ir.ayantech.pushsdk.networking.BooleanCallBack import ir.ayantech.pushsdk.networking.PushNotificationNetworking -import ir.ayantech.pushsdk.networking.SimpleCallBack class NotificationObject( private val notificationId: Long?, @@ -33,24 +33,31 @@ class NotificationObject( message?.action?.doAction() } - fun getNotificationDetail(callback: (NotificationObject<*>) -> Unit) { + fun getNotificationDetail(callback: (success: Boolean, notificationObject: NotificationObject<*>?) -> Unit) { if (notificationId == null) return - PushNotificationNetworking.getNotificationDetail(notificationId) { - callback( - NotificationObject( - it.NotificationID, - MessageDeserializer.stringToMessage( - it.Notification.Data.message, - "" - ), - it.Seen, - it.SendDateTime + PushNotificationNetworking.getNotificationDetail(notificationId) { success, output -> + if (!success) { + callback(success, null) + return@getNotificationDetail + } + output?.let { + callback( + success, + NotificationObject( + it.NotificationID, + MessageDeserializer.stringToMessage( + it.Notification.Data.message, + "" + ), + it.Seen, + it.SendDateTime + ) ) - ) + } } } - fun remove(success: SimpleCallBack) { + fun remove(success: BooleanCallBack) { if (notificationId == null) return PushNotificationNetworking.removeNotification(notificationId, success) } diff --git a/pushsdk/src/main/java/ir/ayantech/pushsdk/networking/PushNotificationNetworking.kt b/pushsdk/src/main/java/ir/ayantech/pushsdk/networking/PushNotificationNetworking.kt index 1a526a4..9ef0a57 100644 --- a/pushsdk/src/main/java/ir/ayantech/pushsdk/networking/PushNotificationNetworking.kt +++ b/pushsdk/src/main/java/ir/ayantech/pushsdk/networking/PushNotificationNetworking.kt @@ -15,7 +15,10 @@ import ir.ayantech.pushsdk.storage.PushNotificationUser typealias SimpleCallBack = () -> Unit +typealias BooleanCallBack = (Boolean) -> Unit + typealias NotificationObjectsCallBack = ( + success: Boolean, totalCount: Long, unSeenCount: Long, notifications: List>, @@ -70,19 +73,22 @@ object PushNotificationNetworking { } fun reportDeviceMobileNumber(mobileNumber: String) { - simpleCall( + ayanApi.ayanCall( + AyanCallStatus { + success { + PreferencesManager.saveToSharedPreferences(Constants.SERVER_NOTIFIED_MOBILE, true) + Log.d( + "AyanPush", + "User mobile number successfully reported to the server." + ) + } + }, EndPoint.ReportDeviceMobileNumber, ReportDeviceMobileNumberInput( mobileNumber, PushNotificationUser.getPushNotificationToken() ) - ) { - PreferencesManager.saveToSharedPreferences(Constants.SERVER_NOTIFIED_MOBILE, true) - Log.d( - "AyanPush", - "User mobile number successfully reported to the server." - ) - } + ) } fun reportDeviceReceivedNotificationStatus( @@ -90,75 +96,91 @@ object PushNotificationNetworking { status: String, extraInfo: Any? = null ) { - simpleCall( + ayanApi.ayanCall( + AyanCallStatus { + success { + Log.d( + "AyanPush", + "Message status with $status status successfully reported to the server." + ) + } + }, EndPoint.ReportDeviceReceivedNotificationStatus, ReportDeviceReceivedNotificationStatusInput(extraInfo, messageId, status) - ) { - Log.d( - "AyanPush", - "Message status with $status status successfully reported to the server." - ) - } + ) } fun getNotificationsList( itemCount: Long, offset: Long = 0L, - callback: (GetNotificationsListOutput) -> Unit + callback: (success: Boolean, output: GetNotificationsListOutput?) -> Unit ) { - simpleCall( + ayanApi.ayanCall( + AyanCallStatus { + success { + it.response?.Parameters?.let { callback(true, it) } + } + failure { + callback(false, null) + } + }, EndPoint.GetNotificationsList, GetNotificationsListInput( itemCount, offset, PushNotificationUser.getPushNotificationToken() ) - ) { - it?.let { callback(it) } - } + ) } - fun getNotificationDetail(notificationId: Long, callback: (GetNotificationDetailOutput) -> Unit) { - simpleCall( + fun getNotificationDetail( + notificationId: Long, + callback: (success: Boolean, output: GetNotificationDetailOutput?) -> Unit + ) { + ayanApi.ayanCall( + AyanCallStatus { + success { + it.response?.Parameters?.let { callback(true, it) } + } + failure { + callback(false, null) + } + }, EndPoint.GetNotificationDetail, - GetNotificationDetailInput(notificationId, PushNotificationUser.getPushNotificationToken()) - ) { - it?.let { callback(it) } - } + GetNotificationDetailInput( + notificationId, + PushNotificationUser.getPushNotificationToken() + ) + ) } - fun removeNotification(notificationId: Long, success: SimpleCallBack) { - simpleCall( + fun removeNotification(notificationId: Long, callback: BooleanCallBack) { + ayanApi.ayanCall( + AyanCallStatus { + success { + callback(true) + } + failure { + callback(false) + } + }, EndPoint.RemoveNotification, RemoveNotificationInput(notificationId, PushNotificationUser.getPushNotificationToken()) - ) { - success() - } - } - - fun removeAllNotifications(success: SimpleCallBack) { - simpleCall( - EndPoint.RemoveAllNotifications, - RemoveAllNotificationsInput(PushNotificationUser.getPushNotificationToken()) - ) { - success() - } + ) } - private inline fun simpleCall( - endPoint: String, - input: Any? = null, - crossinline onSuccess: (GenericOutput?) -> Unit - ) { - ayanApi.ayanCall( + fun removeAllNotifications(callback: BooleanCallBack) { + ayanApi.ayanCall( AyanCallStatus { success { - onSuccess(it.response?.Parameters) + callback(true) + } + failure { + callback(false) } }, - endPoint, - input, - hasIdentity = false + EndPoint.RemoveAllNotifications, + RemoveAllNotificationsInput(PushNotificationUser.getPushNotificationToken()) ) } } \ No newline at end of file