Bläddra i källkod

Add ViewModel

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk 1 år sedan
förälder
incheckning
b7b5907f98

+ 79 - 0
app/src/main/java/com/nextcloud/client/assistant/AssistantViewModel.kt

@@ -0,0 +1,79 @@
+/*
+ * 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
+
+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 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() {
+
+    private val repository = AssistantRepository(user, context)
+
+    private val _taskTypes = MutableStateFlow<TaskTypes?>(null)
+    val taskTypes: StateFlow<TaskTypes?> = _taskTypes
+
+    private val _task = MutableStateFlow<CreatedTask?>(null)
+    val task: StateFlow<CreatedTask?> = _task
+
+    init {
+        viewModelScope.launch(Dispatchers.IO) {
+            _taskTypes.update {
+                repository.getTaskTypes()
+            }
+        }
+    }
+
+    fun deleteTask(id: String) {
+        viewModelScope.launch(Dispatchers.IO) {
+            repository.deleteTask(id)
+        }
+    }
+
+    fun getTask(id: String) {
+        viewModelScope.launch(Dispatchers.IO) {
+            _task.update {
+                repository.getTask(id)
+            }
+        }
+    }
+
+    fun createTask(
+        input: String,
+        type: String,
+        appId: String,
+        identifier: String,
+    ) {
+        viewModelScope.launch(Dispatchers.IO) {
+            repository.createTask(input, type, appId, identifier)
+        }
+    }
+}

+ 13 - 6
app/src/main/java/com/nextcloud/client/assistant/AsssistantScreen.kt

@@ -22,22 +22,29 @@
 package com.nextcloud.client.assistant
 
 import androidx.compose.foundation.ExperimentalFoundationApi
+import androidx.compose.foundation.layout.Box
+import androidx.compose.foundation.layout.fillMaxSize
 import androidx.compose.foundation.layout.padding
 import androidx.compose.foundation.lazy.LazyColumn
+import androidx.compose.foundation.lazy.items
+import androidx.compose.foundation.lazy.itemsIndexed
 import androidx.compose.material3.Scaffold
 import androidx.compose.material3.Text
 import androidx.compose.runtime.Composable
+import androidx.compose.runtime.collectAsState
+import androidx.compose.runtime.getValue
 import androidx.compose.ui.Modifier
+import androidx.compose.ui.unit.dp
 
 @OptIn(ExperimentalFoundationApi::class)
 @Composable
-fun AssistantScreen() {
-    Scaffold {
-        LazyColumn(modifier = Modifier.padding(it)) {
-            stickyHeader {
-                Text(text = "AssistantScreen")
-            }
+fun AssistantScreen(viewModel: AssistantViewModel) {
+    val taskTypes by viewModel.taskTypes.collectAsState()
 
+    LazyColumn(modifier = Modifier.fillMaxSize().padding(16.dp)) {
+        items(taskTypes?.ocs?.data?.types ?: listOf()) {
+            Text(text = it.toString())
         }
     }
+
 }

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

@@ -28,10 +28,12 @@ import android.view.ViewGroup
 import androidx.compose.ui.platform.ViewCompositionStrategy
 import androidx.fragment.app.Fragment
 import com.nextcloud.client.assistant.AssistantScreen
+import com.nextcloud.client.assistant.AssistantViewModel
 import com.nextcloud.utils.extensions.getSerializableArgument
 import com.owncloud.android.databinding.FragmentComposeViewBinding
+import com.owncloud.android.ui.fragment.FileFragment
 
-class ComposeFragment : Fragment() {
+class ComposeFragment : FileFragment() {
 
     private var _binding: FragmentComposeViewBinding? = null
 
@@ -53,12 +55,17 @@ class ComposeFragment : Fragment() {
         binding.composeView.apply {
             setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
             setContent {
-                when(destination) {
+                when (destination) {
                     ComposeDestinations.AssistantScreen -> {
-                        AssistantScreen()
+                        AssistantScreen(
+                            viewModel = AssistantViewModel(
+                                context = requireContext(),
+                                user = containerActivity.storageManager.user
+                            )
+                        )
                     }
-                    else -> {
 
+                    else -> {
                     }
                 }
             }

+ 1 - 1
app/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java

@@ -549,7 +549,7 @@ public abstract class DrawerActivity extends ToolbarActivity
             ComposeFragment composeFragment = new ComposeFragment();
             Bundle bundle = new Bundle();
             bundle.putSerializable(ComposeFragment.destinationKey, ComposeDestinations.AssistantScreen);
-            composeFragment.setArguments( bundle);
+            composeFragment.setArguments(bundle);
 
             getSupportFragmentManager()
                 .beginTransaction()