Skip to content

Commit

Permalink
Make AssistantViewModel testable
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 committed Mar 12, 2024
1 parent b4da7b4 commit c8fa158
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ package com.nextcloud.client.assistant

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.nextcloud.client.assistant.repository.AssistantRepository
import com.nextcloud.common.NextcloudClient
import com.nextcloud.client.assistant.repository.AssistantRepositoryType
import com.owncloud.android.MainApp
import com.owncloud.android.R
import com.owncloud.android.lib.resources.assistant.model.Task
Expand All @@ -35,9 +34,7 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

class AssistantViewModel(client: NextcloudClient) : ViewModel() {

private val repository: AssistantRepository = AssistantRepository(client)
class AssistantViewModel(private val repository: AssistantRepositoryType) : ViewModel() {

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

_taskTypes.update {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.pulltorefresh.rememberPullToRefreshState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
Expand All @@ -49,13 +50,15 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.nextcloud.client.assistant.component.AddTaskAlertDialog
import com.nextcloud.client.assistant.component.CenterText
import com.nextcloud.client.assistant.component.TaskTypesRow
import com.nextcloud.client.assistant.component.TaskView
import com.nextcloud.client.assistant.repository.AssistantMockRepository
import com.nextcloud.ui.composeActivity.ComposeActivity
import com.nextcloud.ui.composeComponents.alertDialog.SimpleAlertDialog
import com.owncloud.android.R
import com.owncloud.android.lib.resources.assistant.model.Task
Expand All @@ -66,8 +69,7 @@ import kotlinx.coroutines.delay
@Suppress("LongMethod")
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AssistantScreen(viewModel: AssistantViewModel) {
val activity = LocalContext.current as Activity
fun AssistantScreen(viewModel: AssistantViewModel, activity: Activity) {
val loading by viewModel.loading.collectAsState()
val selectedTaskType by viewModel.selectedTaskType.collectAsState()
val filteredTaskList by viewModel.filteredTaskList.collectAsState()
Expand Down Expand Up @@ -240,3 +242,17 @@ private fun EmptyTaskList(selectedTaskType: TaskType?, taskTypes: List<TaskType>
)
}
}

@Composable
@Preview
private fun AssistantScreenPreview() {
val mockRepository = AssistantMockRepository()
MaterialTheme(
content = {
AssistantScreen(
viewModel = AssistantViewModel(repository = mockRepository),
activity = ComposeActivity()
)
}
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* 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.Task
import com.owncloud.android.lib.resources.assistant.model.TaskList
import com.owncloud.android.lib.resources.assistant.model.TaskType
import com.owncloud.android.lib.resources.assistant.model.TaskTypes

class AssistantMockRepository : AssistantRepositoryType {
override fun getTaskTypes(): RemoteOperationResult<TaskTypes> {
return RemoteOperationResult<TaskTypes>(RemoteOperationResult.ResultCode.OK).apply {
resultData = TaskTypes(
listOf(
TaskType("1", "FreePrompt", "You can create free prompt text"),
TaskType("2", "Generate Headline", "You can create generate headline text")
)
)
}
}

override fun createTask(input: String, type: String): RemoteOperationResult<Void> {
return RemoteOperationResult<Void>(RemoteOperationResult.ResultCode.OK)
}

override fun getTaskList(appId: String): RemoteOperationResult<TaskList> {
return RemoteOperationResult<TaskList>(RemoteOperationResult.ResultCode.OK).apply {
resultData = TaskList(
listOf(
Task(
1,
"FreePrompt",
null,
"12",
"",
"Give me some text",
"Lorem Ipsum is simply dummy text of the printing and typesetting industry. " +
"Lorem Ipsum has been the industry's standard dummy text ever since the 1500s," +
" when an unknown printer took a galley of type and scrambled it to make a type" +
" specimen book. It has survived not only five centuries, " +
"but also the leap into electronic typesetting, remaining essentially unchanged." +
" It was popularised in the 1960s with the release of Letraset sheets containing " +
"Lorem Ipsum passages, and more recently with desktop publishing software like Aldus" +
" PageMaker including versions of Lorem Ipsum",
"",
""
),
Task(
2,
"GenerateHeadline",
null,
"12",
"",
"Give me some text 2",
"Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
"",
""
)
)
)
}
}

override fun deleteTask(id: Long): RemoteOperationResult<Void> {
return RemoteOperationResult<Void>(RemoteOperationResult.ResultCode.OK)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import com.nextcloud.client.assistant.AssistantScreen
import com.nextcloud.client.assistant.AssistantViewModel
import com.nextcloud.client.assistant.repository.AssistantRepository
import com.nextcloud.common.NextcloudClient
import com.nextcloud.common.User
import com.nextcloud.utils.extensions.getSerializableArgument
Expand Down Expand Up @@ -103,11 +104,12 @@ class ComposeActivity : DrawerActivity() {
}

if (destination == ComposeDestination.AssistantScreen) {
nextcloudClient?.let {
nextcloudClient?.let { client ->
AssistantScreen(
viewModel = AssistantViewModel(
client = it
)
repository = AssistantRepository(client)
),
activity = this
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ private void filterDrawerMenu(final Menu menu, @NonNull final User user) {
DrawerMenuUtil.filterSearchMenuItems(menu, user, getResources());
DrawerMenuUtil.filterTrashbinMenuItem(menu, capability);
DrawerMenuUtil.filterActivityMenuItem(menu, capability);
DrawerMenuUtil.filterAssistantMenuItem(menu, capability);
// DrawerMenuUtil.filterAssistantMenuItem(menu, capability);
DrawerMenuUtil.filterGroupfoldersMenuItem(menu, capability);

DrawerMenuUtil.setupHomeMenuItem(menu, getResources());
Expand Down

0 comments on commit c8fa158

Please sign in to comment.