Skip to content

Commit a57266b

Browse files
authored
Merge branch 'main' into test/bring-back-testsLoadingBehavior
2 parents 910fb04 + 6ec56e3 commit a57266b

File tree

19 files changed

+256
-199
lines changed

19 files changed

+256
-199
lines changed

app/src/androidTest/java/com/github/se/travelpouch/ui/notification/NotificationItemTest.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class NotificationItemTest {
8181
@Before
8282
fun setUp() {
8383

84-
FirebaseApp.initializeApp(ApplicationProvider.getApplicationContext())
84+
FirebaseApp.initializeApp(ApplicationProvider.getApplicationContext())
8585

8686
travelRepository = mock(TravelRepository::class.java)
8787
notificationRepository = mock(NotificationRepository::class.java)
@@ -98,7 +98,6 @@ class NotificationItemTest {
9898
activityViewModel = ActivityViewModel(activityRepository)
9999
documentViewModel = DocumentViewModel(documentRepository, documentsManager, mock())
100100
eventViewModel = EventViewModel(eventRepository)
101-
102101
}
103102

104103
@Test

app/src/androidTest/java/com/github/se/travelpouch/ui/notification/NotificationScreenTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ class NotificationScreenTest {
267267

268268
composeTestRule.onNodeWithText("DECLINE").performClick()
269269
verify(profileRepository, never()).addFriend(anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull())
270-
verify(notificationRepository).addNotification(anyOrNull())
270+
verify(notificationRepository).addNotification(anyOrNull(), anyOrNull(), anyOrNull())
271271
}
272272

273273
// Chat-GPT was helpful in this test because he helped me understand how to separate the capture
@@ -395,6 +395,6 @@ class NotificationScreenTest {
395395
verify(secondLayerTask).addOnSuccessListener(onCompleteListenerCaptor.capture())
396396
composeTestRule.runOnIdle { onCompleteListenerCaptor.firstValue.onSuccess(null) }
397397

398-
verify(notificationRepository).addNotification(anyOrNull())
398+
verify(notificationRepository).addNotification(anyOrNull(), anyOrNull(), anyOrNull())
399399
}
400400
}

app/src/androidTest/java/com/github/se/travelpouch/ui/profile/ProfileEditTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class ProfileEditTest {
191191
// piece of code is executed from the argument captor
192192
composeTestRule.runOnIdle { onCompleteListenerCaptor2.firstValue.onSuccess(mockQuerySnapshot) }
193193

194-
verify(notificationRepository).addNotification(anyOrNull())
194+
verify(notificationRepository).addNotification(anyOrNull(), anyOrNull(), anyOrNull())
195195
}
196196

197197
@Test

app/src/androidTest/java/com/github/se/travelpouch/ui/travel/ParticipantListScreenTest.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ class ParticipantListScreenTest {
422422

423423
// Verify interactions with the repositories
424424
verify(profileRepository).getFsUidByEmail(anyOrNull(), anyOrNull(), anyOrNull())
425-
verify(notificationRepository, never()).addNotification(anyOrNull())
425+
verify(notificationRepository, never()).addNotification(anyOrNull(), anyOrNull(), anyOrNull())
426426
}
427427

428428
@Test
@@ -471,7 +471,7 @@ class ParticipantListScreenTest {
471471

472472
composeTestRule.onNodeWithTag("addUserButton").performClick()
473473
verify(profileRepository).getFsUidByEmail(anyOrNull(), anyOrNull(), anyOrNull())
474-
verify(notificationRepository, never()).addNotification(anyOrNull())
474+
verify(notificationRepository, never()).addNotification(anyOrNull(), anyOrNull(), anyOrNull())
475475
}
476476

477477
@Test
@@ -550,7 +550,7 @@ class ParticipantListScreenTest {
550550
composeTestRule.onNodeWithTag("addUserButton").performClick()
551551

552552
verify(profileRepository).getFsUidByEmail(anyOrNull(), anyOrNull(), anyOrNull())
553-
verify(notificationRepository, never()).addNotification(anyOrNull())
553+
verify(notificationRepository, never()).addNotification(anyOrNull(), anyOrNull(), anyOrNull())
554554
}
555555

556556
@Test
@@ -672,7 +672,8 @@ class ParticipantListScreenTest {
672672
.updateTravel(any(), any(), anyOrNull(), any(), any(), anyOrNull())
673673
composeTestRule.onNodeWithTag("addUserButton").performClick()
674674
verify(profileRepository).getFsUidByEmail(anyOrNull(), anyOrNull(), anyOrNull())
675-
verify(notificationRepository, atLeastOnce()).addNotification(anyOrNull())
675+
verify(notificationRepository, atLeastOnce())
676+
.addNotification(anyOrNull(), anyOrNull(), anyOrNull())
676677
// throw impossible exception
677678
doAnswer { invocation ->
678679
val email = invocation.getArgument<String>(0)
@@ -697,7 +698,7 @@ class ParticipantListScreenTest {
697698
.updateTravel(any(), any(), anyOrNull(), any(), any(), anyOrNull())
698699
doThrow(RuntimeException("Impossible Exception"))
699700
.`when`(notificationRepository)
700-
.addNotification(anyOrNull())
701+
.addNotification(anyOrNull(), anyOrNull(), anyOrNull())
701702
composeTestRule.onNodeWithTag("addUserFab").performClick()
702703
val randomEmail2 = "random.email@example.org"
703704
composeTestRule.onNodeWithTag("addUserEmailField").performScrollTo()
@@ -787,7 +788,7 @@ class ParticipantListScreenTest {
787788
composeTestRule.onNodeWithTag("friendCard").assertIsDisplayed()
788789
composeTestRule.onNodeWithTag("friendCard").assertTextContains("example@mail.com")
789790
composeTestRule.onNodeWithTag("friendCard").performClick()
790-
verify(notificationRepository).addNotification(anyOrNull())
791+
verify(notificationRepository).addNotification(anyOrNull(), anyOrNull(), anyOrNull())
791792
}
792793

793794
@Test

app/src/main/java/com/github/se/travelpouch/MainActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class MainActivity : ComponentActivity() {
6969
}
7070
}
7171
}
72-
FirebaseApp.initializeApp(this)
72+
FirebaseApp.initializeApp(this)
7373
}
7474

7575
@Composable

app/src/main/java/com/github/se/travelpouch/model/notifications/NotificationRepository.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ interface NotificationRepository {
55

66
fun getNewUid(): String
77

8-
fun addNotification(notification: Notification)
8+
fun addNotification(
9+
notification: Notification,
10+
onSuccess: () -> Unit,
11+
onFailure: (Exception) -> Unit
12+
)
913

1014
fun fetchNotificationsForUser(
1115
userId: String,

app/src/main/java/com/github/se/travelpouch/model/notifications/NotificationRepositoryFirestore.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,21 @@ class NotificationRepositoryFirestore(private val firestore: FirebaseFirestore)
2727
*
2828
* @param notification The notification to be added.
2929
*/
30-
override fun addNotification(notification: Notification) {
30+
override fun addNotification(
31+
notification: Notification,
32+
onSuccess: () -> Unit,
33+
onFailure: (Exception) -> Unit
34+
) {
3135
firestore
3236
.collection(FirebasePaths.notifications)
3337
.document(notification.notificationUid)
3438
.set(notification)
35-
.addOnSuccessListener { Log.d("NotificationRepository", "Notification added successfully") }
39+
.addOnSuccessListener {
40+
onSuccess()
41+
Log.d("NotificationRepository", "Notification added successfully")
42+
}
3643
.addOnFailureListener { e ->
44+
onFailure(Exception("An error occurred. Could not send the notification"))
3745
Log.e("NotificationRepository", "Error adding notification", e)
3846
}
3947
}

app/src/main/java/com/github/se/travelpouch/model/notifications/NotificationViewModel.kt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.github.se.travelpouch.model.notifications
33

44
import android.util.Log
55
import androidx.lifecycle.ViewModel
6-
import com.google.firebase.Firebase
76
import com.google.firebase.functions.FirebaseFunctions
87
import com.google.firebase.functions.functions
98
import dagger.hilt.android.lifecycle.HiltViewModel
@@ -57,8 +56,12 @@ constructor(private val notificationRepository: NotificationRepository) : ViewMo
5756
*
5857
* @param notification The notification to be sent.
5958
*/
60-
fun sendNotification(notification: Notification) {
61-
notificationRepository.addNotification(notification)
59+
fun sendNotification(
60+
notification: Notification,
61+
onSuccess: () -> Unit,
62+
onFailure: (Exception) -> Unit
63+
) {
64+
notificationRepository.addNotification(notification, onSuccess, onFailure)
6265
}
6366

6467
/**
@@ -80,19 +83,16 @@ constructor(private val notificationRepository: NotificationRepository) : ViewMo
8083
}
8184

8285
fun sendNotificationToUser(userId: String, notificationContent: NotificationContent) {
83-
val data = hashMapOf(
84-
"userId" to userId,
85-
"message" to notificationContent.toDisplayString(),
86-
)
86+
val data =
87+
hashMapOf(
88+
"userId" to userId,
89+
"message" to notificationContent.toDisplayString(),
90+
)
8791

8892
functions
89-
.getHttpsCallable("sendNotification")
90-
.call(data)
91-
.addOnSuccessListener { result ->
92-
Log.d("Notification", "Success: ${result.data}")
93-
}
94-
.addOnFailureListener { e ->
95-
Log.e("Notification", "Error: ${e.message}")
96-
}
93+
.getHttpsCallable("sendNotification")
94+
.call(data)
95+
.addOnSuccessListener { result -> Log.d("Notification", "Success: ${result.data}") }
96+
.addOnFailureListener { e -> Log.e("Notification", "Error: ${e.message}") }
9797
}
9898
}

app/src/main/java/com/github/se/travelpouch/model/notifications/push/PushNotificationService.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import com.google.firebase.messaging.RemoteMessage
55

66
class PushNotificationService : FirebaseMessagingService() {
77

8-
@Override
9-
override fun onNewToken(token: String) {
10-
super.onNewToken(token)
11-
}
8+
@Override
9+
override fun onNewToken(token: String) {
10+
super.onNewToken(token)
11+
}
1212

13-
@Override
14-
override fun onMessageReceived(remoteMessage: RemoteMessage) {
15-
super.onMessageReceived(remoteMessage)
16-
}
17-
}
13+
@Override
14+
override fun onMessageReceived(remoteMessage: RemoteMessage) {
15+
super.onMessageReceived(remoteMessage)
16+
}
17+
}

app/src/main/java/com/github/se/travelpouch/model/profile/ProfileModelView.kt

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ class ProfileModelView @Inject constructor(private val repository: ProfileReposi
2828
private val profile_ = MutableStateFlow<Profile>(ErrorProfile.errorProfile)
2929
val profile: StateFlow<Profile> = profile_.asStateFlow()
3030

31-
private var _isTokenUpdated = mutableStateOf(false)
32-
val isTokenUpdated: Boolean get() = _isTokenUpdated.value
31+
private var _isTokenUpdated = mutableStateOf(false)
32+
val isTokenUpdated: Boolean
33+
get() = _isTokenUpdated.value
3334

3435
/** The initialisation function of the profile model view. It fetches the profile of the user */
3536
suspend fun initAfterLogin(onSuccess: () -> Unit) {
@@ -91,20 +92,20 @@ class ProfileModelView @Inject constructor(private val repository: ProfileReposi
9192
})
9293
}
9394

94-
private fun addNotificationTokenToProfile(token: String) {
95-
repository.addNotificationTokenToProfile(token, profile_.value.fsUid, {
96-
Log.d("Notification token added", "Notification token added")
97-
}, {
98-
Log.e(onFailureTag, "Failed to add notification token", it)
99-
})
100-
}
95+
private fun addNotificationTokenToProfile(token: String) {
96+
repository.addNotificationTokenToProfile(
97+
token,
98+
profile_.value.fsUid,
99+
{ Log.d("Notification token added", "Notification token added") },
100+
{ Log.e(onFailureTag, "Failed to add notification token", it) })
101+
}
101102

102-
fun updateNotificationTokenIfNeeded(token: String) {
103-
if (!_isTokenUpdated.value) {
104-
addNotificationTokenToProfile(token)
105-
_isTokenUpdated.value = true // Mark the token as updated for this session
106-
}
103+
fun updateNotificationTokenIfNeeded(token: String) {
104+
if (!_isTokenUpdated.value) {
105+
addNotificationTokenToProfile(token)
106+
_isTokenUpdated.value = true // Mark the token as updated for this session
107107
}
108+
}
108109

109110
/**
110111
* This function sends to notification to add a friend

app/src/main/java/com/github/se/travelpouch/model/profile/ProfileRepository.kt

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,22 @@ interface ProfileRepository {
5757
onFailure: (Exception) -> Unit
5858
)
5959

60-
/**
61-
* Adds a notification token to the user's profile in the Firestore database.
62-
*
63-
* @param token The notification token to be added to the profile.
64-
* @param user The user identifier to whom the token belongs.
65-
* @param onSuccess A callback function that is invoked when the token is successfully added.
66-
* @param onFailure A callback function that is invoked with an Exception if an error occurs during the operation.
67-
*/
68-
fun addNotificationTokenToProfile(
69-
token: String,
70-
user: String,
71-
onSuccess: () -> Unit,
72-
onFailure: (Exception) -> Unit
73-
)
74-
60+
/**
61+
* Adds a notification token to the user's profile in the Firestore database.
62+
*
63+
* @param token The notification token to be added to the profile.
64+
* @param user The user identifier to whom the token belongs.
65+
* @param onSuccess A callback function that is invoked when the token is successfully added.
66+
* @param onFailure A callback function that is invoked with an Exception if an error occurs
67+
* during the operation.
68+
*/
69+
fun addNotificationTokenToProfile(
70+
token: String,
71+
user: String,
72+
onSuccess: () -> Unit,
73+
onFailure: (Exception) -> Unit
74+
)
75+
7576
/**
7677
* This function sends to notification to add a friend
7778
*

app/src/main/java/com/github/se/travelpouch/model/profile/ProfileRepositoryFirebase.kt

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -330,28 +330,28 @@ class ProfileRepositoryFirebase(private val db: FirebaseFirestore) : ProfileRepo
330330
}
331331
}
332332

333-
/**
334-
* Adds a notification token to the user's profile in the Firestore database.
335-
*
336-
* @param token The notification token to be added to the profile.
337-
* @param user The user identifier to whom the token belongs.
338-
* @param onSuccess A callback function that is invoked when the token is successfully added.
339-
* @param onFailure A callback function that is invoked with an Exception if an error occurs during the operation.
340-
*/
341-
override fun addNotificationTokenToProfile(
342-
token: String,
343-
user: String,
344-
onSuccess: () -> Unit,
345-
onFailure: (Exception) -> Unit
346-
) {
347-
performFirestoreOperation(
348-
db.collection(collectionPath)
349-
.document(user)
350-
.update("notificationTokens", FieldValue.arrayUnion(token)),
351-
onSuccess,
352-
onFailure
353-
)
354-
}
333+
/**
334+
* Adds a notification token to the user's profile in the Firestore database.
335+
*
336+
* @param token The notification token to be added to the profile.
337+
* @param user The user identifier to whom the token belongs.
338+
* @param onSuccess A callback function that is invoked when the token is successfully added.
339+
* @param onFailure A callback function that is invoked with an Exception if an error occurs
340+
* during the operation.
341+
*/
342+
override fun addNotificationTokenToProfile(
343+
token: String,
344+
user: String,
345+
onSuccess: () -> Unit,
346+
onFailure: (Exception) -> Unit
347+
) {
348+
performFirestoreOperation(
349+
db.collection(collectionPath)
350+
.document(user)
351+
.update("notificationTokens", FieldValue.arrayUnion(token)),
352+
onSuccess,
353+
onFailure)
354+
}
355355
}
356356

357357
/** This class is used to convert a document to a profile, across the project */

app/src/main/java/com/github/se/travelpouch/model/profile/ProfileRepositoryMock.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ class ProfileRepositoryMock : ProfileRepository {
5757
TODO("Not yet implemented")
5858
}
5959

60-
override fun addNotificationTokenToProfile(
61-
token: String,
62-
user: String,
63-
onSuccess: () -> Unit,
64-
onFailure: (Exception) -> Unit
65-
) {
66-
TODO("Not yet implemented")
67-
}
68-
60+
override fun addNotificationTokenToProfile(
61+
token: String,
62+
user: String,
63+
onSuccess: () -> Unit,
64+
onFailure: (Exception) -> Unit
65+
) {
66+
TODO("Not yet implemented")
67+
}
68+
6969
override fun sendFriendNotification(
7070
email: String,
7171
onSuccess: (String) -> Unit,

0 commit comments

Comments
 (0)