diff --git a/app/src/main/java/com/nextcloud/talk/adapters/items/ConversationItem.kt b/app/src/main/java/com/nextcloud/talk/adapters/items/ConversationItem.kt index 3a189db33..ff6343534 100644 --- a/app/src/main/java/com/nextcloud/talk/adapters/items/ConversationItem.kt +++ b/app/src/main/java/com/nextcloud/talk/adapters/items/ConversationItem.kt @@ -59,6 +59,8 @@ class ConversationItem( IFilterable { private var header: GenericTextHeaderItem? = null private val chatMessage = model.lastMessage?.asModel() + var mHolder: ConversationItemViewHolder? = null + constructor( conversation: ConversationModel, @@ -97,6 +99,7 @@ class ConversationItem( position: Int, payloads: List ) { + mHolder = holder val appContext = sharedApplication!!.applicationContext holder.binding.dialogName.setTextColor( ResourcesCompat.getColor( diff --git a/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt b/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt index f525c033e..f01852456 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt @@ -195,7 +195,6 @@ import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_IS_BREAKOUT_ROOM import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_IS_MODERATOR import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_RECORDING_STATE import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_ROOM_TOKEN -import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_SCROLL_OFFSET import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_START_CALL_AFTER_ROOM_SWITCH import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_SWITCH_TO_ROOM import com.nextcloud.talk.utils.permissions.PlatformPermissionUtil @@ -370,7 +369,6 @@ class ChatActivity : var myFirstMessage: CharSequence? = null var checkingLobbyStatus: Boolean = false - private var listScrollOffset: Int = 0 private var conversationVoiceCallMenuItem: MenuItem? = null private var conversationVideoMenuItem: MenuItem? = null @@ -394,10 +392,6 @@ class ChatActivity : private val onBackPressedCallback = object : OnBackPressedCallback(true) { override fun handleOnBackPressed() { val intent = Intent(this@ChatActivity, ConversationsListActivity::class.java) - val bundle = bundleOf() - bundle.putInt(KEY_SCROLL_OFFSET, listScrollOffset) - bundle.putString(KEY_ROOM_TOKEN, roomToken) - intent.putExtras(bundle) startActivity(intent) } } @@ -554,7 +548,6 @@ class ChatActivity : val extras: Bundle? = intent.extras roomToken = extras?.getString(KEY_ROOM_TOKEN).orEmpty() - listScrollOffset = extras?.getInt(KEY_SCROLL_OFFSET, 0) ?: 0 sharedText = extras?.getString(BundleKeys.KEY_SHARED_TEXT).orEmpty() diff --git a/app/src/main/java/com/nextcloud/talk/conversationlist/ConversationsListActivity.kt b/app/src/main/java/com/nextcloud/talk/conversationlist/ConversationsListActivity.kt index 3e2d4177e..3e61744cf 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationlist/ConversationsListActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationlist/ConversationsListActivity.kt @@ -51,7 +51,6 @@ import androidx.core.view.isVisible import androidx.fragment.app.DialogFragment import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.lifecycleScope -import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import androidx.work.Data import androidx.work.OneTimeWorkRequest @@ -142,7 +141,6 @@ import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_FORWARD_MSG_FLAG import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_FORWARD_MSG_TEXT import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_INTERNAL_USER_ID import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_ROOM_TOKEN -import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_SCROLL_OFFSET import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_SCROLL_TO_NOTIFICATION_CATEGORY import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_SHARED_TEXT import com.nextcloud.talk.utils.permissions.PlatformPermissionUtil @@ -237,8 +235,6 @@ class ConversationsListActivity : private var conversationsListBottomDialog: ConversationsListBottomDialog? = null private var searchHelper: MessageSearchHelper? = null private var searchViewDisposable: Disposable? = null - private var roomTokenFromIntent: String? = null - private var scrollOffset: Int = 0 private var filterState = mutableMapOf( MENTION to false, @@ -311,8 +307,6 @@ class ConversationsListActivity : showNotificationWarning() showShareToScreen = hasActivityActionSendIntent() - roomTokenFromIntent = intent.extras?.getString(KEY_ROOM_TOKEN) - scrollOffset = intent.extras?.getInt(KEY_SCROLL_OFFSET, 0) ?: 0 if (!eventBus.isRegistered(this)) { eventBus.register(this) @@ -432,17 +426,9 @@ class ConversationsListActivity : .firstOrNull { ConversationUtils.isNoteToSelfConversation(it) } val isNoteToSelfAvailable = noteToSelf != null handleNoteToSelfShortcut(isNoteToSelfAvailable, noteToSelf?.token ?: "") - if (roomTokenFromIntent != null) { - val item = adapter?.currentItems?.first { - it is ConversationItem && it.model.token == roomTokenFromIntent - } - val pos = adapter?.currentItems?.indexOf(item) - pos?.let { - val layoutManager = binding.recyclerView.layoutManager as LinearLayoutManager - layoutManager.scrollToPosition(scrollOffset) - } - roomTokenFromIntent = null - } + + val pair = appPreferences.conversationListPositionAndOffset + layoutManager?.scrollToPositionWithOffset(pair.first, pair.second) }.collect() } @@ -1890,14 +1876,18 @@ class ConversationsListActivity : val bundle = Bundle() bundle.putString(KEY_ROOM_TOKEN, selectedConversation!!.token) - // bundle.putString(KEY_ROOM_ID, selectedConversation!!.roomId) bundle.putString(KEY_SHARED_TEXT, textToPaste) if (selectedMessageId != null) { bundle.putString(BundleKeys.KEY_MESSAGE_ID, selectedMessageId) selectedMessageId = null } val firstVisible = layoutManager?.findFirstVisibleItemPosition() ?: 0 - bundle.putInt(KEY_SCROLL_OFFSET, firstVisible) + val firstItem = adapter?.getItem(firstVisible) + val firstTop = (firstItem as ConversationItem).mHolder?.itemView?.top + val firstOffset = firstTop?.minus(44) ?: 0 + + appPreferences.setConversationListPositionAndOffset(firstVisible, firstOffset) + val intent = Intent(context, ChatActivity::class.java) intent.putExtras(bundle) startActivity(intent) diff --git a/app/src/main/java/com/nextcloud/talk/utils/bundle/BundleKeys.kt b/app/src/main/java/com/nextcloud/talk/utils/bundle/BundleKeys.kt index ff65e3c7b..cce659c74 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/bundle/BundleKeys.kt +++ b/app/src/main/java/com/nextcloud/talk/utils/bundle/BundleKeys.kt @@ -81,7 +81,6 @@ object BundleKeys { const val KEY_FIELD_MAP: String = "KEY_FIELD_MAP" const val KEY_CHAT_URL: String = "KEY_CHAT_URL" const val KEY_SCROLL_TO_NOTIFICATION_CATEGORY: String = "KEY_SCROLL_TO_NOTIFICATION_CATEGORY" - const val KEY_SCROLL_OFFSET: String = "KEY_SCROLL_OFFSET" const val KEY_FOCUS_INPUT: String = "KEY_FOCUS_INPUT" const val KEY_FROM_QR: String = "KEY_FROM_QR" } diff --git a/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferences.java b/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferences.java index 60699b78c..9600cf3f7 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferences.java +++ b/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferences.java @@ -13,6 +13,8 @@ import android.annotation.SuppressLint; import com.nextcloud.talk.ui.PlaybackSpeed; +import kotlin.Pair; + @SuppressLint("NonConstantResourceId") public interface AppPreferences { @@ -185,5 +187,9 @@ public interface AppPreferences { void setShowRegularNotificationWarning(boolean value); + void setConversationListPositionAndOffset(int position, int offset); + + Pair getConversationListPositionAndOffset(); + void clear(); } diff --git a/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferencesImpl.kt b/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferencesImpl.kt index ec92560cf..1cdc75a43 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferencesImpl.kt +++ b/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferencesImpl.kt @@ -412,6 +412,28 @@ class AppPreferencesImpl(val context: Context) : AppPreferences { } } + override fun setConversationListPositionAndOffset(position: Int, offset: Int) { + runBlocking { + async { + writeString(CONVERSATION_LIST_POSITION_OFFSET, "$position,$offset") + } + } + } + + override fun getConversationListPositionAndOffset(): Pair { + val pairString = runBlocking { + async { readString(CONVERSATION_LIST_POSITION_OFFSET).first() } + }.getCompleted() + + if (pairString.isEmpty()) return Pair(0,0) + + val pairArr = pairString.split(',') + val position = pairArr[0].toInt() + val offset = pairArr[1].toInt() + + return Pair(position, offset) + } + override fun setPhoneBookIntegrationLastRun(currentTimeMillis: Long) = runBlocking { async { @@ -626,6 +648,7 @@ class AppPreferencesImpl(val context: Context) : AppPreferences { const val VOICE_MESSAGE_PLAYBACK_SPEEDS = "voice_message_playback_speeds" const val SHOW_REGULAR_NOTIFICATION_WARNING = "show_regular_notification_warning" const val LAST_NOTIFICATION_WARNING = "last_notification_warning" + const val CONVERSATION_LIST_POSITION_OFFSET = "CONVERSATION_LIST_POSITION_OFFSET" private fun String.convertStringToArray(): Array { var varString = this val floatList = mutableListOf()