Add Pull To Refresh for fetching task list

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-02-29 09:48:29 +01:00 committed by Alper Öztürk
parent 9afdbd89c9
commit 07edd95bf6
2 changed files with 37 additions and 8 deletions

View file

@ -95,7 +95,7 @@ class AssistantViewModel(client: NextcloudClient) : ViewModel() {
}
}
private fun getTaskList(appId: String = "assistant") {
fun getTaskList(appId: String = "assistant", onCompleted: () -> Unit = {}) {
viewModelScope.launch(Dispatchers.IO) {
val result = repository.getTaskList(appId)
@ -106,6 +106,8 @@ class AssistantViewModel(client: NextcloudClient) : ViewModel() {
_loading.update {
false
}
onCompleted()
}
}

View file

@ -23,19 +23,26 @@ package com.nextcloud.client.assistant
import android.app.Activity
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.pulltorefresh.rememberPullToRefreshState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
@ -48,13 +55,14 @@ import com.owncloud.android.R
import com.owncloud.android.lib.resources.assistant.model.Task
import com.owncloud.android.lib.resources.assistant.model.TaskType
import com.owncloud.android.utils.DisplayUtils
import kotlinx.coroutines.delay
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AssistantScreen(viewModel: AssistantViewModel, floatingActionButton: FloatingActionButton) {
// TODO hide sort group, search bar
// TODO top bar, back button causes crash
// TODO generate list according to selection (selectedTask).
// TODO add swipe to refresh
val activity = LocalContext.current as Activity
val loading by viewModel.loading.collectAsState()
val selectedTaskType by viewModel.selectedTaskType.collectAsState()
@ -66,16 +74,35 @@ fun AssistantScreen(viewModel: AssistantViewModel, floatingActionButton: Floatin
mutableStateOf(false)
}
val pullRefreshState = rememberPullToRefreshState()
if (pullRefreshState.isRefreshing) {
LaunchedEffect(true) {
delay(1500)
viewModel.getTaskList(onCompleted = {
pullRefreshState.endRefresh()
})
}
}
floatingActionButton.setOnClickListener {
showAddTaskAlertDialog = true
}
if (loading) {
CenterText(text = stringResource(id = R.string.assistant_screen_loading))
} else {
val tasks = taskList?.resultData?.tasks ?: return
val types = taskTypes?.resultData?.types ?: return
AssistantContent(tasks, types, selectedTaskType, viewModel)
Box(Modifier.nestedScroll(pullRefreshState.nestedScrollConnection)) {
if (loading || pullRefreshState.isRefreshing) {
CenterText(text = stringResource(id = R.string.assistant_screen_loading))
} else {
val tasks = taskList?.resultData?.tasks ?: return
val types = taskTypes?.resultData?.types ?: return
AssistantContent(tasks, types, selectedTaskType, viewModel)
}
if (pullRefreshState.isRefreshing) {
LinearProgressIndicator(modifier = Modifier.fillMaxWidth())
} else {
LinearProgressIndicator(progress = { pullRefreshState.progress }, modifier = Modifier.fillMaxWidth())
}
}
if (isTaskCreated) {