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,14 +64,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) {
launch {
videoFeedViewModel.sendToTop() videoFeedViewModel.sendToTop()
it.arguments?.remove("scrollToTop") it.arguments?.remove("scrollToTop")
} }
}
}
VideoScreen( VideoScreen(
videoFeedView = videoFeedViewModel, videoFeedView = videoFeedViewModel,
@@ -85,14 +81,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) {
launch {
searchFeedViewModel.sendToTop() searchFeedViewModel.sendToTop()
it.arguments?.remove("scrollToTop") it.arguments?.remove("scrollToTop")
} }
}
}
SearchScreen( SearchScreen(
searchFeedViewModel = searchFeedViewModel, searchFeedViewModel = searchFeedViewModel,
@@ -107,15 +99,11 @@ 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) {
launch {
homeFeedViewModel.sendToTop() homeFeedViewModel.sendToTop()
repliesFeedViewModel.sendToTop() repliesFeedViewModel.sendToTop()
it.arguments?.remove("scrollToTop") it.arguments?.remove("scrollToTop")
} }
}
}
HomeScreen( HomeScreen(
homeFeedViewModel = homeFeedViewModel, homeFeedViewModel = homeFeedViewModel,
@@ -140,15 +128,11 @@ 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) {
launch {
notifFeedViewModel.clear() notifFeedViewModel.clear()
notifFeedViewModel.sendToTop() notifFeedViewModel.sendToTop()
it.arguments?.remove("scrollToTop") it.arguments?.remove("scrollToTop")
} }
}
}
NotificationScreen( NotificationScreen(
notifFeedViewModel = notifFeedViewModel, notifFeedViewModel = notifFeedViewModel,
@@ -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,12 +54,14 @@ 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
viewModelScope.launch(Dispatchers.IO) {
_scrollToTop.emit(_scrollToTop.value + 1) _scrollToTop.emit(_scrollToTop.value + 1)
} }
}
suspend fun sentToTop() { suspend fun sentToTop() {
scrolltoTopPending = false scrolltoTopPending = false
@@ -123,12 +123,14 @@ 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
viewModelScope.launch(Dispatchers.IO) {
_scrollToTop.emit(_scrollToTop.value + 1) _scrollToTop.emit(_scrollToTop.value + 1)
} }
}
suspend fun sentToTop() { suspend fun sentToTop() {
scrolltoTopPending = false scrolltoTopPending = false
@@ -141,13 +141,20 @@ 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) {
// Only invalidate when things change. Not in the first run
if (firstTime) {
firstTime = false
} else {
scope.launch(Dispatchers.IO) { scope.launch(Dispatchers.IO) {
NostrHomeDataSource.invalidateFilters() NostrHomeDataSource.invalidateFilters()
homeFeedViewModel.invalidateDataAndSendToTop(true) homeFeedViewModel.invalidateDataAndSendToTop(true)
repliesFeedViewModel.invalidateDataAndSendToTop(true) repliesFeedViewModel.invalidateDataAndSendToTop(true)
} }
} }
}
} }
@Immutable @Immutable
@@ -103,14 +103,17 @@ 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()
}
LaunchedEffect(accountViewModel, account.defaultNotificationFollowList) {
if (firstTime) {
firstTime = false
} else {
NostrAccountDataSource.invalidateFilters()
notifFeedViewModel.clear()
notifFeedViewModel.invalidateDataAndSendToTop(true) notifFeedViewModel.invalidateDataAndSendToTop(true)
} }
}
} }
@Composable @Composable
@@ -133,10 +133,16 @@ 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) {
if (firstTime) {
firstTime = false
} else {
NostrVideoDataSource.resetFilters() NostrVideoDataSource.resetFilters()
videoFeedView.invalidateDataAndSendToTop(true) videoFeedView.invalidateDataAndSendToTop(true)
} }
}
} }
@OptIn(ExperimentalFoundationApi::class) @OptIn(ExperimentalFoundationApi::class)