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
@@ -64,13 +64,9 @@ fun AppNavigation(
composable(route.route, route.arguments, content = { composable(route.route, route.arguments, content = {
val scrollToTop = it.arguments?.getBoolean("scrollToTop") ?: false val scrollToTop = it.arguments?.getBoolean("scrollToTop") ?: false
LaunchedEffect(key1 = it) { if (scrollToTop) {
if (scrollToTop) { videoFeedViewModel.sendToTop()
launch { it.arguments?.remove("scrollToTop")
videoFeedViewModel.sendToTop()
it.arguments?.remove("scrollToTop")
}
}
} }
VideoScreen( VideoScreen(
@@ -85,13 +81,9 @@ fun AppNavigation(
composable(route.route, route.arguments, content = { composable(route.route, route.arguments, content = {
val scrollToTop = it.arguments?.getBoolean("scrollToTop") ?: false val scrollToTop = it.arguments?.getBoolean("scrollToTop") ?: false
LaunchedEffect(key1 = it) { if (scrollToTop) {
if (scrollToTop) { searchFeedViewModel.sendToTop()
launch { it.arguments?.remove("scrollToTop")
searchFeedViewModel.sendToTop()
it.arguments?.remove("scrollToTop")
}
}
} }
SearchScreen( SearchScreen(
@@ -107,14 +99,10 @@ fun AppNavigation(
val scrollToTop = it.arguments?.getBoolean("scrollToTop") ?: false val scrollToTop = it.arguments?.getBoolean("scrollToTop") ?: false
val nip47 = it.arguments?.getString("nip47") val nip47 = it.arguments?.getString("nip47")
LaunchedEffect(key1 = it) { if (scrollToTop) {
if (scrollToTop) { homeFeedViewModel.sendToTop()
launch { repliesFeedViewModel.sendToTop()
homeFeedViewModel.sendToTop() it.arguments?.remove("scrollToTop")
repliesFeedViewModel.sendToTop()
it.arguments?.remove("scrollToTop")
}
}
} }
HomeScreen( HomeScreen(
@@ -140,14 +128,10 @@ fun AppNavigation(
composable(route.route, route.arguments, content = { composable(route.route, route.arguments, content = {
val scrollToTop = it.arguments?.getBoolean("scrollToTop") ?: false val scrollToTop = it.arguments?.getBoolean("scrollToTop") ?: false
LaunchedEffect(key1 = it) { if (scrollToTop) {
if (scrollToTop) { notifFeedViewModel.clear()
launch { notifFeedViewModel.sendToTop()
notifFeedViewModel.clear() it.arguments?.remove("scrollToTop")
notifFeedViewModel.sendToTop()
it.arguments?.remove("scrollToTop")
}
}
} }
NotificationScreen( NotificationScreen(
@@ -287,8 +287,6 @@ private fun AuthorPictureAndComment(
} }
} }
println("AAAA $content")
content.first?.let { content.first?.let {
val route by remember { val route by remember {
derivedStateOf { derivedStateOf {
@@ -54,11 +54,13 @@ open class CardFeedViewModel(val localFilter: FeedFilter<Note>) : ViewModel() {
val scrollToTop = _scrollToTop.asStateFlow() val scrollToTop = _scrollToTop.asStateFlow()
var scrolltoTopPending = false var scrolltoTopPending = false
suspend fun sendToTop() { fun sendToTop() {
if (scrolltoTopPending) return if (scrolltoTopPending) return
scrolltoTopPending = true scrolltoTopPending = true
_scrollToTop.emit(_scrollToTop.value + 1) viewModelScope.launch(Dispatchers.IO) {
_scrollToTop.emit(_scrollToTop.value + 1)
}
} }
suspend fun sentToTop() { suspend fun sentToTop() {
@@ -123,11 +123,13 @@ abstract class FeedViewModel(val localFilter: FeedFilter<Note>) : ViewModel() {
val scrollToTop = _scrollToTop.asStateFlow() val scrollToTop = _scrollToTop.asStateFlow()
var scrolltoTopPending = false var scrolltoTopPending = false
suspend fun sendToTop() { fun sendToTop() {
if (scrolltoTopPending) return if (scrolltoTopPending) return
scrolltoTopPending = true scrolltoTopPending = true
_scrollToTop.emit(_scrollToTop.value + 1) viewModelScope.launch(Dispatchers.IO) {
_scrollToTop.emit(_scrollToTop.value + 1)
}
} }
suspend fun sentToTop() { suspend fun sentToTop() {
@@ -141,11 +141,18 @@ fun WatchAccountForHomeScreen(
val account = remember(accountState) { accountState?.account } ?: return val account = remember(accountState) { accountState?.account } ?: return
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
var firstTime by remember(accountViewModel) { mutableStateOf(true) }
LaunchedEffect(accountViewModel, account.defaultHomeFollowList) { LaunchedEffect(accountViewModel, account.defaultHomeFollowList) {
scope.launch(Dispatchers.IO) { // Only invalidate when things change. Not in the first run
NostrHomeDataSource.invalidateFilters() if (firstTime) {
homeFeedViewModel.invalidateDataAndSendToTop(true) firstTime = false
repliesFeedViewModel.invalidateDataAndSendToTop(true) } else {
scope.launch(Dispatchers.IO) {
NostrHomeDataSource.invalidateFilters()
homeFeedViewModel.invalidateDataAndSendToTop(true)
repliesFeedViewModel.invalidateDataAndSendToTop(true)
}
} }
} }
} }
@@ -103,13 +103,16 @@ fun WatchAccountForNotifications(
val accountState by accountViewModel.accountLiveData.observeAsState() val accountState by accountViewModel.accountLiveData.observeAsState()
val account = remember(accountState) { accountState?.account } ?: return val account = remember(accountState) { accountState?.account } ?: return
LaunchedEffect(accountViewModel, account.defaultNotificationFollowList) { var firstTime by remember(accountViewModel) { mutableStateOf(true) }
NostrAccountDataSource.invalidateFilters()
if (notifFeedViewModel.scrollToTop.value > 0) {
notifFeedViewModel.clear()
}
notifFeedViewModel.invalidateDataAndSendToTop(true) LaunchedEffect(accountViewModel, account.defaultNotificationFollowList) {
if (firstTime) {
firstTime = false
} else {
NostrAccountDataSource.invalidateFilters()
notifFeedViewModel.clear()
notifFeedViewModel.invalidateDataAndSendToTop(true)
}
} }
} }
@@ -133,9 +133,15 @@ fun WatchAccountForVideoScreen(videoFeedView: NostrVideoFeedViewModel, accountVi
val accountState by accountViewModel.accountLiveData.observeAsState() val accountState by accountViewModel.accountLiveData.observeAsState()
val account = remember(accountState) { accountState?.account } ?: return val account = remember(accountState) { accountState?.account } ?: return
var firstTime by remember(accountViewModel) { mutableStateOf(true) }
LaunchedEffect(accountViewModel, account.defaultStoriesFollowList) { LaunchedEffect(accountViewModel, account.defaultStoriesFollowList) {
NostrVideoDataSource.resetFilters() if (firstTime) {
videoFeedView.invalidateDataAndSendToTop(true) firstTime = false
} else {
NostrVideoDataSource.resetFilters()
videoFeedView.invalidateDataAndSendToTop(true)
}
} }
} }