From 12091aed6c2425734f7dbd2966e7a75b24ae89ae Mon Sep 17 00:00:00 2001 From: beleavemebe Date: Mon, 4 Apr 2022 23:58:55 +0300 Subject: [PATCH 1/7] Implement architecture sample --- .../marketplace/ui/home/HomeFragment.kt | 3 ++ core/build.gradle | 2 ++ .../destination/SearchDestination.kt | 15 +++++++++ .../destination/SearchHistoryDestination.kt | 5 +++ .../marketplace/domain/model/search/SortBy.kt | 9 +++++ ui/search/build.gradle | 2 ++ .../marketplace/ui/search/SearchFragment.kt | 16 ++++----- .../ui/search/SearchHistoryFragment.kt | 29 ++++++++++++++++ .../src/main/res/layout/fragment_search.xml | 19 +++++++++++ .../res/layout/fragment_search_history.xml | 11 ++++++- ui/search/src/main/res/layout/view_search.xml | 29 ++++++++++++++++ .../main/res/navigation/nav_graph_search.xml | 33 +++++++++++++++++-- 12 files changed, 160 insertions(+), 13 deletions(-) create mode 100644 core/src/main/java/com/narcissus/marketplace/core/navigation/destination/SearchDestination.kt create mode 100644 core/src/main/java/com/narcissus/marketplace/core/navigation/destination/SearchHistoryDestination.kt create mode 100644 domain/src/main/java/com/narcissus/marketplace/domain/model/search/SortBy.kt create mode 100644 ui/search/src/main/java/com/narcissus/marketplace/ui/search/SearchHistoryFragment.kt create mode 100644 ui/search/src/main/res/layout/fragment_search.xml create mode 100644 ui/search/src/main/res/layout/view_search.xml diff --git a/app/src/main/java/com/narcissus/marketplace/ui/home/HomeFragment.kt b/app/src/main/java/com/narcissus/marketplace/ui/home/HomeFragment.kt index 1d02b236..074061b5 100644 --- a/app/src/main/java/com/narcissus/marketplace/ui/home/HomeFragment.kt +++ b/app/src/main/java/com/narcissus/marketplace/ui/home/HomeFragment.kt @@ -10,7 +10,9 @@ import com.google.android.material.card.MaterialCardView import com.google.android.material.transition.MaterialFadeThrough import com.hannesdorfmann.adapterdelegates4.AsyncListDifferDelegationAdapter import com.narcissus.marketplace.R +import com.narcissus.marketplace.core.navigation.destination.NavDestination import com.narcissus.marketplace.core.navigation.destination.ProductDetailsDestination +import com.narcissus.marketplace.core.navigation.destination.SearchHistoryDestination import com.narcissus.marketplace.core.navigation.navigator import com.narcissus.marketplace.databinding.FragmentHomeBinding import com.narcissus.marketplace.ui.home.recycler.HomeScreenItem @@ -71,6 +73,7 @@ class HomeFragment : Fragment(R.layout.fragment_home) { } private fun navigateToSearch() { + navigator.navigate(SearchHistoryDestination) } private fun navigateToSpecialOffer(link: String) { diff --git a/core/build.gradle b/core/build.gradle index a1b1205e..6b8d4521 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -30,6 +30,8 @@ android { } dependencies { + implementation project(path: ':domain') + implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.1' def coroutines_version = "1.6.0" diff --git a/core/src/main/java/com/narcissus/marketplace/core/navigation/destination/SearchDestination.kt b/core/src/main/java/com/narcissus/marketplace/core/navigation/destination/SearchDestination.kt new file mode 100644 index 00000000..40c34dd5 --- /dev/null +++ b/core/src/main/java/com/narcissus/marketplace/core/navigation/destination/SearchDestination.kt @@ -0,0 +1,15 @@ +package com.narcissus.marketplace.core.navigation.destination + +import com.narcissus.marketplace.domain.model.search.SortBy + +class SearchDestination( + query: String, + department: String? = null, + sortBy: SortBy = SortBy.DO_NOT_SORT, +) : NavDestination { + override val url = buildString { + append("marketplace-app://search?query=$query") + append(";sort_by=${sortBy.code}") + if (department != null) append(";department=$department") + } +} diff --git a/core/src/main/java/com/narcissus/marketplace/core/navigation/destination/SearchHistoryDestination.kt b/core/src/main/java/com/narcissus/marketplace/core/navigation/destination/SearchHistoryDestination.kt new file mode 100644 index 00000000..501d6a1b --- /dev/null +++ b/core/src/main/java/com/narcissus/marketplace/core/navigation/destination/SearchHistoryDestination.kt @@ -0,0 +1,5 @@ +package com.narcissus.marketplace.core.navigation.destination + +object SearchHistoryDestination : NavDestination { + override val url = "marketplace-app://search-history" +} diff --git a/domain/src/main/java/com/narcissus/marketplace/domain/model/search/SortBy.kt b/domain/src/main/java/com/narcissus/marketplace/domain/model/search/SortBy.kt new file mode 100644 index 00000000..ed73dd27 --- /dev/null +++ b/domain/src/main/java/com/narcissus/marketplace/domain/model/search/SortBy.kt @@ -0,0 +1,9 @@ +package com.narcissus.marketplace.domain.model.search + +enum class SortBy( + val code: String +) { + RATING("rating"), + SALES("sales"), + DO_NOT_SORT("do_not_sort"), +} diff --git a/ui/search/build.gradle b/ui/search/build.gradle index 07e960fc..edbc5c23 100644 --- a/ui/search/build.gradle +++ b/ui/search/build.gradle @@ -1,6 +1,7 @@ plugins { id 'com.android.library' id 'org.jetbrains.kotlin.android' + id 'androidx.navigation.safeargs' } android { @@ -39,6 +40,7 @@ dependencies { implementation "io.insert-koin:koin-core:3.1.5" implementation "io.insert-koin:koin-android:3.1.5" + implementation 'androidx.legacy:legacy-support-v4:1.0.0' testImplementation "io.insert-koin:koin-test:3.1.5" implementation 'androidx.navigation:navigation-fragment-ktx:2.4.1' diff --git a/ui/search/src/main/java/com/narcissus/marketplace/ui/search/SearchFragment.kt b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/SearchFragment.kt index 6fbcfd02..14f72124 100644 --- a/ui/search/src/main/java/com/narcissus/marketplace/ui/search/SearchFragment.kt +++ b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/SearchFragment.kt @@ -2,20 +2,16 @@ package com.narcissus.marketplace.ui.search import android.os.Bundle import android.view.View +import android.widget.TextView import androidx.fragment.app.Fragment -import com.narcissus.marketplace.ui.search.databinding.FragmentSearchHistoryBinding +import androidx.navigation.fragment.navArgs -class SearchFragment : Fragment(R.layout.fragment_search_history) { - private var _binding: FragmentSearchHistoryBinding? = null - private val binding get() = _binding!! +class SearchFragment : Fragment(R.layout.fragment_search) { + private val args by navArgs() override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - _binding = FragmentSearchHistoryBinding.bind(view) - } - - override fun onDestroyView() { - super.onDestroyView() - _binding = null + view.findViewById(R.id.tvContent).text = + args.query + " " + args.department + " " + args.sortBy } } diff --git a/ui/search/src/main/java/com/narcissus/marketplace/ui/search/SearchHistoryFragment.kt b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/SearchHistoryFragment.kt new file mode 100644 index 00000000..cd36a4b1 --- /dev/null +++ b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/SearchHistoryFragment.kt @@ -0,0 +1,29 @@ +package com.narcissus.marketplace.ui.search + +import android.os.Bundle +import android.view.View +import androidx.fragment.app.Fragment +import com.narcissus.marketplace.core.navigation.destination.SearchDestination +import com.narcissus.marketplace.core.navigation.navigator +import com.narcissus.marketplace.domain.model.search.SortBy +import com.narcissus.marketplace.ui.search.databinding.FragmentSearchHistoryBinding + +class SearchHistoryFragment : Fragment(R.layout.fragment_search_history) { + private var _binding: FragmentSearchHistoryBinding? = null + private val binding get() = _binding!! + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + _binding = FragmentSearchHistoryBinding.bind(view) + binding.searchView.root.setOnClickListener { + navigator.navigate( + SearchDestination("Query", "Department", SortBy.RATING) + ) + } + } + + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } +} diff --git a/ui/search/src/main/res/layout/fragment_search.xml b/ui/search/src/main/res/layout/fragment_search.xml new file mode 100644 index 00000000..d10f6572 --- /dev/null +++ b/ui/search/src/main/res/layout/fragment_search.xml @@ -0,0 +1,19 @@ + + + + + + diff --git a/ui/search/src/main/res/layout/fragment_search_history.xml b/ui/search/src/main/res/layout/fragment_search_history.xml index 0a3100e0..a18b3c83 100644 --- a/ui/search/src/main/res/layout/fragment_search_history.xml +++ b/ui/search/src/main/res/layout/fragment_search_history.xml @@ -2,7 +2,16 @@ + xmlns:app="http://schemas.android.com/apk/res-auto" + xmlns:tools="http://schemas.android.com/tools" + tools:context=".SearchHistoryFragment"> + + + + + + + + + diff --git a/ui/search/src/main/res/navigation/nav_graph_search.xml b/ui/search/src/main/res/navigation/nav_graph_search.xml index 58f03780..2c60b933 100644 --- a/ui/search/src/main/res/navigation/nav_graph_search.xml +++ b/ui/search/src/main/res/navigation/nav_graph_search.xml @@ -1,11 +1,40 @@ + app:startDestination="@id/searchHistoryFragment"> + + + + + + + android:label="SearchFragment" + tools:layout="@layout/fragment_search"> + + + + + + + + + From e95947311393d1e9c1a42564ba50f93aecff4f16 Mon Sep 17 00:00:00 2001 From: beleavemebe Date: Wed, 13 Apr 2022 21:03:03 +0300 Subject: [PATCH 2/7] Define filtering architecture --- .../domain/model/search/SearchConfiguration.kt | 8 ++++++++ .../narcissus/marketplace/domain/model/search/SortBy.kt | 1 + .../marketplace/domain/model/search/SortDirection.kt | 6 ++++++ .../com/narcissus/marketplace/domain/util/SearchParams.kt | 6 ++---- 4 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 domain/src/main/java/com/narcissus/marketplace/domain/model/search/SearchConfiguration.kt create mode 100644 domain/src/main/java/com/narcissus/marketplace/domain/model/search/SortDirection.kt diff --git a/domain/src/main/java/com/narcissus/marketplace/domain/model/search/SearchConfiguration.kt b/domain/src/main/java/com/narcissus/marketplace/domain/model/search/SearchConfiguration.kt new file mode 100644 index 00000000..3d97b5a1 --- /dev/null +++ b/domain/src/main/java/com/narcissus/marketplace/domain/model/search/SearchConfiguration.kt @@ -0,0 +1,8 @@ +package com.narcissus.marketplace.domain.model.search + +data class SearchConfiguration( + val query: String, + val sortBy: SortBy, + val sortDirection: SortDirection, + val department: String, +) diff --git a/domain/src/main/java/com/narcissus/marketplace/domain/model/search/SortBy.kt b/domain/src/main/java/com/narcissus/marketplace/domain/model/search/SortBy.kt index ed73dd27..27704e3c 100644 --- a/domain/src/main/java/com/narcissus/marketplace/domain/model/search/SortBy.kt +++ b/domain/src/main/java/com/narcissus/marketplace/domain/model/search/SortBy.kt @@ -5,5 +5,6 @@ enum class SortBy( ) { RATING("rating"), SALES("sales"), + PRICE("price"), DO_NOT_SORT("do_not_sort"), } diff --git a/domain/src/main/java/com/narcissus/marketplace/domain/model/search/SortDirection.kt b/domain/src/main/java/com/narcissus/marketplace/domain/model/search/SortDirection.kt new file mode 100644 index 00000000..454c1ebf --- /dev/null +++ b/domain/src/main/java/com/narcissus/marketplace/domain/model/search/SortDirection.kt @@ -0,0 +1,6 @@ +package com.narcissus.marketplace.domain.model.search + +enum class SortDirection(val alias: String) { + DESC("desc"), + ASC("asc"), +} diff --git a/domain/src/main/java/com/narcissus/marketplace/domain/util/SearchParams.kt b/domain/src/main/java/com/narcissus/marketplace/domain/util/SearchParams.kt index 2970c24c..ccddc3ac 100644 --- a/domain/src/main/java/com/narcissus/marketplace/domain/util/SearchParams.kt +++ b/domain/src/main/java/com/narcissus/marketplace/domain/util/SearchParams.kt @@ -1,5 +1,7 @@ package com.narcissus.marketplace.domain.util +import com.narcissus.marketplace.domain.model.search.SortDirection + data class SearchParams( val query: String, val filters: Set = setOf(FilterType.None), @@ -21,8 +23,4 @@ data class SearchParams( NONE } - enum class SortDirection { - DESC, - ASC - } } From d8cc5aeb41a1029fd98e0f0204941d2aba805475 Mon Sep 17 00:00:00 2001 From: ggendrick Date: Fri, 15 Apr 2022 17:37:44 +0300 Subject: [PATCH 3/7] added search history screen --- .../narcissus/marketplace/di/DomainModule.kt | 7 +- .../marketplace/di/ViewModelsModule.kt | 1 + .../marketplace/ui/MarketplaceApp.kt | 4 +- .../ui/product_details/ProductDetailsItem.kt | 4 +- .../res/layout/email_password_text_inputs.xml | 6 +- app/src/main/res/layout/fragment_sign_in.xml | 2 +- app/src/main/res/layout/fragment_sign_up.xml | 2 +- ..._details_main_info_list_rating_section.xml | 4 +- ...m_details_product_about_multiple_lines.xml | 4 +- ...item_details_product_about_single_line.xml | 4 +- .../res/layout/list_item_details_review.xml | 4 +- .../list_item_details_review_preview.xml | 4 +- .../layout/list_item_product_of_the_day.xml | 4 +- .../res/layout/list_item_product_preview.xml | 2 +- app/src/main/res/values/custom_styles.xml | 59 ------------- core/build.gradle | 4 + .../src/main/res/layout/list_item_divider.xml | 10 +++ core/src/main/res/values/styles.xml | 58 +++++++++++++ .../marketplace/data/UserRepositoryImpl.kt | 14 ++++ .../marketplace/data/di/DataModule.kt | 2 +- .../domain/repository/UserRepository.kt | 4 +- .../domain/usecase/ClearSearchHistory.kt | 7 ++ .../domain/usecase/GetSearchHistory.kt | 7 ++ .../domain/usecase/SearchProducts.kt | 2 +- .../usecase/WriteQueryToSearchHistory.kt | 7 ++ .../data/persistence/database/AppDatabase.kt | 4 +- .../persistence/database/SearchHistoryDao.kt | 20 +++++ .../data/persistence/di/PersistenceModule.kt | 3 + .../persistence/model/SearchHistoryEntity.kt | 10 +++ ui/search/build.gradle | 2 + .../ui/search/SearchHistoryFragment.kt | 29 ------- .../ui/search/di/searchViewModelsModule.kt | 9 ++ .../search_history/SearchHistoryFragment.kt | 83 +++++++++++++++++++ .../search_history/SearchHistoryItem.kt | 67 +++++++++++++++ .../search_history/SearchHistoryViewModel.kt | 42 ++++++++++ .../res/layout/fragment_search_history.xml | 33 +++++++- .../res/layout/list_item_search_history.xml | 17 ++++ .../main/res/navigation/nav_graph_search.xml | 2 +- 38 files changed, 428 insertions(+), 119 deletions(-) create mode 100644 core/src/main/res/layout/list_item_divider.xml create mode 100644 domain/src/main/java/com/narcissus/marketplace/domain/usecase/ClearSearchHistory.kt create mode 100644 domain/src/main/java/com/narcissus/marketplace/domain/usecase/GetSearchHistory.kt create mode 100644 domain/src/main/java/com/narcissus/marketplace/domain/usecase/WriteQueryToSearchHistory.kt create mode 100644 persistence/src/main/java/com/narcissus/marketplace/data/persistence/database/SearchHistoryDao.kt create mode 100644 persistence/src/main/java/com/narcissus/marketplace/data/persistence/model/SearchHistoryEntity.kt delete mode 100644 ui/search/src/main/java/com/narcissus/marketplace/ui/search/SearchHistoryFragment.kt create mode 100644 ui/search/src/main/java/com/narcissus/marketplace/ui/search/di/searchViewModelsModule.kt create mode 100644 ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryFragment.kt create mode 100644 ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryItem.kt create mode 100644 ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryViewModel.kt create mode 100644 ui/search/src/main/res/layout/list_item_search_history.xml diff --git a/app/src/main/java/com/narcissus/marketplace/di/DomainModule.kt b/app/src/main/java/com/narcissus/marketplace/di/DomainModule.kt index 973c82a4..d138ec62 100644 --- a/app/src/main/java/com/narcissus/marketplace/di/DomainModule.kt +++ b/app/src/main/java/com/narcissus/marketplace/di/DomainModule.kt @@ -2,6 +2,7 @@ package com.narcissus.marketplace.di import com.narcissus.marketplace.data.di.dataModule import com.narcissus.marketplace.domain.usecase.AddToCart +import com.narcissus.marketplace.domain.usecase.ClearSearchHistory import com.narcissus.marketplace.domain.usecase.GetAuthStateFlow import com.narcissus.marketplace.domain.usecase.GetCart import com.narcissus.marketplace.domain.usecase.GetCartCost @@ -11,6 +12,7 @@ import com.narcissus.marketplace.domain.usecase.GetPeopleAreBuyingProducts import com.narcissus.marketplace.domain.usecase.GetProductDetails import com.narcissus.marketplace.domain.usecase.GetRandomProducts import com.narcissus.marketplace.domain.usecase.GetRecentlyVisitedProducts +import com.narcissus.marketplace.domain.usecase.GetSearchHistory import com.narcissus.marketplace.domain.usecase.GetSimilarProducts import com.narcissus.marketplace.domain.usecase.GetTopRatedProducts import com.narcissus.marketplace.domain.usecase.GetTopSalesProducts @@ -23,6 +25,7 @@ import com.narcissus.marketplace.domain.usecase.SignInWithEmail import com.narcissus.marketplace.domain.usecase.SignInWithGoogle import com.narcissus.marketplace.domain.usecase.SignOut import com.narcissus.marketplace.domain.usecase.SignUpWithEmail +import com.narcissus.marketplace.domain.usecase.WriteQueryToSearchHistory import org.koin.core.context.loadKoinModules import org.koin.dsl.module @@ -36,7 +39,8 @@ val domainModule = module { factory { GetProductDetails(get(), get()) } factory { GetDepartments(get()) } - + factory { GetSearchHistory(get()) } + factory { WriteQueryToSearchHistory(get()) } factory { GetCart(get()) } factory { GetCartItemsAmount(get()) } factory { GetCartCost(get()) } @@ -44,6 +48,7 @@ val domainModule = module { factory { SetCartItemSelected(get()) } factory { SelectAllCartItems(get()) } factory { RemoveSelectedCartItems(get()) } + factory { ClearSearchHistory(get()) } factory { RemoveFromCart(get()) } factory { AddToCart(get()) } factory { SignInWithEmail(get()) } diff --git a/app/src/main/java/com/narcissus/marketplace/di/ViewModelsModule.kt b/app/src/main/java/com/narcissus/marketplace/di/ViewModelsModule.kt index 6b6bb2ef..995f4356 100644 --- a/app/src/main/java/com/narcissus/marketplace/di/ViewModelsModule.kt +++ b/app/src/main/java/com/narcissus/marketplace/di/ViewModelsModule.kt @@ -4,6 +4,7 @@ import com.narcissus.marketplace.ui.cart.CartViewModel import com.narcissus.marketplace.ui.catalog.CatalogViewModel import com.narcissus.marketplace.ui.home.HomeViewModel import com.narcissus.marketplace.ui.product_details.ProductDetailsViewModel +import com.narcissus.marketplace.ui.search.search_history.SearchHistoryViewModel import com.narcissus.marketplace.ui.sign_in.SignInViewModel import com.narcissus.marketplace.ui.sign_up.SignUpViewModel import org.koin.androidx.viewmodel.dsl.viewModel diff --git a/app/src/main/java/com/narcissus/marketplace/ui/MarketplaceApp.kt b/app/src/main/java/com/narcissus/marketplace/ui/MarketplaceApp.kt index 36b86250..5fe6c1ed 100644 --- a/app/src/main/java/com/narcissus/marketplace/ui/MarketplaceApp.kt +++ b/app/src/main/java/com/narcissus/marketplace/ui/MarketplaceApp.kt @@ -8,6 +8,7 @@ import com.narcissus.marketplace.ui.cart.di.cartModule import com.narcissus.marketplace.ui.catalog.di.catalogModule import com.narcissus.marketplace.ui.home.di.homeModule import com.narcissus.marketplace.ui.product_details.di.productDetailsModule +import com.narcissus.marketplace.ui.search.di.searchViewModelsModule import com.narcissus.marketplace.ui.sign_in.di.signInModule import com.narcissus.marketplace.ui.sign_up.di.signUpModule import com.narcissus.marketplace.ui.user.di.userModule @@ -29,7 +30,8 @@ class MarketplaceApp : Application() { userModule, productDetailsModule, signInModule, - signUpModule + signUpModule, + searchViewModelsModule ) } } diff --git a/app/src/main/java/com/narcissus/marketplace/ui/product_details/ProductDetailsItem.kt b/app/src/main/java/com/narcissus/marketplace/ui/product_details/ProductDetailsItem.kt index ad8812ba..9ebca429 100644 --- a/app/src/main/java/com/narcissus/marketplace/ui/product_details/ProductDetailsItem.kt +++ b/app/src/main/java/com/narcissus/marketplace/ui/product_details/ProductDetailsItem.kt @@ -15,7 +15,7 @@ import coil.transform.CircleCropTransformation import com.google.android.material.card.MaterialCardView import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding import com.narcissus.marketplace.R -import com.narcissus.marketplace.databinding.ListItemDetailsDividerBinding +import com.narcissus.marketplace.core.databinding.ListItemDividerBinding import com.narcissus.marketplace.databinding.ListItemDetailsMainInfoBinding import com.narcissus.marketplace.databinding.ListItemDetailsMainInfoPlaceholderBinding import com.narcissus.marketplace.databinding.ListItemDetailsPriceBinding @@ -45,7 +45,7 @@ typealias AboutMultipleLineBinding = ListItemDetailsProductAboutMultipleLinesBin typealias LoadingProductDetailsBinding = ListItemDetailsProductPlaceholderBinding typealias ReviewsPreviewBinding = ListItemDetailsReviewPreviewBinding typealias SimilarProductsListBinding = ListItemRecyclerBinding -typealias DividerBinding = ListItemDetailsDividerBinding +typealias DividerBinding = ListItemDividerBinding sealed class ProductDetailsItem { data class Price(val price: Int) : ProductDetailsItem() { diff --git a/app/src/main/res/layout/email_password_text_inputs.xml b/app/src/main/res/layout/email_password_text_inputs.xml index 9b25c95e..c1afd07b 100644 --- a/app/src/main/res/layout/email_password_text_inputs.xml +++ b/app/src/main/res/layout/email_password_text_inputs.xml @@ -28,12 +28,12 @@ android:paddingTop="0dp" android:paddingEnd="0dp" android:paddingBottom="2dp" - android:textAppearance="@style/S1_subtitle" /> + android:textAppearance="@style/S1_subtitle_primary" /> + android:textAppearance="@style/S1_subtitle_primary" /> diff --git a/app/src/main/res/layout/fragment_sign_in.xml b/app/src/main/res/layout/fragment_sign_in.xml index 89a867f7..dffbcb30 100644 --- a/app/src/main/res/layout/fragment_sign_in.xml +++ b/app/src/main/res/layout/fragment_sign_in.xml @@ -74,7 +74,7 @@ + android:textAppearance="@style/S1_subtitle_primary" /> diff --git a/app/src/main/res/layout/list_item_details_review.xml b/app/src/main/res/layout/list_item_details_review.xml index bcbace5e..c29c7389 100644 --- a/app/src/main/res/layout/list_item_details_review.xml +++ b/app/src/main/res/layout/list_item_details_review.xml @@ -18,7 +18,7 @@ - ?attr/colorOnPrimary - - - - - - - - - - - - - - - - - - - - - diff --git a/core/build.gradle b/core/build.gradle index 6b8d4521..32e21c2c 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -20,6 +20,10 @@ android { proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } + buildFeatures { + viewBinding true + } + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 diff --git a/core/src/main/res/layout/list_item_divider.xml b/core/src/main/res/layout/list_item_divider.xml new file mode 100644 index 00000000..4580a903 --- /dev/null +++ b/core/src/main/res/layout/list_item_divider.xml @@ -0,0 +1,10 @@ + + + + + diff --git a/core/src/main/res/values/styles.xml b/core/src/main/res/values/styles.xml index 438d550e..4d59f86a 100644 --- a/core/src/main/res/values/styles.xml +++ b/core/src/main/res/values/styles.xml @@ -9,4 +9,62 @@ 14sp @font/montserrat_regular_ttf + + + + + + + + + + + + + + + + + + + + + diff --git a/data/src/main/java/com/narcissus/marketplace/data/UserRepositoryImpl.kt b/data/src/main/java/com/narcissus/marketplace/data/UserRepositoryImpl.kt index ca991a24..85c60e62 100644 --- a/data/src/main/java/com/narcissus/marketplace/data/UserRepositoryImpl.kt +++ b/data/src/main/java/com/narcissus/marketplace/data/UserRepositoryImpl.kt @@ -10,7 +10,9 @@ import com.google.firebase.auth.GoogleAuthProvider import com.google.firebase.auth.ktx.userProfileChangeRequest import com.narcissus.marketplace.data.mapper.toProductPreview import com.narcissus.marketplace.data.persistence.database.ProductDao +import com.narcissus.marketplace.data.persistence.database.SearchHistoryDao import com.narcissus.marketplace.data.persistence.model.ProductEntity +import com.narcissus.marketplace.data.persistence.model.SearchHistoryEntity import com.narcissus.marketplace.domain.auth.AuthState import com.narcissus.marketplace.domain.auth.SignInResult import com.narcissus.marketplace.domain.auth.SignOutResult @@ -28,6 +30,7 @@ import kotlinx.coroutines.tasks.await internal class UserRepositoryImpl( private val productsDao: ProductDao, + private val searchHistoryDao: SearchHistoryDao, private val firebaseAuth: FirebaseAuth, ) : UserRepository { override val recentlyVisitedProducts: Flow> = @@ -70,6 +73,17 @@ internal class UserRepositoryImpl( } } + override suspend fun writeToSearchHistory(searchQuery: String) { + searchHistoryDao.insertSearchHistoryItem(SearchHistoryEntity(searchQuery)) + } + + override suspend fun clearSearchHistory() { + searchHistoryDao.clearSearchHistory() + } + + override val searchHistory: Flow> = searchHistoryDao.getSearchHistory() + .map { searchHistoryFlow -> searchHistoryFlow.map { it.productName } } + override suspend fun signInWithEmail(email: String, password: String): SignInResult { val currentUser = firebaseAuth.currentUser return if (currentUser != null) { diff --git a/data/src/main/java/com/narcissus/marketplace/data/di/DataModule.kt b/data/src/main/java/com/narcissus/marketplace/data/di/DataModule.kt index c581ef31..bc9db5c3 100644 --- a/data/src/main/java/com/narcissus/marketplace/data/di/DataModule.kt +++ b/data/src/main/java/com/narcissus/marketplace/data/di/DataModule.kt @@ -40,6 +40,6 @@ val dataModule = module { ProductsPreviewRepositoryImpl(apiService = get()) } single { - UserRepositoryImpl(productsDao = get(), firebaseAuth = get()) + UserRepositoryImpl(productsDao = get(), firebaseAuth = get(), searchHistoryDao = get()) } } diff --git a/domain/src/main/java/com/narcissus/marketplace/domain/repository/UserRepository.kt b/domain/src/main/java/com/narcissus/marketplace/domain/repository/UserRepository.kt index b276a450..0e4cf8ba 100644 --- a/domain/src/main/java/com/narcissus/marketplace/domain/repository/UserRepository.kt +++ b/domain/src/main/java/com/narcissus/marketplace/domain/repository/UserRepository.kt @@ -19,7 +19,9 @@ interface UserRepository { suspend fun signInWithGoogle(idToken: String): SignInResult val authStateFlow: Flow - + suspend fun writeToSearchHistory(searchQuery:String) + suspend fun clearSearchHistory() + val searchHistory:Flow> val recentlyVisitedProducts: Flow> suspend fun writeToVisitedProducts(productPreview: ProductPreview) } diff --git a/domain/src/main/java/com/narcissus/marketplace/domain/usecase/ClearSearchHistory.kt b/domain/src/main/java/com/narcissus/marketplace/domain/usecase/ClearSearchHistory.kt new file mode 100644 index 00000000..941e4f1a --- /dev/null +++ b/domain/src/main/java/com/narcissus/marketplace/domain/usecase/ClearSearchHistory.kt @@ -0,0 +1,7 @@ +package com.narcissus.marketplace.domain.usecase + +import com.narcissus.marketplace.domain.repository.UserRepository + +class ClearSearchHistory(private val userRepository: UserRepository) { + suspend operator fun invoke() = userRepository.clearSearchHistory() +} diff --git a/domain/src/main/java/com/narcissus/marketplace/domain/usecase/GetSearchHistory.kt b/domain/src/main/java/com/narcissus/marketplace/domain/usecase/GetSearchHistory.kt new file mode 100644 index 00000000..d92b9e9f --- /dev/null +++ b/domain/src/main/java/com/narcissus/marketplace/domain/usecase/GetSearchHistory.kt @@ -0,0 +1,7 @@ +package com.narcissus.marketplace.domain.usecase + +import com.narcissus.marketplace.domain.repository.UserRepository + +class GetSearchHistory(private val userRepository: UserRepository) { + operator fun invoke() = userRepository.searchHistory +} diff --git a/domain/src/main/java/com/narcissus/marketplace/domain/usecase/SearchProducts.kt b/domain/src/main/java/com/narcissus/marketplace/domain/usecase/SearchProducts.kt index 2fda1e5e..74612a0f 100644 --- a/domain/src/main/java/com/narcissus/marketplace/domain/usecase/SearchProducts.kt +++ b/domain/src/main/java/com/narcissus/marketplace/domain/usecase/SearchProducts.kt @@ -1,10 +1,10 @@ package com.narcissus.marketplace.domain.usecase import com.narcissus.marketplace.domain.model.ProductPreview +import com.narcissus.marketplace.domain.model.search.SortDirection import com.narcissus.marketplace.domain.repository.ProductsPreviewRepository import com.narcissus.marketplace.domain.util.ActionResult import com.narcissus.marketplace.domain.util.SearchParams -import com.narcissus.marketplace.domain.util.SearchParams.SortDirection as SortDirection import com.narcissus.marketplace.domain.util.SearchParams.SortType as SortType class SearchProducts(private val productsPreviewRepository: ProductsPreviewRepository) { diff --git a/domain/src/main/java/com/narcissus/marketplace/domain/usecase/WriteQueryToSearchHistory.kt b/domain/src/main/java/com/narcissus/marketplace/domain/usecase/WriteQueryToSearchHistory.kt new file mode 100644 index 00000000..56b45e69 --- /dev/null +++ b/domain/src/main/java/com/narcissus/marketplace/domain/usecase/WriteQueryToSearchHistory.kt @@ -0,0 +1,7 @@ +package com.narcissus.marketplace.domain.usecase + +import com.narcissus.marketplace.domain.repository.UserRepository + +class WriteQueryToSearchHistory(private val userRepository: UserRepository) { + suspend operator fun invoke(searchQuery:String) = userRepository.writeToSearchHistory(searchQuery) +} diff --git a/persistence/src/main/java/com/narcissus/marketplace/data/persistence/database/AppDatabase.kt b/persistence/src/main/java/com/narcissus/marketplace/data/persistence/database/AppDatabase.kt index d0fc07fb..c0a9cfd8 100644 --- a/persistence/src/main/java/com/narcissus/marketplace/data/persistence/database/AppDatabase.kt +++ b/persistence/src/main/java/com/narcissus/marketplace/data/persistence/database/AppDatabase.kt @@ -3,13 +3,15 @@ package com.narcissus.marketplace.data.persistence.database import androidx.room.Database import androidx.room.RoomDatabase import com.narcissus.marketplace.data.persistence.model.ProductEntity +import com.narcissus.marketplace.data.persistence.model.SearchHistoryEntity @Database( - entities = [ProductEntity::class], + entities = [ProductEntity::class,SearchHistoryEntity::class], version = 1, ) abstract class AppDatabase : RoomDatabase() { abstract fun productDao(): ProductDao + abstract fun searchHistoryDao():SearchHistoryDao companion object { const val DATABASE_NAME = "marketplace-app.db" diff --git a/persistence/src/main/java/com/narcissus/marketplace/data/persistence/database/SearchHistoryDao.kt b/persistence/src/main/java/com/narcissus/marketplace/data/persistence/database/SearchHistoryDao.kt new file mode 100644 index 00000000..a3be0dc5 --- /dev/null +++ b/persistence/src/main/java/com/narcissus/marketplace/data/persistence/database/SearchHistoryDao.kt @@ -0,0 +1,20 @@ +package com.narcissus.marketplace.data.persistence.database + +import androidx.room.Dao +import androidx.room.Delete +import androidx.room.Insert +import androidx.room.OnConflictStrategy +import androidx.room.Query +import com.narcissus.marketplace.data.persistence.model.SearchHistoryEntity +import kotlinx.coroutines.flow.Flow + +@Dao +interface SearchHistoryDao { + @Query("SELECT * FROM search_history ORDER BY created DESC") + fun getSearchHistory(): Flow> + + @Insert(onConflict = OnConflictStrategy.REPLACE) + suspend fun insertSearchHistoryItem(searchItem:SearchHistoryEntity) + @Query("DELETE FROM search_history") + suspend fun clearSearchHistory() +} diff --git a/persistence/src/main/java/com/narcissus/marketplace/data/persistence/di/PersistenceModule.kt b/persistence/src/main/java/com/narcissus/marketplace/data/persistence/di/PersistenceModule.kt index 51fe1a23..5a3a6c96 100644 --- a/persistence/src/main/java/com/narcissus/marketplace/data/persistence/di/PersistenceModule.kt +++ b/persistence/src/main/java/com/narcissus/marketplace/data/persistence/di/PersistenceModule.kt @@ -18,4 +18,7 @@ val persistenceModule = module { single { get().productDao() } + single { + get().searchHistoryDao() + } } diff --git a/persistence/src/main/java/com/narcissus/marketplace/data/persistence/model/SearchHistoryEntity.kt b/persistence/src/main/java/com/narcissus/marketplace/data/persistence/model/SearchHistoryEntity.kt new file mode 100644 index 00000000..10494e32 --- /dev/null +++ b/persistence/src/main/java/com/narcissus/marketplace/data/persistence/model/SearchHistoryEntity.kt @@ -0,0 +1,10 @@ +package com.narcissus.marketplace.data.persistence.model + +import androidx.room.Entity +import androidx.room.PrimaryKey +@Entity(tableName = "search_history") +data class SearchHistoryEntity ( + @PrimaryKey + val productName:String, + val created: Long = System.currentTimeMillis() + ) diff --git a/ui/search/build.gradle b/ui/search/build.gradle index edbc5c23..aa1b2f11 100644 --- a/ui/search/build.gradle +++ b/ui/search/build.gradle @@ -43,6 +43,8 @@ dependencies { implementation 'androidx.legacy:legacy-support-v4:1.0.0' testImplementation "io.insert-koin:koin-test:3.1.5" + implementation 'com.hannesdorfmann:adapterdelegates4-kotlin-dsl-viewbinding:4.3.1' + implementation 'androidx.navigation:navigation-fragment-ktx:2.4.1' implementation 'androidx.navigation:navigation-ui-ktx:2.4.1' diff --git a/ui/search/src/main/java/com/narcissus/marketplace/ui/search/SearchHistoryFragment.kt b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/SearchHistoryFragment.kt deleted file mode 100644 index cd36a4b1..00000000 --- a/ui/search/src/main/java/com/narcissus/marketplace/ui/search/SearchHistoryFragment.kt +++ /dev/null @@ -1,29 +0,0 @@ -package com.narcissus.marketplace.ui.search - -import android.os.Bundle -import android.view.View -import androidx.fragment.app.Fragment -import com.narcissus.marketplace.core.navigation.destination.SearchDestination -import com.narcissus.marketplace.core.navigation.navigator -import com.narcissus.marketplace.domain.model.search.SortBy -import com.narcissus.marketplace.ui.search.databinding.FragmentSearchHistoryBinding - -class SearchHistoryFragment : Fragment(R.layout.fragment_search_history) { - private var _binding: FragmentSearchHistoryBinding? = null - private val binding get() = _binding!! - - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - _binding = FragmentSearchHistoryBinding.bind(view) - binding.searchView.root.setOnClickListener { - navigator.navigate( - SearchDestination("Query", "Department", SortBy.RATING) - ) - } - } - - override fun onDestroyView() { - super.onDestroyView() - _binding = null - } -} diff --git a/ui/search/src/main/java/com/narcissus/marketplace/ui/search/di/searchViewModelsModule.kt b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/di/searchViewModelsModule.kt new file mode 100644 index 00000000..8b77300c --- /dev/null +++ b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/di/searchViewModelsModule.kt @@ -0,0 +1,9 @@ +package com.narcissus.marketplace.ui.search.di + +import com.narcissus.marketplace.ui.search.search_history.SearchHistoryViewModel +import org.koin.androidx.viewmodel.dsl.viewModel +import org.koin.dsl.module + +val searchViewModelsModule = module { + viewModel{ SearchHistoryViewModel(get(),get(),get()) } +} diff --git a/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryFragment.kt b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryFragment.kt new file mode 100644 index 00000000..b5db6830 --- /dev/null +++ b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryFragment.kt @@ -0,0 +1,83 @@ +package com.narcissus.marketplace.ui.search.search_history + +import android.os.Bundle +import android.util.Log +import android.view.View +import androidx.fragment.app.Fragment +import androidx.lifecycle.lifecycleScope +import androidx.recyclerview.widget.LinearLayoutManager +import com.hannesdorfmann.adapterdelegates4.AsyncListDifferDelegationAdapter +import com.narcissus.marketplace.core.navigation.destination.SearchDestination +import com.narcissus.marketplace.core.navigation.navigator +import com.narcissus.marketplace.domain.model.search.SortBy +import com.narcissus.marketplace.ui.search.R +import com.narcissus.marketplace.ui.search.databinding.FragmentSearchHistoryBinding +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch +import org.koin.androidx.viewmodel.ext.android.viewModel + +class SearchHistoryFragment : Fragment(R.layout.fragment_search_history) { + private var _binding: FragmentSearchHistoryBinding? = null + private val binding get() = _binding!! + private val viewModel: SearchHistoryViewModel by viewModel() + private val historyAdapter = AsyncListDifferDelegationAdapter( + SearchHistoryItem.DIFF_CALLBACK, + SearchHistoryItem.HistoryItem.delegate(), + SearchHistoryItem.Divider.delegate() + ) + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + _binding = FragmentSearchHistoryBinding.bind(view) + binding.searchView.root.setOnClickListener { + navigator.navigate( + SearchDestination("Query", "Department", SortBy.RATING), + ) + } + initRecyclerView() + observeSearchHistory() + initListeners() + } + + private fun initRecyclerView() { + val historyRecyclerView = binding.rvSearchHistory + historyRecyclerView.adapter = historyAdapter + historyRecyclerView.layoutManager = + LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) + } + + private fun initListeners(){ + binding.tvSearchHistoryClearAll.setOnClickListener { + viewModel.clearHistory() + } + } + + private fun observeSearchHistory() { + lifecycleScope.launchWhenCreated { + viewModel.searchHistory.collect { searchHistory -> + if(searchHistory.isNotEmpty()){ + val items:MutableList = mutableListOf() + searchHistory.map { SearchHistoryItem.HistoryItem(it) }.forEach { + items.addAll(listOf(it,SearchHistoryItem.Divider())) + } + items.add(0,SearchHistoryItem.Divider()) + historyAdapter.items = items + }else{ + historyAdapter.items = listOf() + } + + } + } + } + + private fun writeQueryToSearchHistory(query: String) { + viewModel.writeToSearchHistory(query) + } + + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } +} diff --git a/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryItem.kt b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryItem.kt new file mode 100644 index 00000000..77524010 --- /dev/null +++ b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryItem.kt @@ -0,0 +1,67 @@ +package com.narcissus.marketplace.ui.search.search_history + +import android.view.LayoutInflater +import android.view.ViewGroup +import androidx.recyclerview.widget.DiffUtil +import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding +import com.narcissus.marketplace.core.databinding.ListItemDividerBinding +import com.narcissus.marketplace.ui.search.databinding.ListItemSearchHistoryBinding +sealed class SearchHistoryItem { + data class HistoryItem(val itemName:String): SearchHistoryItem() { + companion object { + @JvmStatic + private fun inflateBinding( + layoutInflater: LayoutInflater, + root: ViewGroup, + ) = ListItemSearchHistoryBinding.inflate(layoutInflater, root, false) + + fun delegate() = + adapterDelegateViewBinding( + ::inflateBinding, + ) { + bind { + binding.tvSearchHistoryItemName.text = item.itemName + } + } + } + } + + class Divider : SearchHistoryItem() { + companion object { + @JvmStatic + private fun inflateBinding( + layoutInflater: LayoutInflater, + root: ViewGroup, + ) = ListItemDividerBinding.inflate(layoutInflater, root, false) + + fun delegate() = + adapterDelegateViewBinding( + ::inflateBinding, + ) { + bind {} + } + } + } + + + companion object { + val DIFF_CALLBACK = object : DiffUtil.ItemCallback() { + override fun areItemsTheSame( + oldItem: SearchHistoryItem, + newItem: SearchHistoryItem, + ): Boolean = when(oldItem){ + is HistoryItem-> newItem is HistoryItem &&newItem.itemName==oldItem.itemName + is Divider->newItem is Divider + } + + + override fun areContentsTheSame( + oldItem: SearchHistoryItem, + newItem: SearchHistoryItem, + ): Boolean { + return oldItem == newItem + } + } + } + +} diff --git a/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryViewModel.kt b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryViewModel.kt new file mode 100644 index 00000000..e2898bec --- /dev/null +++ b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryViewModel.kt @@ -0,0 +1,42 @@ +package com.narcissus.marketplace.ui.search.search_history + +import android.util.Log +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import com.narcissus.marketplace.domain.usecase.ClearSearchHistory +import com.narcissus.marketplace.domain.usecase.GetSearchHistory +import com.narcissus.marketplace.domain.usecase.WriteQueryToSearchHistory +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.onEach +import kotlinx.coroutines.launch + +class SearchHistoryViewModel( + private val getSearchHistory: GetSearchHistory, + private val writeQueryToSearchHistory: WriteQueryToSearchHistory, + private val clearSearchHistory: ClearSearchHistory, +) : ViewModel() { + val searchHistory = getSearchHistory() + init{ + + + + } + + + // val searchHistoryViewFlow = searchHistoryFlow.onEach {}.launchIn(CoroutineScope(Dispatchers.Main)) + fun writeToSearchHistory(searchQuery: String) { + viewModelScope.launch { + writeQueryToSearchHistory(searchQuery) + } + } + + fun clearHistory() { + viewModelScope.launch { + clearSearchHistory() + } + } +} diff --git a/ui/search/src/main/res/layout/fragment_search_history.xml b/ui/search/src/main/res/layout/fragment_search_history.xml index a18b3c83..35647353 100644 --- a/ui/search/src/main/res/layout/fragment_search_history.xml +++ b/ui/search/src/main/res/layout/fragment_search_history.xml @@ -4,7 +4,7 @@ android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" - tools:context=".SearchHistoryFragment"> + tools:context=".search_history.SearchHistoryFragment"> + + + + + app:layout_constraintEnd_toEndOf="parent" + android:id="@+id/rvSearchHistory" + android:layout_width="match_parent" + android:layout_height="wrap_content" /> diff --git a/ui/search/src/main/res/layout/list_item_search_history.xml b/ui/search/src/main/res/layout/list_item_search_history.xml new file mode 100644 index 00000000..428e6aec --- /dev/null +++ b/ui/search/src/main/res/layout/list_item_search_history.xml @@ -0,0 +1,17 @@ + + + + + diff --git a/ui/search/src/main/res/navigation/nav_graph_search.xml b/ui/search/src/main/res/navigation/nav_graph_search.xml index 2c60b933..cbc1b071 100644 --- a/ui/search/src/main/res/navigation/nav_graph_search.xml +++ b/ui/search/src/main/res/navigation/nav_graph_search.xml @@ -7,7 +7,7 @@ From 2354ca7cb911cb6eb8a5bc799f007a374bc95f11 Mon Sep 17 00:00:00 2001 From: ggendrick Date: Sun, 17 Apr 2022 20:12:59 +0300 Subject: [PATCH 4/7] mapping fixed --- .../narcissus/marketplace/di/DomainModule.kt | 14 +++++++++ .../search_history/SearchHistoryFragment.kt | 29 ++++++++----------- .../search_history/SearchHistoryViewModel.kt | 22 +++++--------- 3 files changed, 34 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/com/narcissus/marketplace/di/DomainModule.kt b/app/src/main/java/com/narcissus/marketplace/di/DomainModule.kt index aeeeb695..1df4937a 100644 --- a/app/src/main/java/com/narcissus/marketplace/di/DomainModule.kt +++ b/app/src/main/java/com/narcissus/marketplace/di/DomainModule.kt @@ -2,20 +2,29 @@ package com.narcissus.marketplace.di import com.narcissus.marketplace.data.di.dataModule import com.narcissus.marketplace.domain.usecase.AddToCart +import com.narcissus.marketplace.domain.usecase.ClearSearchHistory import com.narcissus.marketplace.domain.usecase.GetAuthStateFlow import com.narcissus.marketplace.domain.usecase.GetCart import com.narcissus.marketplace.domain.usecase.GetCartCost +import com.narcissus.marketplace.domain.usecase.GetCartCostFlow import com.narcissus.marketplace.domain.usecase.GetCartItemsAmount +import com.narcissus.marketplace.domain.usecase.GetCartSelectedItemsCostSnapshot +import com.narcissus.marketplace.domain.usecase.GetCartSelectedItemsSnapshot +import com.narcissus.marketplace.domain.usecase.GetCheckout import com.narcissus.marketplace.domain.usecase.GetDepartments +import com.narcissus.marketplace.domain.usecase.GetOrderList import com.narcissus.marketplace.domain.usecase.GetPeopleAreBuyingProducts import com.narcissus.marketplace.domain.usecase.GetProductDetails import com.narcissus.marketplace.domain.usecase.GetRandomProducts import com.narcissus.marketplace.domain.usecase.GetRecentlyVisitedProducts +import com.narcissus.marketplace.domain.usecase.GetSearchHistory import com.narcissus.marketplace.domain.usecase.GetSimilarProducts import com.narcissus.marketplace.domain.usecase.GetTopRatedProducts import com.narcissus.marketplace.domain.usecase.GetTopSalesProducts +import com.narcissus.marketplace.domain.usecase.MakeAnOrder import com.narcissus.marketplace.domain.usecase.RemoveFromCart import com.narcissus.marketplace.domain.usecase.RemoveSelectedCartItems +import com.narcissus.marketplace.domain.usecase.RestoreCartItems import com.narcissus.marketplace.domain.usecase.SelectAllCartItems import com.narcissus.marketplace.domain.usecase.SetCartItemAmount import com.narcissus.marketplace.domain.usecase.SetCartItemSelected @@ -23,6 +32,8 @@ import com.narcissus.marketplace.domain.usecase.SignInWithEmail import com.narcissus.marketplace.domain.usecase.SignInWithGoogle import com.narcissus.marketplace.domain.usecase.SignOut import com.narcissus.marketplace.domain.usecase.SignUpWithEmail +import com.narcissus.marketplace.domain.usecase.ValidateCard +import com.narcissus.marketplace.domain.usecase.WriteQueryToSearchHistory import org.koin.core.context.loadKoinModules import org.koin.dsl.module @@ -35,6 +46,9 @@ val domainModule = module { factory { GetRecentlyVisitedProducts(get()) } factory { GetProductDetails(get(), get()) } factory { MakeAnOrder(get(), get()) } + factory { WriteQueryToSearchHistory(get()) } + factory { GetSearchHistory(get()) } + factory { ClearSearchHistory(get()) } factory { GetDepartments(get()) } factory { GetOrderList(get()) } factory { GetCart(get()) } diff --git a/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryFragment.kt b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryFragment.kt index b5db6830..4acbc69c 100644 --- a/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryFragment.kt +++ b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryFragment.kt @@ -1,7 +1,6 @@ package com.narcissus.marketplace.ui.search.search_history import android.os.Bundle -import android.util.Log import android.view.View import androidx.fragment.app.Fragment import androidx.lifecycle.lifecycleScope @@ -12,10 +11,6 @@ import com.narcissus.marketplace.core.navigation.navigator import com.narcissus.marketplace.domain.model.search.SortBy import com.narcissus.marketplace.ui.search.R import com.narcissus.marketplace.ui.search.databinding.FragmentSearchHistoryBinding -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.delay -import kotlinx.coroutines.launch import org.koin.androidx.viewmodel.ext.android.viewModel class SearchHistoryFragment : Fragment(R.layout.fragment_search_history) { @@ -25,7 +20,7 @@ class SearchHistoryFragment : Fragment(R.layout.fragment_search_history) { private val historyAdapter = AsyncListDifferDelegationAdapter( SearchHistoryItem.DIFF_CALLBACK, SearchHistoryItem.HistoryItem.delegate(), - SearchHistoryItem.Divider.delegate() + SearchHistoryItem.Divider.delegate(), ) override fun onViewCreated(view: View, savedInstanceState: Bundle?) { @@ -48,7 +43,7 @@ class SearchHistoryFragment : Fragment(R.layout.fragment_search_history) { LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) } - private fun initListeners(){ + private fun initListeners() { binding.tvSearchHistoryClearAll.setOnClickListener { viewModel.clearHistory() } @@ -57,16 +52,16 @@ class SearchHistoryFragment : Fragment(R.layout.fragment_search_history) { private fun observeSearchHistory() { lifecycleScope.launchWhenCreated { viewModel.searchHistory.collect { searchHistory -> - if(searchHistory.isNotEmpty()){ - val items:MutableList = mutableListOf() - searchHistory.map { SearchHistoryItem.HistoryItem(it) }.forEach { - items.addAll(listOf(it,SearchHistoryItem.Divider())) - } - items.add(0,SearchHistoryItem.Divider()) - historyAdapter.items = items - }else{ - historyAdapter.items = listOf() - } + if (searchHistory.isNotEmpty()) { + historyAdapter.items = (searchHistory as MutableList).apply { + add( + 0, + SearchHistoryItem.Divider(), + ) + } + } else { + historyAdapter.items = listOf() + } } } diff --git a/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryViewModel.kt b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryViewModel.kt index e2898bec..393dd8d0 100644 --- a/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryViewModel.kt +++ b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryViewModel.kt @@ -1,33 +1,27 @@ package com.narcissus.marketplace.ui.search.search_history -import android.util.Log import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.narcissus.marketplace.domain.usecase.ClearSearchHistory import com.narcissus.marketplace.domain.usecase.GetSearchHistory import com.narcissus.marketplace.domain.usecase.WriteQueryToSearchHistory -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.flow.MutableStateFlow -import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.map -import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch class SearchHistoryViewModel( - private val getSearchHistory: GetSearchHistory, + getSearchHistory: GetSearchHistory, private val writeQueryToSearchHistory: WriteQueryToSearchHistory, private val clearSearchHistory: ClearSearchHistory, ) : ViewModel() { - val searchHistory = getSearchHistory() - init{ - - - + val searchHistory = getSearchHistory().map { list -> + (list.map { + listOf( + SearchHistoryItem.HistoryItem(it), + SearchHistoryItem.Divider(), + ) + }.flatten()) } - - // val searchHistoryViewFlow = searchHistoryFlow.onEach {}.launchIn(CoroutineScope(Dispatchers.Main)) fun writeToSearchHistory(searchQuery: String) { viewModelScope.launch { writeQueryToSearchHistory(searchQuery) From 91bfb42a720b58e98a1ab19e87155c9418dcc1fb Mon Sep 17 00:00:00 2001 From: ggendrick Date: Sun, 17 Apr 2022 17:27:06 +0000 Subject: [PATCH 5/7] [MegaLinter] Apply linters fixes --- .../narcissus/marketplace/di/ViewModelsModule.kt | 1 - .../narcissus/marketplace/ui/home/HomeFragment.kt | 1 - .../marketplace/data/UserRepositoryImpl.kt | 2 +- .../marketplace/domain/repository/UserRepository.kt | 4 ++-- .../domain/usecase/WriteQueryToSearchHistory.kt | 2 +- .../marketplace/domain/util/SearchParams.kt | 1 - .../data/persistence/database/AppDatabase.kt | 4 ++-- .../data/persistence/database/SearchHistoryDao.kt | 3 +-- .../data/persistence/model/SearchHistoryEntity.kt | 6 +++--- .../ui/search/di/searchViewModelsModule.kt | 2 +- .../search/search_history/SearchHistoryFragment.kt | 1 - .../ui/search/search_history/SearchHistoryItem.kt | 13 +++++-------- .../search/search_history/SearchHistoryViewModel.kt | 12 +++++++----- 13 files changed, 23 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/com/narcissus/marketplace/di/ViewModelsModule.kt b/app/src/main/java/com/narcissus/marketplace/di/ViewModelsModule.kt index 91c6c80d..51e760fc 100644 --- a/app/src/main/java/com/narcissus/marketplace/di/ViewModelsModule.kt +++ b/app/src/main/java/com/narcissus/marketplace/di/ViewModelsModule.kt @@ -5,7 +5,6 @@ import com.narcissus.marketplace.ui.catalog.CatalogViewModel import com.narcissus.marketplace.ui.checkout.CheckoutViewModel import com.narcissus.marketplace.ui.home.HomeViewModel import com.narcissus.marketplace.ui.product_details.ProductDetailsViewModel -import com.narcissus.marketplace.ui.search.search_history.SearchHistoryViewModel import com.narcissus.marketplace.ui.sign_in.SignInViewModel import com.narcissus.marketplace.ui.sign_up.SignUpViewModel import org.koin.androidx.viewmodel.dsl.viewModel diff --git a/app/src/main/java/com/narcissus/marketplace/ui/home/HomeFragment.kt b/app/src/main/java/com/narcissus/marketplace/ui/home/HomeFragment.kt index 074061b5..ec00bab8 100644 --- a/app/src/main/java/com/narcissus/marketplace/ui/home/HomeFragment.kt +++ b/app/src/main/java/com/narcissus/marketplace/ui/home/HomeFragment.kt @@ -10,7 +10,6 @@ import com.google.android.material.card.MaterialCardView import com.google.android.material.transition.MaterialFadeThrough import com.hannesdorfmann.adapterdelegates4.AsyncListDifferDelegationAdapter import com.narcissus.marketplace.R -import com.narcissus.marketplace.core.navigation.destination.NavDestination import com.narcissus.marketplace.core.navigation.destination.ProductDetailsDestination import com.narcissus.marketplace.core.navigation.destination.SearchHistoryDestination import com.narcissus.marketplace.core.navigation.navigator diff --git a/data/src/main/java/com/narcissus/marketplace/data/UserRepositoryImpl.kt b/data/src/main/java/com/narcissus/marketplace/data/UserRepositoryImpl.kt index 85c60e62..e04db7db 100644 --- a/data/src/main/java/com/narcissus/marketplace/data/UserRepositoryImpl.kt +++ b/data/src/main/java/com/narcissus/marketplace/data/UserRepositoryImpl.kt @@ -82,7 +82,7 @@ internal class UserRepositoryImpl( } override val searchHistory: Flow> = searchHistoryDao.getSearchHistory() - .map { searchHistoryFlow -> searchHistoryFlow.map { it.productName } } + .map { searchHistoryFlow -> searchHistoryFlow.map { it.productName } } override suspend fun signInWithEmail(email: String, password: String): SignInResult { val currentUser = firebaseAuth.currentUser diff --git a/domain/src/main/java/com/narcissus/marketplace/domain/repository/UserRepository.kt b/domain/src/main/java/com/narcissus/marketplace/domain/repository/UserRepository.kt index 0e4cf8ba..10b79725 100644 --- a/domain/src/main/java/com/narcissus/marketplace/domain/repository/UserRepository.kt +++ b/domain/src/main/java/com/narcissus/marketplace/domain/repository/UserRepository.kt @@ -19,9 +19,9 @@ interface UserRepository { suspend fun signInWithGoogle(idToken: String): SignInResult val authStateFlow: Flow - suspend fun writeToSearchHistory(searchQuery:String) + suspend fun writeToSearchHistory(searchQuery: String) suspend fun clearSearchHistory() - val searchHistory:Flow> + val searchHistory: Flow> val recentlyVisitedProducts: Flow> suspend fun writeToVisitedProducts(productPreview: ProductPreview) } diff --git a/domain/src/main/java/com/narcissus/marketplace/domain/usecase/WriteQueryToSearchHistory.kt b/domain/src/main/java/com/narcissus/marketplace/domain/usecase/WriteQueryToSearchHistory.kt index 56b45e69..ce3d2d55 100644 --- a/domain/src/main/java/com/narcissus/marketplace/domain/usecase/WriteQueryToSearchHistory.kt +++ b/domain/src/main/java/com/narcissus/marketplace/domain/usecase/WriteQueryToSearchHistory.kt @@ -3,5 +3,5 @@ package com.narcissus.marketplace.domain.usecase import com.narcissus.marketplace.domain.repository.UserRepository class WriteQueryToSearchHistory(private val userRepository: UserRepository) { - suspend operator fun invoke(searchQuery:String) = userRepository.writeToSearchHistory(searchQuery) + suspend operator fun invoke(searchQuery: String) = userRepository.writeToSearchHistory(searchQuery) } diff --git a/domain/src/main/java/com/narcissus/marketplace/domain/util/SearchParams.kt b/domain/src/main/java/com/narcissus/marketplace/domain/util/SearchParams.kt index ccddc3ac..894ebee5 100644 --- a/domain/src/main/java/com/narcissus/marketplace/domain/util/SearchParams.kt +++ b/domain/src/main/java/com/narcissus/marketplace/domain/util/SearchParams.kt @@ -22,5 +22,4 @@ data class SearchParams( PRICE, NONE } - } diff --git a/persistence/src/main/java/com/narcissus/marketplace/data/persistence/database/AppDatabase.kt b/persistence/src/main/java/com/narcissus/marketplace/data/persistence/database/AppDatabase.kt index c0a9cfd8..73fff614 100644 --- a/persistence/src/main/java/com/narcissus/marketplace/data/persistence/database/AppDatabase.kt +++ b/persistence/src/main/java/com/narcissus/marketplace/data/persistence/database/AppDatabase.kt @@ -6,12 +6,12 @@ import com.narcissus.marketplace.data.persistence.model.ProductEntity import com.narcissus.marketplace.data.persistence.model.SearchHistoryEntity @Database( - entities = [ProductEntity::class,SearchHistoryEntity::class], + entities = [ProductEntity::class, SearchHistoryEntity::class], version = 1, ) abstract class AppDatabase : RoomDatabase() { abstract fun productDao(): ProductDao - abstract fun searchHistoryDao():SearchHistoryDao + abstract fun searchHistoryDao(): SearchHistoryDao companion object { const val DATABASE_NAME = "marketplace-app.db" diff --git a/persistence/src/main/java/com/narcissus/marketplace/data/persistence/database/SearchHistoryDao.kt b/persistence/src/main/java/com/narcissus/marketplace/data/persistence/database/SearchHistoryDao.kt index a3be0dc5..2fd6a6bf 100644 --- a/persistence/src/main/java/com/narcissus/marketplace/data/persistence/database/SearchHistoryDao.kt +++ b/persistence/src/main/java/com/narcissus/marketplace/data/persistence/database/SearchHistoryDao.kt @@ -1,7 +1,6 @@ package com.narcissus.marketplace.data.persistence.database import androidx.room.Dao -import androidx.room.Delete import androidx.room.Insert import androidx.room.OnConflictStrategy import androidx.room.Query @@ -14,7 +13,7 @@ interface SearchHistoryDao { fun getSearchHistory(): Flow> @Insert(onConflict = OnConflictStrategy.REPLACE) - suspend fun insertSearchHistoryItem(searchItem:SearchHistoryEntity) + suspend fun insertSearchHistoryItem(searchItem: SearchHistoryEntity) @Query("DELETE FROM search_history") suspend fun clearSearchHistory() } diff --git a/persistence/src/main/java/com/narcissus/marketplace/data/persistence/model/SearchHistoryEntity.kt b/persistence/src/main/java/com/narcissus/marketplace/data/persistence/model/SearchHistoryEntity.kt index 10494e32..7ac17202 100644 --- a/persistence/src/main/java/com/narcissus/marketplace/data/persistence/model/SearchHistoryEntity.kt +++ b/persistence/src/main/java/com/narcissus/marketplace/data/persistence/model/SearchHistoryEntity.kt @@ -3,8 +3,8 @@ package com.narcissus.marketplace.data.persistence.model import androidx.room.Entity import androidx.room.PrimaryKey @Entity(tableName = "search_history") -data class SearchHistoryEntity ( +data class SearchHistoryEntity( @PrimaryKey - val productName:String, + val productName: String, val created: Long = System.currentTimeMillis() - ) +) diff --git a/ui/search/src/main/java/com/narcissus/marketplace/ui/search/di/searchViewModelsModule.kt b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/di/searchViewModelsModule.kt index 8b77300c..275b8d94 100644 --- a/ui/search/src/main/java/com/narcissus/marketplace/ui/search/di/searchViewModelsModule.kt +++ b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/di/searchViewModelsModule.kt @@ -5,5 +5,5 @@ import org.koin.androidx.viewmodel.dsl.viewModel import org.koin.dsl.module val searchViewModelsModule = module { - viewModel{ SearchHistoryViewModel(get(),get(),get()) } + viewModel { SearchHistoryViewModel(get(), get(), get()) } } diff --git a/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryFragment.kt b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryFragment.kt index 4acbc69c..a4aac10c 100644 --- a/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryFragment.kt +++ b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryFragment.kt @@ -62,7 +62,6 @@ class SearchHistoryFragment : Fragment(R.layout.fragment_search_history) { } else { historyAdapter.items = listOf() } - } } } diff --git a/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryItem.kt b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryItem.kt index 77524010..95866ffa 100644 --- a/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryItem.kt +++ b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryItem.kt @@ -7,7 +7,7 @@ import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding import com.narcissus.marketplace.core.databinding.ListItemDividerBinding import com.narcissus.marketplace.ui.search.databinding.ListItemSearchHistoryBinding sealed class SearchHistoryItem { - data class HistoryItem(val itemName:String): SearchHistoryItem() { + data class HistoryItem(val itemName: String) : SearchHistoryItem() { companion object { @JvmStatic private fun inflateBinding( @@ -35,7 +35,7 @@ sealed class SearchHistoryItem { ) = ListItemDividerBinding.inflate(layoutInflater, root, false) fun delegate() = - adapterDelegateViewBinding( + adapterDelegateViewBinding( ::inflateBinding, ) { bind {} @@ -43,18 +43,16 @@ sealed class SearchHistoryItem { } } - companion object { val DIFF_CALLBACK = object : DiffUtil.ItemCallback() { override fun areItemsTheSame( oldItem: SearchHistoryItem, newItem: SearchHistoryItem, - ): Boolean = when(oldItem){ - is HistoryItem-> newItem is HistoryItem &&newItem.itemName==oldItem.itemName - is Divider->newItem is Divider + ): Boolean = when (oldItem) { + is HistoryItem -> newItem is HistoryItem && newItem.itemName == oldItem.itemName + is Divider -> newItem is Divider } - override fun areContentsTheSame( oldItem: SearchHistoryItem, newItem: SearchHistoryItem, @@ -63,5 +61,4 @@ sealed class SearchHistoryItem { } } } - } diff --git a/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryViewModel.kt b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryViewModel.kt index 393dd8d0..affee99c 100644 --- a/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryViewModel.kt +++ b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryViewModel.kt @@ -14,12 +14,14 @@ class SearchHistoryViewModel( private val clearSearchHistory: ClearSearchHistory, ) : ViewModel() { val searchHistory = getSearchHistory().map { list -> - (list.map { - listOf( - SearchHistoryItem.HistoryItem(it), - SearchHistoryItem.Divider(), + ( + list.map { + listOf( + SearchHistoryItem.HistoryItem(it), + SearchHistoryItem.Divider(), + ) + }.flatten() ) - }.flatten()) } fun writeToSearchHistory(searchQuery: String) { From 07fb7aae52a8506a4ef929ebf81f3acc4daffcc9 Mon Sep 17 00:00:00 2001 From: ggendrick Date: Wed, 20 Apr 2022 15:17:29 +0300 Subject: [PATCH 6/7] codestyle fixes --- core/src/main/res/values/strings.xml | 12 +- .../search_history/SearchHistoryFragment.kt | 152 +++++++++--------- .../search_history/SearchHistoryViewModel.kt | 74 +++++---- .../res/layout/fragment_search_history.xml | 101 ++++++------ 4 files changed, 169 insertions(+), 170 deletions(-) diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index 2bc4796e..0ff89159 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -1,5 +1,7 @@ - - - NCS SHOP - NCS Shop Logo - + + + NCS SHOP + NCS Shop Logo + You searched for + Clear + diff --git a/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryFragment.kt b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryFragment.kt index a4aac10c..9d96f1c3 100644 --- a/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryFragment.kt +++ b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryFragment.kt @@ -1,77 +1,75 @@ -package com.narcissus.marketplace.ui.search.search_history - -import android.os.Bundle -import android.view.View -import androidx.fragment.app.Fragment -import androidx.lifecycle.lifecycleScope -import androidx.recyclerview.widget.LinearLayoutManager -import com.hannesdorfmann.adapterdelegates4.AsyncListDifferDelegationAdapter -import com.narcissus.marketplace.core.navigation.destination.SearchDestination -import com.narcissus.marketplace.core.navigation.navigator -import com.narcissus.marketplace.domain.model.search.SortBy -import com.narcissus.marketplace.ui.search.R -import com.narcissus.marketplace.ui.search.databinding.FragmentSearchHistoryBinding -import org.koin.androidx.viewmodel.ext.android.viewModel - -class SearchHistoryFragment : Fragment(R.layout.fragment_search_history) { - private var _binding: FragmentSearchHistoryBinding? = null - private val binding get() = _binding!! - private val viewModel: SearchHistoryViewModel by viewModel() - private val historyAdapter = AsyncListDifferDelegationAdapter( - SearchHistoryItem.DIFF_CALLBACK, - SearchHistoryItem.HistoryItem.delegate(), - SearchHistoryItem.Divider.delegate(), - ) - - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - _binding = FragmentSearchHistoryBinding.bind(view) - binding.searchView.root.setOnClickListener { - navigator.navigate( - SearchDestination("Query", "Department", SortBy.RATING), - ) - } - initRecyclerView() - observeSearchHistory() - initListeners() - } - - private fun initRecyclerView() { - val historyRecyclerView = binding.rvSearchHistory - historyRecyclerView.adapter = historyAdapter - historyRecyclerView.layoutManager = - LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) - } - - private fun initListeners() { - binding.tvSearchHistoryClearAll.setOnClickListener { - viewModel.clearHistory() - } - } - - private fun observeSearchHistory() { - lifecycleScope.launchWhenCreated { - viewModel.searchHistory.collect { searchHistory -> - if (searchHistory.isNotEmpty()) { - historyAdapter.items = (searchHistory as MutableList).apply { - add( - 0, - SearchHistoryItem.Divider(), - ) - } - } else { - historyAdapter.items = listOf() - } - } - } - } - - private fun writeQueryToSearchHistory(query: String) { - viewModel.writeToSearchHistory(query) - } - - override fun onDestroyView() { - super.onDestroyView() - _binding = null - } -} +package com.narcissus.marketplace.ui.search.search_history + +import android.os.Bundle +import android.view.View +import androidx.fragment.app.Fragment +import androidx.lifecycle.lifecycleScope +import androidx.recyclerview.widget.LinearLayoutManager +import com.hannesdorfmann.adapterdelegates4.AsyncListDifferDelegationAdapter +import com.narcissus.marketplace.core.navigation.destination.SearchDestination +import com.narcissus.marketplace.core.navigation.navigator +import com.narcissus.marketplace.domain.model.search.SortBy +import com.narcissus.marketplace.ui.search.R +import com.narcissus.marketplace.ui.search.databinding.FragmentSearchHistoryBinding +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach +import org.koin.androidx.viewmodel.ext.android.viewModel + +class SearchHistoryFragment : Fragment(R.layout.fragment_search_history) { + private var _binding: FragmentSearchHistoryBinding? = null + private val binding get() = _binding!! + private val viewModel: SearchHistoryViewModel by viewModel() + private val historyAdapter = AsyncListDifferDelegationAdapter( + SearchHistoryItem.DIFF_CALLBACK, + SearchHistoryItem.HistoryItem.delegate(), + SearchHistoryItem.Divider.delegate(), + ) + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + _binding = FragmentSearchHistoryBinding.bind(view) + binding.searchView.root.setOnClickListener { + navigator.navigate( + SearchDestination("Query", "Department", SortBy.RATING), + ) + } + initRecyclerView() + observeSearchHistory() + initListeners() + } + + private fun initRecyclerView() { + val historyRecyclerView = binding.rvSearchHistory + historyRecyclerView.adapter = historyAdapter + } + + private fun initListeners() { + binding.tvSearchHistoryClearAll.setOnClickListener { + viewModel.clearHistory() + } + } + + private fun observeSearchHistory() { + viewModel.searchHistory.onEach { searchHistory -> + if (searchHistory.isNotEmpty()) { + historyAdapter.items = searchHistory.toMutableList().apply { + add( + 0, + SearchHistoryItem.Divider(), + ) + } + } else { + historyAdapter.items = emptyList() + } + }.launchIn(viewLifecycleOwner.lifecycleScope) + } + + private fun writeQueryToSearchHistory(query: String) { + viewModel.writeToSearchHistory(query) + } + + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } +} diff --git a/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryViewModel.kt b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryViewModel.kt index affee99c..421ef9bd 100644 --- a/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryViewModel.kt +++ b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryViewModel.kt @@ -1,38 +1,36 @@ -package com.narcissus.marketplace.ui.search.search_history - -import androidx.lifecycle.ViewModel -import androidx.lifecycle.viewModelScope -import com.narcissus.marketplace.domain.usecase.ClearSearchHistory -import com.narcissus.marketplace.domain.usecase.GetSearchHistory -import com.narcissus.marketplace.domain.usecase.WriteQueryToSearchHistory -import kotlinx.coroutines.flow.map -import kotlinx.coroutines.launch - -class SearchHistoryViewModel( - getSearchHistory: GetSearchHistory, - private val writeQueryToSearchHistory: WriteQueryToSearchHistory, - private val clearSearchHistory: ClearSearchHistory, -) : ViewModel() { - val searchHistory = getSearchHistory().map { list -> - ( - list.map { - listOf( - SearchHistoryItem.HistoryItem(it), - SearchHistoryItem.Divider(), - ) - }.flatten() - ) - } - - fun writeToSearchHistory(searchQuery: String) { - viewModelScope.launch { - writeQueryToSearchHistory(searchQuery) - } - } - - fun clearHistory() { - viewModelScope.launch { - clearSearchHistory() - } - } -} +package com.narcissus.marketplace.ui.search.search_history + +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import com.narcissus.marketplace.domain.usecase.ClearSearchHistory +import com.narcissus.marketplace.domain.usecase.GetSearchHistory +import com.narcissus.marketplace.domain.usecase.WriteQueryToSearchHistory +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.launch + +class SearchHistoryViewModel( + getSearchHistory: GetSearchHistory, + private val writeQueryToSearchHistory: WriteQueryToSearchHistory, + private val clearSearchHistory: ClearSearchHistory, +) : ViewModel() { + val searchHistory = getSearchHistory().map { list -> + list.map { + listOf( + SearchHistoryItem.HistoryItem(it), + SearchHistoryItem.Divider(), + ) + }.flatten() + } + + fun writeToSearchHistory(searchQuery: String) { + viewModelScope.launch { + writeQueryToSearchHistory(searchQuery) + } + } + + fun clearHistory() { + viewModelScope.launch { + clearSearchHistory() + } + } +} diff --git a/ui/search/src/main/res/layout/fragment_search_history.xml b/ui/search/src/main/res/layout/fragment_search_history.xml index 35647353..787a4b12 100644 --- a/ui/search/src/main/res/layout/fragment_search_history.xml +++ b/ui/search/src/main/res/layout/fragment_search_history.xml @@ -1,50 +1,51 @@ - - - - - - - - - - - - + + + + + + + + + + + + From 66994a57d6a42dc51f5dec4a3f907e2aef5539ed Mon Sep 17 00:00:00 2001 From: beleavemebe Date: Thu, 21 Apr 2022 14:07:15 +0000 Subject: [PATCH 7/7] [MegaLinter] Apply linters fixes --- .../search/search_history/SearchHistoryFragment.kt | 1 - .../search/search_history/SearchHistoryViewModel.kt | 12 ++++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryFragment.kt b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryFragment.kt index 9d96f1c3..78fc002f 100644 --- a/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryFragment.kt +++ b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryFragment.kt @@ -4,7 +4,6 @@ import android.os.Bundle import android.view.View import androidx.fragment.app.Fragment import androidx.lifecycle.lifecycleScope -import androidx.recyclerview.widget.LinearLayoutManager import com.hannesdorfmann.adapterdelegates4.AsyncListDifferDelegationAdapter import com.narcissus.marketplace.core.navigation.destination.SearchDestination import com.narcissus.marketplace.core.navigation.navigator diff --git a/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryViewModel.kt b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryViewModel.kt index 421ef9bd..7dfd87a0 100644 --- a/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryViewModel.kt +++ b/ui/search/src/main/java/com/narcissus/marketplace/ui/search/search_history/SearchHistoryViewModel.kt @@ -14,12 +14,12 @@ class SearchHistoryViewModel( private val clearSearchHistory: ClearSearchHistory, ) : ViewModel() { val searchHistory = getSearchHistory().map { list -> - list.map { - listOf( - SearchHistoryItem.HistoryItem(it), - SearchHistoryItem.Divider(), - ) - }.flatten() + list.map { + listOf( + SearchHistoryItem.HistoryItem(it), + SearchHistoryItem.Divider(), + ) + }.flatten() } fun writeToSearchHistory(searchQuery: String) {