Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Applying spotless and detekt on search module (JIRA 288) #2246

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions feature/search/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*
* Copyright 2024 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
*/
plugins {
alias(libs.plugins.mifos.android.feature)
alias(libs.plugins.mifos.android.library.compose)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
/*
* Copyright 2024 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
*/
package com.mifos.feature.search

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
Expand All @@ -21,4 +28,4 @@ class ExampleInstrumentedTest {
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.mifos.feature.search.test", appContext.packageName)
}
}
}
9 changes: 9 additions & 0 deletions feature/search/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2024 Mifos Initiative
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
If a copy of the MPL was not distributed with this file,
You can obtain one at https://mozilla.org/MPL/2.0/.
See https://github.com/openMF/android-client/blob/master/LICENSE.md
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

</manifest>
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*
* Copyright 2024 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
*/
package com.mifos.feature.search

import androidx.annotation.VisibleForTesting
Expand Down Expand Up @@ -29,9 +38,9 @@ import com.mifos.feature.search.components.SearchScreenResult

@Composable
fun SearchScreenRoute(
modifier: Modifier = Modifier,
onFabClick: (FabType) -> Unit,
onSearchOptionClick: (SearchedEntity) -> Unit,
modifier: Modifier = Modifier,
viewModel: SearchViewModel = hiltViewModel(),
) {
val state by viewModel.state
Expand All @@ -50,12 +59,12 @@ fun SearchScreenRoute(
@VisibleForTesting
@Composable
internal fun SearchScreenContent(
modifier: Modifier = Modifier,
state: SearchScreenState,
searchResultState: SearchResultState,
onEvent: (SearchScreenEvent) -> Unit,
onFabClick: (FabType) -> Unit,
onResultItemClick: (SearchedEntity) -> Unit
onResultItemClick: (SearchedEntity) -> Unit,
modifier: Modifier = Modifier,
) {
val snackbarHostState = remember { SnackbarHostState() }
var fabButtonState by remember { mutableStateOf<FabButtonState>(FabButtonState.Collapsed) }
Expand All @@ -67,25 +76,25 @@ internal fun SearchScreenContent(
fabButtons = listOf(
FabButton(
fabType = FabType.CLIENT,
iconRes = com.mifos.core.ui.R.drawable.core_ui_ic_person_black_24dp
iconRes = com.mifos.core.ui.R.drawable.core_ui_ic_person_black_24dp,
),
FabButton(
fabType = FabType.CENTER,
iconRes = com.mifos.core.ui.R.drawable.core_ui_ic_centers_24dp
iconRes = com.mifos.core.ui.R.drawable.core_ui_ic_centers_24dp,
),
FabButton(
fabType = FabType.GROUP,
iconRes = com.mifos.core.ui.R.drawable.core_ui_ic_group_black_24dp
)
iconRes = com.mifos.core.ui.R.drawable.core_ui_ic_group_black_24dp,
),
),
fabButtonState = fabButtonState,
onFabButtonStateChange = {
fabButtonState = it
},
onFabClick = onFabClick
onFabClick = onFabClick,
)
},
snackbarHostState = snackbarHostState
snackbarHostState = snackbarHostState,
) {
Column(
modifier = Modifier
Expand All @@ -97,13 +106,13 @@ internal fun SearchScreenContent(
modifier = Modifier
.padding(horizontal = 16.dp),
state = state,
onEvent = onEvent
onEvent = onEvent,
)

SearchScreenResult(
modifier = Modifier.weight(2f),
searchResultState = searchResultState,
onResultItemClick = onResultItemClick
onResultItemClick = onResultItemClick,
)
}
}
Expand All @@ -119,7 +128,7 @@ private fun SearchScreenContentEmptyStateAndLoadingPreview() {
searchResultState = SearchResultState.Loading,
onEvent = {},
onFabClick = {},
onResultItemClick = {}
onResultItemClick = {},
)
}

Expand All @@ -132,7 +141,7 @@ private fun SearchScreenContentInitialEmptyStatePreview() {
searchResultState = SearchResultState.Empty(),
onEvent = {},
onFabClick = {},
onResultItemClick = {}
onResultItemClick = {},
)
}

Expand All @@ -143,12 +152,12 @@ private fun SearchScreenContentEmptyResultPreview() {
modifier = Modifier,
state = SearchScreenState(
searchText = "yyy",
selectedFilter = FilterOption.Groups
selectedFilter = FilterOption.Groups,
),
searchResultState = SearchResultState.Empty(false),
onEvent = {},
onFabClick = {},
onResultItemClick = {}
onResultItemClick = {},
)
}

Expand All @@ -161,15 +170,15 @@ private fun SearchScreenContentErrorPreview() {
searchResultState = SearchResultState.Error("Something went wrong!"),
onEvent = {},
onFabClick = {},
onResultItemClick = {}
onResultItemClick = {},
)
}

@DevicePreviews
@Composable
private fun SearchScreenContentSuccessPreview(
@PreviewParameter(SearchResultPreviewParameter::class)
results: List<SearchedEntity>
results: List<SearchedEntity>,
) {
SearchScreenContent(
modifier = Modifier,
Expand All @@ -179,6 +188,6 @@ private fun SearchScreenContentSuccessPreview(
searchResultState = SearchResultState.Success(results),
onEvent = {},
onFabClick = {},
onResultItemClick = {}
onResultItemClick = {},
)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
/*
* Copyright 2024 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
*/
package com.mifos.feature.search

import com.mifos.core.objects.SearchedEntity

data class SearchUiState(
val isLoading: Boolean = false,
val error: String? = null,
val searchedEntities: List<SearchedEntity> = emptyList()
)
val searchedEntities: List<SearchedEntity> = emptyList(),
)
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*
* Copyright 2024 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
*/
package com.mifos.feature.search

import androidx.compose.runtime.mutableStateOf
Expand Down Expand Up @@ -25,8 +34,8 @@ class SearchViewModel @Inject constructor(
var state = mutableStateOf(SearchScreenState())
private set

private val _searchResultState = MutableStateFlow<SearchResultState>(SearchResultState.Empty())
val searchResult = _searchResultState.asStateFlow()
private val searchResultState = MutableStateFlow<SearchResultState>(SearchResultState.Empty())
val searchResult = searchResultState.asStateFlow()

private var searchJob: Job? = null

Expand All @@ -35,25 +44,25 @@ class SearchViewModel @Inject constructor(
is SearchScreenEvent.UpdateSearchText -> {
viewModelScope.launch {
state.value = state.value.copy(
searchText = event.searchText
searchText = event.searchText,
)
}
}

is SearchScreenEvent.ClearSearchText -> {
viewModelScope.launch {
state.value = state.value.copy(
searchText = ""
searchText = "",
)

_searchResultState.update { SearchResultState.Empty() }
searchResultState.update { SearchResultState.Empty() }
}
}

is SearchScreenEvent.UpdateSelectedFilter -> {
viewModelScope.launch {
state.value = state.value.copy(
selectedFilter = event.filter
selectedFilter = event.filter,
)

getSearchResult()
Expand All @@ -63,8 +72,11 @@ class SearchViewModel @Inject constructor(
is SearchScreenEvent.UpdateExactMatch -> {
viewModelScope.launch {
state.value = state.value.copy(
exactMatch = if (state.value.exactMatch == null) true
else !state.value.exactMatch!!
exactMatch = if (state.value.exactMatch == null) {
true
} else {
!state.value.exactMatch!!
},
)

getSearchResult()
Expand All @@ -76,7 +88,6 @@ class SearchViewModel @Inject constructor(
getSearchResult()
}
}

}
}

Expand All @@ -87,16 +98,16 @@ class SearchViewModel @Inject constructor(
searchJob = searchRepository.searchResources(
query = state.value.searchText,
resources = state.value.selectedFilter?.value,
exactMatch = state.value.exactMatch
exactMatch = state.value.exactMatch,
).onStart {
_searchResultState.update { SearchResultState.Loading }
searchResultState.update { SearchResultState.Loading }
}.catch { throwable ->
_searchResultState.update { SearchResultState.Error(throwable.message.toString()) }
searchResultState.update { SearchResultState.Error(throwable.message.toString()) }
}.onEach { results ->
if (results.isEmpty()) {
_searchResultState.update { SearchResultState.Empty(false) }
searchResultState.update { SearchResultState.Empty(false) }
} else {
_searchResultState.update { SearchResultState.Success(results) }
searchResultState.update { SearchResultState.Success(results) }
}
}.launchIn(viewModelScope)
}
Expand All @@ -113,15 +124,15 @@ sealed interface SearchResultState {
data class SearchScreenState(
val searchText: String = "",
val selectedFilter: FilterOption? = null,
val exactMatch: Boolean? = null
val exactMatch: Boolean? = null,
)

sealed interface SearchScreenEvent {
data class UpdateSearchText(val searchText: String) : SearchScreenEvent

data class UpdateSelectedFilter(val filter: FilterOption? = null) : SearchScreenEvent

data object ClearSearchText: SearchScreenEvent
data object ClearSearchText : SearchScreenEvent

data object UpdateExactMatch : SearchScreenEvent

Expand All @@ -142,4 +153,4 @@ sealed class FilterOption(val label: String, val value: String) {
companion object {
val values = listOf(Clients, Groups, LoanAccounts, SavingsAccounts)
}
}
}
Loading
Loading