Skip to content
Snippets Groups Projects
Commit c6985a8c authored by Adrien Béraud's avatar Adrien Béraud Committed by Pierre Nicolas
Browse files

use default preference values if not set

Change-Id: I336f59540744133c90a8cf4a33094c7a90746cad
parent a5898bcf
No related branches found
No related tags found
No related merge requests found
......@@ -53,6 +53,7 @@ import cx.ring.fragments.ConversationActionsFragment
import cx.ring.fragments.ConversationGalleryFragment
import cx.ring.fragments.ConversationMembersFragment
import cx.ring.interfaces.Colorable
import cx.ring.services.SharedPreferencesServiceImpl.Companion.getConversationColor
import cx.ring.utils.*
import cx.ring.views.AvatarDrawable
import dagger.hilt.android.AndroidEntryPoint
......@@ -228,7 +229,7 @@ class ContactDetailsActivity : AppCompatActivity(), TabLayout.OnTabSelectedListe
conversation
.getColor()
.observeOn(DeviceUtils.uiScheduler)
.subscribe { setColor(it) }
.subscribe { setColor(getConversationColor(this, it)) }
)
binding.pager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
......
......@@ -41,6 +41,8 @@ import cx.ring.client.EmojiChooserBottomSheet
import cx.ring.databinding.FragConversationActionsBinding
import cx.ring.databinding.ItemContactActionBinding
import cx.ring.interfaces.Colorable
import cx.ring.services.SharedPreferencesServiceImpl.Companion.getConversationColor
import cx.ring.services.SharedPreferencesServiceImpl.Companion.getConversationSymbol
import cx.ring.utils.ConversationPath
import cx.ring.utils.DeviceUtils
import dagger.hilt.android.AndroidEntryPoint
......@@ -120,22 +122,14 @@ class ConversationActionsFragment : Fragment(), Colorable {
adapter.actions.add(symbolAction!!)
// Update color on RX color signal.
mDisposableBag.add(
conversation
.getColor()
.observeOn(DeviceUtils.uiScheduler)
.subscribe {
setColor(it) }
)
mDisposableBag.add(conversation.getColor()
.observeOn(DeviceUtils.uiScheduler)
.subscribe { setColor(getConversationColor(requireContext(), it)) })
// Update symbol on RX color signal.
mDisposableBag.add(
conversation
.getSymbol()
.observeOn(DeviceUtils.uiScheduler)
.subscribe {
setSymbol(it.toString()) }
)
mDisposableBag.add(conversation.getSymbol()
.observeOn(DeviceUtils.uiScheduler)
.subscribe { setSymbol(getConversationSymbol(requireContext(), it)) })
@StringRes val infoString = if (conversation.isSwarm)
if (conversation.mode.blockingFirst() == Conversation.Mode.OneToOne)
......@@ -227,7 +221,7 @@ class ConversationActionsFragment : Fragment(), Colorable {
/**
* Set the symbol of the symbol action button.
*/
private fun setSymbol(symbol: String) {
private fun setSymbol(symbol: CharSequence) {
symbolAction?.setSymbol(symbol) // Update emoji action icon
adapter.notifyItemChanged(symbolActionPosition)
}
......@@ -236,18 +230,17 @@ class ConversationActionsFragment : Fragment(), Colorable {
* Set the conversation preferences.
* Always resend the color and emoji, even if they are not changed (to not reset).
*/
fun setConversationPreferences(
private fun setConversationPreferences(
accountId: String,
conversationUri: Uri,
color: Int? = colorAction?.iconTint,
emoji: String = symbolAction?.iconSymbol.toString(),
) {
mConversationFacade.setConversationPreferences(
accountId,
conversationUri,
mapOf(
"symbol" to emoji,
"color" to if (color != null) String.format("#%06X", 0xFFFFFF and color) else ""
accountId, conversationUri, mapOf(
Conversation.KEY_PREFERENCE_CONVERSATION_SYMBOL to emoji,
Conversation.KEY_PREFERENCE_CONVERSATION_COLOR to if (color != null)
String.format("#%06X", 0xFFFFFF and color) else ""
)
)
}
......
......@@ -63,7 +63,9 @@ import cx.ring.mvp.BaseSupportFragment
import cx.ring.service.DRingService
import cx.ring.service.LocationSharingService
import cx.ring.services.NotificationServiceImpl
import cx.ring.services.SharedPreferencesServiceImpl.Companion.getConversationColor
import cx.ring.services.SharedPreferencesServiceImpl.Companion.getConversationPreferences
import cx.ring.services.SharedPreferencesServiceImpl.Companion.getConversationSymbol
import cx.ring.utils.*
import cx.ring.utils.MediaButtonsHelper.MediaButtonsHelperCallback
import cx.ring.views.AvatarDrawable
......@@ -370,11 +372,11 @@ class ConversationFragment : BaseSupportFragment<ConversationPresenter, Conversa
}
override fun setConversationColor(@ColorInt color: Int) {
mAdapter?.setPrimaryColor(color)
mAdapter?.setPrimaryColor(getConversationColor(requireContext(), color))
}
override fun setConversationSymbol(symbol: CharSequence) {
binding?.emojiSend?.text = symbol
binding?.emojiSend?.text = getConversationSymbol(requireContext(), symbol)
}
override fun onDestroyView() {
......
......@@ -21,6 +21,7 @@ import android.content.Context
import android.content.SharedPreferences
import android.os.Build
import android.text.TextUtils
import androidx.annotation.ColorInt
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.content.edit
import androidx.preference.PreferenceManager
......@@ -121,7 +122,7 @@ class SharedPreferencesServiceImpl(private val context: Context, accountService:
preferences: Map<String, String>,
) =
getConversationPreferences(context, accountId, conversationUri)
.edit { preferences.forEach { (k, v) -> putString(k, v) } }
.edit { preferences.forEach { (k, v) -> putString(k, v) } }
override fun hasNetworkConnected(): Boolean = NetworkUtils.isConnectivityAllowed(context)
......@@ -198,5 +199,13 @@ class SharedPreferencesServiceImpl(private val context: Context, accountService:
fun getConversationPreferences(context: Context, accountId: String, conversationUri: Uri): SharedPreferences =
context.getSharedPreferences(accountId + "_" + conversationUri.uri, Context.MODE_PRIVATE)
/** Compute conversation color from preference value, fallback on default */
fun getConversationColor(context: Context, @ColorInt color: Int) =
if (color != 0) color else context.resources.getColor(R.color.color_primary_light)
/** Compute conversation symbol from preference value, fallback on default */
fun getConversationSymbol(context: Context, symbol: CharSequence) =
symbol.ifEmpty { context.resources.getText(R.string.conversation_default_emoji) }
}
}
\ No newline at end of file
......@@ -648,13 +648,17 @@ class Conversation : ConversationHistory {
fun getSymbol(): Observable<CharSequence> = symbol
fun updatePreferences(preferences: Map<String, String>) {
preferences["color"]?.let {
val colorValue = preferences[KEY_PREFERENCE_CONVERSATION_COLOR]
if (colorValue != null) {
// First, we remove the string first character (the #).
// The color format is RRGGBB but we want AARRGGBB.
// So we add FF in front of the color (full opacity).
color.onNext(it.substring(1).toInt(16) or 0xFF000000.toInt())
}
preferences["symbol"]?.let { symbol.onNext(it) }
color.onNext(colorValue.substring(1).toInt(16) or 0xFF000000.toInt())
} else color.onNext(0)
val symbolValue = preferences[KEY_PREFERENCE_CONVERSATION_SYMBOL]
if (symbolValue != null) {
symbol.onNext(symbolValue)
} else symbol.onNext("")
}
fun isGroup(): Boolean = isSwarm && contacts.size > 2
......@@ -716,6 +720,9 @@ class Conversation : ConversationHistory {
companion object {
private val TAG = Conversation::class.simpleName!!
const val KEY_PREFERENCE_CONVERSATION_COLOR = "color"
const val KEY_PREFERENCE_CONVERSATION_SYMBOL = "symbol"
private fun getTypedInteraction(interaction: Interaction) = when (interaction.type) {
Interaction.InteractionType.TEXT -> TextMessage(interaction)
Interaction.InteractionType.CALL -> Call(interaction)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment