Skip to content

Commit

Permalink
Replace deprecated SwipeRefresh composable
Browse files Browse the repository at this point in the history
  • Loading branch information
arkon committed May 20, 2023
1 parent 3569907 commit 1463072
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
8 changes: 4 additions & 4 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[*.{kt,kts}]
indent_size=4
insert_final_newline=true
ij_kotlin_allow_trailing_comma=true
ij_kotlin_allow_trailing_comma_on_call_site=true
indent_size = 4
insert_final_newline = true
ij_kotlin_allow_trailing_comma = true
ij_kotlin_allow_trailing_comma_on_call_site = true
1 change: 0 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ dependencies {
implementation "io.coil-kt:coil-compose:$coil_version"

final accompanist_version = '0.30.1'
implementation "com.google.accompanist:accompanist-swiperefresh:$accompanist_version"
implementation "com.google.accompanist:accompanist-navigation-material:$accompanist_version"

// Preferences
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
Expand All @@ -15,8 +18,6 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.google.accompanist.swiperefresh.SwipeRefresh
import com.google.accompanist.swiperefresh.rememberSwipeRefreshState
import com.livetl.android.R
import com.livetl.android.data.feed.Stream
import com.livetl.android.data.feed.StreamStatus
Expand All @@ -33,14 +34,13 @@ fun StreamsTab(
viewModel: StreamsTabViewModel,
) {
val coroutineScope = rememberCoroutineScope()
val refreshingFeed = rememberSwipeRefreshState(false)

fun refreshFeed() {
coroutineScope.launch {
refreshingFeed.isRefreshing = true
viewModel.isRefreshing = true
viewModel.loadStreams()
withContext(Dispatchers.Main) {
refreshingFeed.isRefreshing = false
viewModel.isRefreshing = false
}
}
}
Expand All @@ -49,19 +49,30 @@ fun StreamsTab(
refreshFeed()
}

SwipeRefresh(
modifier = Modifier.fillMaxSize(),
state = refreshingFeed,
val pullRefreshState = rememberPullRefreshState(
refreshing = viewModel.isRefreshing,
onRefresh = { refreshFeed() },
)

Box(
modifier = Modifier
.fillMaxSize()
.pullRefresh(pullRefreshState),
) {
if (!refreshingFeed.isRefreshing && viewModel.streams.isEmpty()) {
PullRefreshIndicator(
refreshing = viewModel.isRefreshing,
state = pullRefreshState,
modifier = Modifier.align(Alignment.TopCenter),
)

if (!viewModel.isRefreshing && viewModel.streams.isEmpty()) {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center,
) {
Text(stringResource(R.string.empty_streams))
}
return@SwipeRefresh
return
}

LazyColumn(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class StreamsTabViewModel(
private val status: StreamStatus,
) : ViewModel() {

var isRefreshing by mutableStateOf(false)
var streams by mutableStateOf<List<Stream>>(emptyList())

suspend fun loadStreams() {
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
rootProject.name = "LiveTL Android"

include(":app")

0 comments on commit 1463072

Please sign in to comment.