Skip to content

Commit 3ed44c5

Browse files
committed
fix: ui state
Signed-off-by: alperozturk <[email protected]>
1 parent a89e5d5 commit 3ed44c5

File tree

11 files changed

+67
-81
lines changed

11 files changed

+67
-81
lines changed

app/schemas/com.nextcloud.client.database.NextcloudDatabase/94.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"formatVersion": 1,
33
"database": {
44
"version": 94,
5-
"identityHash": "db05652467df368538baf5582ea892ae",
5+
"identityHash": "0f6b884f78553e89f37e2bdda10f805e",
66
"entities": [
77
{
88
"tableName": "arbitrary_data",
@@ -1198,14 +1198,19 @@
11981198
},
11991199
{
12001200
"tableName": "assistant",
1201-
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `type` TEXT, `status` TEXT, `userId` TEXT, `appId` TEXT, `input` TEXT, `output` TEXT, `completionExpectedAt` INTEGER, `progress` INTEGER, `lastUpdated` INTEGER, `scheduledAt` INTEGER, `endedAt` INTEGER)",
1201+
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `accountName` TEXT, `type` TEXT, `status` TEXT, `userId` TEXT, `appId` TEXT, `input` TEXT, `output` TEXT, `completionExpectedAt` INTEGER, `progress` INTEGER, `lastUpdated` INTEGER, `scheduledAt` INTEGER, `endedAt` INTEGER)",
12021202
"fields": [
12031203
{
12041204
"fieldPath": "id",
12051205
"columnName": "id",
12061206
"affinity": "INTEGER",
12071207
"notNull": true
12081208
},
1209+
{
1210+
"fieldPath": "accountName",
1211+
"columnName": "accountName",
1212+
"affinity": "TEXT"
1213+
},
12091214
{
12101215
"fieldPath": "type",
12111216
"columnName": "type",
@@ -1272,7 +1277,7 @@
12721277
],
12731278
"setupQueries": [
12741279
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
1275-
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'db05652467df368538baf5582ea892ae')"
1280+
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '0f6b884f78553e89f37e2bdda10f805e')"
12761281
]
12771282
}
12781283
}

app/src/main/java/com/nextcloud/client/assistant/AssistantScreen.kt

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package com.nextcloud.client.assistant
99

1010
import android.app.Activity
1111
import androidx.compose.foundation.Image
12+
import androidx.compose.foundation.layout.Arrangement
1213
import androidx.compose.foundation.layout.Column
1314
import androidx.compose.foundation.layout.PaddingValues
1415
import androidx.compose.foundation.layout.Spacer
@@ -30,6 +31,7 @@ import androidx.compose.material3.MaterialTheme
3031
import androidx.compose.material3.Scaffold
3132
import androidx.compose.material3.SnackbarHost
3233
import androidx.compose.material3.SnackbarHostState
34+
import androidx.compose.material3.Text
3335
import androidx.compose.material3.pulltorefresh.pullToRefresh
3436
import androidx.compose.material3.pulltorefresh.rememberPullToRefreshState
3537
import androidx.compose.runtime.Composable
@@ -39,15 +41,17 @@ import androidx.compose.runtime.collectAsState
3941
import androidx.compose.runtime.getValue
4042
import androidx.compose.runtime.remember
4143
import androidx.compose.runtime.rememberCoroutineScope
44+
import androidx.compose.ui.Alignment
4245
import androidx.compose.ui.Modifier
4346
import androidx.compose.ui.graphics.ColorFilter
4447
import androidx.compose.ui.res.colorResource
4548
import androidx.compose.ui.res.painterResource
4649
import androidx.compose.ui.res.stringResource
50+
import androidx.compose.ui.text.style.TextAlign
4751
import androidx.compose.ui.tooling.preview.Preview
4852
import androidx.compose.ui.unit.dp
53+
import androidx.compose.ui.unit.sp
4954
import com.nextcloud.client.assistant.component.AddTaskAlertDialog
50-
import com.nextcloud.client.assistant.component.CenterText
5155
import com.nextcloud.client.assistant.extensions.getInputTitle
5256
import com.nextcloud.client.assistant.model.ScreenOverlayState
5357
import com.nextcloud.client.assistant.model.ScreenState
@@ -237,7 +241,9 @@ private fun AssistantContent(
237241
modifier = Modifier
238242
.fillMaxSize()
239243
.padding(paddingValues)
240-
.padding(12.dp)
244+
.padding(12.dp),
245+
verticalArrangement = Arrangement.Top,
246+
horizontalAlignment = Alignment.CenterHorizontally
241247
) {
242248
items(taskList, key = { it.id }) { task ->
243249
TaskView(
@@ -259,20 +265,27 @@ private fun EmptyContent(paddingValues: PaddingValues, iconId: Int?, description
259265
modifier = Modifier
260266
.fillMaxSize()
261267
.padding(paddingValues)
262-
.padding(16.dp)
268+
.padding(16.dp),
269+
verticalArrangement = Arrangement.Center,
270+
horizontalAlignment = Alignment.CenterHorizontally
263271
) {
264272
iconId?.let {
265273
Image(
266274
painter = painterResource(id = iconId),
267-
modifier = Modifier.size(16.dp),
275+
modifier = Modifier.size(32.dp),
268276
colorFilter = ColorFilter.tint(color = colorResource(R.color.text_color)),
269-
contentDescription = "status icon"
277+
contentDescription = "empty content icon"
270278
)
271279

272280
Spacer(modifier = Modifier.height(8.dp))
273281
}
274282

275-
CenterText(text = stringResource(id = descriptionId))
283+
Text(
284+
text = stringResource(descriptionId),
285+
fontSize = 18.sp,
286+
textAlign = TextAlign.Center,
287+
color = colorResource(R.color.text_color)
288+
)
276289
}
277290
}
278291

@@ -313,5 +326,9 @@ private fun AssistantEmptyScreenPreview() {
313326
private fun getMockViewModel(giveEmptyTasks: Boolean): AssistantViewModel {
314327
val mockLocalRepository = MockAssistantLocalRepository()
315328
val mockRemoteRepository = MockAssistantRemoteRepository(giveEmptyTasks)
316-
return AssistantViewModel(remoteRepository = mockRemoteRepository, localRepository = mockLocalRepository)
329+
return AssistantViewModel(
330+
accountName = "test:localhost",
331+
remoteRepository = mockRemoteRepository,
332+
localRepository = mockLocalRepository
333+
)
317334
}

app/src/main/java/com/nextcloud/client/assistant/AssistantViewModel.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import kotlinx.coroutines.isActive
2727
import kotlinx.coroutines.launch
2828

2929
class AssistantViewModel(
30+
private val accountName: String,
3031
private val remoteRepository: AssistantRemoteRepository,
3132
private val localRepository: AssistantLocalRepository
3233
) : ViewModel() {
@@ -86,21 +87,18 @@ class AssistantViewModel(
8687
// endregion
8788

8889
private suspend fun fetchTaskListSuspending() {
89-
val cachedTasks = localRepository.getCachedTasks()
90+
val cachedTasks = localRepository.getCachedTasks(accountName)
9091
if (cachedTasks.isNotEmpty()) {
9192
_filteredTaskList.value = cachedTasks.sortedByDescending { it.id }
92-
updateTaskListScreenState()
9393
}
9494

9595
val taskType = _selectedTaskType.value?.id ?: return
9696
val result = remoteRepository.getTaskList(taskType)
9797
if (result != null) {
9898
taskList = result
9999
_filteredTaskList.value = taskList?.sortedByDescending { it.id }
100-
localRepository.cacheTasks(result)
100+
localRepository.cacheTasks(result, accountName)
101101
}
102-
103-
updateTaskListScreenState()
104102
}
105103

106104
@Suppress("MagicNumber")
@@ -150,7 +148,7 @@ class AssistantViewModel(
150148
fun fetchTaskList() {
151149
viewModelScope.launch(Dispatchers.IO) {
152150
// Try cached data first
153-
val cachedTasks = localRepository.getCachedTasks()
151+
val cachedTasks = localRepository.getCachedTasks(accountName)
154152
if (cachedTasks.isNotEmpty()) {
155153
_filteredTaskList.update {
156154
cachedTasks.sortedByDescending { it.id }
@@ -168,7 +166,7 @@ class AssistantViewModel(
168166
}
169167
}
170168

171-
localRepository.cacheTasks(result)
169+
localRepository.cacheTasks(result, accountName)
172170
updateSnackbarMessage(null)
173171
} else {
174172
updateSnackbarMessage(R.string.assistant_screen_task_list_error_state_message)
@@ -202,7 +200,7 @@ class AssistantViewModel(
202200

203201
if (result.isSuccess) {
204202
removeTaskFromList(id)
205-
localRepository.deleteTask(id)
203+
localRepository.deleteTask(id, accountName)
206204
}
207205
}
208206
}

app/src/main/java/com/nextcloud/client/assistant/component/CenterText.kt

Lines changed: 0 additions & 31 deletions
This file was deleted.

app/src/main/java/com/nextcloud/client/assistant/model/ScreenState.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ sealed class ScreenState {
2424

2525
fun emptyTaskList(): ScreenState = EmptyContent(
2626
descriptionId = R.string.assistant_screen_create_a_new_task_from_bottom_right_text,
27-
iconId = R.drawable.ic_plus
27+
iconId = null
2828
)
2929
}
3030
}

app/src/main/java/com/nextcloud/client/assistant/repository/local/AssistantLocalRepository.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ package com.nextcloud.client.assistant.repository.local
1010
import com.owncloud.android.lib.resources.assistant.v2.model.Task
1111

1212
interface AssistantLocalRepository {
13-
suspend fun cacheTasks(tasks: List<Task>)
14-
suspend fun getCachedTasks(): List<Task>
15-
suspend fun insertTask(task: Task)
16-
suspend fun deleteTask(id: Long)
17-
suspend fun clearAll()
13+
suspend fun cacheTasks(tasks: List<Task>, accountName: String)
14+
suspend fun getCachedTasks(accountName: String): List<Task>
15+
suspend fun insertTask(task: Task, accountName: String)
16+
suspend fun deleteTask(id: Long, accountName: String)
1817
}

app/src/main/java/com/nextcloud/client/assistant/repository/local/AssistantLocalRepositoryImpl.kt

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,29 @@ import com.owncloud.android.lib.resources.assistant.v2.model.TaskOutput
1515

1616
class AssistantLocalRepositoryImpl(private val assistantDao: AssistantDao) : AssistantLocalRepository {
1717

18-
override suspend fun cacheTasks(tasks: List<Task>) {
19-
val entities = tasks.map { it.toEntity() }
18+
override suspend fun cacheTasks(tasks: List<Task>, accountName: String) {
19+
val entities = tasks.map { it.toEntity(accountName) }
2020
assistantDao.insertAssistantTasks(entities)
2121
}
2222

23-
override suspend fun getCachedTasks(): List<Task> {
24-
val entities = assistantDao.getAllAssistantTasks()
23+
override suspend fun getCachedTasks(accountName: String): List<Task> {
24+
val entities = assistantDao.getAssistantTasksByAccount(accountName)
2525
return entities.map { it.toTask() }
2626
}
2727

28-
override suspend fun insertTask(task: Task) {
29-
assistantDao.insertAssistantTask(task.toEntity())
28+
override suspend fun insertTask(task: Task, accountName: String) {
29+
assistantDao.insertAssistantTask(task.toEntity(accountName))
3030
}
3131

32-
override suspend fun deleteTask(id: Long) {
33-
val cached = assistantDao.getAllAssistantTasks().firstOrNull { it.id == id } ?: return
32+
override suspend fun deleteTask(id: Long, accountName: String) {
33+
val cached = assistantDao.getAssistantTasksByAccount(accountName).firstOrNull { it.id == id } ?: return
3434
assistantDao.deleteAssistantTask(cached)
3535
}
3636

37-
override suspend fun clearAll() {
38-
assistantDao.deleteAllAssistantTasks()
39-
}
40-
4137
// region Mapping helpers
42-
private fun Task.toEntity(): AssistantEntity = AssistantEntity(
38+
private fun Task.toEntity(accountName: String): AssistantEntity = AssistantEntity(
4339
id = this.id,
40+
accountName = accountName,
4441
type = this.type,
4542
status = this.status,
4643
userId = this.userId,

app/src/main/java/com/nextcloud/client/assistant/repository/local/MockAssistantLocalRepository.kt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,20 @@ class MockAssistantLocalRepository : AssistantLocalRepository {
1616
private val tasks = mutableListOf<Task>()
1717
private val mutex = Mutex()
1818

19-
override suspend fun cacheTasks(tasks: List<Task>) {
19+
override suspend fun cacheTasks(tasks: List<Task>, accountName: String) {
2020
mutex.withLock {
2121
this.tasks.clear()
2222
this.tasks.addAll(tasks)
2323
}
2424
}
2525

26-
override suspend fun getCachedTasks(): List<Task> = mutex.withLock { tasks.toList() }
26+
override suspend fun getCachedTasks(accountName: String): List<Task> = mutex.withLock { tasks.toList() }
2727

28-
override suspend fun insertTask(task: Task) {
28+
override suspend fun insertTask(task: Task, accountName: String) {
2929
mutex.withLock { tasks.add(task) }
3030
}
3131

32-
override suspend fun deleteTask(id: Long) {
32+
override suspend fun deleteTask(id: Long, accountName: String) {
3333
mutex.withLock { tasks.removeAll { it.id == id } }
3434
}
35-
36-
override suspend fun clearAll() {
37-
mutex.withLock { tasks.clear() }
38-
}
3935
}

app/src/main/java/com/nextcloud/client/database/dao/AssistantDao.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ interface AssistantDao {
3030
@Delete
3131
suspend fun deleteAssistantTask(task: AssistantEntity)
3232

33-
@Query("DELETE FROM ${ProviderMeta.ProviderTableMeta.ASSISTANT_TABLE_NAME}")
34-
suspend fun deleteAllAssistantTasks()
35-
36-
@Query("SELECT * FROM ${ProviderMeta.ProviderTableMeta.ASSISTANT_TABLE_NAME} ORDER BY lastUpdated DESC")
37-
suspend fun getAllAssistantTasks(): List<AssistantEntity>
33+
@Query(
34+
"""
35+
SELECT * FROM ${ProviderMeta.ProviderTableMeta.ASSISTANT_TABLE_NAME}
36+
WHERE accountName = :accountName
37+
ORDER BY lastUpdated DESC
38+
"""
39+
)
40+
suspend fun getAssistantTasksByAccount(accountName: String): List<AssistantEntity>
3841
}

app/src/main/java/com/nextcloud/client/database/entity/AssistantEntity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import com.owncloud.android.db.ProviderMeta
1515
data class AssistantEntity(
1616
@PrimaryKey(autoGenerate = true)
1717
val id: Long = 0L,
18+
val accountName: String?,
1819
val type: String?,
1920
val status: String?,
2021
val userId: String?,

0 commit comments

Comments
 (0)