Skip to content

Commit 1b2d419

Browse files
authored
Merge pull request #5049 from nextcloud/issue-5041-jump-to-coversation
Preserve Conversation List position
2 parents e00ded4 + 38b5098 commit 1b2d419

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
lines changed

app/src/main/java/com/nextcloud/talk/adapters/items/ConversationItem.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class ConversationItem(
5959
IFilterable<String?> {
6060
private var header: GenericTextHeaderItem? = null
6161
private val chatMessage = model.lastMessage?.asModel()
62+
var mHolder: ConversationItemViewHolder? = null
6263

6364
constructor(
6465
conversation: ConversationModel,
@@ -97,6 +98,7 @@ class ConversationItem(
9798
position: Int,
9899
payloads: List<Any>
99100
) {
101+
mHolder = holder
100102
val appContext = sharedApplication!!.applicationContext
101103
holder.binding.dialogName.setTextColor(
102104
ResourcesCompat.getColor(

app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,6 @@ class ChatActivity :
392392
private val onBackPressedCallback = object : OnBackPressedCallback(true) {
393393
override fun handleOnBackPressed() {
394394
val intent = Intent(this@ChatActivity, ConversationsListActivity::class.java)
395-
intent.putExtras(Bundle())
396395
startActivity(intent)
397396
}
398397
}

app/src/main/java/com/nextcloud/talk/conversationlist/ConversationsListActivity.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,16 @@ class ConversationsListActivity :
345345
showSearchOrToolbar()
346346
}
347347

348+
override fun onPause() {
349+
super.onPause()
350+
val firstVisible = layoutManager?.findFirstVisibleItemPosition() ?: 0
351+
val firstItem = adapter?.getItem(firstVisible)
352+
val firstTop = (firstItem as ConversationItem).mHolder?.itemView?.top
353+
val firstOffset = firstTop?.minus(CONVERSATION_ITEM_HEIGHT) ?: 0
354+
355+
appPreferences.setConversationListPositionAndOffset(firstVisible, firstOffset)
356+
}
357+
348358
// if edge to edge is used, add an empty item at the bottom of the list
349359
@Suppress("MagicNumber")
350360
private fun addEmptyItemForEdgeToEdgeIfNecessary() {
@@ -426,6 +436,9 @@ class ConversationsListActivity :
426436
.firstOrNull { ConversationUtils.isNoteToSelfConversation(it) }
427437
val isNoteToSelfAvailable = noteToSelf != null
428438
handleNoteToSelfShortcut(isNoteToSelfAvailable, noteToSelf?.token ?: "")
439+
440+
val pair = appPreferences.conversationListPositionAndOffset
441+
layoutManager?.scrollToPositionWithOffset(pair.first, pair.second)
429442
}.collect()
430443
}
431444

@@ -1873,7 +1886,6 @@ class ConversationsListActivity :
18731886

18741887
val bundle = Bundle()
18751888
bundle.putString(KEY_ROOM_TOKEN, selectedConversation!!.token)
1876-
// bundle.putString(KEY_ROOM_ID, selectedConversation!!.roomId)
18771889
bundle.putString(KEY_SHARED_TEXT, textToPaste)
18781890
if (selectedMessageId != null) {
18791891
bundle.putString(BundleKeys.KEY_MESSAGE_ID, selectedMessageId)
@@ -2221,5 +2233,6 @@ class ConversationsListActivity :
22212233
private const val SIXTEEN_HOURS_IN_SECONDS: Long = 57600
22222234
const val LONG_1000: Long = 1000
22232235
private const val NOTE_TO_SELF_SHORTCUT_ID = "NOTE_TO_SELF_SHORTCUT_ID"
2236+
private const val CONVERSATION_ITEM_HEIGHT = 44
22242237
}
22252238
}

app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferences.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
import com.nextcloud.talk.ui.PlaybackSpeed;
1515

16+
import kotlin.Pair;
17+
1618
@SuppressLint("NonConstantResourceId")
1719
public interface AppPreferences {
1820

@@ -181,5 +183,9 @@ public interface AppPreferences {
181183

182184
void setShowRegularNotificationWarning(boolean value);
183185

186+
void setConversationListPositionAndOffset(int position, int offset);
187+
188+
Pair<Integer, Integer> getConversationListPositionAndOffset();
189+
184190
void clear();
185191
}

app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferencesImpl.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,28 @@ class AppPreferencesImpl(val context: Context) : AppPreferences {
400400
}
401401
}
402402

403+
override fun setConversationListPositionAndOffset(position: Int, offset: Int) {
404+
runBlocking<Unit> {
405+
async {
406+
writeString(CONVERSATION_LIST_POSITION_OFFSET, "$position,$offset")
407+
}
408+
}
409+
}
410+
411+
override fun getConversationListPositionAndOffset(): Pair<Int, Int> {
412+
val pairString = runBlocking {
413+
async { readString(CONVERSATION_LIST_POSITION_OFFSET).first() }
414+
}.getCompleted()
415+
416+
if (pairString.isEmpty()) return Pair(0, 0)
417+
418+
val pairArr = pairString.split(',')
419+
val position = pairArr[0].toInt()
420+
val offset = pairArr[1].toInt()
421+
422+
return Pair(position, offset)
423+
}
424+
403425
override fun setPhoneBookIntegrationLastRun(currentTimeMillis: Long) =
404426
runBlocking<Unit> {
405427
async {
@@ -614,6 +636,7 @@ class AppPreferencesImpl(val context: Context) : AppPreferences {
614636
const val VOICE_MESSAGE_PLAYBACK_SPEEDS = "voice_message_playback_speeds"
615637
const val SHOW_REGULAR_NOTIFICATION_WARNING = "show_regular_notification_warning"
616638
const val LAST_NOTIFICATION_WARNING = "last_notification_warning"
639+
const val CONVERSATION_LIST_POSITION_OFFSET = "CONVERSATION_LIST_POSITION_OFFSET"
617640
private fun String.convertStringToArray(): Array<Float> {
618641
var varString = this
619642
val floatList = mutableListOf<Float>()

0 commit comments

Comments
 (0)