Skip to content

Commit

Permalink
Add functionality to check group members and show relevant content on…
Browse files Browse the repository at this point in the history
… PlacesList screen.
  • Loading branch information
cp-sneh-s committed Dec 9, 2024
1 parent d1088e1 commit b603608
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.canopas.yourspace.ui.component

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
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.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.canopas.yourspace.R
import com.canopas.yourspace.ui.theme.AppTheme

@Composable
fun NoMemberEmptyContent(
loadingInviteCode: Boolean,
addMember: () -> Unit
) {
Column(
modifier = Modifier
.fillMaxSize()
.padding(horizontal = 16.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Icon(
painter = painterResource(id = R.drawable.ic_thread_no_member),
contentDescription = null,
modifier = Modifier.size(64.dp),
tint = AppTheme.colorScheme.textPrimary
)
Spacer(modifier = Modifier.height(24.dp))
Text(
text = stringResource(id = R.string.threads_screen_no_members_title),
style = AppTheme.appTypography.header4
)
Spacer(modifier = Modifier.height(16.dp))
Text(
text = stringResource(id = R.string.threads_screen_no_members_subtitle),
style = AppTheme.appTypography.subTitle1,
color = AppTheme.colorScheme.textDisabled,
textAlign = TextAlign.Center
)

Spacer(modifier = Modifier.height(24.dp))

PrimaryButton(
modifier = Modifier.fillMaxWidth(),
label = stringResource(id = R.string.thread_screen_add_new_member),
onClick = addMember,
showLoader = loadingInviteCode
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package com.canopas.yourspace.ui.flow.geofence.places

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
Expand Down Expand Up @@ -48,6 +50,7 @@ import com.canopas.yourspace.ui.component.AppAlertDialog
import com.canopas.yourspace.ui.component.AppBanner
import com.canopas.yourspace.ui.component.AppProgressIndicator
import com.canopas.yourspace.ui.component.NoInternetScreen
import com.canopas.yourspace.ui.component.NoMemberEmptyContent
import com.canopas.yourspace.ui.flow.geofence.add.components.PlaceAddedPopup
import com.canopas.yourspace.ui.theme.AppTheme

Expand Down Expand Up @@ -83,7 +86,7 @@ fun PlacesListScreen() {
contentColor = AppTheme.colorScheme.textPrimary,
containerColor = AppTheme.colorScheme.surface,
floatingActionButton = {
if (!state.placesLoading && state.connectivityStatus == ConnectivityObserver.Status.Available) {
if (!state.placesLoading && state.connectivityStatus == ConnectivityObserver.Status.Available && state.hasMembers) {
FloatingActionButton(
onClick = { viewModel.navigateToAddPlace() },
containerColor = AppTheme.colorScheme.primary,
Expand All @@ -97,7 +100,15 @@ fun PlacesListScreen() {
}
) {
if (state.connectivityStatus == ConnectivityObserver.Status.Available) {
PlacesListContent(modifier = Modifier.padding(it))
if (state.loadingSpace || state.placesLoading) {
LoadingContent(modifier = Modifier.padding(it))
} else if (state.hasMembers) {
PlacesListContent(modifier = Modifier.padding(it))
} else {
NoMemberEmptyContent(state.loadingInviteCode) {
viewModel.addMember()
}
}
} else {
NoInternetScreen(viewModel::checkInternetConnection)
}
Expand Down Expand Up @@ -136,6 +147,17 @@ fun PlacesListScreen() {
}
}

@Composable
fun LoadingContent(modifier: Modifier) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
modifier = modifier.fillMaxSize()
) {
AppProgressIndicator()
}
}

@Composable
private fun AddPlaceButton() {
Row(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.canopas.yourspace.ui.flow.geofence.places
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.canopas.yourspace.data.models.place.ApiPlace
import com.canopas.yourspace.data.models.space.SpaceInfo
import com.canopas.yourspace.data.models.user.ApiUser
import com.canopas.yourspace.data.repository.SpaceRepository
import com.canopas.yourspace.data.service.auth.AuthService
Expand Down Expand Up @@ -37,6 +38,44 @@ class PlacesListViewModel @Inject constructor(
init {
checkInternetConnection()
loadPlaces()
getCurrentSpace()
}

private fun getCurrentSpace() = viewModelScope.launch(appDispatcher.IO) {
try {
_state.emit(_state.value.copy(loadingSpace = true))
val space = spaceRepository.getCurrentSpaceInfo()
val members = space?.members ?: emptyList()

_state.emit(
_state.value.copy(
currentSpace = space,
loadingSpace = false,
hasMembers = members.size > 1
)
)
} catch (e: Exception) {
Timber.e(e, "Failed to fetch current space")
_state.emit(_state.value.copy(error = e.message, loadingSpace = false))
}
}

fun addMember() = viewModelScope.launch(appDispatcher.IO) {
try {
val space = spaceRepository.getCurrentSpace() ?: return@launch
var inviteCode = _state.value.inviteCode
if (inviteCode.isEmpty()) {
_state.emit(_state.value.copy(loadingInviteCode = true))
inviteCode = spaceRepository.getInviteCode(space.id) ?: return@launch
_state.emit(_state.value.copy(loadingInviteCode = false, inviteCode = inviteCode))
}
appNavigator.navigateTo(
AppDestinations.SpaceInvitation.spaceInvitation(inviteCode, space.name).path
)
} catch (e: Exception) {
Timber.e(e, "Failed to get invite code")
_state.emit(_state.value.copy(error = e.message, loadingInviteCode = false))
}
}

private fun loadPlaces() = viewModelScope.launch(appDispatcher.IO) {
Expand Down Expand Up @@ -146,5 +185,10 @@ data class PlacesListScreenState(
val currentUser: ApiUser? = null,
val placesLoading: Boolean = false,
val places: List<ApiPlace> = emptyList(),
val error: String? = null
val error: String? = null,
val loadingInviteCode: Boolean = false,
val inviteCode: String = "",
val loadingSpace: Boolean = false,
val currentSpace: SpaceInfo? = null,
val hasMembers: Boolean = false
)
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ import com.canopas.yourspace.ui.component.AppAlertDialog
import com.canopas.yourspace.ui.component.AppBanner
import com.canopas.yourspace.ui.component.AppProgressIndicator
import com.canopas.yourspace.ui.component.NoInternetScreen
import com.canopas.yourspace.ui.component.PrimaryButton
import com.canopas.yourspace.ui.component.NoMemberEmptyContent
import com.canopas.yourspace.ui.component.UserProfile
import com.canopas.yourspace.ui.component.motionClickEvent
import com.canopas.yourspace.ui.flow.messages.chat.toFormattedTitle
Expand Down Expand Up @@ -441,45 +441,3 @@ fun ThreadProfile(members: List<ApiUser>) {
}
}
}

@Composable
private fun NoMemberEmptyContent(
loadingInviteCode: Boolean,
addMember: () -> Unit
) {
Column(
modifier = Modifier
.fillMaxSize()
.padding(horizontal = 16.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Icon(
painter = painterResource(id = R.drawable.ic_thread_no_member),
contentDescription = null,
modifier = Modifier.size(64.dp),
tint = AppTheme.colorScheme.textPrimary
)
Spacer(modifier = Modifier.height(24.dp))
Text(
text = stringResource(id = R.string.threads_screen_no_members_title),
style = AppTheme.appTypography.header4
)
Spacer(modifier = Modifier.height(16.dp))
Text(
text = stringResource(id = R.string.threads_screen_no_members_subtitle),
style = AppTheme.appTypography.subTitle1,
color = AppTheme.colorScheme.textDisabled,
textAlign = TextAlign.Center
)

Spacer(modifier = Modifier.height(24.dp))

PrimaryButton(
modifier = Modifier.fillMaxWidth(),
label = stringResource(id = R.string.thread_screen_add_new_member),
onClick = addMember,
showLoader = loadingInviteCode
)
}
}

0 comments on commit b603608

Please sign in to comment.