-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #598 from bounswe/feature/mobile-394-notification-…
…page Feature/mobile 394 notification page
- Loading branch information
Showing
5 changed files
with
229 additions
and
1 deletion.
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
13 changes: 13 additions & 0 deletions
13
resq/mobile/ResQ/app/src/main/java/com/cmpe451/resq/data/models/NotificationItem.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,13 @@ | ||
package com.cmpe451.resq.data.models | ||
|
||
data class NotificationItem( | ||
val id: Int, | ||
val createdAt: String, | ||
val modifiedAt: String, | ||
val userId: Int, | ||
val title: String, | ||
val body: String, | ||
val relatedEntityId: Int, | ||
val notificationType: String, | ||
val read: Boolean | ||
) |
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
123 changes: 123 additions & 0 deletions
123
resq/mobile/ResQ/app/src/main/java/com/cmpe451/resq/ui/views/screens/NotificationScreen.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,123 @@ | ||
package com.cmpe451.resq.ui.views.screens | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.Context | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.foundation.layout.width | ||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.foundation.lazy.items | ||
import androidx.compose.material.Card | ||
import androidx.compose.material.Icon | ||
import androidx.compose.material.MaterialTheme | ||
import androidx.compose.material.Scaffold | ||
import androidx.compose.material.Text | ||
import androidx.compose.material.TopAppBar | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.AccountCircle | ||
import androidx.compose.material.icons.filled.Notifications | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.text.font.FontWeight | ||
import androidx.compose.ui.unit.dp | ||
import androidx.navigation.NavController | ||
import com.cmpe451.resq.data.models.NotificationItem | ||
import com.cmpe451.resq.viewmodels.NotificationViewModel | ||
|
||
@SuppressLint("UnusedMaterialScaffoldPaddingParameter") | ||
@Composable | ||
fun NotificationScreen(navController: NavController, appContext: Context) { | ||
val viewModel = NotificationViewModel(appContext) | ||
val notifications by viewModel.notificationItems | ||
|
||
Scaffold( | ||
topBar = { | ||
TopAppBar( | ||
title = { Text("Notifications") }, | ||
navigationIcon = { | ||
Icon( | ||
Icons.Default.Notifications, | ||
contentDescription = "Notifications" | ||
) | ||
}, | ||
backgroundColor = Color.White, | ||
contentColor = Color.Black | ||
) | ||
} | ||
) { | ||
NotificationList(notifications) | ||
} | ||
} | ||
|
||
@Composable | ||
fun NotificationList(notificationItems: List<NotificationItem>) { | ||
LazyColumn { | ||
items(notificationItems) { item -> | ||
NotificationItemCard(item) | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun NotificationItemCard(notification: NotificationItem) { | ||
Card( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(8.dp), | ||
elevation = 2.dp | ||
) { | ||
Column( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(16.dp) | ||
) { | ||
Row( | ||
verticalAlignment = Alignment.CenterVertically | ||
) { | ||
// Replace this with an actual image using Coil or other image loading library | ||
Icon( | ||
Icons.Default.AccountCircle, | ||
contentDescription = "Profile", | ||
modifier = Modifier.size(40.dp) | ||
) | ||
Spacer(modifier = Modifier.width(8.dp)) | ||
Column { | ||
Text( | ||
text = notification.title, | ||
fontWeight = FontWeight.Bold | ||
) | ||
Text( | ||
text = notification.createdAt, | ||
style = MaterialTheme.typography.body2 | ||
) | ||
} | ||
} | ||
Spacer(modifier = Modifier.height(8.dp)) | ||
Row( | ||
modifier = Modifier.fillMaxWidth(), | ||
horizontalArrangement = Arrangement.SpaceBetween | ||
) { | ||
Text( | ||
text = notification.body, | ||
style = MaterialTheme.typography.body2 | ||
) | ||
/* | ||
if (notification.isActionable) { | ||
Button(onClick = { /* TODO: Handle View action */ }) { | ||
Text("VIEW") | ||
} | ||
} | ||
*/ | ||
} | ||
} | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
resq/mobile/ResQ/app/src/main/java/com/cmpe451/resq/viewmodels/NotificationViewModel.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,69 @@ | ||
package com.cmpe451.resq.viewmodels | ||
|
||
import android.content.Context | ||
import androidx.compose.runtime.State | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.cmpe451.resq.data.models.NotificationItem | ||
import com.cmpe451.resq.data.remote.ResqService | ||
import kotlinx.coroutines.launch | ||
|
||
class NotificationViewModel(appContext: Context) : ViewModel() { | ||
|
||
private val _notificationItems by lazy { mutableStateOf<List<NotificationItem>>(listOf()) } | ||
val notificationItems: State<List<NotificationItem>> = _notificationItems | ||
|
||
init { | ||
fetchNotifications(appContext) | ||
} | ||
|
||
private fun fetchNotifications(appContext: Context) { | ||
viewModelScope.launch { | ||
/* | ||
val notifications = getNotifications(appContext) | ||
notifications.getOrNull()?.let { | ||
_notificationItems.value = it | ||
} | ||
*/ | ||
val notifications = | ||
listOf( | ||
NotificationItem( | ||
id = 1, | ||
createdAt = "2021-05-01T00:00:00.000Z", | ||
modifiedAt = "2021-05-01T00:00:00.000Z", | ||
userId = 1, | ||
title = "Notification Title", | ||
body = "Notification Body", | ||
relatedEntityId = 1, | ||
notificationType = "Notification Type", | ||
read = false | ||
), | ||
NotificationItem( | ||
id = 2, | ||
createdAt = "2021-05-01T00:00:00.000Z", | ||
modifiedAt = "2021-05-01T00:00:00.000Z", | ||
userId = 1, | ||
title = "Notification Title", | ||
body = "Notification Body", | ||
relatedEntityId = 1, | ||
notificationType = "Notification Type", | ||
read = false | ||
), | ||
) | ||
_notificationItems.value = notifications | ||
|
||
} | ||
} | ||
|
||
suspend fun getNotifications(appContext: Context): Result<List<NotificationItem>> { | ||
val api = ResqService(appContext) | ||
val response = api.getNotifications() | ||
if (response.isSuccessful) { | ||
response.body()?.let { | ||
return Result.success(it) | ||
} | ||
} | ||
return Result.failure(Throwable(response.message())) | ||
} | ||
} |