Skip to content

Commit

Permalink
Feature/add catalog search UI (#21)
Browse files Browse the repository at this point in the history
* feat(mobile-app): Updated catalog list viewmodel
* feat(mobile-app): Renamed old files
* feat(mobile-app): Deleted catalog list viewmodel factory
* feat(mobile-app): Added catalog search headline
* feat(mobile-app): Added catalog search string resources
* feat(mobile-app): Added catalog search input field slot
* feat(core-database): Added catalog search dao
* feat(core-database): Added catalog search dao unit tests
* feat(core-database): Added catalog search dao android ui tests
* feat(mobile-app): Added catalog search dao into di modules
* feat(mobile-app): Updated normalize title generation in catalog items
* feat(mobile-app): Updated catalog search headline composable
* feat(mobile-app): Updated catalog search text input slot
* feat(mobile-app): Added catalog search repository
* feat(mobile-app): Added catalog search route ui state
* feat(mobile-app): Added catalog search viewmodel
* feat(mobile-app): Updated drawable and string resources for catalog search ui
* feat(mobile-app): Updated cataloglist banner composable ui
* feat(mobile-app): Updated navigation graph
* feat(mobile-app): Updated catalog search item card composable ui
* feat(mobile-app): Updated catalog search slot composables by search ui state
* feat(mobile-app): Updated catalog search repository
* feat(mobile-app): Updated catalog search viewmodel
* feat(mobile-app): Updated catalog search route composable ui
* feat(mobile-app): Added fake local datasource search feature
* feat(mobile-app): Added fake catalog search repository unit tests
* feat(mobile-app): Updated unit test for settings viewmodel
* feat(mobile-app): Updated catalog settings viewmodel
* feat(mobile-app): Updated catalog search repository, viemwodel and unit tests for viewmodel
* feat(mobile-app): Updated fake catalog local data source
* codefactor: Removed unnecesary parenthesis
  • Loading branch information
marlonlom authored Mar 17, 2024
1 parent 810796f commit 1eb68d9
Show file tree
Hide file tree
Showing 39 changed files with 1,388 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import dev.marlonlom.apps.cappajv.core.database.datasource.LocalDataSourceImpl
import dev.marlonlom.apps.cappajv.core.preferences.UserPreferencesRepository
import dev.marlonlom.apps.cappajv.dataStore
import dev.marlonlom.apps.cappajv.features.catalog_list.CatalogListRepository
import dev.marlonlom.apps.cappajv.features.catalog_search.CatalogSearchRepository
import org.koin.android.ext.koin.androidContext
import org.koin.dsl.module
import java.util.Locale
Expand All @@ -22,7 +23,8 @@ val dataModule = module {
LocalDataSourceImpl(
catalogItemsDao = db.catalogProductsDao(),
catalogPunctuationsDao = db.catalogPunctuationsDao(),
catalogFavoriteItemsDao = db.catalogFavoriteItemsDao()
catalogFavoriteItemsDao = db.catalogFavoriteItemsDao(),
catalogSearchDao = db.catalogSearchDao(),
)
}
}
Expand All @@ -35,6 +37,11 @@ val dataModule = module {
catalogDataService = get(),
)
}
single<CatalogSearchRepository> {
CatalogSearchRepository(
localDataSource = get(),
)
}
single {
UserPreferencesRepository(androidContext().dataStore)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ package dev.marlonlom.apps.cappajv.di

import dev.marlonlom.apps.cappajv.features.catalog_detail.CatalogDetailViewModel
import dev.marlonlom.apps.cappajv.features.catalog_list.CatalogListViewModel
import dev.marlonlom.apps.cappajv.features.catalog_search.CatalogSearchViewModel
import dev.marlonlom.apps.cappajv.features.settings.SettingsViewModel
import org.koin.androidx.viewmodel.dsl.viewModelOf
import org.koin.dsl.module

val viewModelsModule = module {
includes(dataModule)
viewModelOf(::CatalogListViewModel)
viewModelOf(::CatalogSearchViewModel)
viewModelOf(::CatalogDetailViewModel)
viewModelOf(::SettingsViewModel)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import dev.marlonlom.apps.cappajv.core.database.datasource.LocalDataSource
import dev.marlonlom.apps.cappajv.core.database.entities.CatalogItem
import dev.marlonlom.apps.cappajv.core.database.entities.CatalogPunctuation
import dev.marlonlom.apps.cappajv.ui.util.slug
import dev.marlonlom.apps.cappajv.ui.util.toSentenceCase
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
Expand All @@ -28,6 +27,7 @@ import dev.marlonlom.apps.cappajv.core.catalog_source.CatalogItem as RemoteCatal
*
* @property localDataSource Local data source.
* @property catalogDataService Catalog data service.
* @property coroutineDispatcher Coroutine dispatcher for this repository.
*/
class CatalogListRepository(
private val localDataSource: LocalDataSource,
Expand Down Expand Up @@ -96,7 +96,7 @@ private val RemoteCatalogItem.toEntity: CatalogItem
id = id,
title = title,
slug = title.slug,
titleNormalized = title.toSentenceCase,
titleNormalized = title.slug.replace("-", " "),
picture = picture,
category = category,
detail = detail,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,4 @@ class CatalogListViewModel(
/** UI state object for view model */
val uiState = repository.allProducts.stateIn(viewModelScope, SharingStarted.Eagerly, Loading)

companion object {
fun factory(
repository: CatalogListRepository
): ViewModelProvider.Factory = object : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return CatalogListViewModel(repository) as T
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ import kotlinx.coroutines.yield
*
* @author marlonlom
*
* @param modifier
* @param appState Application ui state.
* @param modifier Modifier for this composable.
*/
@ExperimentalFoundationApi
@Composable
fun CatalogListBanner(
appState: CappajvAppState, modifier: Modifier = Modifier
appState: CappajvAppState,
modifier: Modifier = Modifier,
) {
val bannerImagesList = listOf(
painterResource(R.drawable.img_catalog_home_banner_01),
Expand Down Expand Up @@ -82,7 +84,7 @@ fun CatalogListBanner(
.fillMaxWidth()
) { page ->
BannerCard(
page = page,
pageIndex = page,
pagerState = pagerState,
bannerImage = bannerImagesList[page],
)
Expand All @@ -98,18 +100,27 @@ fun CatalogListBanner(

}

/**
* Banner card item content composable ui.
*
* @author marlonlom
*
* @param pageIndex Page index for displayed banner.
* @param pagerState Pager ui state for displayed banner.
* @param bannerImage Banner picture painter resource.
*/
@ExperimentalFoundationApi
@Composable
private fun BannerCard(
page: Int,
pageIndex: Int,
pagerState: PagerState,
bannerImage: Painter
) {
Card(
onClick = {},
shape = MaterialTheme.shapes.large,
modifier = Modifier.graphicsLayer {
val pageOffset = pagerState.currentPage.minus(page).plus(pagerState.currentPageOffsetFraction)
val pageOffset = pagerState.currentPage.minus(pageIndex).plus(pagerState.currentPageOffsetFraction)

lerp(
start = 0.85f,
Expand All @@ -136,6 +147,15 @@ private fun BannerCard(
}
}

/**
* Horizontal pager indicator composable ui.
*
* @author marlonlom
*
* @param activeColor Color that indicates the active displayed banner.
* @param pagerState Pager ui state for displayed banner.
* @param modifier Modifier for this composable.
*/
@ExperimentalFoundationApi
@Composable
fun HorizontalPagerIndicator(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2024 Marlonlom
* SPDX-License-Identifier: Apache-2.0
*/

package dev.marlonlom.apps.cappajv.features.catalog_search

import dev.marlonlom.apps.cappajv.core.database.datasource.LocalDataSource

/**
* Catalog search repository.
*
* @author marlonlom
*
* @property localDataSource Local data source.
*/
class CatalogSearchRepository(
private val localDataSource: LocalDataSource,
) {

/**
* Perform search using provided text.
*
* @param searchText Query text.
*/
fun performSearch(searchText: String) = localDataSource.searchProducts("%$searchText%")

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2024 Marlonlom
* SPDX-License-Identifier: Apache-2.0
*/

package dev.marlonlom.apps.cappajv.features.catalog_search

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import dev.marlonlom.apps.cappajv.features.catalog_search.parts.CatalogSearchHeadline
import dev.marlonlom.apps.cappajv.features.catalog_search.slots.CatalogSearchInputSlot
import dev.marlonlom.apps.cappajv.features.catalog_search.slots.CatalogSearchResultsSlot
import dev.marlonlom.apps.cappajv.ui.main.CappajvAppState
import org.koin.androidx.compose.koinViewModel
import timber.log.Timber

@ExperimentalFoundationApi
@Composable
fun CatalogSearchRoute(
appState: CappajvAppState,
viewModel: CatalogSearchViewModel = koinViewModel(),
) {
val contentHorizontalPadding = when {
appState.isLandscape.not().and(appState.isMediumWidth) -> 40.dp
appState.isLandscape.not().and(appState.isExpandedWidth) -> 80.dp
else -> 20.dp
}

val queryText = rememberSaveable { viewModel.queryText }
val showClearIcon = remember {
derivedStateOf { viewModel.queryText.value.isNotEmpty() }
}

val searchResultState by viewModel.searchResult.collectAsStateWithLifecycle()

Column(
modifier = Modifier
.fillMaxWidth()
.padding(contentHorizontalPadding)
) {
CatalogSearchHeadline(appState)
CatalogSearchInputSlot(
appState = appState,
queryText = queryText,
showClearIcon = showClearIcon,
onSearchReady = viewModel::onQueryTextChanged,
)
CatalogSearchResultsSlot(
appState = appState,
searchResultUiState = searchResultState,
onSearchedItemClicked = {
Timber.d("[CatalogSearchRoute] clicked item[$it] ")
},
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2024 Marlonlom
* SPDX-License-Identifier: Apache-2.0
*/

package dev.marlonlom.apps.cappajv.features.catalog_search

import dev.marlonlom.apps.cappajv.core.database.entities.CatalogItemTuple

/**
* Catalog search ui state.
*
* @author marlonlom
*/
sealed class CatalogSearchUiState {

/**
* Default catalog search ui state.
*
* @author marlonlom
*
*/
data object None : CatalogSearchUiState()

/**
* Searching phase of catalog search ui state.
*
* @author marlonlom
*
*/
data object Searching : CatalogSearchUiState()

/**
* Empty results phase of catalog search ui state.
*
* @author marlonlom
*
*/
data object Empty : CatalogSearchUiState()

/**
* Success result phase of catalog search ui state.
*
* @author marlonlom
*
* @property results Catalog tuples result list.
*/
data class Success(
val results: List<CatalogItemTuple>
) : CatalogSearchUiState()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2024 Marlonlom
* SPDX-License-Identifier: Apache-2.0
*/

package dev.marlonlom.apps.cappajv.features.catalog_search

import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dev.marlonlom.apps.cappajv.features.catalog_search.CatalogSearchUiState.Empty
import dev.marlonlom.apps.cappajv.features.catalog_search.CatalogSearchUiState.None
import dev.marlonlom.apps.cappajv.features.catalog_search.CatalogSearchUiState.Searching
import dev.marlonlom.apps.cappajv.features.catalog_search.CatalogSearchUiState.Success
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

/**
* Catalog search viewmodel.
*
* @author marlonlom
*
* @property repository Catalog search repository.
*/
class CatalogSearchViewModel(
private val repository: CatalogSearchRepository
) : ViewModel() {
var queryText = mutableStateOf("")
private set

private val _searchResult = MutableStateFlow<CatalogSearchUiState>(None)

val searchResult = _searchResult.stateIn(
scope = viewModelScope,
started = SharingStarted.Eagerly,
initialValue = None
)

/** Handles query text value change. */
fun onQueryTextChanged() {
_searchResult.value = if (queryText.value.isNotEmpty()) Searching else None
if (_searchResult.value is None) return
viewModelScope.launch {
_searchResult.update {
val searchResults = repository.performSearch(queryText.value).first()
if (searchResults.isEmpty()) Empty else Success(searchResults)
}
}
}

}
Loading

0 comments on commit 1eb68d9

Please sign in to comment.