Skip to content

Commit c8fa158

Browse files
committed
Make AssistantViewModel testable
Signed-off-by: alperozturk <[email protected]>
1 parent b4da7b4 commit c8fa158

File tree

5 files changed

+115
-13
lines changed

5 files changed

+115
-13
lines changed

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

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

2424
import androidx.lifecycle.ViewModel
2525
import androidx.lifecycle.viewModelScope
26-
import com.nextcloud.client.assistant.repository.AssistantRepository
27-
import com.nextcloud.common.NextcloudClient
26+
import com.nextcloud.client.assistant.repository.AssistantRepositoryType
2827
import com.owncloud.android.MainApp
2928
import com.owncloud.android.R
3029
import com.owncloud.android.lib.resources.assistant.model.Task
@@ -35,9 +34,7 @@ import kotlinx.coroutines.flow.StateFlow
3534
import kotlinx.coroutines.flow.update
3635
import kotlinx.coroutines.launch
3736

38-
class AssistantViewModel(client: NextcloudClient) : ViewModel() {
39-
40-
private val repository: AssistantRepository = AssistantRepository(client)
37+
class AssistantViewModel(private val repository: AssistantRepositoryType) : ViewModel() {
4138

4239
private val _selectedTaskType = MutableStateFlow<TaskType?>(null)
4340
val selectedTaskType: StateFlow<TaskType?> = _selectedTaskType
@@ -88,7 +85,7 @@ class AssistantViewModel(client: NextcloudClient) : ViewModel() {
8885
viewModelScope.launch(Dispatchers.IO) {
8986
val allTaskType = MainApp.getAppContext().getString(R.string.assistant_screen_all_task_type)
9087
val result = arrayListOf(TaskType(null, allTaskType, null))
91-
val taskTypes = repository.getTaskTypes().resultData.types ?: listOf()
88+
val taskTypes = repository.getTaskTypes().resultData.types
9289
result.addAll(taskTypes)
9390

9491
_taskTypes.update {

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import androidx.compose.material3.ExperimentalMaterial3Api
3838
import androidx.compose.material3.FloatingActionButton
3939
import androidx.compose.material3.Icon
4040
import androidx.compose.material3.LinearProgressIndicator
41+
import androidx.compose.material3.MaterialTheme
4142
import androidx.compose.material3.pulltorefresh.rememberPullToRefreshState
4243
import androidx.compose.runtime.Composable
4344
import androidx.compose.runtime.LaunchedEffect
@@ -49,13 +50,15 @@ import androidx.compose.runtime.setValue
4950
import androidx.compose.ui.Alignment
5051
import androidx.compose.ui.Modifier
5152
import androidx.compose.ui.input.nestedscroll.nestedScroll
52-
import androidx.compose.ui.platform.LocalContext
5353
import androidx.compose.ui.res.stringResource
54+
import androidx.compose.ui.tooling.preview.Preview
5455
import androidx.compose.ui.unit.dp
5556
import com.nextcloud.client.assistant.component.AddTaskAlertDialog
5657
import com.nextcloud.client.assistant.component.CenterText
5758
import com.nextcloud.client.assistant.component.TaskTypesRow
5859
import com.nextcloud.client.assistant.component.TaskView
60+
import com.nextcloud.client.assistant.repository.AssistantMockRepository
61+
import com.nextcloud.ui.composeActivity.ComposeActivity
5962
import com.nextcloud.ui.composeComponents.alertDialog.SimpleAlertDialog
6063
import com.owncloud.android.R
6164
import com.owncloud.android.lib.resources.assistant.model.Task
@@ -66,8 +69,7 @@ import kotlinx.coroutines.delay
6669
@Suppress("LongMethod")
6770
@OptIn(ExperimentalMaterial3Api::class)
6871
@Composable
69-
fun AssistantScreen(viewModel: AssistantViewModel) {
70-
val activity = LocalContext.current as Activity
72+
fun AssistantScreen(viewModel: AssistantViewModel, activity: Activity) {
7173
val loading by viewModel.loading.collectAsState()
7274
val selectedTaskType by viewModel.selectedTaskType.collectAsState()
7375
val filteredTaskList by viewModel.filteredTaskList.collectAsState()
@@ -240,3 +242,17 @@ private fun EmptyTaskList(selectedTaskType: TaskType?, taskTypes: List<TaskType>
240242
)
241243
}
242244
}
245+
246+
@Composable
247+
@Preview
248+
private fun AssistantScreenPreview() {
249+
val mockRepository = AssistantMockRepository()
250+
MaterialTheme(
251+
content = {
252+
AssistantScreen(
253+
viewModel = AssistantViewModel(repository = mockRepository),
254+
activity = ComposeActivity()
255+
)
256+
}
257+
)
258+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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.Task
26+
import com.owncloud.android.lib.resources.assistant.model.TaskList
27+
import com.owncloud.android.lib.resources.assistant.model.TaskType
28+
import com.owncloud.android.lib.resources.assistant.model.TaskTypes
29+
30+
class AssistantMockRepository : AssistantRepositoryType {
31+
override fun getTaskTypes(): RemoteOperationResult<TaskTypes> {
32+
return RemoteOperationResult<TaskTypes>(RemoteOperationResult.ResultCode.OK).apply {
33+
resultData = TaskTypes(
34+
listOf(
35+
TaskType("1", "FreePrompt", "You can create free prompt text"),
36+
TaskType("2", "Generate Headline", "You can create generate headline text")
37+
)
38+
)
39+
}
40+
}
41+
42+
override fun createTask(input: String, type: String): RemoteOperationResult<Void> {
43+
return RemoteOperationResult<Void>(RemoteOperationResult.ResultCode.OK)
44+
}
45+
46+
override fun getTaskList(appId: String): RemoteOperationResult<TaskList> {
47+
return RemoteOperationResult<TaskList>(RemoteOperationResult.ResultCode.OK).apply {
48+
resultData = TaskList(
49+
listOf(
50+
Task(
51+
1,
52+
"FreePrompt",
53+
null,
54+
"12",
55+
"",
56+
"Give me some text",
57+
"Lorem Ipsum is simply dummy text of the printing and typesetting industry. " +
58+
"Lorem Ipsum has been the industry's standard dummy text ever since the 1500s," +
59+
" when an unknown printer took a galley of type and scrambled it to make a type" +
60+
" specimen book. It has survived not only five centuries, " +
61+
"but also the leap into electronic typesetting, remaining essentially unchanged." +
62+
" It was popularised in the 1960s with the release of Letraset sheets containing " +
63+
"Lorem Ipsum passages, and more recently with desktop publishing software like Aldus" +
64+
" PageMaker including versions of Lorem Ipsum",
65+
"",
66+
""
67+
),
68+
Task(
69+
2,
70+
"GenerateHeadline",
71+
null,
72+
"12",
73+
"",
74+
"Give me some text 2",
75+
"Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
76+
"",
77+
""
78+
)
79+
)
80+
)
81+
}
82+
}
83+
84+
override fun deleteTask(id: Long): RemoteOperationResult<Void> {
85+
return RemoteOperationResult<Void>(RemoteOperationResult.ResultCode.OK)
86+
}
87+
}

app/src/main/java/com/nextcloud/ui/composeActivity/ComposeActivity.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import androidx.compose.runtime.remember
3333
import androidx.compose.runtime.setValue
3434
import com.nextcloud.client.assistant.AssistantScreen
3535
import com.nextcloud.client.assistant.AssistantViewModel
36+
import com.nextcloud.client.assistant.repository.AssistantRepository
3637
import com.nextcloud.common.NextcloudClient
3738
import com.nextcloud.common.User
3839
import com.nextcloud.utils.extensions.getSerializableArgument
@@ -103,11 +104,12 @@ class ComposeActivity : DrawerActivity() {
103104
}
104105

105106
if (destination == ComposeDestination.AssistantScreen) {
106-
nextcloudClient?.let {
107+
nextcloudClient?.let { client ->
107108
AssistantScreen(
108109
viewModel = AssistantViewModel(
109-
client = it
110-
)
110+
repository = AssistantRepository(client)
111+
),
112+
activity = this
111113
)
112114
}
113115
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ private void filterDrawerMenu(final Menu menu, @NonNull final User user) {
468468
DrawerMenuUtil.filterSearchMenuItems(menu, user, getResources());
469469
DrawerMenuUtil.filterTrashbinMenuItem(menu, capability);
470470
DrawerMenuUtil.filterActivityMenuItem(menu, capability);
471-
DrawerMenuUtil.filterAssistantMenuItem(menu, capability);
471+
// DrawerMenuUtil.filterAssistantMenuItem(menu, capability);
472472
DrawerMenuUtil.filterGroupfoldersMenuItem(menu, capability);
473473

474474
DrawerMenuUtil.setupHomeMenuItem(menu, getResources());

0 commit comments

Comments
 (0)