app: add support for refreshing hottest posts list

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2021-03-05 12:22:52 +05:30
parent 5990d27a07
commit 4585757ac2
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
4 changed files with 52 additions and 0 deletions

View file

@ -1,11 +1,14 @@
package dev.msfjarvis.lobsters.ui.main
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.BottomNavigation
import androidx.compose.material.BottomNavigationItem
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
@ -13,6 +16,7 @@ import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.compose.KEY_ROUTE
import androidx.navigation.compose.NavHost
@ -21,6 +25,7 @@ import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.navigate
import androidx.navigation.compose.rememberNavController
import androidx.paging.compose.collectAsLazyPagingItems
import dev.msfjarvis.lobsters.R
import dev.msfjarvis.lobsters.ui.navigation.Destination
import dev.msfjarvis.lobsters.ui.posts.HottestPosts
import dev.msfjarvis.lobsters.ui.posts.SavedPosts
@ -51,6 +56,12 @@ fun LobstersApp() {
}
Scaffold(
topBar = {
LobstersTopBar(
currentDestination = currentDestination,
reloadPosts = { viewModel.reloadPosts() },
)
},
bottomBar = {
LobstersBottomNav(
currentDestination,
@ -80,6 +91,33 @@ fun LobstersApp() {
}
}
@OptIn(ExperimentalAnimationApi::class)
@Composable
fun LobstersTopBar(
currentDestination: Destination,
reloadPosts: () -> Unit,
) {
TopAppBar(
title = {
Text(
text = stringResource(id = R.string.app_name),
modifier = Modifier.padding(vertical = 8.dp),
)
},
actions = {
if (currentDestination == Destination.Hottest) {
IconResource(
resourceId = R.drawable.ic_refresh_24px,
contentDescription = stringResource(id = R.string.refresh_posts_content_description),
modifier = Modifier
.padding(horizontal = 8.dp, vertical = 8.dp)
.clickable { reloadPosts() },
)
}
}
)
}
@Composable
fun LobstersBottomNav(
currentDestination: Destination,

View file

@ -35,6 +35,10 @@ class LobstersViewModel @Inject constructor(
}.launchIn(viewModelScope)
}
fun reloadPosts() {
pagingSource.invalidate()
}
fun toggleSave(post: SavedPost) {
viewModelScope.launch {
val isSaved = lobstersRepository.isPostSaved(post.shortId)

View file

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF000000"
android:pathData="M17.65,6.35C16.2,4.9 14.21,4 12,4c-4.42,0 -7.99,3.58 -7.99,8s3.57,8 7.99,8c3.73,0 6.84,-2.55 7.73,-6h-2.08c-0.82,2.33 -3.04,4 -5.65,4 -3.31,0 -6,-2.69 -6,-6s2.69,-6 6,-6c1.66,0 3.14,0.69 4.22,1.78L13,11h7V4l-2.35,2.35z" />
</vector>

View file

@ -8,4 +8,5 @@
<string name="avatar_content_description">%1$s\'s avatar</string>
<string name="add_to_saved_posts">Add to saved posts</string>
<string name="remove_from_saved_posts">Remove from saved posts</string>
<string name="refresh_posts_content_description">Refresh posts</string>
</resources>