Skip to content

Commit

Permalink
Bug fixes v3 (#43)
Browse files Browse the repository at this point in the history
* Messages screen fixes

* Fixes of setting screen

* Minor fixes

* Minor fix

* Lint fixes

* Fix test
  • Loading branch information
cp-radhika-s authored May 8, 2024
1 parent 0e87cff commit 736bd1e
Show file tree
Hide file tree
Showing 18 changed files with 197 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Divider
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
Expand Down Expand Up @@ -70,6 +71,10 @@ fun OtpInputField(
}
}
)

LaunchedEffect(key1 = Unit) {
focusRequester.requestFocus()
}
}

@OptIn(ExperimentalComposeUiApi::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ class MapViewModel @Inject constructor(
withContext(appDispatcher.IO) {
_state.emit(_state.value.copy(defaultCameraPosition = locationManager.getLastLocation()))
}
userPreferences.currentSpaceState.collectLatest {
userPreferences.currentSpaceState.collectLatest { spaceId ->
locationJob?.cancel()
listenMemberLocation()
if (spaceId.isNotEmpty()) listenMemberLocation()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class JoinSpaceViewModel @Inject constructor(

fun onCodeChanged(code: String) {
_state.value = _state.value.copy(inviteCode = code)
if (code.length == 6) {
verifyAndJoinSpace()
}
}

fun verifyAndJoinSpace() = viewModelScope.launch(appDispatcher.IO) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ fun MessagesContent(modifier: Modifier) {
state.loading,
state.append,
state.messagesByDate,
state.newMessagesToAppend,
state.threadMembers,
state.currentUserId,
loadMore = { viewModel.loadMore() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.launch
import timber.log.Timber
import java.util.Calendar
import java.util.Date
import javax.inject.Inject

private const val MESSAGE_PAGE_LIMIT = 20
Expand Down Expand Up @@ -76,10 +75,12 @@ class MessagesViewModel @Inject constructor(
.collectLatest { messages ->
val filter = messages.filter { it.created_at != null }
if (filter.isNotEmpty()) {
val newMessages =
allMessages.filter { msg -> filter.any { it.id != msg.id } } + filter
val appendedMessages =
state.value.newMessagesToAppend.filter { append -> filter.any { it.id != append.id } }
val newMessages = allMessages + filter
_state.value =
state.value.copy(
newMessagesToAppend = appendedMessages,
messagesByDate = newMessages.groupMessagesByDate()
)
markMessagesAsSeen(filter)
Expand Down Expand Up @@ -117,7 +118,7 @@ class MessagesViewModel @Inject constructor(
)
)
try {
val from = allMessages.minByOrNull { it.createdAtMs }?.created_at ?: Date()
val from = allMessages.minByOrNull { it.createdAtMs }?.created_at
val newMessages = messagesRepository.getMessages(
threadId,
from = from,
Expand All @@ -126,11 +127,11 @@ class MessagesViewModel @Inject constructor(

hasMoreData = newMessages.isNotEmpty()

val existingMsg =
if (newMessages.isEmpty()) allMessages else allMessages.filter { msg -> newMessages.any { it.id != msg.id } }
val messages =
if (newMessages.isEmpty()) allMessages else allMessages + newMessages
_state.emit(
state.value.copy(
messagesByDate = (existingMsg + newMessages).groupMessagesByDate(),
messagesByDate = messages.groupMessagesByDate(),
append = false,
loadingMessages = false,
error = null
Expand Down Expand Up @@ -305,7 +306,11 @@ class MessagesViewModel @Inject constructor(
listenMessages()
}

messagesRepository.sendMessage(message, userId, threadId)
val newMessage = messagesRepository.generateMessage(message, userId, threadId)
val newMessages = state.value.newMessagesToAppend.toMutableList()
newMessages.add(newMessage)
_state.emit(_state.value.copy(newMessagesToAppend = newMessages))
messagesRepository.sendMessage(newMessage)
} catch (e: Exception) {
Timber.e(e, "Failed to send message")
_state.emit(_state.value.copy(error = e.message))
Expand Down Expand Up @@ -368,5 +373,6 @@ data class MessagesScreenState(
val selectAll: Boolean = true,
val error: String? = null,
val newMessage: String = "",
val newMessagesToAppend: List<ApiThreadMessage> = emptyList(),
val isNewThread: Boolean = false
)
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
Expand All @@ -45,7 +44,6 @@ import com.canopas.yourspace.ui.component.UserProfile
import com.canopas.yourspace.ui.component.reachedBottom
import com.canopas.yourspace.ui.flow.messages.chat.toFormattedTitle
import com.canopas.yourspace.ui.theme.AppTheme
import kotlinx.coroutines.flow.distinctUntilChanged
import java.util.concurrent.TimeUnit

@OptIn(ExperimentalFoundationApi::class)
Expand All @@ -55,6 +53,7 @@ fun ColumnScope.MessageList(
loading: Boolean,
append: Boolean,
messagesByDate: Map<Long, List<ApiThreadMessage>>,
newMessagesToAppend: List<ApiThreadMessage>,
members: List<UserInfo>,
currentUserId: String,
loadMore: () -> Unit
Expand All @@ -64,11 +63,9 @@ fun ColumnScope.MessageList(
}

LaunchedEffect(reachedBottom) {
snapshotFlow { reachedBottom }
.distinctUntilChanged()
.collect {
loadMore()
}
if (reachedBottom && messagesByDate.isNotEmpty()) {
loadMore()
}
}

LazyColumn(
Expand All @@ -79,6 +76,23 @@ fun ColumnScope.MessageList(
contentPadding = PaddingValues(16.dp),
reverseLayout = true
) {
itemsIndexed(newMessagesToAppend, key = { index, item -> item.id }) { index, message ->
val by =
members.firstOrNull { it.user.id == message.sender_id }

val myLatestMsg =
newMessagesToAppend.firstOrNull { it.sender_id == currentUserId }?.id == message.id
MessageContent(
previousMessage = if (index < newMessagesToAppend.size - 1) newMessagesToAppend[index + 1] else null,
message,
by = by,
seenBy = emptyList(),
isGroupChat = members.size > 2,
isSender = true,
isLatestMsg = myLatestMsg
)
}

messagesByDate.forEach { section ->
val messages = section.value

Expand Down Expand Up @@ -184,6 +198,7 @@ fun LazyItemScope.MessageContent(
}

MessageBubble(
isSent = message.isSent,
message = message.message,
timeLabel = timeLabel,
seenLabel = seenLabel,
Expand All @@ -202,6 +217,7 @@ fun shouldShowUserDetails(previousMessage: ApiThreadMessage?, message: ApiThread

@Composable
fun MessageBubble(
isSent: Boolean,
message: String,
timeLabel: String,
seenLabel: String,
Expand All @@ -222,7 +238,7 @@ fun MessageBubble(
.wrapContentWidth()
.widthIn(max = screenWidth * 0.8f, min = 100.dp)
) {
if (timeLabel.isNotEmpty()) {
if (timeLabel.isNotEmpty() && isSent) {
Text(
text = timeLabel,
style = AppTheme.appTypography.label3.copy(color = AppTheme.colorScheme.textDisabled),
Expand All @@ -238,7 +254,7 @@ fun MessageBubble(
modifier = Modifier
.background(
color = if (isSender) {
AppTheme.colorScheme.primary.copy(alpha = 0.7f)
AppTheme.colorScheme.primary.copy(alpha = if (isSent) 0.7f else 0.5f)
} else {
AppTheme.colorScheme.containerNormalOnSurface
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,7 @@ fun EnablePermissionsContent(modifier: Modifier) {
description = stringResource(R.string.enable_permission_notification_access_desc),
isGranted = notificationPermissionStates?.status == PermissionStatus.Granted,
onClick = {
if (notificationPermissionStates?.status is PermissionStatus.Denied ||
notificationPermissionStates?.status?.shouldShowRationale == true
) {
showNotificationRational = true
} else if (notificationPermissionStates?.status != PermissionStatus.Granted) {
if (notificationPermissionStates?.status != PermissionStatus.Granted) {
notificationPermissionStates?.launchPermissionRequest()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import com.canopas.yourspace.data.models.space.ApiSpace
import com.canopas.yourspace.data.models.user.ApiUser
import com.canopas.yourspace.data.utils.Config
import com.canopas.yourspace.ui.component.AppAlertDialog
import com.canopas.yourspace.ui.component.AppProgressIndicator
import com.canopas.yourspace.ui.component.motionClickEvent
import com.canopas.yourspace.ui.theme.AppTheme

Expand Down Expand Up @@ -119,16 +120,8 @@ private fun SettingsContent(modifier: Modifier) {
)
}

state.selectedSpace?.let { space ->
SpaceSettingsContent(space) {
viewModel.navigateToSpaceSettings()
}
Divider(
modifier = Modifier
.padding(bottom = 16.dp)
.fillMaxWidth(),
color = AppTheme.colorScheme.outline
)
SpaceSettingsContent(state.spaces, state.loadingSpaces) {
viewModel.navigateToSpaceSettings(it)
}

OtherSettingsContent(viewModel)
Expand Down Expand Up @@ -201,34 +194,58 @@ private fun openUrl(context: Activity, url: String) {

@Composable
private fun SpaceSettingsContent(
space: ApiSpace,
openSpaceSettings: () -> Unit
spaces: List<ApiSpace>,
loading: Boolean,
openSpaceSettings: (String) -> Unit
) {
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.fillMaxWidth()
.motionClickEvent {
openSpaceSettings()
}
.padding(bottom = 16.dp)
.padding(horizontal = 16.dp)
) {
Row(modifier = Modifier.fillMaxWidth()) {
Text(
text = stringResource(id = R.string.setting_space_settings, space.name),
text = stringResource(id = R.string.setting_spaces),
style = AppTheme.appTypography.header3,
color = AppTheme.colorScheme.textPrimary,
textAlign = TextAlign.Start,
modifier = Modifier
.weight(1f)
)
Icon(
Icons.Default.KeyboardArrowRight,
contentDescription = "",
modifier = Modifier.padding(horizontal = 8.dp),
tint = AppTheme.colorScheme.textSecondary
.padding(start = 16.dp, bottom = 16.dp, end = 10.dp)
)
if (loading) {
AppProgressIndicator()
}
}

spaces.forEach { space ->
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.fillMaxWidth()
.motionClickEvent {
openSpaceSettings(space.id)
}
.padding(bottom = 16.dp)
.padding(horizontal = 16.dp)
) {
Text(
text = space.name,
style = AppTheme.appTypography.subTitle2,
color = AppTheme.colorScheme.textPrimary,
textAlign = TextAlign.Start,
modifier = Modifier
.weight(1f)
)
Icon(
Icons.Default.KeyboardArrowRight,
contentDescription = "",
modifier = Modifier.padding(horizontal = 8.dp),
tint = AppTheme.colorScheme.textSecondary
)
}
}

Divider(
modifier = Modifier
.padding(bottom = 16.dp)
.fillMaxWidth(),
color = AppTheme.colorScheme.outline
)
}

@Composable
Expand Down
Loading

0 comments on commit 736bd1e

Please sign in to comment.