Skip to content

Commit 3c8271f

Browse files
committed
Add AssistantRepository
Signed-off-by: alperozturk <[email protected]>
1 parent 698dc63 commit 3c8271f

File tree

8 files changed

+207
-43
lines changed

8 files changed

+207
-43
lines changed

app/src/androidTest/java/com/nextcloud/client/assistant/AssistantRepositoryTests.kt

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
package com.nextcloud.client.assistant
2323

2424
import com.nextcloud.client.account.UserAccountManagerImpl
25-
import com.nextcloud.operations.assistant.AssistantRepository
25+
import com.nextcloud.client.assistant.repository.AssistantRepository
2626
import com.owncloud.android.AbstractOnServerIT
2727
import org.junit.Assert.assertTrue
2828
import org.junit.Before
@@ -40,13 +40,31 @@ class AssistantRepositoryTests: AbstractOnServerIT() {
4040

4141
@Test
4242
fun testGetTaskTypes() {
43-
assertTrue(sut?.getTaskTypes()?.ocs?.data?.types?.isNotEmpty() == true)
43+
assertTrue(sut?.getTaskTypes()?.resultData?.isNotEmpty() == true)
4444
}
4545

46-
@Test
46+
/*
47+
48+
@Test
4749
fun testGetTaskList() {
4850
assertTrue(sut?.getTaskList("assistant")?.ocs?.data?.types?.isNotEmpty() == true)
4951
}
5052
53+
@Test
54+
fun testCreateTask() {
55+
val input = "How many files I have?"
56+
val type = "OCP\\TextProcessing\\HeadlineTaskType"
57+
val result = sut?.createTask(input, type)
58+
assertTrue(result != null)
59+
}
60+
61+
@Test
62+
fun testDeleteTask() {
63+
val taskList = sut?.getTaskList("assistant")?.ocs?.data
64+
assertTrue(sut?.getTaskList("assistant")?.ocs?.data?.types?.isNotEmpty() == true)
65+
}
66+
67+
*/
68+
5169

5270
}

app/src/androidTest/java/com/owncloud/android/AbstractOnServerIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public static void beforeAll() {
112112

113113
@After
114114
public void after() {
115-
deleteAllFilesOnServer();
115+
// deleteAllFilesOnServer();
116116

117117
super.after();
118118
}

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

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,39 +21,41 @@
2121

2222
package com.nextcloud.client.assistant
2323

24-
import android.content.Context
2524
import androidx.lifecycle.ViewModel
2625
import androidx.lifecycle.viewModelScope
27-
import com.nextcloud.client.account.User
28-
import com.nextcloud.operations.assistant.AssistantRepository
29-
import com.nextcloud.operations.assistant.model.CreatedTask
30-
import com.nextcloud.operations.assistant.model.TaskTypes
26+
import com.nextcloud.client.assistant.repository.AssistantRepository
27+
import com.nextcloud.common.NextcloudClient
28+
import com.owncloud.android.lib.common.operations.RemoteOperationResult
29+
import com.owncloud.android.lib.resources.assistant.model.TaskTypes
3130
import kotlinx.coroutines.Dispatchers
3231
import kotlinx.coroutines.flow.MutableStateFlow
3332
import kotlinx.coroutines.flow.StateFlow
3433
import kotlinx.coroutines.flow.update
3534
import kotlinx.coroutines.launch
3635

37-
class AssistantViewModel(context: Context, user: User) : ViewModel() {
36+
class AssistantViewModel(client: NextcloudClient) : ViewModel() {
3837

39-
private var repository: AssistantRepository? = null
38+
private val repository: AssistantRepository = AssistantRepository(client)
4039

41-
private val _taskTypes = MutableStateFlow<TaskTypes?>(null)
42-
val taskTypes: StateFlow<TaskTypes?> = _taskTypes
40+
private val _taskTypes = MutableStateFlow<RemoteOperationResult<TaskTypes>?>(null)
41+
val taskTypes: StateFlow<RemoteOperationResult<TaskTypes>?> = _taskTypes
4342

44-
private val _task = MutableStateFlow<CreatedTask?>(null)
43+
/*
44+
private val _task = MutableStateFlow<CreatedTask?>(null)
4545
val task: StateFlow<CreatedTask?> = _task
46+
*/
47+
4648

4749
init {
4850
viewModelScope.launch(Dispatchers.IO) {
49-
repository = AssistantRepository(user, context)
50-
51+
val result = repository.getTaskTypes()
5152
_taskTypes.update {
52-
repository?.getTaskTypes()
53+
result
5354
}
5455
}
5556
}
5657

58+
/*
5759
fun deleteTask(id: String) {
5860
viewModelScope.launch(Dispatchers.IO) {
5961
repository?.deleteTask(id)
@@ -76,4 +78,13 @@ class AssistantViewModel(context: Context, user: User) : ViewModel() {
7678
repository?.createTask(input, type, identifier = " ")
7779
}
7880
}
81+
*/
82+
83+
84+
fun createTask(
85+
input: String,
86+
type: String,
87+
) {
88+
}
89+
7990
}

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,10 @@ import androidx.compose.foundation.lazy.LazyColumn
3232
import androidx.compose.foundation.lazy.items
3333
import androidx.compose.foundation.rememberScrollState
3434
import androidx.compose.foundation.text.KeyboardOptions
35-
import androidx.compose.material3.ButtonColors
3635
import androidx.compose.material3.ButtonDefaults
3736
import androidx.compose.material3.FilledTonalButton
3837
import androidx.compose.material3.Text
3938
import androidx.compose.material3.TextField
40-
import androidx.compose.material3.TextFieldDefaults
4139
import androidx.compose.runtime.Composable
4240
import androidx.compose.runtime.collectAsState
4341
import androidx.compose.runtime.getValue
@@ -50,10 +48,9 @@ import androidx.compose.ui.res.stringResource
5048
import androidx.compose.ui.text.input.KeyboardType
5149
import androidx.compose.ui.unit.dp
5250
import com.google.android.material.floatingactionbutton.FloatingActionButton
53-
import com.nextcloud.android.common.ui.theme.utils.ColorRole
54-
import com.nextcloud.operations.assistant.model.OcsType
5551
import com.nextcloud.ui.composeComponents.SimpleAlertDialog
5652
import com.owncloud.android.R
53+
import com.owncloud.android.lib.resources.assistant.model.TaskType
5754

5855
@OptIn(ExperimentalFoundationApi::class)
5956
@Composable
@@ -77,14 +74,16 @@ fun AssistantScreen(viewModel: AssistantViewModel, floatingActionButton: Floatin
7774
.padding(16.dp)
7875
) {
7976
stickyHeader {
80-
taskTypes?.let { it ->
81-
TaskTypesRow(selectedTaskType, data = it.ocs.data.types) { taskId ->
82-
selectedTaskType = taskId
77+
taskTypes?.let { taskTypes ->
78+
taskTypes.resultData?.types.let {
79+
TaskTypesRow(selectedTaskType, data = it) { taskId ->
80+
selectedTaskType = taskId
81+
}
8382
}
8483
}
8584
}
8685

87-
items(taskTypes?.ocs?.data?.types ?: listOf()) {
86+
items(taskTypes?.resultData?.types ?: listOf()) {
8887
Text(text = it.toString())
8988
}
9089
}
@@ -131,13 +130,13 @@ private fun AddTaskAlertDialog(viewModel: AssistantViewModel, type: String, dism
131130
}
132131

133132
@Composable
134-
private fun TaskTypesRow(selectedTaskType: String?, data: List<OcsType>, selectTaskType: (String) -> Unit) {
133+
private fun TaskTypesRow(selectedTaskType: String?, data: List<TaskType>?, selectTaskType: (String) -> Unit) {
135134
Row(
136135
modifier = Modifier
137136
.fillMaxWidth()
138137
.horizontalScroll(rememberScrollState())
139138
) {
140-
data.forEach {
139+
data?.forEach {
141140
FilledTonalButton(
142141
onClick = { selectTaskType(it.id) },
143142
colors = ButtonDefaults.buttonColors(
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Nextcloud Android client application
3+
*
4+
* @author Alper Ozturk
5+
* Copyright (C) 2024 Alper Ozturk
6+
* Copyright (C) 2024 Nextcloud GmbH
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*/
21+
22+
package com.nextcloud.client.assistant.repository
23+
24+
import com.nextcloud.common.NextcloudClient
25+
import com.owncloud.android.lib.common.operations.RemoteOperationResult
26+
import com.owncloud.android.lib.resources.assistant.GetTaskTypesRemoteOperation
27+
import com.owncloud.android.lib.resources.assistant.model.TaskType
28+
import com.owncloud.android.lib.resources.assistant.model.TaskTypes
29+
30+
class AssistantRepository(private val client: NextcloudClient): AssistantRepositoryType {
31+
32+
override fun getTaskTypes(): RemoteOperationResult<TaskTypes> {
33+
return GetTaskTypesRemoteOperation().execute(client)
34+
}
35+
36+
/*
37+
// TODO Check return type
38+
override fun getTaskList(appId: String): TaskTypes? {
39+
return operation.get("/ocs/v2.php/textprocessing/tasks/app/$appId", TaskTypes::class.java)
40+
}
41+
42+
// TODO Check return type
43+
override fun deleteTask(id: String): CreatedTask? {
44+
return operation.delete("/ocs/v2.php/textprocessing/task/$id", TaskTypes::class.java)
45+
}
46+
47+
// TODO Check return type
48+
override fun getTask(id: String): CreatedTask? {
49+
return operation.get("/ocs/v2.php/textprocessing/task/$id", TaskTypes::class.java)
50+
}
51+
52+
override fun createTask(
53+
input: String,
54+
type: String,
55+
appId: String,
56+
identifier: String,
57+
): CreatedTask? {
58+
val json = JSONObject().apply {
59+
put("input", input)
60+
put("type", type)
61+
put("appId", appId)
62+
put("identifier", identifier)
63+
}
64+
65+
return operation.post(
66+
"/ocs/v2.php/textprocessing/schedule",
67+
CreatedTask::class.java,
68+
json
69+
)
70+
}
71+
*/
72+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Nextcloud Android client application
3+
*
4+
* @author Alper Ozturk
5+
* Copyright (C) 2024 Alper Ozturk
6+
* Copyright (C) 2024 Nextcloud GmbH
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*/
21+
22+
package com.nextcloud.client.assistant.repository
23+
24+
import com.owncloud.android.lib.common.operations.RemoteOperationResult
25+
import com.owncloud.android.lib.resources.assistant.model.TaskType
26+
import com.owncloud.android.lib.resources.assistant.model.TaskTypes
27+
28+
interface AssistantRepositoryType {
29+
fun getTaskTypes(): RemoteOperationResult<TaskTypes>
30+
31+
/*
32+
fun getTask(id: String): CreatedTask?
33+
34+
fun deleteTask(id: String): CreatedTask?
35+
36+
fun getTaskList(appId: String): TaskTypes?
37+
38+
fun createTask(
39+
input: String,
40+
type: String,
41+
appId: String = "assistant",
42+
identifier: String = ""
43+
): CreatedTask?
44+
*/
45+
46+
}

app/src/main/java/com/nextcloud/ui/composeFragment/ComposeFragment.kt

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,24 @@ import android.view.LayoutInflater
2626
import android.view.View
2727
import android.view.ViewGroup
2828
import androidx.compose.runtime.Composable
29+
import androidx.compose.runtime.LaunchedEffect
30+
import androidx.compose.runtime.getValue
31+
import androidx.compose.runtime.mutableStateOf
32+
import androidx.compose.runtime.remember
33+
import androidx.compose.runtime.setValue
2934
import androidx.compose.ui.platform.ViewCompositionStrategy
3035
import androidx.fragment.app.Fragment
3136
import com.google.android.material.floatingactionbutton.FloatingActionButton
3237
import com.nextcloud.client.assistant.AssistantScreen
3338
import com.nextcloud.client.assistant.AssistantViewModel
39+
import com.nextcloud.common.NextcloudClient
40+
import com.nextcloud.common.User
3441
import com.nextcloud.utils.extensions.getSerializableArgument
3542
import com.owncloud.android.R
3643
import com.owncloud.android.databinding.FragmentComposeViewBinding
44+
import com.owncloud.android.lib.common.OwnCloudClientFactory
45+
import com.owncloud.android.lib.common.accounts.AccountUtils
46+
import com.owncloud.android.lib.common.utils.Log_OC
3747
import com.owncloud.android.ui.fragment.FileFragment
3848
import kotlinx.coroutines.Dispatchers
3949
import kotlinx.coroutines.withContext
@@ -71,18 +81,31 @@ class ComposeFragment : FileFragment() {
7181
@Composable
7282
private fun Content(destination: ComposeDestinations?) {
7383
val floatingActionButton: FloatingActionButton = requireActivity().findViewById(R.id.fab_main)
84+
var nextcloudClient by remember { mutableStateOf<NextcloudClient?>(null) }
7485

75-
return when (destination) {
76-
ComposeDestinations.AssistantScreen -> {
77-
AssistantScreen(
78-
viewModel = AssistantViewModel(
79-
context = requireContext(),
80-
user = containerActivity.storageManager.user
81-
),
82-
floatingActionButton
83-
)
84-
}
85-
else -> {
86+
LaunchedEffect(Unit) {
87+
nextcloudClient = getNextcloudClient()
88+
}
89+
90+
return if (destination == ComposeDestinations.AssistantScreen && nextcloudClient != null) {
91+
AssistantScreen(
92+
viewModel = AssistantViewModel(
93+
client = nextcloudClient!!
94+
),
95+
floatingActionButton
96+
)
97+
} else {
98+
99+
}
100+
}
101+
102+
private suspend fun getNextcloudClient(): NextcloudClient? {
103+
return withContext(Dispatchers.IO) {
104+
try {
105+
OwnCloudClientFactory.createNextcloudClient(containerActivity.storageManager.user, requireContext())
106+
} catch (e: AccountUtils.AccountNotFoundException) {
107+
Log_OC.e(this, "Error caught at init of AssistantRepository", e)
108+
null
86109
}
87110
}
88111
}

app/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,9 @@
7474
import com.nextcloud.client.network.ConnectivityService;
7575
import com.nextcloud.client.preferences.AppPreferences;
7676
import com.nextcloud.client.utils.IntentUtil;
77-
import com.nextcloud.common.NextcloudClient;
7877
import com.nextcloud.java.util.Optional;
7978
import com.nextcloud.model.WorkerState;
8079
import com.nextcloud.model.WorkerStateLiveData;
81-
import com.nextcloud.operations.assistant.AssistantRepository;
82-
import com.nextcloud.operations.assistant.model.CreatedTask;
83-
import com.nextcloud.operations.assistant.model.Ocs;
84-
import com.nextcloud.operations.assistant.model.TaskTypes;
8580
import com.nextcloud.utils.extensions.BundleExtensionsKt;
8681
import com.nextcloud.utils.extensions.IntentExtensionsKt;
8782
import com.nextcloud.utils.view.FastScrollUtils;

0 commit comments

Comments
 (0)