From f8214b0604b0ccfb37d44b87e8cd382454e94cd7 Mon Sep 17 00:00:00 2001
From: Stanislav Dmitrenko <7953703+avently@users.noreply.github.com>
Date: Fri, 25 Nov 2022 15:50:22 +0300
Subject: [PATCH] android: Chat items for preferences (#1409)
* android: Chat items for preferences
* fix filled
Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com>
Co-authored-by: JRoberts <8711996+jr-simplex@users.noreply.github.com>
---
.../java/chat/simplex/app/model/ChatModel.kt | 47 +++++++++++++------
.../java/chat/simplex/app/model/SimpleXAPI.kt | 8 ++++
.../app/views/chat/item/CIChatFeatureView.kt | 35 ++++++++++++++
.../app/views/chat/item/CIEventView.kt | 31 ++++++------
.../app/views/chat/item/ChatItemView.kt | 3 ++
.../app/src/main/res/values-de/strings.xml | 5 ++
.../app/src/main/res/values-ru/strings.xml | 5 ++
.../app/src/main/res/values/strings.xml | 5 ++
8 files changed, 110 insertions(+), 29 deletions(-)
create mode 100644 apps/android/app/src/main/java/chat/simplex/app/views/chat/item/CIChatFeatureView.kt
diff --git a/apps/android/app/src/main/java/chat/simplex/app/model/ChatModel.kt b/apps/android/app/src/main/java/chat/simplex/app/model/ChatModel.kt
index ad0b2676da..95e72d1248 100644
--- a/apps/android/app/src/main/java/chat/simplex/app/model/ChatModel.kt
+++ b/apps/android/app/src/main/java/chat/simplex/app/model/ChatModel.kt
@@ -1161,6 +1161,17 @@ data class ChatItem (
quotedItem = null,
file = null
)
+
+ fun getChatFeatureSample(feature: Feature, enabled: FeatureEnabled): ChatItem {
+ val content = CIContent.RcvChatFeature(feature = feature, enabled = enabled)
+ return ChatItem(
+ chatDir = CIDirection.DirectRcv(),
+ meta = CIMeta.getSample(1, Clock.System.now(), content.text, CIStatus.RcvRead(), itemDeleted = false, itemEdited = false, editable = false),
+ content = content,
+ quotedItem = null,
+ file = null
+ )
+ }
}
}
@@ -1257,22 +1268,28 @@ sealed class CIContent: ItemContent {
@Serializable @SerialName("sndGroupEvent") class SndGroupEventContent(val sndGroupEvent: SndGroupEvent): CIContent() { override val msgContent: MsgContent? get() = null }
@Serializable @SerialName("rcvConnEvent") class RcvConnEventContent(val rcvConnEvent: RcvConnEvent): CIContent() { override val msgContent: MsgContent? get() = null }
@Serializable @SerialName("sndConnEvent") class SndConnEventContent(val sndConnEvent: SndConnEvent): CIContent() { override val msgContent: MsgContent? get() = null }
+ @Serializable @SerialName("rcvChatFeature") class RcvChatFeature(val feature: Feature, val enabled: FeatureEnabled): CIContent() { override val msgContent: MsgContent? get() = null }
+ @Serializable @SerialName("sndChatFeature") class SndChatFeature(val feature: Feature, val enabled: FeatureEnabled): CIContent() { override val msgContent: MsgContent? get() = null }
+ @Serializable @SerialName("rcvChatFeatureRejected") class RcvChatFeatureRejected(val feature: Feature): CIContent() { override val msgContent: MsgContent? get() = null }
- override val text: String get() = when(this) {
- is SndMsgContent -> msgContent.text
- is RcvMsgContent -> msgContent.text
- is SndDeleted -> generalGetString(R.string.deleted_description)
- is RcvDeleted -> generalGetString(R.string.deleted_description)
- is SndCall -> status.text(duration)
- is RcvCall -> status.text(duration)
- is RcvIntegrityError -> msgError.text
- is RcvGroupInvitation -> groupInvitation.text
- is SndGroupInvitation -> groupInvitation.text
- is RcvGroupEventContent -> rcvGroupEvent.text
- is SndGroupEventContent -> sndGroupEvent.text
- is RcvConnEventContent -> rcvConnEvent.text
- is SndConnEventContent -> sndConnEvent.text
- }
+ override val text: String get() = when (this) {
+ is SndMsgContent -> msgContent.text
+ is RcvMsgContent -> msgContent.text
+ is SndDeleted -> generalGetString(R.string.deleted_description)
+ is RcvDeleted -> generalGetString(R.string.deleted_description)
+ is SndCall -> status.text(duration)
+ is RcvCall -> status.text(duration)
+ is RcvIntegrityError -> msgError.text
+ is RcvGroupInvitation -> groupInvitation.text
+ is SndGroupInvitation -> groupInvitation.text
+ is RcvGroupEventContent -> rcvGroupEvent.text
+ is SndGroupEventContent -> sndGroupEvent.text
+ is RcvConnEventContent -> rcvConnEvent.text
+ is SndConnEventContent -> sndConnEvent.text
+ is RcvChatFeature -> "${feature.text()}: ${enabled.text}"
+ is SndChatFeature -> "${feature.text()}: ${enabled.text}"
+ is RcvChatFeatureRejected -> "${feature.text()}: ${generalGetString(R.string.feature_received_prohibited)}"
+ }
}
@Serializable
diff --git a/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt b/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt
index 0628715727..e799c7459e 100644
--- a/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt
+++ b/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt
@@ -2009,6 +2009,14 @@ data class FeatureEnabled(
val forUser: Boolean,
val forContact: Boolean
) {
+ val text: String
+ get() = when {
+ forUser && forContact -> generalGetString(R.string.feature_enabled)
+ forUser -> generalGetString(R.string.feature_enabled_for_you)
+ forContact -> generalGetString(R.string.feature_enabled_for_contact)
+ else -> generalGetString(R.string.feature_off)
+ }
+
companion object {
fun enabled(user: ChatPreference, contact: ChatPreference): FeatureEnabled =
when {
diff --git a/apps/android/app/src/main/java/chat/simplex/app/views/chat/item/CIChatFeatureView.kt b/apps/android/app/src/main/java/chat/simplex/app/views/chat/item/CIChatFeatureView.kt
new file mode 100644
index 0000000000..1a684f8804
--- /dev/null
+++ b/apps/android/app/src/main/java/chat/simplex/app/views/chat/item/CIChatFeatureView.kt
@@ -0,0 +1,35 @@
+package chat.simplex.app.views.chat.item
+
+import androidx.compose.foundation.layout.*
+import androidx.compose.material.*
+import androidx.compose.runtime.Composable
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.graphics.Color
+import androidx.compose.ui.unit.dp
+import androidx.compose.ui.unit.sp
+import chat.simplex.app.model.*
+import chat.simplex.app.ui.theme.*
+
+@Composable
+fun CIChatFeatureView(
+ chatItem: ChatItem,
+ feature: Feature,
+ enabled: FeatureEnabled?
+) {
+ Row(
+ verticalAlignment = Alignment.CenterVertically
+ ) {
+ val iconColor = if (enabled != null) {
+ if (enabled.forUser) SimplexGreen else if (enabled.forContact) WarningYellow else HighOrLowlight
+ } else Color.Red
+
+ Icon(feature.icon(true), feature.text(), Modifier.size(15.dp), tint = iconColor)
+ Text(
+ chatEventText(chatItem),
+ Modifier,
+ // this is important. Otherwise, aligning will be bad because annotated string has a Span with size 12.sp
+ fontSize = 12.sp
+ )
+ }
+}
diff --git a/apps/android/app/src/main/java/chat/simplex/app/views/chat/item/CIEventView.kt b/apps/android/app/src/main/java/chat/simplex/app/views/chat/item/CIEventView.kt
index 08be3a421c..b6694cb891 100644
--- a/apps/android/app/src/main/java/chat/simplex/app/views/chat/item/CIEventView.kt
+++ b/apps/android/app/src/main/java/chat/simplex/app/views/chat/item/CIEventView.kt
@@ -18,32 +18,35 @@ import chat.simplex.app.ui.theme.SimpleXTheme
@Composable
fun CIEventView(ci: ChatItem) {
- fun withGroupEventStyle(builder: AnnotatedString.Builder, text: String) {
- return builder.withStyle(SpanStyle(fontSize = 12.sp, fontWeight = FontWeight.Light, color = HighOrLowlight)) { append(text) }
- }
-
Surface {
Row(
Modifier.padding(horizontal = 6.dp, vertical = 6.dp),
verticalAlignment = Alignment.Bottom
) {
Text(
- buildAnnotatedString {
- val memberDisplayName = ci.memberDisplayName
- if (memberDisplayName != null) {
- withGroupEventStyle(this, memberDisplayName)
- append(" ")
- }
- withGroupEventStyle(this, ci.content.text)
- append(" ")
- withGroupEventStyle(this, ci.timestampText)
- },
+ chatEventText(ci),
style = MaterialTheme.typography.body1.copy(lineHeight = 22.sp)
)
}
}
}
+private fun withGroupEventStyle(builder: AnnotatedString.Builder, text: String) {
+ return builder.withStyle(SpanStyle(fontSize = 12.sp, fontWeight = FontWeight.Light, color = HighOrLowlight)) { append(text) }
+}
+
+fun chatEventText(ci: ChatItem): AnnotatedString =
+ buildAnnotatedString {
+ val memberDisplayName = ci.memberDisplayName
+ if (memberDisplayName != null) {
+ withGroupEventStyle(this, memberDisplayName)
+ append(" ")
+ }
+ withGroupEventStyle(this, ci.content.text)
+ append(" ")
+ withGroupEventStyle(this, ci.timestampText)
+ }
+
@Preview(showBackground = true)
@Preview(
uiMode = Configuration.UI_MODE_NIGHT_YES,
diff --git a/apps/android/app/src/main/java/chat/simplex/app/views/chat/item/ChatItemView.kt b/apps/android/app/src/main/java/chat/simplex/app/views/chat/item/ChatItemView.kt
index 23210e6ba5..dab38eef13 100644
--- a/apps/android/app/src/main/java/chat/simplex/app/views/chat/item/ChatItemView.kt
+++ b/apps/android/app/src/main/java/chat/simplex/app/views/chat/item/ChatItemView.kt
@@ -170,6 +170,9 @@ fun ChatItemView(
is CIContent.SndGroupEventContent -> CIEventView(cItem)
is CIContent.RcvConnEventContent -> CIEventView(cItem)
is CIContent.SndConnEventContent -> CIEventView(cItem)
+ is CIContent.RcvChatFeature -> CIChatFeatureView(cItem, c.feature, c.enabled)
+ is CIContent.SndChatFeature -> CIChatFeatureView(cItem, c.feature, c.enabled)
+ is CIContent.RcvChatFeatureRejected -> CIChatFeatureView(cItem, c.feature, null)
}
}
}
diff --git a/apps/android/app/src/main/res/values-de/strings.xml b/apps/android/app/src/main/res/values-de/strings.xml
index 7bc9785fbe..39a6609056 100644
--- a/apps/android/app/src/main/res/values-de/strings.xml
+++ b/apps/android/app/src/main/res/values-de/strings.xml
@@ -942,6 +942,11 @@
***Your preferences
***Full deletion
***Voice messages
+ ***enabled
+ ***enabled for you
+ ***enabled for contact
+ ***off
+ ***received, prohibited
***Allow your contacts to irreversibly delete sent messages.
***Allow irreversible message deletion only if your contact allows it to you.
***Contacts can mark messages for deletion; you will be able to view them.
diff --git a/apps/android/app/src/main/res/values-ru/strings.xml b/apps/android/app/src/main/res/values-ru/strings.xml
index 67fd38b343..ccdeaddc77 100644
--- a/apps/android/app/src/main/res/values-ru/strings.xml
+++ b/apps/android/app/src/main/res/values-ru/strings.xml
@@ -941,6 +941,11 @@
Ваши предпочтения
Полное удаление
Голосовые сообщения
+ включено
+ включено для вас
+ включено для контакта
+ выключено
+ получено, не разрешено
Разрешить вашим контактам необратимо удалять отправленные сообщения.
Разрешить необратимое удаление сообщений, только если ваш контакт разрешает это вам.
Контакты могут помечать сообщения для удаления; вы сможете просмотреть их.
diff --git a/apps/android/app/src/main/res/values/strings.xml b/apps/android/app/src/main/res/values/strings.xml
index 39daffbf13..48c521b50d 100644
--- a/apps/android/app/src/main/res/values/strings.xml
+++ b/apps/android/app/src/main/res/values/strings.xml
@@ -942,6 +942,11 @@
Your preferences
Full deletion
Voice messages
+ enabled
+ enabled for you
+ enabled for contact
+ off
+ received, prohibited
Allow your contacts to irreversibly delete sent messages.
Allow irreversible message deletion only if your contact allows it to you.
Contacts can mark messages for deletion; you will be able to view them.