Skip to content

Commit 6204a30

Browse files
Merge branch 'feature/home-pagination' into develop
2 parents 9a5e19c + 1233ab8 commit 6204a30

19 files changed

Lines changed: 449 additions & 37 deletions

File tree

features/account/src/main/res/layout/fragment_account.xml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,20 @@
1313
android:text="ACCOUNT\n Under Construction"
1414
android:textSize="30dp"
1515
android:textStyle="bold"
16-
app:layout_constraintBottom_toBottomOf="parent"
16+
app:layout_constraintBottom_toTopOf="@+id/lavUnderConstruction"
1717
app:layout_constraintEnd_toEndOf="parent"
1818
app:layout_constraintStart_toStartOf="parent"
19-
app:layout_constraintTop_toTopOf="parent"
20-
app:layout_constraintVertical_bias="0.25" />
19+
app:layout_constraintTop_toTopOf="parent" />
2120

2221
<com.airbnb.lottie.LottieAnimationView
2322
android:id="@+id/lavUnderConstruction"
2423
android:layout_width="0dp"
2524
android:layout_height="0dp"
26-
android:layout_marginTop="8dp"
2725
app:layout_constraintBottom_toBottomOf="parent"
2826
app:layout_constraintDimensionRatio="1:1"
2927
app:layout_constraintEnd_toEndOf="parent"
3028
app:layout_constraintStart_toStartOf="parent"
31-
app:layout_constraintTop_toBottomOf="@+id/tvTitle"
32-
app:layout_constraintVertical_bias="0.4"
29+
app:layout_constraintTop_toTopOf="parent"
3330
app:layout_constraintWidth_percent=".6"
3431
app:lottie_autoPlay="true"
3532
app:lottie_fileName="construction_process.json" />

features/favorites/src/main/res/layout/fragment_favorites.xml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<layout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto">
4+
45
<androidx.constraintlayout.widget.ConstraintLayout
56
android:layout_width="match_parent"
67
android:layout_height="match_parent">
@@ -14,22 +15,20 @@
1415
android:textSize="30dp"
1516
android:textStyle="bold"
1617
app:layout_constraintBottom_toBottomOf="parent"
18+
app:layout_constraintBottom_toTopOf="@+id/lavUnderConstruction"
1719
app:layout_constraintEnd_toEndOf="parent"
1820
app:layout_constraintStart_toStartOf="parent"
19-
app:layout_constraintTop_toTopOf="parent"
20-
app:layout_constraintVertical_bias="0.25" />
21+
app:layout_constraintTop_toTopOf="parent" />
2122

2223
<com.airbnb.lottie.LottieAnimationView
2324
android:id="@+id/lavUnderConstruction"
2425
android:layout_width="0dp"
2526
android:layout_height="0dp"
26-
android:layout_marginTop="8dp"
2727
app:layout_constraintBottom_toBottomOf="parent"
2828
app:layout_constraintDimensionRatio="1:1"
2929
app:layout_constraintEnd_toEndOf="parent"
3030
app:layout_constraintStart_toStartOf="parent"
31-
app:layout_constraintTop_toBottomOf="@+id/tvTitle"
32-
app:layout_constraintVertical_bias="0.4"
31+
app:layout_constraintTop_toTopOf="parent"
3332
app:layout_constraintWidth_percent=".6"
3433
app:lottie_autoPlay="true"
3534
app:lottie_fileName="construction_process.json" />

features/home/src/main/java/com/smarttoolfactory/home/adapter/HomeViewPager2FragmentStateAdapter.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,22 @@ class HomeViewPager2FragmentStateAdapter(fragmentManager: FragmentManager, lifec
2323
override fun createFragment(position: Int): Fragment {
2424

2525
return when (position) {
26-
27-
// Fragment with Flow
2826
0 -> NavHostContainerFragment.createNavHostContainerFragment(
2927
R.layout.fragment_navhost_property_list_flow,
3028
R.id.nested_nav_host_fragment_property_list
3129
)
3230

33-
// Fragment with Pagination
34-
else -> NavHostContainerFragment.createNavHostContainerFragment(
31+
// Fragment with RxJava3
32+
1 -> NavHostContainerFragment.createNavHostContainerFragment(
3533
R.layout.fragment_navhost_property_list_rxjava3,
3634
R.id.nested_nav_host_fragment_property_list
3735
)
36+
37+
// Fragment with Flow + Pagination
38+
else -> NavHostContainerFragment.createNavHostContainerFragment(
39+
R.layout.fragment_navhost_property_list_paged,
40+
R.id.nested_nav_host_fragment_property_list
41+
)
3842
}
3943
}
4044
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.smarttoolfactory.home.adapter
2+
3+
class LoadingAdapter

features/home/src/main/java/com/smarttoolfactory/home/adapter/PropertyListAdapter.kt

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,11 @@ class PropertyItemListAdapter(
7878
*/
7979
class PropertyItemDiffCallback : DiffUtil.ItemCallback<PropertyItem>() {
8080

81-
override fun areItemsTheSame(
82-
oldItem: PropertyItem,
83-
newItem: PropertyItem
84-
): Boolean {
85-
return oldItem === newItem
81+
override fun areItemsTheSame(oldItem: PropertyItem, newItem: PropertyItem): Boolean {
82+
return oldItem.id == newItem.id
8683
}
8784

88-
override fun areContentsTheSame(
89-
oldItem: PropertyItem,
90-
newItem: PropertyItem
91-
): Boolean {
92-
return oldItem.id == newItem.id
85+
override fun areContentsTheSame(oldItem: PropertyItem, newItem: PropertyItem): Boolean {
86+
return oldItem == newItem
9387
}
9488
}

features/home/src/main/java/com/smarttoolfactory/home/di/HomeComponent.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.smarttoolfactory.home.di
22

33
import androidx.fragment.app.Fragment
44
import com.smarttoolfactory.core.di.CoreModuleDependencies
5+
import com.smarttoolfactory.home.propertylist.PagedPropertyListFragment
56
import com.smarttoolfactory.home.propertylist.PropertyListFlowFragment
67
import com.smarttoolfactory.home.propertylist.PropertyListRxjava3Fragment
78
import dagger.BindsInstance
@@ -15,6 +16,7 @@ interface HomeComponent {
1516

1617
fun inject(fragment: PropertyListFlowFragment)
1718
fun inject(fragment: PropertyListRxjava3Fragment)
19+
fun inject(fragment: PagedPropertyListFragment)
1820

1921
@Component.Factory
2022
interface Factory {

features/home/src/main/java/com/smarttoolfactory/home/di/HomeModule.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package com.smarttoolfactory.home.di
22

33
import androidx.fragment.app.Fragment
44
import androidx.lifecycle.ViewModelProvider
5+
import com.smarttoolfactory.home.viewmodel.PagedPropertyListViewModel
6+
import com.smarttoolfactory.home.viewmodel.PagedPropertyListViewModelFactory
57
import com.smarttoolfactory.home.viewmodel.PropertyListFlowViewModelFactory
68
import com.smarttoolfactory.home.viewmodel.PropertyListRxJava3ViewModelFactory
79
import com.smarttoolfactory.home.viewmodel.PropertyListViewModelFlow
@@ -18,13 +20,29 @@ import kotlinx.coroutines.SupervisorJob
1820
@Module
1921
class HomeModule {
2022

23+
/**
24+
* Property ViewModel that uses Flow for data operation
25+
*/
2126
@Provides
2227
fun providePropertyListViewModelFlow(
2328
fragment: Fragment,
2429
factory: PropertyListFlowViewModelFactory
2530
) =
2631
ViewModelProvider(fragment, factory).get(PropertyListViewModelFlow::class.java)
2732

33+
/**
34+
* Property ViewModel that uses Flow for data operation with Pagaination
35+
*/
36+
@Provides
37+
fun providePagedPropertyListViewModel(
38+
fragment: Fragment,
39+
factory: PagedPropertyListViewModelFactory
40+
) =
41+
ViewModelProvider(fragment, factory).get(PagedPropertyListViewModel::class.java)
42+
43+
/**
44+
* Property ViewModel that uses Rxjava for data operations
45+
*/
2846
@Provides
2947
fun providePropertyListViewModelRxJava3(
3048
fragment: Fragment,
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package com.smarttoolfactory.home.propertylist
2+
3+
import android.os.Bundle
4+
import android.widget.Toast
5+
import androidx.core.os.bundleOf
6+
import androidx.fragment.app.activityViewModels
7+
import androidx.recyclerview.widget.LinearLayoutManager
8+
import com.smarttoolfactory.core.di.CoreModuleDependencies
9+
import com.smarttoolfactory.core.ui.fragment.DynamicNavigationFragment
10+
import com.smarttoolfactory.core.util.EndlessScrollListener
11+
import com.smarttoolfactory.core.util.observe
12+
import com.smarttoolfactory.home.R
13+
import com.smarttoolfactory.home.adapter.PropertyItemListAdapter
14+
import com.smarttoolfactory.home.databinding.FragmentPropertyListPagedBinding
15+
import com.smarttoolfactory.home.di.DaggerHomeComponent
16+
import com.smarttoolfactory.home.viewmodel.HomeToolbarVM
17+
import com.smarttoolfactory.home.viewmodel.PagedPropertyListViewModel
18+
import dagger.hilt.android.EntryPointAccessors
19+
import javax.inject.Inject
20+
21+
class PagedPropertyListFragment :
22+
DynamicNavigationFragment<FragmentPropertyListPagedBinding>(),
23+
EndlessScrollListener.ScrollToBottomListener {
24+
25+
@Inject
26+
lateinit var viewModel: PagedPropertyListViewModel
27+
28+
lateinit var itemListAdapter: PropertyItemListAdapter
29+
30+
/**
31+
* Listener for listening scrolling to last item of RecyclerView
32+
*/
33+
private lateinit var endlessScrollListener: EndlessScrollListener
34+
35+
/**
36+
* ViewModel for setting sort filter on top menu and property list fragments
37+
*/
38+
private val toolbarVM by activityViewModels<HomeToolbarVM>()
39+
40+
override fun getLayoutRes(): Int = R.layout.fragment_property_list_paged
41+
42+
override fun onCreate(savedInstanceState: Bundle?) {
43+
initCoreDependentInjection()
44+
super.onCreate(savedInstanceState)
45+
viewModel.refreshPropertyList()
46+
}
47+
48+
override fun bindViews() {
49+
dataBinding.viewModel = viewModel
50+
51+
dataBinding.recyclerView.apply {
52+
53+
// Set Layout manager
54+
val linearLayoutManager =
55+
LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
56+
57+
this.layoutManager = linearLayoutManager
58+
59+
// Set RecyclerViewAdapter
60+
itemListAdapter = PropertyItemListAdapter(
61+
R.layout.row_property,
62+
viewModel::onClick,
63+
viewModel::onLikeButtonClick
64+
65+
)
66+
67+
// Set Adapter
68+
this.adapter = itemListAdapter
69+
70+
// Set RecyclerView layout manager, adapter, and scroll listener for infinite scrolling
71+
endlessScrollListener =
72+
EndlessScrollListener(linearLayoutManager, this@PagedPropertyListFragment)
73+
this.addOnScrollListener(endlessScrollListener)
74+
}
75+
76+
val swipeRefreshLayout = dataBinding.swipeRefreshLayout
77+
78+
swipeRefreshLayout.setOnRefreshListener {
79+
swipeRefreshLayout.isRefreshing = false
80+
viewModel.refreshPropertyList()
81+
}
82+
83+
subscribeViewModelSortChange()
84+
85+
subscribeGoToDetailScreen()
86+
}
87+
88+
/**
89+
* When sort key is fetched from database change the one belong to Toolbar
90+
*/
91+
private fun subscribeViewModelSortChange() {
92+
viewLifecycleOwner.observe(viewModel.orderKey) {
93+
toolbarVM.currentSortFilter = it
94+
}
95+
}
96+
97+
private fun subscribeToolbarSortChange() {
98+
99+
viewLifecycleOwner.observe(toolbarVM.queryBySort) {
100+
it.getContentIfNotHandled()?.let { orderBy ->
101+
viewModel.refreshPropertyList(orderBy)
102+
toolbarVM.currentSortFilter = orderBy
103+
}
104+
}
105+
}
106+
107+
private fun subscribeGoToDetailScreen() {
108+
109+
viewModel.goToDetailScreen.observe(
110+
viewLifecycleOwner,
111+
{
112+
113+
it.getContentIfNotHandled()?.let { propertyItem ->
114+
val bundle = bundleOf("property" to propertyItem)
115+
}
116+
}
117+
)
118+
}
119+
120+
private fun initCoreDependentInjection() {
121+
122+
val coreModuleDependencies = EntryPointAccessors.fromApplication(
123+
requireActivity().applicationContext,
124+
CoreModuleDependencies::class.java
125+
)
126+
127+
DaggerHomeComponent.factory().create(
128+
dependentModule = coreModuleDependencies,
129+
fragment = this
130+
)
131+
.inject(this)
132+
}
133+
134+
override fun onResume() {
135+
super.onResume()
136+
subscribeToolbarSortChange()
137+
}
138+
139+
override fun onPause() {
140+
super.onPause()
141+
toolbarVM.queryBySort.removeObservers(viewLifecycleOwner)
142+
}
143+
144+
override fun onScrollToBottom() {
145+
viewModel.getPropertyList()
146+
Toast.makeText(requireContext(), "BOTTOM SCROLL", Toast.LENGTH_SHORT).show()
147+
}
148+
}

0 commit comments

Comments
 (0)