Skip to content

Commit

Permalink
Merge pull request #214 from SwEnt-Group13/fix/follow-offline-mod
Browse files Browse the repository at this point in the history
Adapt the "following" feature to work in offline mode
  • Loading branch information
Romainhir authored Nov 27, 2024
2 parents a8c3d95 + dbe1380 commit ce5b824
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.android.unio.components.association

import android.content.Context
import android.net.ConnectivityManager
import android.net.Network
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertIsNotDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.core.content.ContextCompat
import androidx.core.content.ContextCompat.getSystemService
import androidx.navigation.NavHostController
import androidx.test.core.app.ApplicationProvider
import com.android.unio.R
Expand Down Expand Up @@ -79,6 +83,8 @@ class AssociationProfileTest : TearDown() {
@MockK private lateinit var userRepository: UserRepositoryFirestore
@MockK private lateinit var imageRepository: ImageRepositoryFirebaseStorage

@MockK private lateinit var connectivityManager: ConnectivityManager

@get:Rule val composeTestRule = createComposeRule()

@get:Rule val hiltRule = HiltAndroidRule(this)
Expand All @@ -89,11 +95,14 @@ class AssociationProfileTest : TearDown() {
hiltRule.inject()

mockkStatic(FirebaseFirestore::class)
mockkStatic(Network::class)
mockkStatic(ContextCompat::class)
val db = mockk<FirebaseFirestore>()
val collection = mockk<CollectionReference>()
val query = mockk<Query>()
val task = mock<Task<QuerySnapshot>>()

every { getSystemService(any(), ConnectivityManager::class.java) } returns connectivityManager
every { Firebase.firestore } returns db
every { db.collection(any()) } returns collection
every { collection.whereIn(any(FieldPath::class), any()) } returns query
Expand Down Expand Up @@ -180,6 +189,8 @@ class AssociationProfileTest : TearDown() {

@Test
fun testAssociationProfileDisplayComponent() {
every { connectivityManager?.activeNetwork } returns mockk<Network>()

composeTestRule.setContent {
AssociationProfileScaffold(
navigationAction, userViewModel, eventViewModel, associationViewModel) {}
Expand Down Expand Up @@ -227,6 +238,8 @@ class AssociationProfileTest : TearDown() {

@Test
fun testFollowAssociation() {
every { connectivityManager?.activeNetwork } returns mockk<Network>()

val context: Context = ApplicationProvider.getApplicationContext()
composeTestRule.setContent {
AssociationProfileScaffold(
Expand Down Expand Up @@ -258,8 +271,34 @@ class AssociationProfileTest : TearDown() {
assert(associationViewModel.selectedAssociation.value!!.followersCount == currentCount)
}

@Test
fun testFollowOffline() {
val context: Context = ApplicationProvider.getApplicationContext()
every { connectivityManager?.activeNetwork } returns null
composeTestRule.setContent {
AssociationProfileScaffold(
navigationAction, userViewModel, eventViewModel, associationViewModel) {}
}
// Disable internet connction in the test

val currentCount = associationViewModel.selectedAssociation.value!!.followersCount

composeTestRule
.onNodeWithTag(AssociationProfileTestTags.FOLLOW_BUTTON)
.assertDisplayComponentInScroll()
composeTestRule
.onNodeWithText(context.getString(R.string.association_follow))
.assertIsDisplayed()

composeTestRule.onNodeWithTag(AssociationProfileTestTags.FOLLOW_BUTTON).performClick()
assert(!userViewModel.user.value?.followedAssociations!!.contains(associations.first().uid))
assert(associationViewModel.selectedAssociation.value!!.followersCount == currentCount)
}

@Test
fun testButtonBehavior() {
every { connectivityManager?.activeNetwork } returns mockk<Network>()

composeTestRule.setContent {
AssociationProfileScaffold(
navigationAction, userViewModel, eventViewModel, associationViewModel) {}
Expand Down Expand Up @@ -292,6 +331,8 @@ class AssociationProfileTest : TearDown() {

@Test
fun testGoBackButton() {
every { connectivityManager?.activeNetwork } returns mockk<Network>()

composeTestRule.setContent {
AssociationProfileScaffold(
navigationAction, userViewModel, eventViewModel, associationViewModel) {}
Expand All @@ -306,6 +347,8 @@ class AssociationProfileTest : TearDown() {

@Test
fun testAssociationProfileGoodId() {
every { connectivityManager?.activeNetwork } returns mockk<Network>()

composeTestRule.setContent {
AssociationProfileScaffold(
navigationAction, userViewModel, eventViewModel, associationViewModel) {}
Expand All @@ -317,6 +360,8 @@ class AssociationProfileTest : TearDown() {

@Test
fun testAssociationProfileNoId() {
every { connectivityManager?.activeNetwork } returns mockk<Network>()

associationViewModel.selectAssociation("3")
composeTestRule.setContent {
AssociationProfileScreen(
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/com/android/unio/model/utils/Utils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.android.unio.model.utils

import android.content.Context
import android.net.ConnectivityManager
import androidx.core.content.ContextCompat.getSystemService

object Utils {

fun checkInternetConnection(context: Context): Boolean {
val connectivityManager = getSystemService(context, ConnectivityManager::class.java)
return connectivityManager?.activeNetwork != null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import com.android.unio.model.event.EventViewModel
import com.android.unio.model.strings.test_tags.AssociationProfileTestTags
import com.android.unio.model.user.User
import com.android.unio.model.user.UserViewModel
import com.android.unio.model.utils.Utils
import com.android.unio.ui.event.EventCard
import com.android.unio.ui.image.AsyncImageWrapper
import com.android.unio.ui.navigation.NavigationAction
Expand Down Expand Up @@ -267,6 +268,7 @@ private fun AssociationProfileContent(
eventViewModel: EventViewModel,
associationViewModel: AssociationViewModel
) {
val context = LocalContext.current
val association by associationViewModel.selectedAssociation.collectAsState()
val user by userViewModel.user.collectAsState()

Expand All @@ -281,13 +283,21 @@ private fun AssociationProfileContent(
mutableStateOf(user!!.followedAssociations.contains(association!!.uid))
}
var enableButton by remember { mutableStateOf(true) }
val isConnected = Utils.checkInternetConnection(context)

val onFollow = {
enableButton = false
associationViewModel.updateFollow(association!!, user!!, isFollowed) {
userViewModel.refreshUser()
enableButton = true
if (isConnected) {
enableButton = false
associationViewModel.updateFollow(association!!, user!!, isFollowed) {
userViewModel.refreshUser()
enableButton = true
}
isFollowed = !isFollowed
} else {
Toast.makeText(
context, context.getString(R.string.no_internet_connection), Toast.LENGTH_SHORT)
.show()
}
isFollowed = !isFollowed
}

val onMemberClick = { member: User ->
Expand Down Expand Up @@ -559,6 +569,7 @@ private fun AssociationHeader(
style = AppTypography.headlineSmall,
modifier =
Modifier.padding(bottom = 14.dp).testTag(AssociationProfileTestTags.HEADER_MEMBERS))

if (isFollowed) {
OutlinedButton(
enabled = enableButton,
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/com/android/unio/ui/authentication/Welcome.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.android.unio.ui.authentication

import android.content.Context
import android.net.ConnectivityManager
import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.background
Expand Down Expand Up @@ -42,14 +41,14 @@ import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.text.style.TextAlign.Companion.Center
import androidx.compose.ui.unit.dp
import androidx.core.content.ContextCompat.getSystemService
import com.android.unio.R
import com.android.unio.model.strings.test_tags.WelcomeTestTags
import com.android.unio.model.user.SignInState
import com.android.unio.model.user.UserViewModel
import com.android.unio.model.user.isValidEmail
import com.android.unio.model.user.isValidPassword
import com.android.unio.model.user.signInOrCreateAccount
import com.android.unio.model.utils.Utils.checkInternetConnection
import com.android.unio.ui.theme.AppTypography
import com.google.firebase.Firebase
import com.google.firebase.auth.EmailAuthProvider
Expand Down Expand Up @@ -169,9 +168,10 @@ fun WelcomeScreen(userViewModel: UserViewModel) {
fun handleAuthentication(email: String, password: String, context: Context) {

// Check internet connectivity
val connectivityManager = getSystemService(context, ConnectivityManager::class.java)
if (connectivityManager?.activeNetwork == null) {
Toast.makeText(context, "You appear to be offline.", Toast.LENGTH_SHORT).show()
val isConnected = checkInternetConnection(context)
if (!isConnected) {
Toast.makeText(context, context.getString(R.string.no_internet_connection), Toast.LENGTH_SHORT)
.show()
return
}

Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@
* Other
*/

<!-- General Strings -->
<string name="no_internet_connection">Vous semblez être hors-ligne</string>

<!-- EventListOverview Strings -->
<string name="event_upcomingEvents">Évènements à venir</string>
<string name="event_tab_all">Tous</string>
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,9 @@
* Other
*/

<!-- General Strings -->
<string name="no_internet_connection">You appear to be offline</string>

<!-- EventListOverview Strings -->
<string name="event_upcomingEvents">Upcoming Events</string>
<string name="event_tab_all">All</string>
Expand Down

0 comments on commit ce5b824

Please sign in to comment.