Keeping the position of the feed in navigation

This commit is contained in:
Vitor Pamplona 2023-06-03 17:02:09 -04:00
parent 450fa2b70d
commit ca90866877
7 changed files with 50 additions and 48 deletions

View File

@ -64,13 +64,9 @@ fun AppNavigation(
composable(route.route, route.arguments, content = {
val scrollToTop = it.arguments?.getBoolean("scrollToTop") ?: false
LaunchedEffect(key1 = it) {
if (scrollToTop) {
launch {
videoFeedViewModel.sendToTop()
it.arguments?.remove("scrollToTop")
}
}
if (scrollToTop) {
videoFeedViewModel.sendToTop()
it.arguments?.remove("scrollToTop")
}
VideoScreen(
@ -85,13 +81,9 @@ fun AppNavigation(
composable(route.route, route.arguments, content = {
val scrollToTop = it.arguments?.getBoolean("scrollToTop") ?: false
LaunchedEffect(key1 = it) {
if (scrollToTop) {
launch {
searchFeedViewModel.sendToTop()
it.arguments?.remove("scrollToTop")
}
}
if (scrollToTop) {
searchFeedViewModel.sendToTop()
it.arguments?.remove("scrollToTop")
}
SearchScreen(
@ -107,14 +99,10 @@ fun AppNavigation(
val scrollToTop = it.arguments?.getBoolean("scrollToTop") ?: false
val nip47 = it.arguments?.getString("nip47")
LaunchedEffect(key1 = it) {
if (scrollToTop) {
launch {
homeFeedViewModel.sendToTop()
repliesFeedViewModel.sendToTop()
it.arguments?.remove("scrollToTop")
}
}
if (scrollToTop) {
homeFeedViewModel.sendToTop()
repliesFeedViewModel.sendToTop()
it.arguments?.remove("scrollToTop")
}
HomeScreen(
@ -140,14 +128,10 @@ fun AppNavigation(
composable(route.route, route.arguments, content = {
val scrollToTop = it.arguments?.getBoolean("scrollToTop") ?: false
LaunchedEffect(key1 = it) {
if (scrollToTop) {
launch {
notifFeedViewModel.clear()
notifFeedViewModel.sendToTop()
it.arguments?.remove("scrollToTop")
}
}
if (scrollToTop) {
notifFeedViewModel.clear()
notifFeedViewModel.sendToTop()
it.arguments?.remove("scrollToTop")
}
NotificationScreen(

View File

@ -287,8 +287,6 @@ private fun AuthorPictureAndComment(
}
}
println("AAAA $content")
content.first?.let {
val route by remember {
derivedStateOf {

View File

@ -54,11 +54,13 @@ open class CardFeedViewModel(val localFilter: FeedFilter<Note>) : ViewModel() {
val scrollToTop = _scrollToTop.asStateFlow()
var scrolltoTopPending = false
suspend fun sendToTop() {
fun sendToTop() {
if (scrolltoTopPending) return
scrolltoTopPending = true
_scrollToTop.emit(_scrollToTop.value + 1)
viewModelScope.launch(Dispatchers.IO) {
_scrollToTop.emit(_scrollToTop.value + 1)
}
}
suspend fun sentToTop() {

View File

@ -123,11 +123,13 @@ abstract class FeedViewModel(val localFilter: FeedFilter<Note>) : ViewModel() {
val scrollToTop = _scrollToTop.asStateFlow()
var scrolltoTopPending = false
suspend fun sendToTop() {
fun sendToTop() {
if (scrolltoTopPending) return
scrolltoTopPending = true
_scrollToTop.emit(_scrollToTop.value + 1)
viewModelScope.launch(Dispatchers.IO) {
_scrollToTop.emit(_scrollToTop.value + 1)
}
}
suspend fun sentToTop() {

View File

@ -141,11 +141,18 @@ fun WatchAccountForHomeScreen(
val account = remember(accountState) { accountState?.account } ?: return
val scope = rememberCoroutineScope()
var firstTime by remember(accountViewModel) { mutableStateOf(true) }
LaunchedEffect(accountViewModel, account.defaultHomeFollowList) {
scope.launch(Dispatchers.IO) {
NostrHomeDataSource.invalidateFilters()
homeFeedViewModel.invalidateDataAndSendToTop(true)
repliesFeedViewModel.invalidateDataAndSendToTop(true)
// Only invalidate when things change. Not in the first run
if (firstTime) {
firstTime = false
} else {
scope.launch(Dispatchers.IO) {
NostrHomeDataSource.invalidateFilters()
homeFeedViewModel.invalidateDataAndSendToTop(true)
repliesFeedViewModel.invalidateDataAndSendToTop(true)
}
}
}
}

View File

@ -103,13 +103,16 @@ fun WatchAccountForNotifications(
val accountState by accountViewModel.accountLiveData.observeAsState()
val account = remember(accountState) { accountState?.account } ?: return
LaunchedEffect(accountViewModel, account.defaultNotificationFollowList) {
NostrAccountDataSource.invalidateFilters()
if (notifFeedViewModel.scrollToTop.value > 0) {
notifFeedViewModel.clear()
}
var firstTime by remember(accountViewModel) { mutableStateOf(true) }
notifFeedViewModel.invalidateDataAndSendToTop(true)
LaunchedEffect(accountViewModel, account.defaultNotificationFollowList) {
if (firstTime) {
firstTime = false
} else {
NostrAccountDataSource.invalidateFilters()
notifFeedViewModel.clear()
notifFeedViewModel.invalidateDataAndSendToTop(true)
}
}
}

View File

@ -133,9 +133,15 @@ fun WatchAccountForVideoScreen(videoFeedView: NostrVideoFeedViewModel, accountVi
val accountState by accountViewModel.accountLiveData.observeAsState()
val account = remember(accountState) { accountState?.account } ?: return
var firstTime by remember(accountViewModel) { mutableStateOf(true) }
LaunchedEffect(accountViewModel, account.defaultStoriesFollowList) {
NostrVideoDataSource.resetFilters()
videoFeedView.invalidateDataAndSendToTop(true)
if (firstTime) {
firstTime = false
} else {
NostrVideoDataSource.resetFilters()
videoFeedView.invalidateDataAndSendToTop(true)
}
}
}