Эх сурвалжийг харах

Add AssistantRepository

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk 1 жил өмнө
parent
commit
b2c0202d42

+ 21 - 3
app/src/androidTest/java/com/nextcloud/client/assistant/AssistantRepositoryTests.kt

@@ -22,7 +22,7 @@
 package com.nextcloud.client.assistant
 
 import com.nextcloud.client.account.UserAccountManagerImpl
-import com.nextcloud.operations.assistant.AssistantRepository
+import com.nextcloud.client.assistant.repository.AssistantRepository
 import com.owncloud.android.AbstractOnServerIT
 import org.junit.Assert.assertTrue
 import org.junit.Before
@@ -40,13 +40,31 @@ class AssistantRepositoryTests: AbstractOnServerIT() {
 
     @Test
     fun testGetTaskTypes() {
-        assertTrue(sut?.getTaskTypes()?.ocs?.data?.types?.isNotEmpty() == true)
+        assertTrue(sut?.getTaskTypes()?.resultData?.isNotEmpty() == true)
     }
 
-    @Test
+    /*
+
+       @Test
     fun testGetTaskList() {
         assertTrue(sut?.getTaskList("assistant")?.ocs?.data?.types?.isNotEmpty() == true)
     }
 
+    @Test
+    fun testCreateTask() {
+        val input = "How many files I have?"
+        val type = "OCP\\TextProcessing\\HeadlineTaskType"
+        val result = sut?.createTask(input, type)
+        assertTrue(result != null)
+    }
+
+    @Test
+    fun testDeleteTask() {
+        val taskList = sut?.getTaskList("assistant")?.ocs?.data
+        assertTrue(sut?.getTaskList("assistant")?.ocs?.data?.types?.isNotEmpty() == true)
+    }
+
+     */
+
 
 }

+ 1 - 1
app/src/androidTest/java/com/owncloud/android/AbstractOnServerIT.java

@@ -112,7 +112,7 @@ public abstract class AbstractOnServerIT extends AbstractIT {
 
     @After
     public void after() {
-        deleteAllFilesOnServer();
+        // deleteAllFilesOnServer();
 
         super.after();
     }

+ 24 - 13
app/src/main/java/com/nextcloud/client/assistant/AssistantViewModel.kt

@@ -21,39 +21,41 @@
 
 package com.nextcloud.client.assistant
 
-import android.content.Context
 import androidx.lifecycle.ViewModel
 import androidx.lifecycle.viewModelScope
-import com.nextcloud.client.account.User
-import com.nextcloud.operations.assistant.AssistantRepository
-import com.nextcloud.operations.assistant.model.CreatedTask
-import com.nextcloud.operations.assistant.model.TaskTypes
+import com.nextcloud.client.assistant.repository.AssistantRepository
+import com.nextcloud.common.NextcloudClient
+import com.owncloud.android.lib.common.operations.RemoteOperationResult
+import com.owncloud.android.lib.resources.assistant.model.TaskTypes
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.flow.MutableStateFlow
 import kotlinx.coroutines.flow.StateFlow
 import kotlinx.coroutines.flow.update
 import kotlinx.coroutines.launch
 
-class AssistantViewModel(context: Context, user: User) : ViewModel() {
+class AssistantViewModel(client: NextcloudClient) : ViewModel() {
 
-    private var repository: AssistantRepository? = null
+    private val repository: AssistantRepository = AssistantRepository(client)
 
-    private val _taskTypes = MutableStateFlow<TaskTypes?>(null)
-    val taskTypes: StateFlow<TaskTypes?> = _taskTypes
+    private val _taskTypes = MutableStateFlow<RemoteOperationResult<TaskTypes>?>(null)
+    val taskTypes: StateFlow<RemoteOperationResult<TaskTypes>?> = _taskTypes
 
-    private val _task = MutableStateFlow<CreatedTask?>(null)
+    /*
+     private val _task = MutableStateFlow<CreatedTask?>(null)
     val task: StateFlow<CreatedTask?> = _task
+     */
+
 
     init {
         viewModelScope.launch(Dispatchers.IO) {
-            repository = AssistantRepository(user, context)
-
+            val result = repository.getTaskTypes()
             _taskTypes.update {
-                repository?.getTaskTypes()
+                result
             }
         }
     }
 
+    /*
     fun deleteTask(id: String) {
         viewModelScope.launch(Dispatchers.IO) {
             repository?.deleteTask(id)
@@ -76,4 +78,13 @@ class AssistantViewModel(context: Context, user: User) : ViewModel() {
             repository?.createTask(input, type, identifier = " ")
         }
     }
+     */
+
+
+    fun createTask(
+        input: String,
+        type: String,
+    ) {
+    }
+
 }

+ 9 - 10
app/src/main/java/com/nextcloud/client/assistant/AsssistantScreen.kt

@@ -32,12 +32,10 @@ import androidx.compose.foundation.lazy.LazyColumn
 import androidx.compose.foundation.lazy.items
 import androidx.compose.foundation.rememberScrollState
 import androidx.compose.foundation.text.KeyboardOptions
-import androidx.compose.material3.ButtonColors
 import androidx.compose.material3.ButtonDefaults
 import androidx.compose.material3.FilledTonalButton
 import androidx.compose.material3.Text
 import androidx.compose.material3.TextField
-import androidx.compose.material3.TextFieldDefaults
 import androidx.compose.runtime.Composable
 import androidx.compose.runtime.collectAsState
 import androidx.compose.runtime.getValue
@@ -50,10 +48,9 @@ import androidx.compose.ui.res.stringResource
 import androidx.compose.ui.text.input.KeyboardType
 import androidx.compose.ui.unit.dp
 import com.google.android.material.floatingactionbutton.FloatingActionButton
-import com.nextcloud.android.common.ui.theme.utils.ColorRole
-import com.nextcloud.operations.assistant.model.OcsType
 import com.nextcloud.ui.composeComponents.SimpleAlertDialog
 import com.owncloud.android.R
+import com.owncloud.android.lib.resources.assistant.model.TaskType
 
 @OptIn(ExperimentalFoundationApi::class)
 @Composable
@@ -77,14 +74,16 @@ fun AssistantScreen(viewModel: AssistantViewModel, floatingActionButton: Floatin
             .padding(16.dp)
     ) {
         stickyHeader {
-            taskTypes?.let { it ->
-                TaskTypesRow(selectedTaskType, data = it.ocs.data.types) { taskId ->
-                    selectedTaskType = taskId
+            taskTypes?.let { taskTypes ->
+                taskTypes.resultData?.types.let {
+                    TaskTypesRow(selectedTaskType, data = it) { taskId ->
+                        selectedTaskType = taskId
+                    }
                 }
             }
         }
 
-        items(taskTypes?.ocs?.data?.types ?: listOf()) {
+        items(taskTypes?.resultData?.types ?: listOf()) {
             Text(text = it.toString())
         }
     }
@@ -131,13 +130,13 @@ private fun AddTaskAlertDialog(viewModel: AssistantViewModel, type: String, dism
 }
 
 @Composable
-private fun TaskTypesRow(selectedTaskType: String?, data: List<OcsType>, selectTaskType: (String) -> Unit) {
+private fun TaskTypesRow(selectedTaskType: String?, data: List<TaskType>?, selectTaskType: (String) -> Unit) {
     Row(
         modifier = Modifier
             .fillMaxWidth()
             .horizontalScroll(rememberScrollState())
     ) {
-        data.forEach {
+        data?.forEach {
             FilledTonalButton(
                 onClick = { selectTaskType(it.id) },
                 colors = ButtonDefaults.buttonColors(

+ 72 - 0
app/src/main/java/com/nextcloud/client/assistant/repository/AssistantRepository.kt

@@ -0,0 +1,72 @@
+/*
+ * Nextcloud Android client application
+ *
+ * @author Alper Ozturk
+ * Copyright (C) 2024 Alper Ozturk
+ * Copyright (C) 2024 Nextcloud GmbH
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package com.nextcloud.client.assistant.repository
+
+import com.nextcloud.common.NextcloudClient
+import com.owncloud.android.lib.common.operations.RemoteOperationResult
+import com.owncloud.android.lib.resources.assistant.GetTaskTypesRemoteOperation
+import com.owncloud.android.lib.resources.assistant.model.TaskType
+import com.owncloud.android.lib.resources.assistant.model.TaskTypes
+
+class AssistantRepository(private val client: NextcloudClient): AssistantRepositoryType {
+
+    override fun getTaskTypes(): RemoteOperationResult<TaskTypes> {
+        return GetTaskTypesRemoteOperation().execute(client)
+    }
+
+    /*
+      // TODO Check return type
+     override fun getTaskList(appId: String): TaskTypes? {
+        return operation.get("/ocs/v2.php/textprocessing/tasks/app/$appId", TaskTypes::class.java)
+    }
+
+    // TODO Check return type
+    override fun deleteTask(id: String): CreatedTask? {
+        return operation.delete("/ocs/v2.php/textprocessing/task/$id", TaskTypes::class.java)
+    }
+
+    // TODO Check return type
+    override fun getTask(id: String): CreatedTask? {
+        return operation.get("/ocs/v2.php/textprocessing/task/$id", TaskTypes::class.java)
+    }
+
+    override fun createTask(
+        input: String,
+        type: String,
+        appId: String,
+        identifier: String,
+    ): CreatedTask? {
+        val json = JSONObject().apply {
+            put("input", input)
+            put("type", type)
+            put("appId", appId)
+            put("identifier", identifier)
+        }
+
+        return operation.post(
+            "/ocs/v2.php/textprocessing/schedule",
+            CreatedTask::class.java,
+            json
+        )
+    }
+     */
+}

+ 46 - 0
app/src/main/java/com/nextcloud/client/assistant/repository/AssistantRepositoryType.kt

@@ -0,0 +1,46 @@
+/*
+ * Nextcloud Android client application
+ *
+ * @author Alper Ozturk
+ * Copyright (C) 2024 Alper Ozturk
+ * Copyright (C) 2024 Nextcloud GmbH
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package com.nextcloud.client.assistant.repository
+
+import com.owncloud.android.lib.common.operations.RemoteOperationResult
+import com.owncloud.android.lib.resources.assistant.model.TaskType
+import com.owncloud.android.lib.resources.assistant.model.TaskTypes
+
+interface AssistantRepositoryType {
+    fun getTaskTypes(): RemoteOperationResult<TaskTypes>
+
+    /*
+     fun getTask(id: String): CreatedTask?
+
+    fun deleteTask(id: String): CreatedTask?
+
+    fun getTaskList(appId: String): TaskTypes?
+
+    fun createTask(
+        input: String,
+        type: String,
+        appId: String = "assistant",
+        identifier: String = ""
+    ): CreatedTask?
+     */
+
+}

+ 34 - 11
app/src/main/java/com/nextcloud/ui/composeFragment/ComposeFragment.kt

@@ -26,14 +26,24 @@ import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
 import androidx.compose.runtime.Composable
+import androidx.compose.runtime.LaunchedEffect
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.remember
+import androidx.compose.runtime.setValue
 import androidx.compose.ui.platform.ViewCompositionStrategy
 import androidx.fragment.app.Fragment
 import com.google.android.material.floatingactionbutton.FloatingActionButton
 import com.nextcloud.client.assistant.AssistantScreen
 import com.nextcloud.client.assistant.AssistantViewModel
+import com.nextcloud.common.NextcloudClient
+import com.nextcloud.common.User
 import com.nextcloud.utils.extensions.getSerializableArgument
 import com.owncloud.android.R
 import com.owncloud.android.databinding.FragmentComposeViewBinding
+import com.owncloud.android.lib.common.OwnCloudClientFactory
+import com.owncloud.android.lib.common.accounts.AccountUtils
+import com.owncloud.android.lib.common.utils.Log_OC
 import com.owncloud.android.ui.fragment.FileFragment
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.withContext
@@ -71,18 +81,31 @@ class ComposeFragment : FileFragment() {
     @Composable
     private fun Content(destination: ComposeDestinations?) {
         val floatingActionButton: FloatingActionButton = requireActivity().findViewById(R.id.fab_main)
+        var nextcloudClient by remember { mutableStateOf<NextcloudClient?>(null) }
 
-        return when (destination) {
-            ComposeDestinations.AssistantScreen -> {
-                AssistantScreen(
-                    viewModel = AssistantViewModel(
-                        context = requireContext(),
-                        user = containerActivity.storageManager.user
-                    ),
-                    floatingActionButton
-                )
-            }
-            else -> {
+        LaunchedEffect(Unit) {
+            nextcloudClient = getNextcloudClient()
+        }
+
+        return if (destination == ComposeDestinations.AssistantScreen && nextcloudClient != null) {
+            AssistantScreen(
+                viewModel = AssistantViewModel(
+                    client = nextcloudClient!!
+                ),
+                floatingActionButton
+            )
+        } else {
+
+        }
+    }
+
+    private suspend fun getNextcloudClient(): NextcloudClient? {
+        return withContext(Dispatchers.IO) {
+            try {
+                OwnCloudClientFactory.createNextcloudClient(containerActivity.storageManager.user, requireContext())
+            } catch (e: AccountUtils.AccountNotFoundException) {
+                Log_OC.e(this, "Error caught at init of AssistantRepository", e)
+                null
             }
         }
     }

+ 0 - 5
app/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java

@@ -74,14 +74,9 @@ import com.nextcloud.client.network.ClientFactory;
 import com.nextcloud.client.network.ConnectivityService;
 import com.nextcloud.client.preferences.AppPreferences;
 import com.nextcloud.client.utils.IntentUtil;
-import com.nextcloud.common.NextcloudClient;
 import com.nextcloud.java.util.Optional;
 import com.nextcloud.model.WorkerState;
 import com.nextcloud.model.WorkerStateLiveData;
-import com.nextcloud.operations.assistant.AssistantRepository;
-import com.nextcloud.operations.assistant.model.CreatedTask;
-import com.nextcloud.operations.assistant.model.Ocs;
-import com.nextcloud.operations.assistant.model.TaskTypes;
 import com.nextcloud.utils.extensions.BundleExtensionsKt;
 import com.nextcloud.utils.extensions.IntentExtensionsKt;
 import com.nextcloud.utils.view.FastScrollUtils;