forked from openedx/openedx-app-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Notifications Inbox Screen (openedx#77)
Fixes: LEARNER-10345
- Loading branch information
1 parent
9221aec
commit 0a65db2
Showing
21 changed files
with
965 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
notifications/src/main/java/org/openedx/notifications/data/api/NotificationsApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,17 @@ | ||
package org.openedx.notifications.data.api | ||
|
||
import org.openedx.notifications.data.model.InboxNotificationsResponse | ||
import org.openedx.notifications.data.model.NotificationsCountResponse | ||
import retrofit2.http.GET | ||
import retrofit2.http.Query | ||
|
||
interface NotificationsApi { | ||
@GET(APIConstants.NOTIFICATION_COUNT) | ||
suspend fun getUnreadNotificationsCount(): NotificationsCountResponse | ||
|
||
@GET(APIConstants.NOTIFICATIONS_INBOX) | ||
suspend fun getInboxNotifications( | ||
@Query("app_name") appName: String, | ||
@Query("page") page: Int, | ||
): InboxNotificationsResponse | ||
} |
113 changes: 113 additions & 0 deletions
113
...ications/src/main/java/org/openedx/notifications/data/model/InboxNotificationsResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package org.openedx.notifications.data.model | ||
|
||
import com.google.gson.annotations.SerializedName | ||
import org.openedx.core.utils.TimeUtils | ||
import org.openedx.notifications.domain.model.InboxNotifications | ||
import org.openedx.notifications.domain.model.InboxSection | ||
import org.openedx.notifications.domain.model.Pagination | ||
import java.util.Date | ||
import org.openedx.notifications.domain.model.NotificationContent as DomainNotificationContent | ||
import org.openedx.notifications.domain.model.NotificationItem as DomainNotificationItem | ||
|
||
|
||
data class InboxNotificationsResponse( | ||
@SerializedName("next") val next: String?, | ||
@SerializedName("previous") val previous: String?, | ||
@SerializedName("count") val count: Int, | ||
@SerializedName("num_pages") val numPages: Int, | ||
@SerializedName("current_page") val currentPage: Int, | ||
@SerializedName("start") val start: Int, | ||
@SerializedName("results") val results: List<NotificationItem>, | ||
) { | ||
fun mapToDomain(): InboxNotifications = InboxNotifications( | ||
pagination = Pagination( | ||
next = next.orEmpty(), | ||
previous = previous.orEmpty(), | ||
count = count, | ||
numPages = numPages, | ||
currentPage = currentPage, | ||
start = start, | ||
), | ||
notifications = organizeNotificationsBySection() | ||
) | ||
|
||
private fun organizeNotificationsBySection(): Map<InboxSection, List<DomainNotificationItem>> { | ||
val currentDate = Date() | ||
val recentThresholdMillis = currentDate.time - DAY_IN_MILLIS | ||
val weekThresholdMillis = currentDate.time - WEEK_IN_MILLIS | ||
|
||
val notifications = results.map { it.mapToDomain() } | ||
|
||
return mapOf( | ||
InboxSection.RECENT to notifications.filter { | ||
(it.created?.time ?: 0L) >= recentThresholdMillis | ||
}, | ||
InboxSection.THIS_WEEK to notifications.filter { | ||
val createdTime = it.created?.time ?: 0L | ||
createdTime in weekThresholdMillis until recentThresholdMillis | ||
}, | ||
InboxSection.OLDER to notifications.filter { | ||
(it.created?.time ?: 0L) < weekThresholdMillis | ||
} | ||
) | ||
} | ||
|
||
companion object { | ||
private const val DAY_IN_MILLIS = 24 * 60 * 60 * 1000L | ||
private const val WEEK_IN_MILLIS = 7 * DAY_IN_MILLIS | ||
} | ||
} | ||
|
||
data class NotificationItem( | ||
@SerializedName("id") val id: Int, | ||
@SerializedName("app_name") val appName: String, | ||
@SerializedName("notification_type") val notificationType: String, | ||
@SerializedName("content_context") val contentContext: NotificationContent, | ||
@SerializedName("content") val content: String, | ||
@SerializedName("content_url") val contentUrl: String, | ||
@SerializedName("last_read") val lastRead: String?, | ||
@SerializedName("last_seen") val lastSeen: String?, | ||
@SerializedName("created") val created: String, | ||
) { | ||
fun mapToDomain(): DomainNotificationItem = DomainNotificationItem( | ||
id = id, | ||
appName = appName, | ||
notificationType = notificationType, | ||
contentContext = contentContext.mapToDomain(), | ||
content = content, | ||
contentUrl = contentUrl, | ||
lastRead = TimeUtils.iso8601ToDate(lastRead ?: ""), | ||
lastSeen = TimeUtils.iso8601ToDate(lastSeen ?: ""), | ||
created = TimeUtils.iso8601ToDate(created), | ||
) | ||
} | ||
|
||
data class NotificationContent( | ||
@SerializedName("p") val paragraph: String, | ||
@SerializedName("strong") val strongText: String, | ||
@SerializedName("topic_id") val topicId: String?, | ||
@SerializedName("parent_id") val parentId: String?, | ||
@SerializedName("thread_id") val threadId: String?, | ||
@SerializedName("comment_id") val commentId: String?, | ||
@SerializedName("post_title") val postTitle: String, | ||
@SerializedName("course_name") val courseName: String, | ||
@SerializedName("replier_name") val replierName: String, | ||
@SerializedName("email_content") val emailContent: String, | ||
@SerializedName("author_name") val authorName: String?, | ||
@SerializedName("author_pronoun") val authorPronoun: String?, | ||
) { | ||
fun mapToDomain(): DomainNotificationContent = DomainNotificationContent( | ||
paragraph = paragraph, | ||
strongText = strongText, | ||
topicId = topicId.orEmpty(), | ||
parentId = parentId.orEmpty(), | ||
threadId = threadId.orEmpty(), | ||
commentId = commentId.orEmpty(), | ||
postTitle = postTitle, | ||
courseName = courseName, | ||
replierName = replierName, | ||
emailContent = emailContent, | ||
authorName = authorName.orEmpty(), | ||
authorPronoun = authorPronoun.orEmpty(), | ||
) | ||
} |
9 changes: 9 additions & 0 deletions
9
...ations/src/main/java/org/openedx/notifications/data/repository/NotificationsRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,19 @@ | ||
package org.openedx.notifications.data.repository | ||
|
||
import org.openedx.notifications.data.api.APIConstants | ||
import org.openedx.notifications.data.api.NotificationsApi | ||
import org.openedx.notifications.domain.model.InboxNotifications | ||
import org.openedx.notifications.domain.model.NotificationsCount | ||
|
||
class NotificationsRepository(private val api: NotificationsApi) { | ||
suspend fun getUnreadNotificationsCount(): NotificationsCount { | ||
return api.getUnreadNotificationsCount().mapToDomain() | ||
} | ||
|
||
suspend fun getInboxNotifications(page: Int): InboxNotifications { | ||
return api.getInboxNotifications( | ||
appName = APIConstants.APP_NAME_DISCUSSION, | ||
page = page | ||
).mapToDomain() | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...ions/src/main/java/org/openedx/notifications/domain/interactor/NotificationsInteractor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
package org.openedx.notifications.domain.interactor | ||
|
||
import org.openedx.notifications.data.repository.NotificationsRepository | ||
import org.openedx.notifications.domain.model.InboxNotifications | ||
import org.openedx.notifications.domain.model.NotificationsCount | ||
|
||
class NotificationsInteractor(private val repository: NotificationsRepository) { | ||
|
||
suspend fun getUnreadNotificationsCount(): NotificationsCount { | ||
return repository.getUnreadNotificationsCount() | ||
} | ||
|
||
suspend fun getInboxNotifications(page: Int): InboxNotifications { | ||
return repository.getInboxNotifications(page) | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
notifications/src/main/java/org/openedx/notifications/domain/model/InboxNotifications.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.openedx.notifications.domain.model | ||
|
||
import java.util.Date | ||
|
||
data class InboxNotifications( | ||
val pagination: Pagination, | ||
val notifications: Map<InboxSection, List<NotificationItem>>, | ||
) | ||
|
||
data class Pagination( | ||
val next: String, | ||
val previous: String, | ||
val count: Int, | ||
val numPages: Int, | ||
val currentPage: Int, | ||
val start: Int, | ||
) | ||
|
||
data class NotificationItem( | ||
val id: Int, | ||
val appName: String, | ||
val notificationType: String, | ||
val contentContext: NotificationContent, | ||
val content: String, | ||
val contentUrl: String, | ||
val lastRead: Date?, | ||
val lastSeen: Date?, | ||
val created: Date?, | ||
) | ||
|
||
data class NotificationContent( | ||
val paragraph: String, | ||
val strongText: String, | ||
val topicId: String, | ||
val parentId: String, | ||
val threadId: String, | ||
val commentId: String, | ||
val postTitle: String, | ||
val courseName: String, | ||
val replierName: String, | ||
val emailContent: String, | ||
val authorName: String, | ||
val authorPronoun: String, | ||
) |
11 changes: 11 additions & 0 deletions
11
notifications/src/main/java/org/openedx/notifications/domain/model/InboxSection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.openedx.notifications.domain.model | ||
|
||
import org.openedx.notifications.R | ||
|
||
enum class InboxSection(val titleResId: Int) { | ||
RECENT(R.string.notifications_recent), | ||
|
||
THIS_WEEK(R.string.notifications_this_week), | ||
|
||
OLDER(R.string.notifications_older); | ||
} |
Oops, something went wrong.