Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functionality to check group members and show relevant content on PlacesList screen. #131

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
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,
title: Int,
subtitle: Int,
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 = title),
style = AppTheme.appTypography.header4
)
Spacer(modifier = Modifier.height(16.dp))
Text(
text = stringResource(id = 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 @@ -36,6 +36,7 @@ import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.canopas.yourspace.R
import com.canopas.yourspace.domain.utils.ConnectivityObserver
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.flow.geofence.component.PlaceNameContent
Expand Down Expand Up @@ -145,6 +146,12 @@ fun LocateOnMapScreen() {
} else {
NoInternetScreen(viewModel::checkInternetConnection)
}

if (state.error != null) {
AppBanner(msg = state.error!!.message!!) {
viewModel.resetErrorState()
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ class LocateOnMapViewModel @Inject constructor(
val currentSpaceId = userPreferences.currentSpace ?: return@launch
val currentUser = userPreferences.currentUser ?: return@launch

if (latitude == 0.0 || longitude == 0.0) {
_state.value = _state.value.copy(
error = Exception("Invalid location.")
)
return@launch
}
cp-sneh-s marked this conversation as resolved.
Show resolved Hide resolved

_state.emit(state.value.copy(addingPlace = true))
try {
val memberIds =
Expand Down Expand Up @@ -131,6 +138,10 @@ class LocateOnMapViewModel @Inject constructor(
}
}
}

fun resetErrorState() {
_state.value = _state.value.copy(error = null)
}
}

data class LocateOnMapState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ fun ChoosePlaceNameScreen() {
val state by viewModel.state.collectAsState()

if (state.error != null) {
AppBanner(msg = state.error!!) {
val errorMessage = state.error?.message ?: state.error.toString()
AppBanner(msg = errorMessage) {
viewModel.resetErrorState()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ class ChoosePlaceNameViewModel @Inject constructor(
val currentSpaceId = userPreferences.currentSpace ?: return@launch
val currentUser = userPreferences.currentUser ?: return@launch

if (selectedLatitude == 0.0 || selectedLongitude == 0.0) {
_state.value = _state.value.copy(
error = Exception("Invalid location.")
)
return@launch
}
cp-sneh-s marked this conversation as resolved.
Show resolved Hide resolved

_state.emit(state.value.copy(addingPlace = true))
try {
val memberIds = spaceRepository.getMemberBySpaceId(currentSpaceId)?.map { it.user_id }
Expand Down
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,19 @@ 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(
loadingInviteCode = state.loadingInviteCode,
title = R.string.place_list_screen_no_members_title,
subtitle = R.string.place_list_screen_no_members_subtitle
) {
viewModel.addMember()
}
}
} else {
NoInternetScreen(viewModel::checkInternetConnection)
}
Expand Down Expand Up @@ -136,6 +151,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))
}
cp-sneh-s marked this conversation as resolved.
Show resolved Hide resolved
}

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 @@ -160,7 +160,11 @@ private fun ThreadsContent(modifier: Modifier) {
deleteThread = { viewModel.deleteThread(it) }
)
} else {
NoMemberEmptyContent(state.loadingInviteCode) {
NoMemberEmptyContent(
loadingInviteCode = state.loadingInviteCode,
title = R.string.threads_screen_no_members_title,
subtitle = R.string.threads_screen_no_members_subtitle
) {
viewModel.addMember()
}
}
Expand Down Expand Up @@ -441,45 +445,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
)
}
}
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 @@ -301,4 +301,7 @@
<string name="space_setting_change_admin_title">Change Admin</string>
<string name="space_setting_change_admin_description">To leave the group, you must assign another member as admin. This action is irreversible unless the new admin changes it.</string>
<string name="change_admin_button">Change Admin</string>

<string name="place_list_screen_no_members_title">Add members to add places</string>
<string name="place_list_screen_no_members_subtitle">At least one member needs to join your group to be able to add places.</string>
cp-sneh-s marked this conversation as resolved.
Show resolved Hide resolved
</resources>
Loading