@@ -23,6 +23,17 @@ import org.mifos.mobile.core.data.util.NetworkMonitor
2323import org.mifos.mobile.core.model.entity.MifosNotification
2424import org.mifos.mobile.feature.notification.NotificationUiState.Loading
2525
26+ /* *
27+ * The ViewModel for the Notification screen.
28+ *
29+ * This class is responsible for the business logic of the Notification screen. It fetches
30+ * notifications from the [NotificationRepository], manages the UI state, and handles user
31+ * interactions like refreshing the list and dismissing notifications. It's also aware of the
32+ * network status, thanks to [NetworkMonitor].
33+ *
34+ * @param notificationRepositoryImp The repository that provides access to notification data.
35+ * @param networkMonitor A utility to monitor the device's network connectivity.
36+ */
2637internal class NotificationViewModel (
2738 private val notificationRepositoryImp : NotificationRepository ,
2839 networkMonitor : NetworkMonitor ,
@@ -48,6 +59,10 @@ internal class NotificationViewModel(
4859 }
4960 }
5061
62+ /* *
63+ * Kicks off the process of loading notifications from the repository. This function updates the
64+ * UI state to reflect the current status of the operation, such as Loading, Success, or Error.
65+ */
5166 fun loadNotifications () {
5267 _notificationUiState .value = Loading
5368 viewModelScope.launch {
@@ -78,18 +93,36 @@ internal class NotificationViewModel(
7893 }
7994 }
8095
96+ /* *
97+ * Initiates a refresh of the notifications. This is typically triggered by a user action, like
98+ * a pull-to-refresh gesture. It sets the refreshing state and then calls [loadNotifications]
99+ * to fetch the latest data.
100+ */
81101 fun refreshNotifications () {
82102 _isRefreshing .value = true
83103 loadNotifications()
84104 }
85105
106+ /* *
107+ * Marks a specific notification as read. This involves updating the notification's state in
108+ * the local repository to ensure the change is persisted.
109+ *
110+ * @param notification The [MifosNotification] to be marked as read.
111+ */
86112 fun dismissNotification (notification : MifosNotification ) {
87113 viewModelScope.launch {
88114 notificationRepositoryImp.saveNotification(notification.copy(read = true ))
89115 notificationRepositoryImp.updateReadStatus(notification, true )
90116 }
91117 }
92118
119+ /* *
120+ * Sorts a list of notifications. The sorting logic prioritizes unread notifications and then
121+ * sorts them by timestamp, so the most recent ones appear first.
122+ *
123+ * @param notifications The list of [MifosNotification]s to be sorted.
124+ * @return A new list containing the sorted notifications.
125+ */
93126 private fun sortNotifications (notifications : List <MifosNotification >): List <MifosNotification > {
94127 return notifications.sortedWith(
95128 compareByDescending<MifosNotification > { ! it.isRead() }
@@ -98,9 +131,34 @@ internal class NotificationViewModel(
98131 }
99132}
100133
134+ /* *
135+ * A sealed interface that represents the various states the Notification screen can be in. This
136+ * allows for exhaustive state handling in the UI, ensuring a predictable user experience.
137+ */
101138internal sealed interface NotificationUiState {
139+ /* *
140+ * Indicates that the notifications are currently being loaded. This is the ideal time to show
141+ * a progress indicator.
142+ */
102143 data object Loading : NotificationUiState
144+
145+ /* *
146+ * Represents a successful fetch of notifications.
147+ *
148+ * @param notifications The list of notifications to be displayed on the screen.
149+ */
103150 data class Success (val notifications : List <MifosNotification >) : NotificationUiState
151+
152+ /* *
153+ * Signals that an error occurred while trying to fetch notifications.
154+ *
155+ * @param errorMessage A descriptive message about the error that can be shown to the user.
156+ */
104157 data class Error (val errorMessage : String? ) : NotificationUiState
158+
159+ /* *
160+ * Used when the notification list is empty. This state allows for showing a user-friendly
161+ * message indicating that there are no notifications.
162+ */
105163 data object Empty : NotificationUiState
106164}
0 commit comments