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 = {
val scrollToTop = it.arguments?.getBoolean("scrollToTop") ?: false
LaunchedEffect(key1 = it) {
if (scrollToTop) {
launch {
videoFeedViewModel.sendToTop()
it.arguments?.remove("scrollToTop")
}
}
}
VideoScreen(
videoFeedView = videoFeedViewModel,
@@ -85,14 +81,10 @@ 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")
}
}
}
SearchScreen(
searchFeedViewModel = searchFeedViewModel,
@@ -107,15 +99,11 @@ 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")
}
}
}
HomeScreen(
homeFeedViewModel = homeFeedViewModel,
@@ -140,15 +128,11 @@ 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")
}
}
}
NotificationScreen(
notifFeedViewModel = notifFeedViewModel,
@@ -287,8 +287,6 @@ private fun AuthorPictureAndComment(
}
}
println("AAAA $content")
content.first?.let {
val route by remember {
derivedStateOf {
@@ -54,12 +54,14 @@ 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
viewModelScope.launch(Dispatchers.IO) {
_scrollToTop.emit(_scrollToTop.value + 1)
}
}
suspend fun sentToTop() {
scrolltoTopPending = false
@@ -123,12 +123,14 @@ 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
viewModelScope.launch(Dispatchers.IO) {
_scrollToTop.emit(_scrollToTop.value + 1)
}
}
suspend fun sentToTop() {
scrolltoTopPending = false
@@ -141,7 +141,13 @@ fun WatchAccountForHomeScreen(
val account = remember(accountState) { accountState?.account } ?: return
val scope = rememberCoroutineScope()
var firstTime by remember(accountViewModel) { mutableStateOf(true) }
LaunchedEffect(accountViewModel, account.defaultHomeFollowList) {
// Only invalidate when things change. Not in the first run
if (firstTime) {
firstTime = false
} else {
scope.launch(Dispatchers.IO) {
NostrHomeDataSource.invalidateFilters()
homeFeedViewModel.invalidateDataAndSendToTop(true)
@@ -149,6 +155,7 @@ fun WatchAccountForHomeScreen(
}
}
}
}
@Immutable
class TabItem(val resource: Int, val viewModel: FeedViewModel, val routeForLastRead: String, val scrollStateKey: String)
@@ -103,15 +103,18 @@ 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) }
LaunchedEffect(accountViewModel, account.defaultNotificationFollowList) {
if (firstTime) {
firstTime = false
} else {
NostrAccountDataSource.invalidateFilters()
notifFeedViewModel.clear()
notifFeedViewModel.invalidateDataAndSendToTop(true)
}
}
}
@Composable
fun SummaryBar(model: UserReactionsViewModel) {
@@ -133,11 +133,17 @@ 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) {
if (firstTime) {
firstTime = false
} else {
NostrVideoDataSource.resetFilters()
videoFeedView.invalidateDataAndSendToTop(true)
}
}
}
@OptIn(ExperimentalFoundationApi::class)
@Composable