Skip to content
Open
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
49 changes: 27 additions & 22 deletions app/src/main/java/com/nextcloud/talk/ui/ComposeChatAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
package com.nextcloud.talk.ui

import android.content.Context
import android.content.ContextWrapper
import android.util.Log
import android.view.View.TEXT_ALIGNMENT_VIEW_START
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
Expand Down Expand Up @@ -90,7 +89,6 @@ import coil.compose.AsyncImage
import com.elyeproj.loaderviewlibrary.LoaderImageView
import com.elyeproj.loaderviewlibrary.LoaderTextView
import com.nextcloud.talk.R
import com.nextcloud.talk.activities.MainActivity
import com.nextcloud.talk.adapters.messages.PreviewMessageViewHolder.Companion.KEY_MIMETYPE
import com.nextcloud.talk.application.NextcloudTalkApplication
import com.nextcloud.talk.application.NextcloudTalkApplication.Companion.sharedApplication
Expand All @@ -107,6 +105,7 @@ import com.nextcloud.talk.models.json.opengraph.Reference
import com.nextcloud.talk.ui.theme.ViewThemeUtils
import com.nextcloud.talk.users.UserManager
import com.nextcloud.talk.utils.DateUtils
import com.nextcloud.talk.utils.DisplayUtils
import com.nextcloud.talk.utils.DrawableUtils.getDrawableResourceIdForMimeType
import com.nextcloud.talk.utils.message.MessageUtils
import com.nextcloud.talk.utils.preview.ComposePreviewUtils
Expand Down Expand Up @@ -219,15 +218,10 @@ class ComposeChatAdapter(
val items = mutableStateListOf<ChatMessage>()
val currentUser: User = viewModel.userManager.currentUser.blockingGet()
val colorScheme = viewModel.viewThemeUtils.getColorScheme(viewModel.context)
val highEmphasisColorInt = viewModel.context.resources.getColor(R.color.high_emphasis_text, null)

fun Context.findMainActivityOrNull(): MainActivity? {
var context = this
while (context is ContextWrapper) {
if (context is MainActivity) return context
context = context.baseContext
}
return null
val highEmphasisColorInt = if (DisplayUtils.isAppThemeDarkMode(viewModel.context)) {
Color.White.toArgb()
} else {
Color.Black.toArgb()
}

fun addMessages(messages: MutableList<ChatMessage>, append: Boolean) {
Expand Down Expand Up @@ -450,23 +444,21 @@ class ComposeChatAdapter(
val incoming = message.actorId != currentUser.userId
val color = if (incoming) {
if (message.isDeleted) {
LocalContext.current.resources.getColor(
R.color.bg_message_list_incoming_bubble_deleted,
null
)
getColorFromTheme(LocalContext.current, R.color.bg_message_list_incoming_bubble_deleted)
} else {
LocalContext.current.resources.getColor(
R.color.bg_message_list_incoming_bubble,
null
)
getColorFromTheme(LocalContext.current, R.color.bg_message_list_incoming_bubble)
}
} else {
val outgoingBubbleColor = viewModel.viewThemeUtils.talk
.getOutgoingMessageBubbleColor(LocalContext.current, message.isDeleted, false)

if (message.isDeleted) {
ColorUtils.setAlphaComponent(colorScheme.surfaceVariant.toArgb(), HALF_OPACITY)
ColorUtils.setAlphaComponent(outgoingBubbleColor, HALF_OPACITY)
} else {
colorScheme.surfaceVariant.toArgb()
outgoingBubbleColor
}
}

val shape = if (incoming) incomingShape else outgoingShape

val rowModifier = if (message.id == messageId && playAnimation) {
Expand Down Expand Up @@ -555,6 +547,19 @@ class ComposeChatAdapter(
}
}

private fun getColorFromTheme(context: Context, resourceId: Int): Int {
val isDarkMode = DisplayUtils.isAppThemeDarkMode(context)
val nightConfig = android.content.res.Configuration()
nightConfig.uiMode = android.content.res.Configuration.UI_MODE_NIGHT_YES
val nightContext = context.createConfigurationContext(nightConfig)

return if (isDarkMode) {
nightContext.getColor(resourceId)
} else {
context.getColor(resourceId)
}
}

@Composable
private fun TimeDisplay(message: ChatMessage) {
val timeString = DateUtils(LocalContext.current)
Expand Down Expand Up @@ -973,7 +978,7 @@ class ComposeChatAdapter(
it.link?.let { Text(it, fontSize = TIME_TEXT_SIZE) }
it.thumb?.let {
val errorPlaceholderImage: Int = R.drawable.ic_mimetype_image
val loadedImage = loadImage(it, LocalContext.current, errorPlaceholderImage)
val loadedImage = load(it, LocalContext.current, errorPlaceholderImage)
AsyncImage(
model = loadedImage,
contentDescription = stringResource(R.string.nc_sent_an_image),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,20 @@ class TalkSpecificViewThemeUtils @Inject constructor(
}
}

fun getOutgoingMessageBubbleColor(context: Context, deleted: Boolean, isPlayed: Boolean): Int {
return withScheme(context) { scheme ->
val bgBubbleColor = if (deleted) {
ColorUtils.setAlphaComponent(dynamicColor.surfaceVariant().getArgb(scheme), HALF_ALPHA_INT)
} else if (isPlayed) {
ContextCompat.getColor(context, R.color.bg_message_list_outgoing_bubble_audio_played)
} else {
dynamicColor.surfaceVariant().getArgb(scheme)
}

return@withScheme bgBubbleColor
}
}

fun colorOutgoingQuoteText(textView: TextView) {
withScheme(textView) { scheme ->
textView.setTextColor(dynamicColor.onSurfaceVariant().getArgb(scheme))
Expand Down
25 changes: 22 additions & 3 deletions app/src/main/java/com/nextcloud/talk/utils/DisplayUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,19 @@ import com.nextcloud.talk.ui.theme.ViewThemeUtils
import com.nextcloud.talk.utils.ApiUtils.getUrlForAvatar
import com.nextcloud.talk.utils.ApiUtils.getUrlForFederatedAvatar
import com.nextcloud.talk.utils.ApiUtils.getUrlForGuestAvatar
import com.nextcloud.talk.utils.preferences.AppPreferencesImpl
import com.nextcloud.talk.utils.text.Spans.MentionChipSpan
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import org.greenrobot.eventbus.EventBus
import third.parties.fresco.BetterImageSpan
import java.text.DateFormat
import java.util.Date
import java.util.regex.Pattern

object DisplayUtils {
private val TAG = DisplayUtils::class.java.getSimpleName()
private val TAG = DisplayUtils::class.java.simpleName
private const val INDEX_LUMINATION = 2
private const val HSL_SIZE = 3
private const val MAX_LIGHTNESS = 0.92
Expand All @@ -89,6 +93,21 @@ object DisplayUtils {
return currentNightMode == Configuration.UI_MODE_NIGHT_YES
}

@OptIn(ExperimentalCoroutinesApi::class)
fun isAppThemeDarkMode(context: Context): Boolean =
runBlocking {
val appPreferences = AppPreferencesImpl(context)
val themeKey = context.resources.getString(R.string.nc_settings_theme_key)
val theme = appPreferences.readString(themeKey).first()
return@runBlocking when (theme) {
"night_no" -> false
"night_yes" -> true
"battery_saver" -> true
else ->
return@runBlocking isDarkModeOn(context)
}
}

fun setClickableString(string: String, url: String, textView: TextView) {
val spannableString = SpannableString(string)
spannableString.setSpan(
Expand Down Expand Up @@ -159,7 +178,7 @@ object DisplayUtils {
viewThemeUtils.material.colorChipDrawable(context, chip)
}
val config = context.resources.configuration
chip.setLayoutDirection(config.layoutDirection)
chip.layoutDirection = config.layoutDirection
val drawable: Int
val isCall = "call" == type || "calls" == type
val isGroup = "groups" == type || "user-group" == type
Expand Down Expand Up @@ -217,7 +236,7 @@ object DisplayUtils {
// A hack to refresh the chip icon
emojiEditText?.post {
emojiEditText.setTextKeepState(
emojiEditText.getText(),
emojiEditText.text,
TextView.BufferType.SPANNABLE
)
}
Expand Down
Loading